1- import { spawn } from 'child_process' ;
1+ import { spawn } from 'child_process' ;
22import path from 'path' ;
3- import { fileURLToPath } from 'url' ;
3+ import { fileURLToPath } from 'url' ;
44
55/**
66 * Creates a new instance of TypeScript Server.
@@ -24,6 +24,7 @@ function createTSServerInstance() {
2424
2525 tsserverProcess . stdout . on ( 'data' , ( data ) => {
2626 const lines = data . split ( '\n' ) ;
27+ console . log ( lines ) ;
2728 for ( const line of lines ) {
2829 if ( line . trim ( ) . startsWith ( '{' ) ) {
2930 const message = JSON . parse ( line . trim ( ) ) ;
@@ -50,22 +51,18 @@ function createTSServerInstance() {
5051 } , 10000 ) ; // 10 seconds timeout
5152 } ) ;
5253 }
54+
5355 /**
5456 * Handles incoming data from the TypeScript Server.
5557 * @param {string } line - A line of data received from tsserver.
5658 */
5759 function onData ( line ) {
5860 try {
59- console . log ( line ) ;
6061 const response = JSON . parse ( line ) ;
61- // Check if it's a command response and has a matching sequence number
6262 if ( response . request_seq !== undefined && pendingCommands . has ( response . request_seq ) ) {
63- const { resolve } = pendingCommands . get ( response . request_seq ) ;
63+ const { resolve} = pendingCommands . get ( response . request_seq ) ;
6464 pendingCommands . delete ( response . request_seq ) ;
6565 resolve ( response ) ;
66- } else if ( response . type === 'event' ) {
67- // Handle or log event messages from tsserver
68- console . log ( 'Event from tsserver:' , response ) ;
6966 }
7067 } catch ( e ) {
7168 console . error ( 'Error parsing line from tsserver:' , e ) ;
@@ -74,32 +71,36 @@ function createTSServerInstance() {
7471
7572 /**
7673 * Sends a command to the TypeScript Server.
74+ * Special handling for 'open' command as it does not receive a response.
7775 * @param {Object } command - The command object to send.
7876 * @param {number } [timeout=5000] - The timeout in milliseconds for the command.
7977 * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
8078 */
8179 function sendCommand ( command , timeout = 5000 ) {
80+ if ( command . command === "open" ) {
81+ // For 'open' command, resolve immediately as no response is expected
82+ tsserverProcess . stdin . write ( `${ JSON . stringify ( command ) } \n` ) ;
83+ return Promise . resolve ( ) ;
84+ }
8285 return new Promise ( ( resolve , reject ) => {
8386 if ( ! tsserverProcess ) {
8487 reject ( new Error ( 'tsserver is not initialized' ) ) ;
8588 return ;
8689 }
8790
88- const seq = ++ seqNumber ;
89- pendingCommands . set ( seq , { resolve, reject } ) ;
91+ command . seq = ++ seqNumber ;
92+ command . type = 'request' ;
93+ pendingCommands . set ( command . seq , { resolve, reject} ) ;
9094
9195 const timeoutId = setTimeout ( ( ) => {
92- if ( pendingCommands . has ( seq ) ) {
93- pendingCommands . delete ( seq ) ;
96+ if ( pendingCommands . has ( command . seq ) ) {
97+ pendingCommands . delete ( command . seq ) ;
9498 reject ( new Error ( 'tsserver response timeout' ) ) ;
9599 }
96100 } , timeout ) ;
97101
98- command . seq = seq ;
99- command . type = 'request' ;
100- console . log ( command ) ;
101-
102102 if ( tsserverProcess . stdin . writable ) {
103+ console . log ( command ) ;
103104 tsserverProcess . stdin . write ( `${ JSON . stringify ( command ) } \n` ) ;
104105 } else {
105106 clearTimeout ( timeoutId ) ;
@@ -114,14 +115,53 @@ function createTSServerInstance() {
114115 * @param {number } timeout - The timeout in milliseconds for the command.
115116 * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
116117 */
117- function openFile ( filePath , timeout ) {
118+ function openFile ( filePath , timeout = 5000 ) {
118119 const command = {
119120 command : 'open' ,
120- arguments : { file : filePath }
121+ arguments : { file : filePath }
121122 } ;
122123 return sendCommand ( command , timeout ) ;
123124 }
124125
126+ /**
127+ * Sends a 'definition' request to the TypeScript Server.
128+ * @param {string } filePath - The path to the file.
129+ * @param {number } line - The line number of the position.
130+ * @param {number } offset - The offset in the line of the position.
131+ * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
132+ */
133+ function getDefinition ( filePath , line , offset ) {
134+ const command = {
135+ command : "definition" ,
136+ arguments : {
137+ file : filePath ,
138+ line : line ,
139+ offset : offset
140+ }
141+ } ;
142+ return sendCommand ( command ) ;
143+ }
144+
145+
146+ /**
147+ * Sends a 'references' request to the TypeScript Server.
148+ * @param {string } filePath - The path to the file.
149+ * @param {number } line - The line number of the position.
150+ * @param {number } offset - The offset in the line of the position.
151+ * @returns {Promise<Object> } A promise that resolves with the response from tsserver.
152+ */
153+ function findReferences ( filePath , line , offset ) {
154+ const command = {
155+ command : "references" ,
156+ arguments : {
157+ file : filePath ,
158+ line : line ,
159+ offset : offset
160+ }
161+ } ;
162+ return sendCommand ( command ) ;
163+ }
164+
125165 /**
126166 * Kills the TypeScript Server process.
127167 */
@@ -133,7 +173,13 @@ function createTSServerInstance() {
133173 }
134174 }
135175
136- return { init : initTSServer , openFile, killServer : killTSServer } ;
176+ return {
177+ init : initTSServer ,
178+ openFile,
179+ killServer : killTSServer ,
180+ getDefinition : getDefinition ,
181+ findReferences : findReferences
182+ } ;
137183}
138184
139185export default createTSServerInstance ;
0 commit comments