forked from intersystems-community/vscode-objectscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectScriptCodeLensProvider.ts
183 lines (166 loc) · 6.8 KB
/
ObjectScriptCodeLensProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import * as vscode from "vscode";
import { clsLangId, config, intLangId, macLangId } from "../extension";
import { currentFile } from "../utils";
import { AtelierAPI } from "../api";
export class ObjectScriptCodeLensProvider implements vscode.CodeLensProvider {
public provideCodeLenses(
document: vscode.TextDocument,
token: vscode.CancellationToken
): vscode.ProviderResult<vscode.CodeLens[]> {
if (document.languageId == clsLangId) {
return this.classMembers(document);
}
if ([macLangId, intLangId].includes(document.languageId)) {
return this.routineLabels(document);
}
return [];
}
private classMembers(document: vscode.TextDocument): vscode.CodeLens[] {
const file = currentFile(document);
const result = new Array<vscode.CodeLens>();
const className = file.name.slice(0, -4);
const { debugThisMethod, copyToClipboard } = config("debug");
const methodPattern = /(?:^(ClassMethod|Query)\s)([^(]+)\((.*)/i;
const xdataPattern = /^XData\s([^[{\s]+)/i;
const superPattern = new RegExp(
`^\\s*Class\\s+${className.replace(/\./g, "\\.")}\\s+Extends\\s+(?:(?:\\(([^)]+)\\))|(?:([^\\s]+)))`,
"i"
);
const api = new AtelierAPI(document.uri);
let superclasses: string[] = [];
let inComment = false;
for (let i = 0; i < document.lineCount; i++) {
const line = document.lineAt(i);
const text = this.stripLineComments(line.text);
if (text.match(/\/\*/)) {
inComment = true;
}
if (inComment) {
if (text.match(/\*\//)) {
inComment = false;
}
continue;
}
const methodMatch = text.match(methodPattern);
const xdataMatch = text.match(xdataPattern);
const superMatch = text.match(superPattern);
if (superMatch) {
const [, superclassesList, superclass] = superMatch;
if (superclass) {
superclasses = [superclass];
} else {
superclasses = superclassesList.replace(/\s+/g, "").split(",");
}
} else if (xdataMatch && api.active) {
let [, xdataName] = xdataMatch;
xdataName = xdataName.trim();
let cmd: vscode.Command = undefined;
if (
(xdataName == "BPL" && superclasses.includes("Ens.BusinessProcessBPL")) ||
(xdataName == "DTL" && superclasses.includes("Ens.DataTransformDTL"))
) {
cmd = {
title: "Open Graphical Editor",
command: "vscode-objectscript.openPathInBrowser",
tooltip: "Open graphical editor in an external browser",
arguments: [
`/csp/${api.config.ns.toLowerCase()}/EnsPortal.${
xdataName == "BPL" ? `BPLEditor.zen?BP=${className}.BPL` : `DTLEditor.zen?DT=${className}.DTL`
}`,
document.uri,
],
};
} else if (xdataName == "RuleDefinition" && superclasses.includes("Ens.Rule.Definition")) {
cmd = {
title: "Reopen in Graphical Editor",
command: "workbench.action.toggleEditorType",
tooltip: "Replace text editor with graphical editor",
};
} else if (xdataName == "KPI" && superclasses.includes("%DeepSee.KPI")) {
cmd = {
title: "Test KPI",
command: "vscode-objectscript.openPathInBrowser",
tooltip: "Open testing page in an external browser",
arguments: [`/csp/${api.config.ns.toLowerCase()}/${className}.cls`, document.uri],
};
}
if (cmd) result.push(new vscode.CodeLens(new vscode.Range(i, 0, i, 80), cmd));
} else if (methodMatch && (debugThisMethod || copyToClipboard)) {
const [, kind, name, paramsRaw] = methodMatch;
let params = paramsRaw;
params = params.replace(/"[^"]*"/g, '""');
params = params.replace(/{[^{}]*}|{[^{}]*{[^{}]*}[^{}]*}/g, '""');
params = params.replace(/\([^()]*\)/g, "");
const args = params.split(")")[0];
const paramsCount = args.length ? args.split(",").length : params.includes(")") ? 0 : 1; // Need a positive paramsCount when objectscript.multilineMethodArgs is true
const methodName = name + (kind == "Query" ? "Func" : "");
debugThisMethod &&
kind == "ClassMethod" &&
result.push(this.addDebugThisMethod(i, [`##class(${className}).${methodName}`, paramsCount > 0]));
copyToClipboard &&
result.push(
this.addCopyToClipboard(i, [`##class(${className}).${methodName}(${Array(paramsCount).join(",")})`])
);
}
}
return result;
}
private async routineLabels(document: vscode.TextDocument): Promise<vscode.CodeLens[]> {
const file = currentFile(document);
const result = new Array<vscode.CodeLens>();
const routineName = file.name.split(".").slice(0, -1).join(".");
const { debugThisMethod, copyToClipboard } = config("debug");
if (!debugThisMethod && !copyToClipboard) {
// Return early if both types are turned off
return result;
}
const symbols: vscode.DocumentSymbol[] = await vscode.commands.executeCommand(
"vscode.executeDocumentSymbolProvider",
document.uri
);
let labelledLine1 = false;
if (symbols) {
symbols
.filter((symbol) => symbol.kind === vscode.SymbolKind.Method)
.forEach((symbol) => {
const line = symbol.selectionRange.start.line;
const labelMatch = document.lineAt(line).text.match(/^(\w[^(\n\s]+)(?:\(([^)]*)\))?/i);
if (labelMatch) {
if (line === 1) {
labelledLine1 = true;
}
const [, name, parens] = labelMatch;
debugThisMethod &&
result.push(this.addDebugThisMethod(line, [`${name}^${routineName}`, parens && parens !== "()"]));
copyToClipboard && result.push(this.addCopyToClipboard(line, [`${name}^${routineName}`]));
}
});
}
// Add lenses at the top only if the first code line had no label
if (!labelledLine1) {
debugThisMethod && result.push(this.addDebugThisMethod(0, [`^${routineName}`, false]));
copyToClipboard && result.push(this.addCopyToClipboard(0, [`^${routineName}`]));
}
return result;
}
private addDebugThisMethod(line: number, args: any[]) {
return new vscode.CodeLens(new vscode.Range(line, 0, line, 80), {
title: `Debug`,
command: "vscode-objectscript.debug",
arguments: args,
});
}
private addCopyToClipboard(line: number, args: any[]) {
return new vscode.CodeLens(new vscode.Range(line, 0, line, 80), {
title: `Copy Invocation`,
command: "vscode-objectscript.copyToClipboard",
arguments: args,
});
}
private stripLineComments(text: string) {
text = text.replace(/\/\/.*$/, "");
text = text.replace(/#+;.*$/, "");
text = text.replace(/;.*$/, "");
return text;
}
}