|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { config } from "../extension"; |
| 3 | +import { currentFile } from "../utils"; |
| 4 | + |
| 5 | +export class ObjectScriptCodeLensProvider implements vscode.CodeLensProvider { |
| 6 | + public provideCodeLenses( |
| 7 | + document: vscode.TextDocument, |
| 8 | + token: vscode.CancellationToken |
| 9 | + ): vscode.ProviderResult<vscode.CodeLens[]> { |
| 10 | + const result = new Array<vscode.CodeLens>(); |
| 11 | + |
| 12 | + if (document.fileName.toLowerCase().endsWith(".cls")) { |
| 13 | + result.push(...this.classMethods(document)); |
| 14 | + } |
| 15 | + if (document.fileName.toLowerCase().endsWith(".mac")) { |
| 16 | + result.push(...this.routineLabels(document)); |
| 17 | + } |
| 18 | + return result; |
| 19 | + } |
| 20 | + |
| 21 | + private classMethods(document: vscode.TextDocument): vscode.CodeLens[] { |
| 22 | + const file = currentFile(document); |
| 23 | + const result = new Array<vscode.CodeLens>(); |
| 24 | + |
| 25 | + if (!file.name.match(/\.cls$/i)) { |
| 26 | + return result; |
| 27 | + } |
| 28 | + const className = file.name.split(".").slice(0, -1).join("."); |
| 29 | + |
| 30 | + const { debugThisMethod, copyToClipboard } = config("debug"); |
| 31 | + const pattern = /(?:^ClassMethod\s)([^(]+)\((.*)/i; |
| 32 | + let inComment = false; |
| 33 | + for (let i = 0; i < document.lineCount; i++) { |
| 34 | + const line = document.lineAt(i); |
| 35 | + const text = this.stripLineComments(line.text); |
| 36 | + |
| 37 | + if (text.match(/\/\*/)) { |
| 38 | + inComment = true; |
| 39 | + } |
| 40 | + |
| 41 | + if (inComment) { |
| 42 | + if (text.match(/\*\//)) { |
| 43 | + inComment = false; |
| 44 | + } |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + const methodMatch = text.match(pattern); |
| 49 | + if (methodMatch) { |
| 50 | + const [, name, paramsRaw] = methodMatch; |
| 51 | + let params = paramsRaw; |
| 52 | + params = params.replace(/"[^"]*"/g, '""'); |
| 53 | + params = params.replace(/{[^{}]*}|{[^{}]*{[^{}]*}[^{}]*}/g, '""'); |
| 54 | + params = params.replace(/\([^()]*\)/g, ""); |
| 55 | + params = params.split(")")[0]; |
| 56 | + const paramsCount = params.length ? params.split(",").length : 0; |
| 57 | + |
| 58 | + debugThisMethod && result.push(this.addDebugThisMethod(i, [`##class(${className}).${name}`, paramsCount > 0])); |
| 59 | + copyToClipboard && |
| 60 | + result.push(this.addCopyToClipboard(i, [`##class(${className}).${name}(${Array(paramsCount).join(",")})`])); |
| 61 | + } |
| 62 | + } |
| 63 | + return result; |
| 64 | + } |
| 65 | + |
| 66 | + private routineLabels(document: vscode.TextDocument): vscode.CodeLens[] { |
| 67 | + const file = currentFile(document); |
| 68 | + const result = new Array<vscode.CodeLens>(); |
| 69 | + |
| 70 | + if (!file.name.match(/\.mac$/i)) { |
| 71 | + return result; |
| 72 | + } |
| 73 | + const routineName = file.name.split(".").slice(0, -1).join("."); |
| 74 | + |
| 75 | + const { debugThisMethod, copyToClipboard } = config("debug"); |
| 76 | + |
| 77 | + debugThisMethod && result.push(this.addDebugThisMethod(0, [`^${routineName}`, false])); |
| 78 | + copyToClipboard && result.push(this.addCopyToClipboard(0, [`^${routineName}`])); |
| 79 | + |
| 80 | + let inComment = false; |
| 81 | + for (let i = 1; i < document.lineCount; i++) { |
| 82 | + const line = document.lineAt(i); |
| 83 | + const text = this.stripLineComments(line.text); |
| 84 | + |
| 85 | + if (text.match(/\/\*/)) { |
| 86 | + inComment = true; |
| 87 | + } |
| 88 | + |
| 89 | + if (inComment) { |
| 90 | + if (text.match(/\*\//)) { |
| 91 | + inComment = false; |
| 92 | + } |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + const labelMatch = text.match(/^(\w[^(\n\s]+)(?:\(([^)]*)\))?/i); |
| 97 | + if (labelMatch) { |
| 98 | + const [, name, parens] = labelMatch; |
| 99 | + |
| 100 | + debugThisMethod && result.push(this.addDebugThisMethod(i, [`${name}^${routineName}`, parens !== "()"])); |
| 101 | + copyToClipboard && result.push(this.addCopyToClipboard(i, [`${name}^${routineName}`])); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + private addDebugThisMethod(line: number, args: any[]) { |
| 109 | + return new vscode.CodeLens(new vscode.Range(line, 0, line, 80), { |
| 110 | + title: `Debug this Method`, |
| 111 | + command: "vscode-objectscript.debug", |
| 112 | + arguments: args, |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + private addCopyToClipboard(line: number, args: any[]) { |
| 117 | + return new vscode.CodeLens(new vscode.Range(line, 0, line, 80), { |
| 118 | + title: `Copy Invocation`, |
| 119 | + command: "vscode-objectscript.copyToClipboard", |
| 120 | + arguments: args, |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + private stripLineComments(text: string) { |
| 125 | + text = text.replace(/\/\/.*$/, ""); |
| 126 | + text = text.replace(/#+;.*$/, ""); |
| 127 | + text = text.replace(/;.*$/, ""); |
| 128 | + return text; |
| 129 | + } |
| 130 | +} |
0 commit comments