Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
fix: restart qmlls client if the configurations are changed
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwu1105 committed Mar 5, 2023
1 parent ad3db49 commit b822c4e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
93 changes: 47 additions & 46 deletions src/qmlls/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Observable } from 'rxjs'
import {
combineLatest,
concatMap,
defer,
of,
Expand All @@ -22,6 +23,7 @@ import type { URI } from 'vscode-uri'
import { getEnabledFromConfig$ } from '../configurations'
import type { CommandArgs } from '../run'
import { run, wrapAndJoinCommandArgsWithQuotes } from '../run'
import type { ToolCommand } from '../tool-utils'
import { getToolCommand$ } from '../tool-utils'

export function registerQmlLanguageServer$({
Expand All @@ -30,62 +32,61 @@ export function registerQmlLanguageServer$({
}: RegisterQmlLanguageServerArgs) {
const client$ = new ReplaySubject<LanguageClient | undefined>(1)

const createClient$ = getToolCommand$({
tool: 'qmlls',
resource: undefined,
extensionUri,
}).pipe(
concatMap(result => {
if (result.kind !== 'Success') return of(result)
const createClient$ = (toolCommand: ToolCommand) =>
using(
() => {
const client = createClient({
command: toolCommand.command,
options: toolCommand.options,
outputChannel,
})
client$.next(client)

return defer(async () => {
const checkResult = await checkQmllsExists(result.value.command)
if (checkResult.kind !== 'Success') return checkResult
return result
})
}),
concatMap(result => {
if (result.kind !== 'Success') return of(result)
return { unsubscribe: () => client.dispose() }
},
() =>
client$.pipe(
stopPreviousClient(),
concatMap(client =>
defer(async () => {
await client?.start()
return {
kind: 'Success',
value: `qmlls is running with the command: '${wrapAndJoinCommandArgsWithQuotes(
[...toolCommand.command, ...toolCommand.options],
)}'`,
} as const
}),
),
),
)

return using(
() => {
const client = createClient({
command: result.value.command,
options: result.value.options,
outputChannel,
})
client$.next(client)
const createClientIfQmllsExists$ = (toolCommand: ToolCommand) =>
defer(async () => checkQmllsExists(toolCommand.command)).pipe(
concatMap(checkResult => {
if (checkResult.kind !== 'Success') return of(checkResult)
return createClient$(toolCommand)
}),
)

return { unsubscribe: () => client.dispose() }
},
() =>
client$.pipe(
stopPreviousClient(),
concatMap(client =>
defer(async () => {
await client?.start()
return {
kind: 'Success',
value: `qmlls is running with the command: '${wrapAndJoinCommandArgsWithQuotes(
[...result.value.command, ...result.value.options],
)}'`,
} as const
}),
),
),
)
return combineLatest([
getEnabledFromConfig$({ tool: 'qmlls', resource: undefined }),
getToolCommand$({
tool: 'qmlls',
resource: undefined,
extensionUri,
}),
)

return getEnabledFromConfig$({ tool: 'qmlls', resource: undefined }).pipe(
switchMap(enabled => {
]).pipe(
switchMap(([enabled, result]) => {
if (!enabled)
return defer(async () => {
client$.next(undefined)
return { kind: 'Success', value: 'qmlls has been stopped' } as const
})

return createClient$
if (result.kind !== 'Success') return of(result)

return createClientIfQmllsExists$(result.value)
}),
)
}
Expand Down
6 changes: 3 additions & 3 deletions src/tool-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Observable } from 'rxjs'
import { combineLatest, concatMap, defer, iif, map, of } from 'rxjs'
import { combineLatest, defer, iif, map, of, switchMap } from 'rxjs'
import type { URI } from 'vscode-uri'
import { getOptionsFromConfig$, getPathFromConfig$ } from './configurations'
import { resolveScriptCommand$ } from './python'
Expand All @@ -17,7 +17,7 @@ export function getToolCommand$({
getOptionsFromConfig$({ tool, resource }),
]),
).pipe(
concatMap(([path, options]) =>
switchMap(([path, options]) =>
iif(
() => path.length > 0,

Expand Down Expand Up @@ -51,7 +51,7 @@ export type GetToolCommandResult =
| SuccessResult<ToolCommand>
| ErrorResult<'NotFound'>

type ToolCommand = {
export type ToolCommand = {
readonly command: CommandArgs
readonly options: CommandArgs
}

0 comments on commit b822c4e

Please sign in to comment.