@@ -40,14 +40,25 @@ namespace TagCloseRequest {
4040 ) ;
4141}
4242
43- let lsApi : { getLS ( ) : LanguageClient } | undefined ;
43+ let lsApi :
44+ | {
45+ getLS ( ) : LanguageClient ;
46+ restartLS ( showNotification : boolean ) : Promise < void > ;
47+ }
48+ | undefined ;
4449
4550export function activate ( context : ExtensionContext ) {
4651 // The extension is activated on TS/JS/Svelte files because else it might be too late to configure the TS plugin:
4752 // If we only activate on Svelte file and the user opens a TS file first, the configuration command is issued too late.
4853 // We wait until there's a Svelte file open and only then start the actual language client.
4954 const tsPlugin = new TsPlugin ( context ) ;
5055
56+ context . subscriptions . push (
57+ commands . registerCommand ( 'svelte.restartLanguageServer' , async ( ) => {
58+ await lsApi ?. restartLS ( true ) ;
59+ } )
60+ ) ;
61+
5162 if ( workspace . textDocuments . some ( ( doc ) => doc . languageId === 'svelte' ) ) {
5263 lsApi = activateSvelteLanguageServer ( context ) ;
5364 tsPlugin . askToEnable ( ) ;
@@ -150,7 +161,10 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {
150161 console . log ( 'setting server runtime to' , serverRuntime ) ;
151162 }
152163
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' ) ;
153166 const clientOptions : LanguageClientOptions = {
167+ outputChannel,
154168 documentSelector : [ { scheme : 'file' , language : 'svelte' } ] ,
155169 revealOutputChannelOn : RevealOutputChannelOn . Never ,
156170 synchronize : {
@@ -187,7 +201,7 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {
187201 }
188202 } ;
189203
190- let ls = createLanguageServer ( serverOptions , clientOptions ) ;
204+ const ls = createLanguageServer ( serverOptions , clientOptions ) ;
191205 ls . start ( ) . then ( ( ) => {
192206 const tagRequestor = ( document : TextDocument , position : Position ) => {
193207 const param = ls . code2ProtocolConverter . asTextDocumentPositionParams (
@@ -222,22 +236,15 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {
222236 }
223237 } ) ;
224238
225- context . subscriptions . push (
226- commands . registerCommand ( 'svelte.restartLanguageServer' , async ( ) => {
227- await restartLS ( true ) ;
228- } )
229- ) ;
230-
231239 let restartingLs = false ;
232240 async function restartLS ( showNotification : boolean ) {
233241 if ( restartingLs ) {
234242 return ;
235243 }
236244
237245 restartingLs = true ;
238- await ls . stop ( ) ;
239- ls = createLanguageServer ( serverOptions , clientOptions ) ;
240- await ls . start ( ) ;
246+ outputChannel . clear ( ) ;
247+ await ls . restart ( ) ;
241248 if ( showNotification ) {
242249 window . showInformationMessage ( 'Svelte language server restarted.' ) ;
243250 }
@@ -325,7 +332,8 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {
325332 } ) ;
326333
327334 return {
328- getLS
335+ getLS,
336+ restartLS
329337 } ;
330338}
331339
0 commit comments