Skip to content

Commit b861bab

Browse files
committed
feat: added support for reload, rename, saveto, signatureHelp, status, typeDefinition
1 parent b1bee94 commit b861bab

File tree

3 files changed

+352
-12
lines changed

3 files changed

+352
-12
lines changed

src/exp.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,23 @@ for (let file of FILES) {
7373

7474
const documentHighlightsResp = await tsServer.documentHighlights(file.documentHighlights.filePath, file.documentHighlights.line, file.documentHighlights.offset, file.documentHighlights.filesToSearch);
7575
console.log('documentHighlightsResp', JSON.stringify(documentHighlightsResp));
76+
77+
const reloadResp = await tsServer.reload(file.reload.filePath, file.reload.tempFilePath);
78+
console.log('reloadResp', JSON.stringify(reloadResp));
79+
80+
const renameResp = await tsServer.rename(file.rename.filePath, file.rename.line, file.rename.offset, file.rename.findInComments, file.rename.findInStrings);
81+
console.log('renameResp', JSON.stringify(renameResp));
82+
83+
await tsServer.saveto(file.saveto.filePath, file.saveto.tempFilePath);
84+
85+
const signatureHelpResp = await tsServer.signatureHelp(file.signatureHelp.filePath, file.signatureHelp.line, file.signatureHelp.offset, file.signatureHelp.triggerReason);
86+
console.log('signatureHelpResp', JSON.stringify(signatureHelpResp));
87+
88+
const statusResp = await tsServer.status();
89+
console.log('statusResp', JSON.stringify(statusResp));
90+
91+
const typeDefinitionResp = await tsServer.typeDefinition(file.typeDefinition.filePath, file.typeDefinition.line, file.typeDefinition.offset);
92+
console.log('typeDefinitionResp', JSON.stringify(typeDefinitionResp));
93+
7694
}
7795
//tsServer.exitServer();

src/utils/server.js

Lines changed: 285 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ function createTSServerInstance() {
126126
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
127127
*/
128128
function sendCommand(command, timeout = 5000) {
129-
if (command.command === "open" || command.command === "geterr" || command.command === "geterrForProject") {
129+
if (command.command === "open" || command.command === "geterr" || command.command === "geterrForProject" || command.command === "saveto") {
130130
// For 'open' command, resolve immediately as no response is expected
131131
// geterr and geterrForProject returns result as events so resolve geterr and wait for response in events
132+
// saveTo command also does not return any response
132133
tsserverProcess.stdin.write(`${JSON.stringify(command)}\n`);
133134
return Promise.resolve();
134135
}
@@ -255,11 +256,32 @@ function createTSServerInstance() {
255256

256257

257258
/**
258-
* Sends a 'references' request to the TypeScript Server.
259-
* @param {string} filePath - The path to the file.
260-
* @param {number} line - The line number of the position.
261-
* @param {number} offset - The offset in the line of the position.
262-
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
259+
* Sends a 'references' request to the TypeScript Server. This command is used to
260+
* find all references to a symbol at a specified position in a file. It's commonly
261+
* used to identify where a variable, function, class, or other symbol is used throughout
262+
* the codebase.
263+
*
264+
* @param {string} filePath - The path to the TypeScript file.
265+
* @param {number} line - The line number where the symbol is located.
266+
* @param {number} offset - The character offset (position) in the line where the symbol is located.
267+
*
268+
* @returns {Promise<Object[]>} A promise that resolves with an array of reference items.
269+
* Each item represents a reference to the symbol and includes:
270+
* - `file`: The file in which the reference occurs.
271+
* - `start`: The starting position of the reference (line and character).
272+
* - `end`: The ending position of the reference (line and character).
273+
* - `lineText`: The text of the line containing the reference.
274+
* - `isWriteAccess`: A boolean indicating if the reference is a write access (modification).
275+
* - `isDefinition`: A boolean indicating if the reference is the definition of the symbol.
276+
*
277+
* Example usage:
278+
* ```
279+
* references('path/to/file.ts', 10, 5).then(refs => {
280+
* console.log('Symbol references:', refs);
281+
* });
282+
* ```
283+
* This function is crucial for understanding how and where symbols are used in a project,
284+
* facilitating code comprehension and refactoring.
263285
*/
264286
function findReferences(filePath, line, offset) {
265287
const command = {
@@ -274,11 +296,32 @@ function createTSServerInstance() {
274296
}
275297

276298
/**
277-
* Sends a 'FindSourceDefinition' request to the TypeScript Server.
278-
* @param {string} filePath - The path to the file.
279-
* @param {number} line - The line number of the position.
280-
* @param {number} offset - The offset in the line of the position.
281-
* @returns {Promise<Object>} A promise that resolves with the response from tsserver.
299+
* Sends a 'findSourceDefinition' request to the TypeScript Server. This command is utilized to
300+
* locate the original source definition of a symbol at a given position in a TypeScript file. It
301+
* is particularly useful for tracing the origin of symbols, especially in cases where symbols are
302+
* re-exported, helping developers navigate to the actual definition rather than a re-exported reference.
303+
*
304+
* @param {string} filePath - The path to the TypeScript file.
305+
* @param {number} line - The line number where the symbol whose definition is to be found is located.
306+
* @param {number} offset - The character offset within the line where the symbol is located.
307+
*
308+
* @returns {Promise<Object>} A promise that resolves with the location information of the symbol's
309+
* source definition. The response object typically includes:
310+
* - `file`: String indicating the file path where the source definition is located.
311+
* - `start`: Object representing the start position of the definition, containing:
312+
* - `line`: The line number of the start position (1-based).
313+
* - `offset`: The character offset at the start line (1-based).
314+
* - `end`: Object representing the end position of the definition, similar in structure
315+
* to the `start` object.
316+
*
317+
* Example usage:
318+
* ```
319+
* findSourceDefinition('path/to/file.ts', 10, 5).then(definition => {
320+
* console.log('Source definition location:', definition);
321+
* });
322+
* ```
323+
* This function is essential for developers in complex TypeScript projects, providing a means to
324+
* quickly navigate to the original declaration of symbols, enhancing code understanding and navigation.
282325
*/
283326
function findSourceDefinition(filePath, line, offset) {
284327
const command = {
@@ -912,6 +955,231 @@ function createTSServerInstance() {
912955
return sendCommand(command);
913956
}
914957

958+
/**
959+
* Sends a 'reload' request to the TypeScript Server to reload the contents of a file from disk.
960+
* This command is useful for ensuring the server's view of a file is synchronized with the latest
961+
* content, particularly after external changes to the file.
962+
*
963+
* @param {string} filePath - The path to the file to be reloaded.
964+
* @param {string} tempFilePath - The path to a temporary file that contains the new content. This allows
965+
* reloading the file content without modifying the original file on disk.
966+
* @param {string} [projectFileName] - Optional. The name of the project file (absolute pathname required)
967+
* that contains the TypeScript file. Providing this helps the TypeScript
968+
* server accurately interpret the file in the context of the specified project.
969+
*
970+
* @returns {Promise<Object>} A promise that resolves with the server's response after the file has been
971+
* reloaded. The response typically includes a status indicating whether the
972+
* reload was successful.
973+
*
974+
* Example usage:
975+
* ```
976+
* reload('path/to/file.ts', 'path/to/tempFile.ts', 'path/to/project.tsconfig.json').then(response => {
977+
* console.log('File reload response:', response);
978+
* });
979+
* ```
980+
* This function is essential in development environments where files are frequently modified
981+
* outside the editor and need to be synchronized with the TypeScript server.
982+
*/
983+
function reload(filePath, tempFilePath, projectFileName = "") {
984+
const command = {
985+
command: "reload",
986+
arguments: {
987+
file: filePath,
988+
tmpfile: tempFilePath,
989+
projectFileName: projectFileName
990+
}
991+
};
992+
return sendCommand(command);
993+
}
994+
995+
996+
/**
997+
* Sends a 'rename' request to the TypeScript Server to perform a comprehensive renaming operation
998+
* for a symbol at a specified location in a file. It updates references to the symbol across the
999+
* entire codebase, including in comments and strings if specified.
1000+
*
1001+
* @param {string} filePath - The path to the TypeScript file.
1002+
* @param {number} line - The line number where the symbol to be renamed is located.
1003+
* @param {number} offset - The character offset (position) in the line where the symbol is located.
1004+
* @param {boolean} [findInComments=false] - Whether to find/change the text in comments.
1005+
* @param {boolean} [findInStrings=false] - Whether to find/change the text in strings.
1006+
*
1007+
* @returns {Promise<Object>} A promise that resolves with the rename information from tsserver.
1008+
* The response object includes:
1009+
* - `canRename`: A boolean indicating if the symbol can be renamed.
1010+
* - `locs`: An array of location objects where each object represents
1011+
* a file with references to the symbol. Each location object includes:
1012+
* - `file`: The file in which references are found.
1013+
* - `locs`: An array of span objects representing the reference locations.
1014+
* Each span object includes:
1015+
* - `start`: The starting line and character position of the reference.
1016+
* - `end`: The ending line and character position of the reference.
1017+
* - `displayName`: The full display name of the symbol.
1018+
* - `fullDisplayName`: The full display name of the symbol with container information.
1019+
* - `kind`: The kind of the symbol (e.g., 'variable', 'function').
1020+
* - `kindModifiers`: The kind modifiers of the symbol (e.g., 'public', 'static').
1021+
*
1022+
* Example usage:
1023+
* ```
1024+
* rename('path/to/file.ts', 10, 5, true, true).then(renameInfo => {
1025+
* console.log('Rename information:', renameInfo);
1026+
* });
1027+
* ```
1028+
* This function is essential for refactoring, providing a safe and consistent way to rename symbols
1029+
* across a project, including their occurrences in comments and strings if required.
1030+
*/
1031+
//TODO: check multi file rename and functional object rename for js without config
1032+
function rename(filePath, line, offset, findInComments = false, findInStrings = false) {
1033+
const command = {
1034+
command: "rename",
1035+
arguments: {
1036+
file: filePath,
1037+
line: line,
1038+
offset: offset,
1039+
findInComments: findInComments,
1040+
findInStrings: findInStrings
1041+
}
1042+
};
1043+
return sendCommand(command);
1044+
}
1045+
1046+
/**
1047+
* Sends a 'saveto' request to the TypeScript Server. This command instructs the server
1048+
* to save the server's current view of a file's contents to a specified temporary file.
1049+
* It's primarily used for debugging purposes. Note that the server does not send a response
1050+
* to a "saveto" request.
1051+
*
1052+
* @param {string} filePath - The path to the original TypeScript file.
1053+
* @param {string} tempFilePath - The path where the server's view of the file's current contents should be saved.
1054+
* @param {string} [projectFileName] - Optional. The name of the project file (absolute pathname required)
1055+
* that contains the TypeScript file.
1056+
*
1057+
* Example usage:
1058+
* ```
1059+
* saveto('path/to/originalFile.ts', 'path/to/tempFile.ts', 'path/to/project.tsconfig.json');
1060+
* ```
1061+
* This command is useful for debugging, allowing the current state of the file as seen by the TypeScript server
1062+
* to be saved to a specific location.
1063+
*/
1064+
function saveto(filePath, tempFilePath, projectFileName = '') {
1065+
const command = {
1066+
command: "saveto",
1067+
arguments: {
1068+
file: filePath,
1069+
tmpfile: tempFilePath,
1070+
projectFileName: projectFileName
1071+
}
1072+
};
1073+
return sendCommand(command);
1074+
}
1075+
1076+
/**
1077+
* Sends a 'signatureHelp' request to the TypeScript Server. This command is used to obtain
1078+
* information about the signature of a function or method at a specific position in a file.
1079+
* The response includes details about the function signatures and their parameters.
1080+
*
1081+
* @param {string} filePath - The path to the TypeScript file.
1082+
* @param {number} line - The line number where the function or method is invoked.
1083+
* @param {number} offset - The character offset in the line where the invocation occurs.
1084+
* @param {Object} [triggerReason] - The reason why signature help was invoked, with properties:
1085+
* - `kind`: The type of trigger reason ('invoked', 'characterTyped', 'retrigger').
1086+
* - `triggerCharacter`: The character that triggered the help (for 'characterTyped').
1087+
*
1088+
* @returns {Promise<Object>} A promise that resolves with the signature help information, which includes:
1089+
* - `items`: Array of objects representing each signature. Each object includes:
1090+
* - `label`: String representation of the function signature.
1091+
* - `documentation`: Optional documentation for the function.
1092+
* - `parameters`: Array of parameter information objects, each with:
1093+
* - `label`: The parameter name.
1094+
* - `documentation`: Optional documentation for the parameter.
1095+
* - `applicableSpan`: Object representing the span for which signature help is applicable.
1096+
* - `selectedItemIndex`: Number indicating the default selected signature.
1097+
* - `argumentIndex`: Number indicating the index of the argument where the cursor is located.
1098+
* - `argumentCount`: Number indicating the total number of arguments in the function call.
1099+
*
1100+
* Example usage:
1101+
* ```
1102+
* signatureHelp('path/to/file.ts', 15, 10, { kind: 'characterTyped', triggerCharacter: '(' }).then(help => {
1103+
* console.log('Signature help:', help);
1104+
* });
1105+
* ```
1106+
* This function is essential for providing inline function/method signature information in development environments.
1107+
*/
1108+
//TODO: experiment usecases with different trigger reason
1109+
function signatureHelp(filePath, line, offset, triggerReason) {
1110+
const command = {
1111+
command: "signatureHelp",
1112+
arguments: {
1113+
file: filePath,
1114+
line: line,
1115+
offset: offset,
1116+
triggerReason: triggerReason
1117+
}
1118+
};
1119+
return sendCommand(command);
1120+
}
1121+
1122+
/**
1123+
* Sends a 'status' request to the TypeScript Server. This command queries the current
1124+
* status of the server, providing information about its operational state. This can
1125+
* include details such as the server's version, the number of projects currently loaded,
1126+
* and any ongoing operations.
1127+
*
1128+
* @returns {Promise<Object>} A promise that resolves with the status information from tsserver.
1129+
* The response typically includes details about the server's state,
1130+
* including its version and the status of loaded projects.
1131+
*
1132+
* Example usage:
1133+
* ```
1134+
* status().then(serverStatus => {
1135+
* console.log('TypeScript server status:', serverStatus);
1136+
* });
1137+
* ```
1138+
* This function is useful for monitoring the TypeScript server and diagnosing issues with its operation.
1139+
*/
1140+
function status() {
1141+
const command = {
1142+
command: "status"
1143+
};
1144+
return sendCommand(command);
1145+
}
1146+
1147+
/**
1148+
* Sends a 'typeDefinition' request to the TypeScript Server. This command is used to
1149+
* find the type definition of a symbol at a specified location in a TypeScript file.
1150+
* It is useful for navigating to the definition of the type of a symbol, such as
1151+
* the type of a variable, parameter, or property.
1152+
*
1153+
* @param {string} filePath - The path to the TypeScript file.
1154+
* @param {number} line - The line number where the symbol is located.
1155+
* @param {number} offset - The character offset (position) in the line where the symbol is located.
1156+
*
1157+
* @returns {Promise<Object>} A promise that resolves with the location of the symbol's type definition.
1158+
* The response typically includes:
1159+
* - `file`: The file path of the type definition.
1160+
* - `start`: The starting position of the type definition (line and character).
1161+
* - `end`: The ending position of the type definition (line and character).
1162+
*
1163+
* Example usage:
1164+
* ```
1165+
* typeDefinition('path/to/file.ts', 10, 5).then(definition => {
1166+
* console.log('Type definition location:', definition);
1167+
* });
1168+
* ```
1169+
* This function is crucial for understanding and navigating to the types used in a TypeScript codebase.
1170+
*/
1171+
function typeDefinition(filePath, line, offset) {
1172+
const command = {
1173+
command: "typeDefinition",
1174+
arguments: {
1175+
file: filePath,
1176+
line: line,
1177+
offset: offset
1178+
}
1179+
};
1180+
return sendCommand(command);
1181+
}
1182+
9151183
/**
9161184
* Terminates the TypeScript Server process.
9171185
* Warning: Use this function with caution. Prefer using the exitServer function for a graceful shutdown.
@@ -954,6 +1222,12 @@ function createTSServerInstance() {
9541222
getNavTree,
9551223
getNavTreeFull,
9561224
documentHighlights,
1225+
reload,
1226+
rename,
1227+
saveto,
1228+
signatureHelp,
1229+
status,
1230+
typeDefinition,
9571231
exitServer
9581232
};
9591233
}

0 commit comments

Comments
 (0)