-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
128 lines (117 loc) · 4.39 KB
/
Copy pathindex.ts
File metadata and controls
128 lines (117 loc) · 4.39 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
import * as fs from 'fs';
import CAPABILITIES from './capabilities.json';
import {LanguageIdentifiers, LanguageServer} from './LSPLanguageIdentifiers';
import path from 'path';
import {TextDocument} from './TextDocument';
import {FilePosition} from './FilePosition';
import {LSPFileLocation} from './LSPFileLocation';
const Client = require('rpc-websockets').Client;
const HOST = process.env.WS_HOST || 'localhost';
const LANGUAGE_SERVERS: Record<LanguageIdentifiers, LanguageServer> = {
[LanguageIdentifiers.PYTHON]: {port: 2087, extensions: ['.py']},
[LanguageIdentifiers.TYPESCRIPT]: {port: 2089, extensions: ['.ts']},
[LanguageIdentifiers.JAVASCRIPT]: {port: 2089, extensions: ['.js']},
};
const walkDir = (dir: string, extensions = ['']): string[] => {
return fs.readdirSync(dir).flatMap(entry => {
const fullPath = path.join(dir, entry);
if (fs.statSync(fullPath).isDirectory()) {
return walkDir(fullPath);
}
if (extensions && !extensions.some(e => fullPath.endsWith(e))) {
return [];
}
return [fullPath];
});
};
async function callLsp(
rootUri: string,
lang: LanguageIdentifiers
): Promise<number> {
const ls = LANGUAGE_SERVERS[lang];
const rpcClient = new Client(`ws://${HOST}:${ls.port}`);
const files = walkDir(rootUri, ls.extensions);
rpcClient.on('open', async () => {
rpcClient.on('textDocument/publishDiagnostics', () => {
console.log('Got a publishDiagnostics request, skipping');
});
await rpcClient.call('initialize', {
rootUri,
capabilities: CAPABILITIES,
});
await rpcClient.call('initialized');
for (const file of files) {
if (ls.extensions.some(e => file.endsWith(e))) {
const document = fs.readFileSync(file).toString();
const textDocument = new TextDocument(file, lang, document);
await rpcClient.call('textDocument/didOpen', {document, textDocument});
}
}
const interestingFile = path.join(rootUri, 'json_rpc_encoder.py');
const textDocument = new TextDocument(interestingFile, lang);
const symbols = await rpcClient.call('textDocument/documentSymbol', {
textDocument,
});
console.log(JSON.stringify(symbols, null, 4));
const fileReferences: {[word_position: string]: LSPFileLocation} = {};
const fileDefinitions: {[word_position: string]: LSPFileLocation} = {};
for (const f of files) {
const interestingFileContent = fs.readFileSync(f).toString();
let interestingFileLines: string[];
if (interestingFileContent.includes('\r\n')) {
interestingFileLines = interestingFileContent.split('\r\n');
} else {
interestingFileLines = interestingFileContent.split('\n');
}
const computedReferences: Set<string> = new Set();
for (let i = 0; i < interestingFileLines.length; i++) {
const lineText = interestingFileLines[i];
const words = lineText.split(/\s+|\)|\(|:|=|,|\./);
for (const w of words) {
if (w) {
const position = new FilePosition(i, lineText.indexOf(w));
const definitions = await rpcClient.call(
'textDocument/definition',
{
textDocument: {uri: f},
position,
}
);
if (definitions.length > 0) {
const localDefs = definitions.filter((d: LSPFileLocation) =>
d['uri'].startsWith(rootUri)
);
if (localDefs.length > 0) {
fileDefinitions[`${f}:${position.toString()}:${w}`] =
definitions;
}
}
if (!computedReferences.has(w)) {
fileReferences[`${f}:${position.toString()}:${w}`] =
await rpcClient.call('textDocument/references', {
textDocument: {uri: f},
position,
context: {includeDeclaration: true},
});
computedReferences.add(w);
}
}
}
}
}
fs.writeFileSync(
'../../results/file_references.json',
JSON.stringify(fileReferences, null, 2)
);
fs.writeFileSync(
'../../results/file_definitions.json',
JSON.stringify(fileDefinitions, null, 2)
);
rpcClient.close();
});
return 0;
}
const rootUri = "path/to/example/python/dir";
callLsp(rootUri, LanguageIdentifiers.PYTHON)
.then(() => console.log('Success'))
.catch(e => console.error(e));