Skip to content

Commit 7db19c2

Browse files
committed
feat: added support for sendChange, closeFile, exitServer
1 parent a9c9ad4 commit 7db19c2

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

src/exp.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ 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+
tsServer.exitServer();

src/utils/server.js

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,48 @@ function createTSServerInstance() {
134134
return sendCommand(command, timeout);
135135
}
136136

137+
/**
138+
* Sends a 'change' command to the TypeScript Server.
139+
* @param {string} filePath - The path to the file.
140+
* @param {Object} start - The start position of the change (line and offset).
141+
* @param {Object} end - The end position of the change (line and offset).
142+
* @param {string} newText - The new text to replace in the range.
143+
*/
144+
function sendChange(filePath, start, end, newText) {
145+
const command = {
146+
command: "change",
147+
arguments: {
148+
file: filePath,
149+
line: start.line,
150+
offset: start.offset,
151+
endLine: end.line,
152+
endOffset: end.offset,
153+
insertString: newText
154+
}
155+
};
156+
// The 'change' command does not require a response from the server
157+
if (tsserverProcess && tsserverProcess.stdin.writable) {
158+
tsserverProcess.stdin.write(`${JSON.stringify(command)}\n`);
159+
}
160+
}
161+
162+
/**
163+
* Sends a 'close' command to the TypeScript Server.
164+
* @param {string} filePath - The path to the file being closed.
165+
*/
166+
function closeFile(filePath) {
167+
const command = {
168+
command: "close",
169+
arguments: {
170+
file: filePath
171+
}
172+
};
173+
// The 'close' command does not require a response from the server
174+
if (tsserverProcess && tsserverProcess.stdin.writable) {
175+
tsserverProcess.stdin.write(`${JSON.stringify(command)}\n`);
176+
}
177+
}
178+
137179
/**
138180
* Sends a 'definition' request to the TypeScript Server.
139181
* @param {string} filePath - The path to the file.
@@ -212,7 +254,24 @@ function createTSServerInstance() {
212254
}
213255

214256
/**
215-
* Kills the TypeScript Server process.
257+
* Sends an 'exit' command to the TypeScript Server to gracefully shut it down.
258+
* @function exitServer
259+
*/
260+
function exitServer() {
261+
if (tsserverProcess && tsserverProcess.stdin.writable) {
262+
const command = {
263+
command: "exit"
264+
};
265+
tsserverProcess.stdin.write(`${JSON.stringify(command)}\n`);
266+
}
267+
tsserverProcess = null;
268+
}
269+
270+
/**
271+
* Terminates the TypeScript Server process.
272+
* Warning: Use this function with caution. Prefer using the exitServer function for a graceful shutdown.
273+
* @see exitServer - Sends an 'exit' command to the TypeScript Server for a graceful shutdown.
274+
* @function killTSServer
216275
*/
217276
function killTSServer() {
218277
if (tsserverProcess) {
@@ -225,11 +284,14 @@ function createTSServerInstance() {
225284
return {
226285
init: initTSServer,
227286
openFile,
287+
sendChange,
288+
closeFile,
228289
killServer: killTSServer,
229-
getDefinition: getDefinition,
230-
findReferences: findReferences,
231-
getQuickInfo: getQuickInfo,
232-
findSourceDefinition: findSourceDefinition
290+
getDefinition,
291+
findReferences,
292+
getQuickInfo,
293+
findSourceDefinition,
294+
exitServer
233295
};
234296
}
235297

0 commit comments

Comments
 (0)