Skip to content

Commit a56ed1e

Browse files
committed
feat: added findReferences and findSourceDefinition
1 parent 590f2fe commit a56ed1e

File tree

4 files changed

+79
-39
lines changed

4 files changed

+79
-39
lines changed

src/exp.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import createTSServerInstance from './utils/server.js';
2+
import {FILES} from "./utils/testconfig.js";
3+
4+
const tsServer = createTSServerInstance();
5+
await tsServer.init();
6+
console.log('server intializeed');
7+
8+
for (let file of FILES) {
9+
await tsServer.openFile(file.filepath);
10+
11+
const definitonResp = await tsServer.getDefinition(file.filepath, file.definition.line, file.definition.offset);
12+
console.log("resp+++", JSON.stringify(definitonResp));
13+
14+
const findReferencesResp = await tsServer.findReferences(file.filepath, file.reference.line, file.reference.offset);
15+
console.log('find reference resp', JSON.stringify(findReferencesResp));
16+
//await tsServer.killServer();
17+
const quickInfoResp = await tsServer.getQuickInfo(file.filepath, file.quickInfo.line, file.quickInfo.offset);
18+
console.log('quickInfoResp', JSON.stringify(quickInfoResp));
19+
20+
const findSourceDefinitionResp = await tsServer.findSourceDefinition(file.filepath, file.findSourceDefinition.line, file.findSourceDefinition.offset);
21+
console.log('findSourceDefinitionResp', JSON.stringify(findSourceDefinitionResp));
22+
}

src/index.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,3 @@
1-
import createTSServerInstance from './utils/startTsserver.js';
1+
import createTSServerInstance from "./utils/server.js";
22

3-
const tsServer = createTSServerInstance();
4-
await tsServer.init();
5-
console.log('server intializeed');
6-
await tsServer.openFile( '/home/charly/repo/vscode/extensions/typescript-language-features/web/src/workerSession.ts');
7-
8-
const definitonResp = await tsServer.getDefinition('/home/charly/repo/vscode/extensions/typescript-language-features/web/src/workerSession.ts', 26, 23)
9-
console.log("resp+++", JSON.stringify(definitonResp));
10-
11-
const findReferencesResp = await tsServer.getDefinition('/home/charly/repo/vscode/extensions/typescript-language-features/web/src/workerSession.ts', 11, 16)
12-
console.log('find reference resp',JSON.stringify(findReferencesResp))
13-
//await tsServer.killServer();
14-
15-
16-
// Initialize tsserver
17-
/*
18-
tsServer.init()
19-
.then(() => {
20-
console.log('tsserver is ready');
21-
22-
// Path to the TypeScript file you want to open
23-
const filePath = '/home/charly/repo/tsIntelligence/src/utils/startTsserver.js ';
24-
25-
// Open a TypeScript file
26-
return tsServer.openFile(filePath, 10000); // Set a 10-second timeout for response
27-
})
28-
.then(response => {
29-
console.log('File opened successfully:', response);
30-
// Handle the response for opening the file
31-
32-
// Optionally, you can kill the server after processing the response
33-
// tsServer.killServer();
34-
})
35-
.catch(error => {
36-
console.error('Error:', error);
37-
});
38-
*/
3+
export default createTSServerInstance;
Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function createTSServerInstance() {
1818
return new Promise((resolve, reject) => {
1919
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2020
const tsserverPath = path.join(__dirname, '..', '..', 'node_modules', 'typescript', 'bin', 'tsserver');
21-
const nodePath = '/home/charly/.nvm/versions/node/v20.10.0/bin/node';
21+
const nodePath = 'node';
2222
tsserverProcess = spawn(nodePath, [tsserverPath]);
2323
tsserverProcess.stdout.setEncoding('utf8');
2424

@@ -142,6 +142,25 @@ function createTSServerInstance() {
142142
return sendCommand(command);
143143
}
144144

145+
/**
146+
* Sends a 'quickinfo' request to the TypeScript Server.
147+
* @param {string} filePath - The path to the file.
148+
* @param {number} line - The line number of the position.
149+
* @param {number} offset - The offset in the line of the position.
150+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
151+
*/
152+
function getQuickInfo(filePath, line, offset) {
153+
const command = {
154+
command: "quickinfo",
155+
arguments: {
156+
file: filePath,
157+
line: line,
158+
offset: offset
159+
}
160+
};
161+
return sendCommand(command);
162+
}
163+
145164

146165
/**
147166
* Sends a 'references' request to the TypeScript Server.
@@ -162,6 +181,25 @@ function createTSServerInstance() {
162181
return sendCommand(command);
163182
}
164183

184+
/**
185+
* Sends a 'FindSourceDefinition' request to the TypeScript Server.
186+
* @param {string} filePath - The path to the file.
187+
* @param {number} line - The line number of the position.
188+
* @param {number} offset - The offset in the line of the position.
189+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
190+
*/
191+
function findSourceDefinition(filePath, line, offset) {
192+
const command = {
193+
command: "findSourceDefinition",
194+
arguments: {
195+
file: filePath,
196+
line: line,
197+
offset: offset
198+
}
199+
};
200+
return sendCommand(command);
201+
}
202+
165203
/**
166204
* Kills the TypeScript Server process.
167205
*/
@@ -178,7 +216,9 @@ function createTSServerInstance() {
178216
openFile,
179217
killServer: killTSServer,
180218
getDefinition: getDefinition,
181-
findReferences: findReferences
219+
findReferences: findReferences,
220+
getQuickInfo: getQuickInfo,
221+
findSourceDefinition: findSourceDefinition
182222
};
183223
}
184224

src/utils/testconfig.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const FILES = [{
2+
filepath: '/home/charly/repo/tsIntelligence/src/index.js',
3+
definition: {line: 11, offset: 49},
4+
reference: {line: 14, offset: 50},
5+
quickInfo: {line: 17, offset: 43},
6+
findSourceDefinition: {line: 21, offset: 48}
7+
}, {
8+
filepath: '/home/charly/repo/vscode/extensions/typescript-language-features/web/src/webServer.ts',
9+
definition: {line: 49, offset: 36},
10+
reference: {line: 49, offset: 36},
11+
quickInfo: {line: 49, offset: 36},
12+
findSourceDefinition: {line: 49, offset: 36}
13+
}];

0 commit comments

Comments
 (0)