Skip to content

Commit

Permalink
feat(qmllint): support notification from server to client
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwu1105 committed Aug 17, 2022
1 parent a98a537 commit cc6798a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
10 changes: 5 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ let qmlLintClient: LanguageClient | undefined = undefined
export async function activate({
asAbsolutePath,
extensionPath,
subscriptions,
}: ExtensionContext) {
const startResult = await startQmlLintClient({
asAbsolutePath,
extensionPath,
// eslint-disable-next-line no-console
onNotification: n => console.log(n.message),
})
if (startResult.kind === 'NotFoundError') {
// TODO: Show error to user
Expand All @@ -27,12 +30,9 @@ export async function activate({
return
}

subscriptions.push(startResult)

qmlLintClient = startResult.value
// TODO: Enable the following and add to subscriptions.
// qmlLintClient.onNotification(
// ErrorNotification,
// ({ message }: ErrorNotification) => console.error(message),
// )
}

export async function deactivate() {
Expand Down
28 changes: 23 additions & 5 deletions src/qmllint/client.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import * as path from 'node:path'
import type { ExtensionContext } from 'vscode'
import type {
Disposable,
LanguageClientOptions,
ServerOptions,
} from 'vscode-languageclient/node'
import { LanguageClient, TransportKind } from 'vscode-languageclient/node'
import type { NotFoundError } from '../python'
import { resolveScriptCommand } from '../python'
import type { SuccessResult } from '../result-types'
import type { InitializationOptions } from './server'
import type { InitializationOptions } from './server/server'
import { QmlLintNotification } from './server/server'

export async function startClient({
asAbsolutePath,
extensionPath,
onNotification,
}: StartClientArgs): Promise<StartClientResult> {
const serverModule = asAbsolutePath(path.join('out', 'qmllint', 'server.js'))
const serverModule = asAbsolutePath(
path.join('out', 'qmllint', 'server', 'main.js'),
)

const debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] }

Expand Down Expand Up @@ -46,17 +51,30 @@ export async function startClient({

const client = new LanguageClient('qmllint', serverOptions, clientOptions)

const disposables = [
client,
client.onNotification(QmlLintNotification, onNotification),
]

await client.start()

return { kind: 'Success', value: client }
return {
kind: 'Success',
value: client,
dispose: () => disposables.forEach(d => d.dispose()),
}
}

type StartClientArgs = Pick<
ExtensionContext,
'asAbsolutePath' | 'extensionPath'
>
> & {
readonly onNotification: (n: QmlLintNotification) => void
}

type StartClientResult = SuccessResult<LanguageClient> | NotFoundError
type StartClientResult =
| (SuccessResult<LanguageClient> & Disposable)
| NotFoundError

export async function stopClient(client: LanguageClient) {
return client.stop()
Expand Down
3 changes: 3 additions & 0 deletions src/qmllint/server/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { startServer } from './server'

startServer()
30 changes: 18 additions & 12 deletions src/qmllint/server.ts → src/qmllint/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {

import { fileURLToPath, pathToFileURL } from 'node:url'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { toDiagnostic } from './converters'
import { lint } from './lint'
import { toDiagnostic } from '../converters'
import { lint } from '../lint'

function startServer() {
export function startServer() {
let initializationOptions: InitializationOptions

const connection = createConnection(ProposedFeatures.all)
Expand All @@ -37,16 +37,19 @@ function startServer() {
})

if (result.kind === 'StdErrError') {
const n: ErrorNotification = { message: result.stderr }
return connection.sendNotification(ErrorNotification, n)
const n: QmlLintNotification = { kind: 'Error', message: result.stderr }
return connection.sendNotification(QmlLintNotification, n)
}
if (result.kind === 'ParseError') {
const n: ErrorNotification = result
return connection.sendNotification(ErrorNotification, n)
const n: QmlLintNotification = { kind: 'Error', message: result.message }
return connection.sendNotification(QmlLintNotification, n)
}
if (result.kind === 'ExecError') {
const n: ErrorNotification = { message: result.error.message }
return connection.sendNotification(ErrorNotification, n)
const n: QmlLintNotification = {
kind: 'Error',
message: result.error.message,
}
return connection.sendNotification(QmlLintNotification, n)
}

return result.value.files.forEach(file =>
Expand All @@ -64,7 +67,10 @@ function startServer() {

export type InitializationOptions = { readonly qmlLintCommand: string[] }

export const ErrorNotification = 'ErrorNotification'
export type ErrorNotification = { readonly message: string }
export const QmlLintNotification = 'QmlLintNotification'
export type QmlLintNotification = ErrorNotification

startServer()
type ErrorNotification = {
readonly kind: 'Error'
readonly message: string
}

0 comments on commit cc6798a

Please sign in to comment.