8
8
LanguageClient ,
9
9
ServerOptions ,
10
10
Executable ,
11
+ State as ClientState
11
12
} from 'vscode-languageclient/node' ;
12
13
import ShortUniqueId from 'short-unique-id' ;
13
14
import TelemetryReporter from 'vscode-extension-telemetry' ;
@@ -170,9 +171,17 @@ async function startClients(folders = prunedFolderNames()) {
170
171
if ( ! clients . has ( folder ) ) {
171
172
const commandPrefix = shortUid . seq ( ) ;
172
173
const client = newClient ( command , folder , commandPrefix ) ;
174
+ client . onReady ( ) . then ( ( ) => {
175
+ reporter . sendTelemetryEvent ( 'startClient' ) ;
176
+ } ) ;
177
+ client . onDidChangeState ( ( event ) => {
178
+ if ( event . newState === ClientState . Stopped ) {
179
+ clients . delete ( folder ) ;
180
+ reporter . sendTelemetryEvent ( 'stopClient' ) ;
181
+ }
182
+ } ) ;
173
183
disposables . push ( client . start ( ) ) ;
174
184
clients . set ( folder , { commandPrefix, client } ) ;
175
- reporter . sendTelemetryEvent ( 'startClient' ) ;
176
185
} else {
177
186
console . log ( `Client for folder: ${ folder } already started` ) ;
178
187
}
@@ -217,6 +226,10 @@ function newClient(cmd: string, location: string, commandPrefix: string) {
217
226
documentSelector : [ { scheme : 'file' , language : 'terraform' , pattern : `${ f . uri . fsPath } /**/*` } ] ,
218
227
workspaceFolder : f ,
219
228
initializationOptions : initializationOptions ,
229
+ initializationFailedHandler : ( error ) => {
230
+ reporter . sendTelemetryException ( error ) ;
231
+ return false ;
232
+ } ,
220
233
outputChannel : setup ,
221
234
revealOutputChannelOn : 4 // hide always
222
235
} ;
@@ -235,8 +248,6 @@ async function stopClients(folders = prunedFolderNames()) {
235
248
for ( const folder of folders ) {
236
249
if ( clients . has ( folder ) ) {
237
250
promises . push ( clients . get ( folder ) . client . stop ( ) ) ;
238
- clients . delete ( folder ) ;
239
- reporter . sendTelemetryEvent ( 'stopClient' ) ;
240
251
} else {
241
252
console . log ( `Attempted to stop a client for folder: ${ folder } but no client exists` ) ;
242
253
}
0 commit comments