@jpoly1219/context-extractor 0.2.7 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/app.d.ts +7 -4
- package/dist/src/app.js +319 -97
- package/dist/src/ast.d.ts +6 -0
- package/dist/src/ast.js +80 -0
- package/dist/src/codeql.js +17 -7
- package/dist/src/constants.js +17 -7
- package/dist/src/core.d.ts +0 -1
- package/dist/src/core.js +17 -7
- package/dist/src/main.d.ts +8 -2
- package/dist/src/main.js +53 -13
- package/dist/src/ocaml-driver.d.ts +55 -5
- package/dist/src/ocaml-driver.js +296 -191
- package/dist/src/ocaml-utils/_build/default/test_parser.bc.cjs +194658 -0
- package/dist/src/runner.js +143 -10
- package/dist/src/tree-sitter-files/queries/hole-queries/typescript.scm +36 -0
- package/dist/src/tree-sitter-files/queries/relevant-headers-queries/typescript-get-toplevel-headers.scm +22 -0
- package/dist/src/tree-sitter-files/queries/relevant-types-queries/typescript-extract-identifiers.scm +10 -0
- package/dist/src/tree-sitter-files/queries/relevant-types-queries/typescript-find-typedecl-given-typeidentifier.scm +11 -0
- package/dist/src/tree-sitter-files/wasms/tree-sitter-ocaml.wasm +0 -0
- package/dist/src/tree-sitter-files/wasms/tree-sitter-typescript.wasm +0 -0
- package/dist/src/tree-sitter-files/wasms/tree-sitter.wasm +0 -0
- package/dist/src/tree-sitter.d.ts +40 -0
- package/dist/src/tree-sitter.js +311 -0
- package/dist/src/types.d.ts +80 -9
- package/dist/src/types.js +1 -6
- package/dist/src/typescript-driver.d.ts +86 -12
- package/dist/src/typescript-driver.js +1276 -205
- package/dist/src/typescript-type-checker.d.ts +22 -2
- package/dist/src/typescript-type-checker.js +290 -9
- package/dist/src/utils.d.ts +11 -1
- package/dist/src/utils.js +87 -10
- package/dist/src/vscode-ide.d.ts +29 -0
- package/dist/src/vscode-ide.js +161 -0
- package/package.json +12 -6
package/dist/src/app.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Language, Context } from "./types";
|
1
|
+
import { Language, Context, IDE, Position } from "./types";
|
2
2
|
export declare class App {
|
3
3
|
private language;
|
4
4
|
private languageDriver;
|
@@ -7,10 +7,13 @@ export declare class App {
|
|
7
7
|
private sketchPath;
|
8
8
|
private repoPath;
|
9
9
|
private result;
|
10
|
-
private
|
11
|
-
|
10
|
+
private logStream;
|
11
|
+
private ide;
|
12
|
+
private cursorPosition;
|
13
|
+
constructor(language: Language, sketchPath: string, repoPath: string, ide: IDE, cursorPosition: Position);
|
12
14
|
init(): Promise<void>;
|
13
|
-
|
15
|
+
run2(version: number): Promise<void>;
|
16
|
+
run(version: number): Promise<void>;
|
14
17
|
close(): void;
|
15
18
|
getSavedResult(): Context | null;
|
16
19
|
}
|
package/dist/src/app.js
CHANGED
@@ -15,136 +15,337 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
15
15
|
}) : function(o, v) {
|
16
16
|
o["default"] = v;
|
17
17
|
});
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
};
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
19
|
+
var ownKeys = function(o) {
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
21
|
+
var ar = [];
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
23
|
+
return ar;
|
24
|
+
};
|
25
|
+
return ownKeys(o);
|
26
|
+
};
|
27
|
+
return function (mod) {
|
28
|
+
if (mod && mod.__esModule) return mod;
|
29
|
+
var result = {};
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
31
|
+
__setModuleDefault(result, mod);
|
32
|
+
return result;
|
33
|
+
};
|
34
|
+
})();
|
25
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
26
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
27
37
|
};
|
28
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
29
39
|
exports.CompletionEngine = exports.App = void 0;
|
30
|
-
const path = __importStar(require("path"));
|
31
40
|
const fs = __importStar(require("fs"));
|
41
|
+
const path = __importStar(require("path"));
|
32
42
|
const child_process_1 = require("child_process");
|
33
43
|
const child_process_2 = require("child_process");
|
34
44
|
const openai_1 = __importDefault(require("openai"));
|
35
45
|
const main_1 = require("../ts-lsp-client-dist/src/main");
|
36
|
-
// import { LspClient, JSONRPCEndpoint } from "ts-lsp-client";
|
37
|
-
const types_1 = require("./types");
|
38
46
|
// TODO: Bundle the drivers as barrel exports.
|
39
47
|
const typescript_driver_1 = require("./typescript-driver");
|
40
48
|
const ocaml_driver_1 = require("./ocaml-driver");
|
41
49
|
const utils_1 = require("./utils");
|
50
|
+
// import * as pprof from "pprof";
|
42
51
|
class App {
|
43
|
-
constructor(language, sketchPath, repoPath) {
|
44
|
-
|
45
|
-
|
46
|
-
// relevantTypes: string[];
|
47
|
-
// relevantHeaders: string[];
|
48
|
-
// } | null = null;
|
52
|
+
constructor(language, sketchPath, repoPath, ide, cursorPosition) {
|
53
|
+
this.languageServer = null;
|
54
|
+
this.lspClient = null;
|
49
55
|
this.result = null;
|
50
|
-
// Optional timeout for forced termination
|
51
|
-
this.timeout = setTimeout(() => {
|
52
|
-
if (!this.languageServer.killed) {
|
53
|
-
console.log('Forcibly killing the process...');
|
54
|
-
this.languageServer.kill('SIGKILL');
|
55
|
-
}
|
56
|
-
}, 5000);
|
57
56
|
this.language = language;
|
58
57
|
this.sketchPath = sketchPath;
|
59
58
|
this.repoPath = repoPath;
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
59
|
+
this.ide = ide;
|
60
|
+
this.cursorPosition = cursorPosition;
|
61
|
+
switch (ide) {
|
62
|
+
case "vscode": {
|
63
|
+
// Most things will be done via vscode's command api
|
64
|
+
this.languageServer = null;
|
65
|
+
this.lspClient = null;
|
66
|
+
this.logStream = null;
|
67
|
+
}
|
68
|
+
case "standalone": {
|
69
|
+
const r = (() => {
|
70
|
+
switch (language) {
|
71
|
+
case "typescript": {
|
72
|
+
const sources = (0, utils_1.getAllTSFiles)(this.repoPath);
|
73
|
+
this.languageDriver = new typescript_driver_1.TypeScriptDriver(this.ide, sources, repoPath);
|
74
|
+
// PERF: 6ms
|
75
|
+
// return spawn("typescript-language-server", ["--stdio", "--log-level", "3"], { stdio: ["pipe", "pipe", "pipe"] });
|
76
|
+
return (0, child_process_1.spawn)("node", ["/home/jacob/projects/typescript-language-server/lib/cli.mjs", "--stdio", "--log-level", "4"], { stdio: ["pipe", "pipe", "pipe"] });
|
77
|
+
}
|
78
|
+
case "ocaml": {
|
79
|
+
this.languageDriver = new ocaml_driver_1.OcamlDriver();
|
80
|
+
try {
|
81
|
+
(0, child_process_2.execSync)(`eval $(opam env --switch=. --set-switch)`, { shell: "/bin/bash" });
|
82
|
+
// execSync("opam switch .", { shell: "/bin/bash" })
|
83
|
+
const currDir = __dirname;
|
84
|
+
process.chdir(path.dirname(sketchPath));
|
85
|
+
// execSync("which dune", { shell: "/bin/bash" })
|
86
|
+
(0, child_process_1.spawn)("dune", ["build", "-w"]);
|
87
|
+
process.chdir(currDir);
|
88
|
+
}
|
89
|
+
catch (err) {
|
90
|
+
console.log("ERROR:", err);
|
91
|
+
}
|
92
|
+
// TODO: Spawn a dune build -w on sketch directory.
|
93
|
+
// try {
|
94
|
+
// execSync("which dune", { shell: "/bin/bash" })
|
95
|
+
// spawn("dune", ["build", "-w"]);
|
96
|
+
// } catch (err) {
|
97
|
+
// console.log("ERROR:", err)
|
98
|
+
// }
|
99
|
+
// process.chdir(currDir);
|
100
|
+
return (0, child_process_1.spawn)("ocamllsp", ["--stdio"]);
|
101
|
+
}
|
79
102
|
}
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
103
|
+
})();
|
104
|
+
const e = new main_1.JSONRPCEndpoint(r.stdin, r.stdout);
|
105
|
+
const c = new main_1.LspClient(e);
|
106
|
+
this.languageServer = r;
|
107
|
+
this.lspClient = c;
|
108
|
+
const sources = (0, utils_1.getAllTSFiles)(this.repoPath);
|
109
|
+
this.languageDriver = new typescript_driver_1.TypeScriptDriver(this.ide, sources, repoPath);
|
110
|
+
// Logging tsserver output
|
111
|
+
const logStream = fs.createWriteStream(`tsserver-${(0, utils_1.getTimestampForFilename)()}.log`, { flags: "w" });
|
112
|
+
this.logStream = logStream;
|
113
|
+
// r.stdout.pipe(this.logStream);
|
114
|
+
// r.stderr.pipe(this.logStream);
|
115
|
+
// Helper function to prepend timestamps
|
116
|
+
const logWithTimestamp = (data) => {
|
117
|
+
var _a;
|
118
|
+
const timestamp = new Date().toISOString();
|
119
|
+
// console.log(timestamp)
|
120
|
+
// console.log(timestamp, data.toString())
|
121
|
+
(_a = this.logStream) === null || _a === void 0 ? void 0 : _a.write(`\n\n=*=*=*=*=*=[${timestamp}] ${data.toString()}\n\n`);
|
122
|
+
};
|
123
|
+
// Capture and log stdout and stderr with timestamps
|
124
|
+
// r.stdout.on("data", logWithTimestamp);
|
125
|
+
// r.stderr.on("data", logWithTimestamp);
|
126
|
+
// console.log(r.pid)
|
127
|
+
// this.languageServer.on('close', (code) => {
|
128
|
+
// if (code !== 0) {
|
129
|
+
// console.log(`ls process exited with code ${code}`);
|
130
|
+
// }
|
131
|
+
// });
|
132
|
+
// // Clear timeout once the process exits
|
133
|
+
// this.languageServer.on('exit', () => {
|
134
|
+
// // clearTimeout(this.timeout);
|
135
|
+
// this.logStream?.close();
|
136
|
+
// console.log('Process terminated cleanly.');
|
137
|
+
// });
|
138
|
+
// const logFile = fs.createWriteStream("log.txt");
|
139
|
+
// r.stdout.on('data', (d) => logFile.write(d));
|
140
|
+
this.languageDriver.init(this.lspClient, this.sketchPath);
|
141
|
+
// this.languageDriver.injectHole(this.sketchPath)
|
142
|
+
}
|
143
|
+
}
|
144
|
+
}
|
145
|
+
async init() {
|
146
|
+
await this.languageDriver.init(this.lspClient, this.sketchPath);
|
147
|
+
}
|
148
|
+
async run2(version) {
|
149
|
+
if (this.lspClient) {
|
150
|
+
const files = (0, utils_1.getAllTSFiles)(this.repoPath);
|
151
|
+
files.map(filepath => {
|
152
|
+
var _a;
|
153
|
+
if (fs.lstatSync(filepath).isFile()) {
|
154
|
+
(_a = this.lspClient) === null || _a === void 0 ? void 0 : _a.didOpen({
|
155
|
+
textDocument: {
|
156
|
+
uri: `file://${filepath}`,
|
157
|
+
languageId: "typescript",
|
158
|
+
text: fs.readFileSync(filepath).toString("ascii"),
|
159
|
+
version: version
|
160
|
+
}
|
161
|
+
});
|
89
162
|
}
|
163
|
+
});
|
164
|
+
}
|
165
|
+
// console.time("getHoleContext");
|
166
|
+
// PERF: 801ms
|
167
|
+
// const holeContext = await this.languageDriver.getHoleContext(
|
168
|
+
// this.lspClient,
|
169
|
+
// this.sketchPath,
|
170
|
+
// this.logStream
|
171
|
+
// );
|
172
|
+
// const holeContext = await this.languageDriver.getHoleContextWithCompilerAPI(
|
173
|
+
// this.sketchPath,
|
174
|
+
// this.logStream
|
175
|
+
// );
|
176
|
+
// this.cursorPosition = { line: 3, character: 56 };
|
177
|
+
const holeContext = await this.languageDriver.getHoleContextWithTreesitter(this.sketchPath, this.cursorPosition, this.logStream);
|
178
|
+
// console.timeEnd("getHoleContext");
|
179
|
+
// console.dir(holeContext, { depth: null })
|
180
|
+
// console.time("extractRelevantTypes");
|
181
|
+
const relevantTypes = await this.languageDriver.extractRelevantTypesWithTreesitter(this.lspClient, holeContext.fullHoverResult, holeContext.functionName, holeContext.range.start.line,
|
182
|
+
// holeContext.range.start.character,
|
183
|
+
new Map(), holeContext.source, new Map(), this.logStream);
|
184
|
+
// console.timeEnd("extractRelevantTypes");
|
185
|
+
// console.dir(relevantTypes, { depth: null })
|
186
|
+
// console.time("extractRelevantHeaders")
|
187
|
+
let repo = [];
|
188
|
+
if (this.language === "typescript") {
|
189
|
+
repo = (0, utils_1.getAllTSFiles)(this.repoPath);
|
190
|
+
}
|
191
|
+
else if (this.language === "ocaml") {
|
192
|
+
repo = (0, utils_1.getAllOCamlFiles)(this.repoPath);
|
193
|
+
}
|
194
|
+
const relevantHeaders = await this.languageDriver.extractRelevantHeadersWithTreesitter(this.lspClient, repo, relevantTypes, holeContext.functionTypeSpan, holeContext.functionName, this.repoPath);
|
195
|
+
// console.dir(relevantHeaders, { depth: null })
|
196
|
+
// console.timeEnd("extractRelevantHeaders")
|
197
|
+
const relevantTypesToReturn = new Map();
|
198
|
+
relevantTypes.forEach(({ typeSpan: v, sourceFile: src }, _) => {
|
199
|
+
if (relevantTypesToReturn.has(src)) {
|
200
|
+
const updated = relevantTypesToReturn.get(src);
|
201
|
+
updated.push(v);
|
202
|
+
relevantTypesToReturn.set(src, updated);
|
90
203
|
}
|
91
|
-
|
92
|
-
|
93
|
-
const c = new main_1.LspClient(e);
|
94
|
-
this.languageServer = r;
|
95
|
-
this.lspClient = c;
|
96
|
-
this.languageServer.on('close', (code) => {
|
97
|
-
if (code !== 0) {
|
98
|
-
console.log(`ls process exited with code ${code}`);
|
204
|
+
else {
|
205
|
+
relevantTypesToReturn.set(src, [v]);
|
99
206
|
}
|
100
207
|
});
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
208
|
+
const relevantHeadersToReturn = new Map();
|
209
|
+
relevantHeaders.forEach(({ typeSpan: v, sourceFile: src }) => {
|
210
|
+
// console.log(v, src)
|
211
|
+
if (relevantHeadersToReturn.has(src)) {
|
212
|
+
const updated = relevantHeadersToReturn.get(src);
|
213
|
+
if (!updated.includes(v)) {
|
214
|
+
updated.push(v);
|
215
|
+
}
|
216
|
+
relevantHeadersToReturn.set(src, updated);
|
217
|
+
}
|
218
|
+
else {
|
219
|
+
relevantHeadersToReturn.set(src, [v]);
|
220
|
+
}
|
105
221
|
});
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
222
|
+
this.result = {
|
223
|
+
holeType: holeContext.functionTypeSpan,
|
224
|
+
relevantTypes: relevantTypesToReturn,
|
225
|
+
relevantHeaders: relevantHeadersToReturn
|
226
|
+
};
|
111
227
|
}
|
112
|
-
async run() {
|
228
|
+
async run(version) {
|
229
|
+
// const profile = await pprof.time.start(10000); // Collect for 10s
|
113
230
|
// const outputFile = fs.createWriteStream("output.txt");
|
114
231
|
try {
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
232
|
+
// PERF: 94ms
|
233
|
+
// await this.init();
|
234
|
+
if (this.lspClient) {
|
235
|
+
const files = (0, utils_1.getAllTSFiles)(this.repoPath);
|
236
|
+
files.map(filepath => {
|
237
|
+
var _a;
|
238
|
+
if (fs.lstatSync(filepath).isFile()) {
|
239
|
+
(_a = this.lspClient) === null || _a === void 0 ? void 0 : _a.didOpen({
|
240
|
+
textDocument: {
|
241
|
+
uri: `file://${filepath}`,
|
242
|
+
languageId: "typescript",
|
243
|
+
text: fs.readFileSync(filepath).toString("ascii"),
|
244
|
+
version: version
|
245
|
+
}
|
246
|
+
});
|
247
|
+
}
|
248
|
+
});
|
127
249
|
}
|
128
|
-
|
250
|
+
console.time("getHoleContext");
|
251
|
+
// PERF: 801ms
|
252
|
+
// const holeContext = await this.languageDriver.getHoleContext(
|
253
|
+
// this.lspClient,
|
254
|
+
// this.sketchPath,
|
255
|
+
// this.logStream
|
256
|
+
// );
|
257
|
+
const holeContext = await this.languageDriver.getHoleContextWithCompilerAPI(this.sketchPath, this.logStream);
|
258
|
+
// console.dir(holeContext, { depth: null })
|
259
|
+
console.timeEnd("getHoleContext");
|
260
|
+
// console.time("extractRelevantTypes");
|
261
|
+
// await this.lspClient.documentSymbol({
|
262
|
+
// textDocument: {
|
263
|
+
// uri: `file://${this.repoPath}prelude.ts`,
|
264
|
+
// }
|
265
|
+
// });
|
266
|
+
// await this.lspClient.documentSymbol({
|
267
|
+
// textDocument: {
|
268
|
+
// uri: `file://${this.repoPath}injected_sketch.ts`,
|
269
|
+
// }
|
270
|
+
// });
|
271
|
+
// let start = performance.now()
|
272
|
+
// await this.lspClient.typeDefinition({
|
273
|
+
// textDocument: {
|
274
|
+
// uri: `file://${this.repoPath}injected_sketch.ts`,
|
275
|
+
// },
|
276
|
+
// position: {
|
277
|
+
// character: 35,
|
278
|
+
// line: 1
|
279
|
+
// }
|
280
|
+
// });
|
281
|
+
// let end = performance.now()
|
282
|
+
// console.log("elapsed:", end - start)
|
283
|
+
console.time("extractRelevantTypes");
|
284
|
+
// const relevantTypes = await this.languageDriver.extractRelevantTypes(
|
285
|
+
// this.lspClient,
|
286
|
+
// // NOTE: sometimes fullHoverResult isn't representative of the actual file contents, especially with generic functions.
|
287
|
+
// holeContext.trueHoleFunction ? holeContext.trueHoleFunction : holeContext.fullHoverResult,
|
288
|
+
// holeContext.functionName,
|
289
|
+
// holeContext.range.start.line, // NOTE: this could just default to 0, because we inject the true declaration at the top
|
290
|
+
// new Map<string, TypeSpanAndSourceFile>(),
|
291
|
+
// holeContext.source,
|
292
|
+
// new Map<string, string>(),
|
293
|
+
// this.logStream
|
294
|
+
// );
|
295
|
+
const relevantTypes = await this.languageDriver.extractRelevantTypesWithCompilerAPI(holeContext.trueHoleFunction ? holeContext.trueHoleFunction : holeContext.fullHoverResult, holeContext.functionName, holeContext.range.start.line, // NOTE: this could just default to 0, because we inject the true declaration at the top
|
296
|
+
holeContext.range.start.character, new Map(), holeContext.source, new Map(), this.logStream);
|
297
|
+
console.timeEnd("extractRelevantTypes");
|
298
|
+
// this.logStream.write(`\n\n=*=*=*=*=*=[begin extracting relevant headers][${new Date().toISOString()}]\n\n`);
|
299
|
+
// const sleep = (ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms));
|
300
|
+
// (async () => {
|
301
|
+
// console.log("Waiting...");
|
302
|
+
// await sleep(2000);
|
303
|
+
// console.log("Done!");
|
304
|
+
// })();
|
305
|
+
// console.timeEnd("extractRelevantTypes");
|
306
|
+
// console.dir(relevantTypes, { depth: null })
|
307
|
+
// Postprocess the map.
|
308
|
+
// console.time("extractRelevantTypes postprocess");
|
309
|
+
// if (this.language === Language.TypeScript) {
|
310
|
+
// relevantTypes.delete("_()");
|
311
|
+
// for (const [k, { typeSpan: v, sourceFile: src }] of relevantTypes.entries()) {
|
312
|
+
// relevantTypes.set(k, { typeSpan: v.slice(0, -1), sourceFile: src });
|
313
|
+
// }
|
314
|
+
// } else if (this.language === Language.OCaml) {
|
315
|
+
// relevantTypes.delete("_");
|
316
|
+
// }
|
317
|
+
// console.timeEnd("extractRelevantTypes postprocess");
|
318
|
+
// console.time("extractRelevantHeaders repo");
|
129
319
|
let repo = [];
|
130
|
-
if (this.language ===
|
320
|
+
if (this.language === "typescript") {
|
131
321
|
repo = (0, utils_1.getAllTSFiles)(this.repoPath);
|
132
322
|
}
|
133
|
-
else if (this.language ===
|
323
|
+
else if (this.language === "ocaml") {
|
134
324
|
repo = (0, utils_1.getAllOCamlFiles)(this.repoPath);
|
135
325
|
}
|
136
|
-
|
326
|
+
// console.timeEnd("extractRelevantHeaders repo");
|
327
|
+
// console.time("extractRelevantHeaders");
|
328
|
+
console.time("extractRelevantHeaders");
|
329
|
+
const relevantHeaders = await this.languageDriver.extractRelevantHeaders(this.lspClient, repo, relevantTypes, holeContext.functionTypeSpan, this.repoPath);
|
330
|
+
console.timeEnd("extractRelevantHeaders");
|
331
|
+
// const relevantHeaders: { typeSpan: string, sourceFile: string }[] = []
|
332
|
+
// console.timeEnd("extractRelevantHeaders");
|
137
333
|
// console.log(relevantHeaders)
|
334
|
+
// console.log(relevantHeaders.size)
|
335
|
+
// console.dir(relevantHeaders, { depth: null })
|
138
336
|
// Postprocess the map.
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
}
|
337
|
+
// console.time("extractRelevantHaders postprocess");
|
338
|
+
// if (this.language === Language.TypeScript) {
|
339
|
+
// relevantTypes.delete("");
|
340
|
+
// for (const [k, { typeSpan: v, sourceFile: src }] of relevantTypes.entries()) {
|
341
|
+
// relevantTypes.set(k, { typeSpan: v + ";", sourceFile: src });
|
342
|
+
// }
|
343
|
+
// for (const obj of relevantHeaders) {
|
344
|
+
// obj.typeSpan += ";";
|
345
|
+
// }
|
346
|
+
// }
|
347
|
+
// console.timeEnd("extractRelevantHeaders postprocess");
|
348
|
+
// console.time("toReturn");
|
148
349
|
const relevantTypesToReturn = new Map();
|
149
350
|
relevantTypes.forEach(({ typeSpan: v, sourceFile: src }, _) => {
|
150
351
|
if (relevantTypesToReturn.has(src)) {
|
@@ -158,6 +359,7 @@ class App {
|
|
158
359
|
});
|
159
360
|
const relevantHeadersToReturn = new Map();
|
160
361
|
relevantHeaders.forEach(({ typeSpan: v, sourceFile: src }) => {
|
362
|
+
// console.log(v, src)
|
161
363
|
if (relevantHeadersToReturn.has(src)) {
|
162
364
|
const updated = relevantHeadersToReturn.get(src);
|
163
365
|
if (!updated.includes(v)) {
|
@@ -169,11 +371,25 @@ class App {
|
|
169
371
|
relevantHeadersToReturn.set(src, [v]);
|
170
372
|
}
|
171
373
|
});
|
374
|
+
// console.timeEnd("toReturn");
|
172
375
|
this.result = {
|
173
376
|
holeType: holeContext.functionTypeSpan,
|
174
377
|
relevantTypes: relevantTypesToReturn,
|
175
378
|
relevantHeaders: relevantHeadersToReturn
|
176
379
|
};
|
380
|
+
if (this.lspClient) {
|
381
|
+
const files = (0, utils_1.getAllTSFiles)(this.repoPath);
|
382
|
+
files.map(filepath => {
|
383
|
+
var _a;
|
384
|
+
if (fs.lstatSync(filepath).isFile()) {
|
385
|
+
(_a = this.lspClient) === null || _a === void 0 ? void 0 : _a.didClose({
|
386
|
+
textDocument: {
|
387
|
+
uri: `file://${filepath}`,
|
388
|
+
}
|
389
|
+
});
|
390
|
+
}
|
391
|
+
});
|
392
|
+
}
|
177
393
|
}
|
178
394
|
catch (err) {
|
179
395
|
console.error("Error during execution:", err);
|
@@ -182,17 +398,23 @@ class App {
|
|
182
398
|
finally {
|
183
399
|
// outputFile.end();
|
184
400
|
}
|
401
|
+
// const buf = await pprof.encode(profile());
|
402
|
+
// fs.writeFile('wall.pb.gz', buf, (err) => {
|
403
|
+
// if (err) throw err;
|
404
|
+
// });
|
185
405
|
}
|
186
406
|
close() {
|
407
|
+
var _a;
|
187
408
|
// TODO:
|
188
409
|
try {
|
189
|
-
this.lspClient.exit();
|
410
|
+
(_a = this.lspClient) === null || _a === void 0 ? void 0 : _a.exit();
|
190
411
|
}
|
191
412
|
catch (err) {
|
192
413
|
console.log(err);
|
193
414
|
}
|
194
415
|
}
|
195
416
|
getSavedResult() {
|
417
|
+
// console.dir(this.result, { depth: null })
|
196
418
|
return this.result;
|
197
419
|
}
|
198
420
|
}
|
@@ -238,10 +460,10 @@ class CompletionEngine {
|
|
238
460
|
generateTypesAndHeadersPrompt(sketchFileContent, holeType, relevantTypes, relevantHeaders) {
|
239
461
|
let holeConstruct = "";
|
240
462
|
switch (this.language) {
|
241
|
-
case
|
463
|
+
case "typescript": {
|
242
464
|
holeConstruct = "_()";
|
243
465
|
}
|
244
|
-
case
|
466
|
+
case "ocaml": {
|
245
467
|
holeConstruct = "_";
|
246
468
|
}
|
247
469
|
}
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import Parser from "web-tree-sitter";
|
2
|
+
import { RangeInFileWithContents } from "./types";
|
3
|
+
export type AstPath = Parser.SyntaxNode[];
|
4
|
+
export declare function getAst(filepath: string, fileContents: string): Promise<Parser.Tree | undefined>;
|
5
|
+
export declare function getTreePathAtCursor(ast: Parser.Tree, cursorIndex: number): Promise<AstPath>;
|
6
|
+
export declare function getScopeAroundRange(range: RangeInFileWithContents): Promise<RangeInFileWithContents | undefined>;
|
package/dist/src/ast.js
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getAst = getAst;
|
4
|
+
exports.getTreePathAtCursor = getTreePathAtCursor;
|
5
|
+
exports.getScopeAroundRange = getScopeAroundRange;
|
6
|
+
const tree_sitter_1 = require("./tree-sitter");
|
7
|
+
async function getAst(filepath, fileContents) {
|
8
|
+
const parser = await (0, tree_sitter_1.getParserForFile)(filepath);
|
9
|
+
if (!parser) {
|
10
|
+
return undefined;
|
11
|
+
}
|
12
|
+
try {
|
13
|
+
const ast = parser.parse(fileContents);
|
14
|
+
if (!ast) {
|
15
|
+
return undefined;
|
16
|
+
}
|
17
|
+
return ast;
|
18
|
+
}
|
19
|
+
catch (e) {
|
20
|
+
return undefined;
|
21
|
+
}
|
22
|
+
}
|
23
|
+
async function getTreePathAtCursor(ast, cursorIndex) {
|
24
|
+
const path = [ast.rootNode];
|
25
|
+
while (path[path.length - 1].childCount > 0) {
|
26
|
+
let foundChild = false;
|
27
|
+
for (const child of path[path.length - 1].children) {
|
28
|
+
if (child && child.startIndex <= cursorIndex && child.endIndex >= cursorIndex) {
|
29
|
+
path.push(child);
|
30
|
+
foundChild = true;
|
31
|
+
break;
|
32
|
+
}
|
33
|
+
}
|
34
|
+
if (!foundChild) {
|
35
|
+
break;
|
36
|
+
}
|
37
|
+
}
|
38
|
+
return path;
|
39
|
+
}
|
40
|
+
async function getScopeAroundRange(range) {
|
41
|
+
var _a, _b, _c, _d;
|
42
|
+
const ast = await getAst(range.filepath, range.contents);
|
43
|
+
if (!ast) {
|
44
|
+
return undefined;
|
45
|
+
}
|
46
|
+
const { start: s, end: e } = range.range;
|
47
|
+
const lines = range.contents.split("\n");
|
48
|
+
const startIndex = lines.slice(0, s.line).join("\n").length +
|
49
|
+
((_b = (_a = lines[s.line]) === null || _a === void 0 ? void 0 : _a.slice(s.character).length) !== null && _b !== void 0 ? _b : 0);
|
50
|
+
const endIndex = lines.slice(0, e.line).join("\n").length +
|
51
|
+
((_d = (_c = lines[e.line]) === null || _c === void 0 ? void 0 : _c.slice(0, e.character).length) !== null && _d !== void 0 ? _d : 0);
|
52
|
+
let node = ast.rootNode;
|
53
|
+
while (node.childCount > 0) {
|
54
|
+
let foundChild = false;
|
55
|
+
for (const child of node.children) {
|
56
|
+
if (child && child.startIndex < startIndex && child.endIndex > endIndex) {
|
57
|
+
node = child;
|
58
|
+
foundChild = true;
|
59
|
+
break;
|
60
|
+
}
|
61
|
+
}
|
62
|
+
if (!foundChild) {
|
63
|
+
break;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
return {
|
67
|
+
contents: node.text,
|
68
|
+
filepath: range.filepath,
|
69
|
+
range: {
|
70
|
+
start: {
|
71
|
+
line: node.startPosition.row,
|
72
|
+
character: node.startPosition.column,
|
73
|
+
},
|
74
|
+
end: {
|
75
|
+
line: node.endPosition.row,
|
76
|
+
character: node.endPosition.column,
|
77
|
+
},
|
78
|
+
},
|
79
|
+
};
|
80
|
+
}
|
package/dist/src/codeql.js
CHANGED
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
15
15
|
}) : function(o, v) {
|
16
16
|
o["default"] = v;
|
17
17
|
});
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
};
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
19
|
+
var ownKeys = function(o) {
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
21
|
+
var ar = [];
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
23
|
+
return ar;
|
24
|
+
};
|
25
|
+
return ownKeys(o);
|
26
|
+
};
|
27
|
+
return function (mod) {
|
28
|
+
if (mod && mod.__esModule) return mod;
|
29
|
+
var result = {};
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
31
|
+
__setModuleDefault(result, mod);
|
32
|
+
return result;
|
33
|
+
};
|
34
|
+
})();
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
26
36
|
exports.getRelevantHeaders4 = exports.getRelevantHeaders3 = exports.getRelevantHeaders = exports.extractTypesAndLocations = exports.extractRelevantContextWithCodeQL = exports.extractHeadersWithCodeQL = exports.extractRelevantTypesWithCodeQL = exports.extractHoleType = exports.createDatabaseWithCodeQL = void 0;
|
27
37
|
const fs = __importStar(require("fs"));
|