|
1 | 1 | import * as vscode from "vscode"; |
2 | 2 | import * as models from "./models"; |
| 3 | +import { builtinFunctionDefinitions } from "./functionDefinitions"; |
3 | 4 |
|
4 | 5 | export const G_FUNCTION_DEFINITION = |
5 | 6 | /(\bfunction\b)[\t ]*([a-zA-Z_][\w]*)[\t ]*\(([^\(\)]*)\)/g; // keep in sync with syntax file rascript.tmLanguage.json #function-definitions regex |
@@ -348,7 +349,7 @@ export function detectClass( |
348 | 349 | export function getClassData( |
349 | 350 | text: string, |
350 | 351 | commentBounds: models.CommentBounds[] |
351 | | -) { |
| 352 | +): Map<string, models.ClassScope> { |
352 | 353 | let classes = new Map<string, models.ClassScope>(); |
353 | 354 | let m: RegExpExecArray | null; |
354 | 355 | while ((m = G_CLASS_DEFINITION.exec(text))) { |
@@ -506,3 +507,130 @@ export function newHoverText( |
506 | 507 | lines: lines, |
507 | 508 | }; |
508 | 509 | } |
| 510 | + |
| 511 | +export function parseDocument( |
| 512 | + document: vscode.TextDocument |
| 513 | +): models.ParsedDocument { |
| 514 | + let text = document.getText(); |
| 515 | + let commentBounds = getCommentBoundsList(document); |
| 516 | + let classes = getClassData(text, commentBounds); |
| 517 | + let functionDefinitions = new Map<string, models.ClassFunction[]>(); |
| 518 | + let words = new Map<string, models.HoverData[]>(); // should be renamed to hoverData or something |
| 519 | + let completionFunctions: string[] = []; |
| 520 | + let completionVariables: string[] = []; |
| 521 | + let completionClasses: string[] = []; |
| 522 | + // Parse each build in function in the document |
| 523 | + for (let i = 0; i < builtinFunctionDefinitions.length; i++) { |
| 524 | + let fn = builtinFunctionDefinitions[i]; |
| 525 | + |
| 526 | + // Add hover data |
| 527 | + let comment = fn.commentDoc.join("\n"); |
| 528 | + let hover = newHoverText( |
| 529 | + fn.key, |
| 530 | + -1, |
| 531 | + G_FUNTION, |
| 532 | + "", |
| 533 | + comment, |
| 534 | + fn.url, |
| 535 | + ...fn.args |
| 536 | + ); |
| 537 | + let definitions = words.get(fn.key); |
| 538 | + if (definitions !== undefined) { |
| 539 | + definitions.push(hover); |
| 540 | + } else { |
| 541 | + words.set(fn.key, [hover]); |
| 542 | + } |
| 543 | + |
| 544 | + // Add completion data |
| 545 | + completionFunctions.push(fn.key); |
| 546 | + } |
| 547 | + |
| 548 | + // Parse each class in the document |
| 549 | + for (const [className, classScope] of classes) { |
| 550 | + // add hover info |
| 551 | + let pos = document.positionAt(classScope.start); |
| 552 | + let comment = getCommentText(document, pos); |
| 553 | + let hover = newHoverText( |
| 554 | + className, |
| 555 | + classScope.start, |
| 556 | + G_CLASS, |
| 557 | + "", |
| 558 | + comment, |
| 559 | + "", |
| 560 | + ...classScope.constructorArgs |
| 561 | + ); |
| 562 | + let definitions = words.get(className); |
| 563 | + if (definitions !== undefined) { |
| 564 | + definitions.push(hover); |
| 565 | + } else { |
| 566 | + words.set(className, [hover]); |
| 567 | + } |
| 568 | + |
| 569 | + // add completion info |
| 570 | + completionClasses.push(className); |
| 571 | + } |
| 572 | + |
| 573 | + // Parse each function in the document |
| 574 | + let m: RegExpExecArray | null; |
| 575 | + while ((m = G_FUNCTION_DEFINITION.exec(text))) { |
| 576 | + // dont parse if its in a comment |
| 577 | + if (inCommentBound(m.index, commentBounds)) { |
| 578 | + continue; |
| 579 | + } |
| 580 | + let className = detectClass(m.index, classes); |
| 581 | + let pos = document.positionAt(m.index); |
| 582 | + let comment = getCommentText(document, pos); |
| 583 | + let list = functionDefinitions.get(m[2]); |
| 584 | + let a = m[3].split(",").map((s) => s.trim()); |
| 585 | + var args = a.filter(function (el) { |
| 586 | + return el !== null && el !== "" && el !== undefined; |
| 587 | + }); |
| 588 | + |
| 589 | + // add definition info |
| 590 | + let item = createClassFunction(className, m[2], pos, ...args); |
| 591 | + if (list !== undefined) { |
| 592 | + list.push(item); |
| 593 | + } else { |
| 594 | + functionDefinitions.set(m[2], [item]); |
| 595 | + } |
| 596 | + |
| 597 | + // add hover info |
| 598 | + let hover = newHoverText( |
| 599 | + m[2], |
| 600 | + m.index, |
| 601 | + G_FUNTION, |
| 602 | + className, |
| 603 | + comment, |
| 604 | + "", |
| 605 | + ...args |
| 606 | + ); |
| 607 | + let definitions = words.get(m[2]); |
| 608 | + if (definitions !== undefined) { |
| 609 | + definitions.push(hover); |
| 610 | + } else { |
| 611 | + words.set(m[2], [hover]); |
| 612 | + } |
| 613 | + |
| 614 | + // add completion info |
| 615 | + completionFunctions.push(m[2]); |
| 616 | + } |
| 617 | + |
| 618 | + // Parse each variable in the document |
| 619 | + while ((m = G_VARIABLES.exec(text))) { |
| 620 | + // dont parse if its in a comment |
| 621 | + if (inCommentBound(m.index, commentBounds)) { |
| 622 | + continue; |
| 623 | + } |
| 624 | + |
| 625 | + // add completion info |
| 626 | + completionVariables.push(m[1]); |
| 627 | + } |
| 628 | + return { |
| 629 | + classes: classes, |
| 630 | + functionDefinitions: functionDefinitions, |
| 631 | + hoverData: words, |
| 632 | + completionFunctions: completionFunctions, |
| 633 | + completionVariables: completionVariables, |
| 634 | + completionClasses: completionClasses, |
| 635 | + }; |
| 636 | +} |
0 commit comments