@@ -40,14 +40,25 @@ namespace TagCloseRequest {
40
40
) ;
41
41
}
42
42
43
- let lsApi : { getLS ( ) : LanguageClient } | undefined ;
43
+ let lsApi :
44
+ | {
45
+ getLS ( ) : LanguageClient ;
46
+ restartLS ( showNotification : boolean ) : Promise < void > ;
47
+ }
48
+ | undefined ;
44
49
45
50
export function activate ( context : ExtensionContext ) {
46
51
// The extension is activated on TS/JS/Svelte files because else it might be too late to configure the TS plugin:
47
52
// If we only activate on Svelte file and the user opens a TS file first, the configuration command is issued too late.
48
53
// We wait until there's a Svelte file open and only then start the actual language client.
49
54
const tsPlugin = new TsPlugin ( context ) ;
50
55
56
+ context . subscriptions . push (
57
+ commands . registerCommand ( 'svelte.restartLanguageServer' , async ( ) => {
58
+ await lsApi ?. restartLS ( true ) ;
59
+ } )
60
+ ) ;
61
+
51
62
if ( workspace . textDocuments . some ( ( doc ) => doc . languageId === 'svelte' ) ) {
52
63
lsApi = activateSvelteLanguageServer ( context ) ;
53
64
tsPlugin . askToEnable ( ) ;
@@ -150,7 +161,10 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {
150
161
console . log ( 'setting server runtime to' , serverRuntime ) ;
151
162
}
152
163
164
+ // Manually create the output channel so that it'll be reused and won't lose focus during restarts
165
+ const outputChannel = window . createOutputChannel ( 'Svelte' , 'svelte' ) ;
153
166
const clientOptions : LanguageClientOptions = {
167
+ outputChannel,
154
168
documentSelector : [ { scheme : 'file' , language : 'svelte' } ] ,
155
169
revealOutputChannelOn : RevealOutputChannelOn . Never ,
156
170
synchronize : {
@@ -187,7 +201,7 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {
187
201
}
188
202
} ;
189
203
190
- let ls = createLanguageServer ( serverOptions , clientOptions ) ;
204
+ const ls = createLanguageServer ( serverOptions , clientOptions ) ;
191
205
ls . start ( ) . then ( ( ) => {
192
206
const tagRequestor = ( document : TextDocument , position : Position ) => {
193
207
const param = ls . code2ProtocolConverter . asTextDocumentPositionParams (
@@ -222,22 +236,15 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {
222
236
}
223
237
} ) ;
224
238
225
- context . subscriptions . push (
226
- commands . registerCommand ( 'svelte.restartLanguageServer' , async ( ) => {
227
- await restartLS ( true ) ;
228
- } )
229
- ) ;
230
-
231
239
let restartingLs = false ;
232
240
async function restartLS ( showNotification : boolean ) {
233
241
if ( restartingLs ) {
234
242
return ;
235
243
}
236
244
237
245
restartingLs = true ;
238
- await ls . stop ( ) ;
239
- ls = createLanguageServer ( serverOptions , clientOptions ) ;
240
- await ls . start ( ) ;
246
+ outputChannel . clear ( ) ;
247
+ await ls . restart ( ) ;
241
248
if ( showNotification ) {
242
249
window . showInformationMessage ( 'Svelte language server restarted.' ) ;
243
250
}
@@ -325,7 +332,8 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {
325
332
} ) ;
326
333
327
334
return {
328
- getLS
335
+ getLS,
336
+ restartLS
329
337
} ;
330
338
}
331
339
0 commit comments