Skip to content

Commit 8e590a1

Browse files
committed
feat: added support for getCompletionDetails,getCompileOnSaveAffectedFileList,compileOnSaveEmitFile,getDefinitionAndBoundSpan,getImplementations,format,formatOnKey,
1 parent 781154e commit 8e590a1

File tree

3 files changed

+224
-6
lines changed

3 files changed

+224
-6
lines changed

src/exp.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ await tsServer.init();
66
console.log('server intializeed');
77

88
for (let file of FILES) {
9-
tsServer.openFile(file.filepath);
9+
tsServer.openFile(file.filepath);
1010

1111
const definitonResp = await tsServer.getDefinition(file.filepath, file.definition.line, file.definition.offset);
1212
console.log("resp+++", JSON.stringify(definitonResp));
@@ -20,9 +20,30 @@ for (let file of FILES) {
2020
const findSourceDefinitionResp = await tsServer.findSourceDefinition(file.filepath, file.findSourceDefinition.line, file.findSourceDefinition.offset);
2121
console.log('findSourceDefinitionResp', JSON.stringify(findSourceDefinitionResp));
2222

23-
const completionInfoResp = await tsServer.getCompletionInfo(file.filepath, file.getCompletionInfo.line, file.getCompletionInfo.offset);
23+
const completionInfoResp = await tsServer.getCompletionInfo(file.filepath, file.getCompletionInfo.line, file.getCompletionInfo.offset);
2424
console.assert('completionInfoResp', JSON.stringify(completionInfoResp));
2525

26+
const getCompletionDetailsResp = await tsServer.getCompletionDetails(file.filepath, file.getCompletionDetails.line, file.getCompletionDetails.offset, completionInfoResp.body.entries[0].name);
27+
console.assert('getCompletionDetailsResp', JSON.stringify(getCompletionDetailsResp));
28+
29+
const getCompileOnSaveResp = await tsServer.getCompileOnSaveAffectedFileList(file.filepath);
30+
console.assert('getCompileOnSaveResp', JSON.stringify(getCompileOnSaveResp));
31+
32+
const compileOnSaveEmitFileResp = await tsServer.compileOnSaveEmitFile(file.filepath);
33+
console.assert('compileOnSaveEmitFileResp', JSON.stringify(compileOnSaveEmitFileResp));
34+
35+
const getDefinitionAndBoundSpanResp = await tsServer.getDefinitionAndBoundSpan(file.filepath, file.getDefinitionAndBoundSpan.line, file.getDefinitionAndBoundSpan.offset);
36+
console.assert('getDefinitionAndBoundSpanResp', JSON.stringify(getDefinitionAndBoundSpanResp));
37+
38+
const getImplementationsResp = await tsServer.getImplementations(file.filepath, file.getImplementations.line, file.getImplementations.offset);
39+
console.assert('getImplementationsResp', JSON.stringify(getImplementationsResp));
40+
41+
const formatResp = await tsServer.format(file.filepath, file.format.line, file.format.offset, file.format.endLine, file.format.endOffset);
42+
console.assert('formatResp', JSON.stringify(formatResp));
43+
44+
const formatOnKeyResp = await tsServer.formatOnKey(file.filepath, file.formatOnKey.line, file.formatOnKey.offset, file.formatOnKey.key);
45+
console.assert('formatOnKeyResp', JSON.stringify(formatOnKeyResp));
46+
2647

2748
}
2849
tsServer.exitServer();

src/utils/server.js

Lines changed: 188 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {spawn} from 'child_process';
22
import path from 'path';
33
import {fileURLToPath} from 'url';
44

5+
// @INCLUDE_IN_API_DOCS
56
/**
67
* Creates a new instance of TypeScript Server.
78
* @returns {Object} An object containing methods to interact with TypeScript Server.
@@ -309,6 +310,141 @@ function createTSServerInstance() {
309310
return sendCommand(command);
310311
}
311312

313+
/**
314+
* Sends a 'completionEntryDetails' request to the TypeScript Server.
315+
* @param {string} filePath - The path to the file.
316+
* @param {number} line - The line number of the position.
317+
* @param {number} offset - The offset in the line of the position.
318+
* @param {string} entryName - The name of the completion entry.
319+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
320+
*/
321+
function getCompletionDetails(filePath, line, offset, entryName) {
322+
const command = {
323+
command: "completionEntryDetails",
324+
arguments: {
325+
file: filePath,
326+
line: line,
327+
offset: offset,
328+
entryNames: [entryName]
329+
}
330+
};
331+
return sendCommand(command);
332+
}
333+
334+
/**
335+
* Sends a 'compileOnSaveAffectedFileList' request to the TypeScript Server.
336+
* @param {string} filePath - The path to the file.
337+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
338+
*/
339+
function getCompileOnSaveAffectedFileList(filePath) {
340+
const command = {
341+
command: "compileOnSaveAffectedFileList",
342+
arguments: {
343+
file: filePath
344+
}
345+
};
346+
return sendCommand(command);
347+
}
348+
349+
/**
350+
* Sends a 'compileOnSaveEmitFile' command to the TypeScript Server.
351+
* @param {string} filePath - The path to the TypeScript file.
352+
* @param {boolean} [forced=false] - Force emit even if there are errors.
353+
* @param {boolean} [includeLinePosition=false] - Include line position in the response.
354+
* @param {boolean} [richResponse=false] - If true, returns response as an object with detailed emit results.
355+
* @returns {Promise<boolean | EmitResult>} A promise that resolves with a boolean indicating success
356+
* or an EmitResult object containing detailed information about the emit process.
357+
* - If a boolean: true if the emit was successful, false otherwise.
358+
* - If an EmitResult object:
359+
* - `emitSkipped`: A boolean indicating whether the emit was skipped.
360+
* - `diagnostics`: An array of Diagnostic or DiagnosticWithLinePosition objects, providing
361+
* detailed information about each diagnostic message.
362+
* - `Diagnostic`: An object representing a diagnostic message, typically containing:
363+
* - `start`: The starting position of the diagnostic message.
364+
* - `length`: The length of the diagnostic message.
365+
* - `text`: The text of the diagnostic message.
366+
* - `DiagnosticWithLinePosition`: An extended version of Diagnostic, including line and
367+
* character position information:
368+
* - `start`: The starting position of the diagnostic message (line and character).
369+
* - `end`: The ending position of the diagnostic message (line and character).
370+
* - `text`: The text of the diagnostic message.
371+
*/
372+
function compileOnSaveEmitFile(filePath, forced = false, includeLinePosition = false, richResponse = false) {
373+
const command = {
374+
command: "compileOnSaveEmitFile",
375+
arguments: {
376+
file: filePath,
377+
forced: forced,
378+
includeLinePosition: includeLinePosition,
379+
richResponse: richResponse
380+
}
381+
};
382+
return sendCommand(command);
383+
}
384+
385+
/**
386+
* Sends a 'definitionAndBoundSpan' request to the TypeScript Server.
387+
* @param {string} filePath - The path to the file.
388+
* @param {number} line - The line number of the position.
389+
* @param {number} offset - The offset in the line of the position.
390+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
391+
*/
392+
function getDefinitionAndBoundSpan(filePath, line, offset) {
393+
const command = {
394+
command: "definitionAndBoundSpan",
395+
arguments: {
396+
file: filePath,
397+
line: line,
398+
offset: offset
399+
}
400+
};
401+
return sendCommand(command);
402+
}
403+
404+
/**
405+
* Sends an 'implementation' request to the TypeScript Server.
406+
* @param {string} filePath - The path to the file.
407+
* @param {number} line - The line number of the position.
408+
* @param {number} offset - The offset in the line of the position.
409+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
410+
*/
411+
function getImplementations(filePath, line, offset) {
412+
const command = {
413+
command: "implementation",
414+
arguments: {
415+
file: filePath,
416+
line: line,
417+
offset: offset
418+
}
419+
};
420+
return sendCommand(command);
421+
}
422+
423+
/**
424+
* Sends a 'format' request to the TypeScript Server.
425+
* @param {string} filePath - The path to the file.
426+
* @param {number} startLine - The starting line number of the format range.
427+
* @param {number} startOffset - The starting offset in the start line.
428+
* @param {number} endLine - The ending line number of the format range.
429+
* @param {number} endOffset - The ending offset in the end line.
430+
* @param {object} [formatOptions] - Optional formatting options.
431+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
432+
*/
433+
function format(filePath, startLine, startOffset, endLine, endOffset, formatOptions = {}) {
434+
const command = {
435+
command: "format",
436+
arguments: {
437+
file: filePath,
438+
line: startLine,
439+
offset: startOffset,
440+
endLine: endLine,
441+
endOffset: endOffset,
442+
options: formatOptions
443+
}
444+
};
445+
return sendCommand(command);
446+
}
447+
312448
/**
313449
* Sends an 'exit' command to the TypeScript Server to gracefully shut it down.
314450
* @function exitServer
@@ -323,6 +459,50 @@ function createTSServerInstance() {
323459
tsserverProcess = null;
324460
}
325461

462+
/**
463+
* Sends a 'formatonkey' request to the TypeScript Server. This command is used
464+
* for formatting code at a specific position in a file, typically in response
465+
* to a keystroke. It's commonly used for auto-formatting a line of code when
466+
* a specific key (like a closing brace or semi-colon) is pressed.
467+
*
468+
* @param {string} filePath - The path to the TypeScript file. The path should
469+
* be absolute or relative to the TypeScript server's
470+
* current working directory.
471+
* @param {number} line - The 1-based line number in the file where the key was
472+
* pressed. This and the offset together point to the
473+
* specific position in the file.
474+
* @param {number} offset - The 1-based character offset in the line where the
475+
* key was pressed. This is typically the position
476+
* right after where the key was pressed.
477+
* @param {string} key - The character corresponding to the key pressed. This
478+
* is typically a single character like ';' or '}' that
479+
* triggers the formatting action.
480+
* @param {object} [formatOptions] - Optional formatting options to customize
481+
* the formatting behavior. These options might
482+
* include settings like tab size, indent size,
483+
* whether to insert spaces, and so on.
484+
* Example: { tabSize: 4, indentSize: 4, insertSpace: true }
485+
*
486+
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
487+
* The response typically includes an array of code edits
488+
* that should be applied to the file to achieve the
489+
* desired formatting. Each edit suggests changes like
490+
* text insertions, deletions, or replacements.
491+
*/
492+
function formatOnKey(filePath, line, offset, key, formatOptions = {}) {
493+
const command = {
494+
command: "formatonkey",
495+
arguments: {
496+
file: filePath,
497+
line: line,
498+
offset: offset,
499+
key: key,
500+
options: formatOptions
501+
}
502+
};
503+
return sendCommand(command);
504+
}
505+
326506
/**
327507
* Terminates the TypeScript Server process.
328508
* Warning: Use this function with caution. Prefer using the exitServer function for a graceful shutdown.
@@ -333,7 +513,7 @@ function createTSServerInstance() {
333513
if (tsserverProcess) {
334514
tsserverProcess.kill();
335515
tsserverProcess = null;
336-
console.log('tsserver process terminated');
516+
console.log('tsserver pgetCompletionDetailsrocess terminated');
337517
}
338518
}
339519

@@ -348,6 +528,13 @@ function createTSServerInstance() {
348528
getQuickInfo,
349529
findSourceDefinition,
350530
getCompletionInfo,
531+
getCompletionDetails,
532+
getCompileOnSaveAffectedFileList,
533+
compileOnSaveEmitFile,
534+
getDefinitionAndBoundSpan,
535+
getImplementations,
536+
format,
537+
formatOnKey,
351538
exitServer
352539
};
353540
}

src/utils/testconfig.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
export const FILES = [{
2-
filepath: '/home/charly/repo/tsIntelligence/src/index.js',
2+
filepath: '/home/charly/repo/tsIntelligence/src/exp.js',
33
definition: {line: 11, offset: 49},
4+
getDefinitionAndBoundSpan: {line: 11, offset: 49},
5+
getImplementations: {line: 11, offset: 49},
46
reference: {line: 14, offset: 50},
57
quickInfo: {line: 17, offset: 43},
68
findSourceDefinition: {line: 21, offset: 48},
7-
getCompletionInfo: {line: 20, offset: 52}
9+
getCompletionInfo: {line: 20, offset: 52},
10+
getCompletionDetails: {line: 20, offset: 52},
11+
format: {line: 9, offset: 0, endLine: 44, endOffset: 0},
12+
formatOnKey: {line: 44, offset: 154, key: ';'}
813

914
}, {
1015
filepath: '/home/charly/repo/vscode/extensions/typescript-language-features/web/src/webServer.ts',
1116
definition: {line: 49, offset: 36},
17+
getDefinitionAndBoundSpan: {line: 49, offset: 36},
18+
getImplementations: {line: 49, offset: 36},
1219
reference: {line: 49, offset: 36},
1320
quickInfo: {line: 49, offset: 36},
1421
findSourceDefinition: {line: 49, offset: 36},
15-
getCompletionInfo: {line: 40, offset: 40}
22+
getCompletionInfo: {line: 40, offset: 40},
23+
getCompletionDetails: {line: 40, offset: 37},
24+
format: {line: 9, offset: 0, endLine: 44, endOffset: 0},
25+
formatOnKey: {line: 23, offset: 76, key: ';'}
1626
}];

0 commit comments

Comments
 (0)