11import type { Observable } from 'rxjs'
22import {
3+ combineLatest ,
34 concatMap ,
45 defer ,
56 of ,
@@ -22,6 +23,7 @@ import type { URI } from 'vscode-uri'
2223import { getEnabledFromConfig$ } from '../configurations'
2324import type { CommandArgs } from '../run'
2425import { run , wrapAndJoinCommandArgsWithQuotes } from '../run'
26+ import type { ToolCommand } from '../tool-utils'
2527import { getToolCommand$ } from '../tool-utils'
2628
2729export function registerQmlLanguageServer$ ( {
@@ -30,62 +32,61 @@ export function registerQmlLanguageServer$({
3032} : RegisterQmlLanguageServerArgs ) {
3133 const client$ = new ReplaySubject < LanguageClient | undefined > ( 1 )
3234
33- const createClient$ = getToolCommand$ ( {
34- tool : 'qmlls' ,
35- resource : undefined ,
36- extensionUri,
37- } ) . pipe (
38- concatMap ( result => {
39- if ( result . kind !== 'Success' ) return of ( result )
35+ const createClient$ = ( toolCommand : ToolCommand ) =>
36+ using(
37+ ( ) => {
38+ const client = createClient ( {
39+ command : toolCommand . command ,
40+ options : toolCommand . options ,
41+ outputChannel,
42+ } )
43+ client$ . next ( client )
4044
41- return defer ( async ( ) => {
42- const checkResult = await checkQmllsExists ( result . value . command )
43- if ( checkResult . kind !== 'Success' ) return checkResult
44- return result
45- } )
46- } ) ,
47- concatMap ( result => {
48- if ( result . kind !== 'Success' ) return of ( result )
45+ return { unsubscribe : ( ) => client . dispose ( ) }
46+ } ,
47+ ( ) =>
48+ client$ . pipe (
49+ stopPreviousClient ( ) ,
50+ concatMap ( client =>
51+ defer ( async ( ) => {
52+ await client ?. start ( )
53+ return {
54+ kind : 'Success' ,
55+ value : `qmlls is running with the command: '${ wrapAndJoinCommandArgsWithQuotes (
56+ [ ...toolCommand . command , ...toolCommand . options ] ,
57+ ) } '`,
58+ } as const
59+ } ) ,
60+ ) ,
61+ ) ,
62+ )
4963
50- return using(
51- ( ) => {
52- const client = createClient ( {
53- command : result . value . command ,
54- options : result . value . options ,
55- outputChannel,
56- } )
57- client$ . next ( client )
64+ const createClientIfQmllsExists$ = ( toolCommand : ToolCommand ) =>
65+ defer ( async ( ) => checkQmllsExists ( toolCommand . command ) ) . pipe (
66+ concatMap ( checkResult => {
67+ if ( checkResult . kind !== 'Success' ) return of ( checkResult )
68+ return createClient$ ( toolCommand )
69+ } ) ,
70+ )
5871
59- return { unsubscribe : ( ) => client . dispose ( ) }
60- } ,
61- ( ) =>
62- client$ . pipe (
63- stopPreviousClient ( ) ,
64- concatMap ( client =>
65- defer ( async ( ) => {
66- await client ?. start ( )
67- return {
68- kind : 'Success' ,
69- value : `qmlls is running with the command: '${ wrapAndJoinCommandArgsWithQuotes (
70- [ ...result . value . command , ...result . value . options ] ,
71- ) } '`,
72- } as const
73- } ) ,
74- ) ,
75- ) ,
76- )
72+ return combineLatest ( [
73+ getEnabledFromConfig$ ( { tool : 'qmlls' , resource : undefined } ) ,
74+ getToolCommand$ ( {
75+ tool : 'qmlls' ,
76+ resource : undefined ,
77+ extensionUri,
7778 } ) ,
78- )
79-
80- return getEnabledFromConfig$ ( { tool : 'qmlls' , resource : undefined } ) . pipe (
81- switchMap ( enabled => {
79+ ] ) . pipe (
80+ switchMap ( ( [ enabled , result ] ) => {
8281 if ( ! enabled )
8382 return defer ( async ( ) => {
8483 client$ . next ( undefined )
8584 return { kind : 'Success' , value : 'qmlls has been stopped' } as const
8685 } )
8786
88- return createClient$
87+ if ( result . kind !== 'Success' ) return of ( result )
88+
89+ return createClientIfQmllsExists$ ( result . value )
8990 } ) ,
9091 )
9192}
0 commit comments