forked from intersystems-community/vscode-objectscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassDefinition.ts
More file actions
147 lines (136 loc) · 5.54 KB
/
classDefinition.ts
File metadata and controls
147 lines (136 loc) · 5.54 KB
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
import * as vscode from "vscode";
import { onlyUnique } from ".";
import { AtelierAPI } from "../api";
import { DocumentContentProvider } from "../providers/DocumentContentProvider";
export class ClassDefinition {
public get uri(): vscode.Uri {
return DocumentContentProvider.getUri(this._classFileName, this._workspaceFolder, this._namespace);
}
public static normalizeClassName(className: string, withExtension = false): string {
return className.replace(/^%(\b\w+\b)$/, "%Library.$1") + (withExtension ? ".cls" : "");
}
private _className: string;
private _classFileName: string;
private _workspaceFolder: string;
private _namespace: string;
public constructor(className: string, workspaceFolder?: string, namespace?: string) {
this._workspaceFolder = workspaceFolder;
this._namespace = namespace;
if (className.endsWith(".cls")) {
className = className.replace(/\.cls$/i, "");
}
this._className = ClassDefinition.normalizeClassName(className, false);
this._classFileName = ClassDefinition.normalizeClassName(className, true);
}
public async getDocument(): Promise<vscode.TextDocument> {
return vscode.workspace.openTextDocument(this.uri);
}
public async methods(scope: "any" | "class" | "instance" = "any"): Promise<any[]> {
const methods = [];
const filterScope = (method) => scope === "any" || method.scope === scope;
const api = new AtelierAPI(this.uri);
const getMethods = (content) => {
const extend = [];
content.forEach((el) => {
methods.push(...el.content.methods);
extend.push(...el.content.super.map((extendName) => ClassDefinition.normalizeClassName(extendName, true)));
});
if (extend.length) {
return api.actionIndex(extend).then((data) => getMethods(data.result.content));
}
return methods.filter(filterScope).filter(onlyUnique).sort();
};
return api.actionIndex([this._classFileName]).then((data) => getMethods(data.result.content));
}
public async properties(): Promise<any[]> {
const properties = [];
const api = new AtelierAPI(this.uri);
const getProperties = (content) => {
const extend = [];
content.forEach((el) => {
properties.push(...el.content.properties);
extend.push(...el.content.super.map((extendName) => ClassDefinition.normalizeClassName(extendName, true)));
});
if (extend.length) {
return api.actionIndex(extend).then((data) => getProperties(data.result.content));
}
return properties.filter(onlyUnique).sort();
};
return api.actionIndex([this._classFileName]).then((data) => getProperties(data.result.content));
}
public async parameters(): Promise<any[]> {
const parameters = [];
const api = new AtelierAPI(this.uri);
const getParameters = (content) => {
const extend = [];
content.forEach((el) => {
parameters.push(...el.content.parameters);
extend.push(...el.content.super.map((extendName) => ClassDefinition.normalizeClassName(extendName, true)));
});
if (extend.length) {
return api.actionIndex(extend).then((data) => getParameters(data.result.content));
}
return parameters.filter(onlyUnique).sort();
};
return api.actionIndex([this._classFileName]).then((data) => getParameters(data.result.content));
}
public async super(): Promise<string[]> {
const api = new AtelierAPI(this.uri);
const sql = `SELECT PrimarySuper FROM %Dictionary.CompiledClass
WHERE Name %inlist (SELECT $LISTFROMSTRING(Super, ',') FROM %Dictionary.CompiledClass WHERE Name = ?)`;
return api
.actionQuery(sql, [this._className])
.then((data) =>
data.result.content
.reduce(
(list: string[], el: { PrimarySuper: string }) =>
list.concat(el.PrimarySuper.split("~").filter((item) => item.length)),
[]
)
.filter((name: string) => name !== this._className)
)
.then((data) => data);
}
public async includeCode(): Promise<string[]> {
const api = new AtelierAPI(this.uri);
const sql = `SELECT LIST(IncludeCode) List FROM %Dictionary.CompiledClass WHERE Name %INLIST (
SELECT $LISTFROMSTRING(PrimarySuper, '~') FROM %Dictionary.CompiledClass WHERE Name = ?)`;
const defaultIncludes = ["%occInclude", "%occErrors"];
return api
.actionQuery(sql, [this._className])
.then((data) =>
data.result.content.reduce(
(list: string[], el: { List: string }) => list.concat(el.List.split(",")),
defaultIncludes
)
)
.then((data) => data);
}
public async getMemberLocation(name: string): Promise<vscode.Location> {
let pattern;
if (name.startsWith("#")) {
pattern = `(Parameter) ${name.substr(1)}(?=[( ;])`;
} else {
pattern = `((Class)?Method|Property|RelationShip) (${name}|"${name}")(?=[( ])`;
}
return this.getDocument().then((document) => {
for (let i = 0; i < document.lineCount; i++) {
const line = document.lineAt(i);
if (line.text.match(pattern)) {
return new vscode.Location(this.uri, new vscode.Position(i, 0));
}
}
return;
});
}
public async getMemberLocations(name: string): Promise<vscode.Location[]> {
const extendList = await this.super();
return Promise.all([
await this.getMemberLocation(name),
...extendList.map(async (docName) => {
const classDef = new ClassDefinition(docName);
return classDef.getMemberLocation(name);
}),
]).then((data) => data.filter((el) => el != null));
}
}