-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.ts
224 lines (179 loc) · 5.36 KB
/
index.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import * as fs from "fs/promises";
import { connection, getWorkspaceFolder, PossibleInclude, watchedFilesChangeEvent } from '../../connection';
import { documents, parser } from '..';
import Linter from '../../../../../language/linter';
import { DidChangeWatchedFilesParams, FileChangeType } from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import { glob } from "glob";
import * as path from "path";
import { TextDocument } from 'vscode-languageserver-textdocument';
const projectFilesGlob = `**/*.{rpgle,sqlrpgle,rpgleinc,rpgleh}`;
interface iProject {
big?: boolean;
includePath?: string[]
}
export let includePath: {[workspaceUri: string]: string[]} = {};
export let isEnabled = false;
/**
* Assumes client has workspace
*/
export async function initialise() {
isEnabled = true;
loadWorkspace();
watchedFilesChangeEvent.push((params: DidChangeWatchedFilesParams) => {
params.changes.forEach(fileEvent => {
const pathData = path.parse(fileEvent.uri);
const ext = pathData.ext.toLowerCase();
switch (fileEvent.type) {
case FileChangeType.Created:
case FileChangeType.Changed:
switch (ext) {
case `.rpgleinc`:
case `.rpgleh`:
loadLocalFile(fileEvent.uri);
currentIncludes = [];
break;
case `.json`:
if (pathData.base === `iproj.json`) {
updateIProj(fileEvent.uri);
}
break;
}
break;
default:
parser.clearParsedCache(fileEvent.uri);
break;
}
})
});
connection.onRequest(`getCache`, (uri: string) => {
return parser.getParsedCache(uri);
});
}
async function loadWorkspace() {
const progress = await connection.window.createWorkDoneProgress();
const workspaces = await connection.workspace.getWorkspaceFolders();
let handleBigProjects = false;
progress.begin(`RPGLE`, undefined, `Loading workspaces`);
if (workspaces) {
let uris: string[] = [];
for (const workspaceUri of workspaces) {
const folderPath = URI.parse(workspaceUri.uri).fsPath;
progress.report(`Starting search of ${workspaceUri.name}`);
console.log(`Starting search of: ${folderPath}`);
const files = glob.sync(projectFilesGlob, {
cwd: folderPath,
absolute: true,
nocase: true,
});
progress.report(`Found RPGLE files: ${files.length}`);
console.log(`Found RPGLE files: ${files.length}`);
uris.push(...files.map(file => URI.from({
scheme: `file`,
path: file
}).toString()));
const iprojFiles = glob.sync(`**/iproj.json`, {
cwd: folderPath,
absolute: true,
nocase: true,
});
if (iprojFiles.length > 0) {
const base = iprojFiles[0];
const iprojUri = URI.from({
scheme: `file`,
path: base
}).toString();
const iproj = await updateIProj(iprojUri);
if (iproj.big) {
handleBigProjects = true;
}
}
};
if (handleBigProjects) {
progress.report(`Big mode detected!`);
console.log(`Big mode detected!`);
}
if (uris.length < 1000 || handleBigProjects) {
await Promise.allSettled(uris.map((uri, i) => {
progress.report(`Loading ${i}/${uris.length}`);
return loadLocalFile(uri);
}));
} else {
progress.report(`Disabling project mode for large project.`);
console.log(`Disabling project mode for large project.`);
isEnabled = false;
}
}
progress.done();
}
async function updateIProj(uri: string): Promise<iProject> {
const workspace = await getWorkspaceFolder(uri);
if (workspace) {
const document = await getTextDoc(uri);
const content = document?.getText();
if (content) {
try {
const asJson = JSON.parse(content) as iProject;
if (asJson.includePath && Array.isArray(asJson.includePath)) {
const includeArray: any[] = asJson.includePath;
const invalid = includeArray.some(v => typeof v !== `string`);
if (!invalid) {
includePath[workspace.uri] = asJson.includePath;
} else {
console.log(`${uri} -> 'includePath' is not a valid string array.`);
}
}
return asJson;
} catch (e) {
console.log(`Unable to parse JSON in ${uri}.`);
}
}
}
return {};
}
async function loadLocalFile(uri: string) {
const document = await getTextDoc(uri);
if (document) {
const content = document?.getText();
const cache = await parser.getDocs(uri, content, {withIncludes: true, butIgnoreMembers: true});
if (cache) {
if (content.length >= 6 && content.substring(0, 6).toUpperCase() === `**FREE`) {
Linter.getErrors({
uri,
content,
}, {
CollectReferences: true
}, cache);
}
}
}
}
export async function getTextDoc(uri: string): Promise<TextDocument | undefined> {
let document = documents.get(uri);
if (document) {
return document;
}
try {
const content = await fs.readFile(URI.parse(uri).fsPath, { encoding: `utf-8` });
return TextDocument.create(uri, `rpgle`, 1, content);
} catch (e) {}
return;
}
let currentIncludes: PossibleInclude[] = [];
export async function getIncludes(baseUri: string) {
const workspace = await getWorkspaceFolder(baseUri);
if (workspace) {
const workspacePath = URI.parse(workspace?.uri).path;
if (!currentIncludes || currentIncludes && currentIncludes.length === 0) {
currentIncludes = glob.sync(`**/*.{rpgleinc,rpgleh}`, {
cwd: workspacePath,
nocase: true,
absolute: true
}).map(truePath => ({
uri: URI.file(truePath).toString(),
relative: path.relative(workspacePath, truePath)
}))
}
}
return currentIncludes;
}