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

Commit

Permalink
feat(qmllint): add qmllint path and options configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwu1105 committed Aug 25, 2022
1 parent d56b9d7 commit e978688
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 23 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
- [x] Support spaces in paths
- [x] Unit tests for ALL TypeScript scripts
- [ ] Resolve predefined variables in tool paths
- [ ] Configurations
- [x] Configurations
- [x] Enable/disable `qmllint` integration
- [ ] Set qmllint path
- [ ] Set qmllint args
- [x] Set qmllint path
- [x] Set qmllint args
- [x] Support multi-root projects
- [x] includes Python configuration
- [ ] Ensures we use resource scope
Expand Down
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@
"default": true,
"markdownDescription": "Enable the qmllint integration.",
"scope": "resource"
},
"qtForPython.qmllint.path": {
"type": "string",
"default": "",
"markdownDescription": "The path to `qmllint` executable. Set to empty string to automatically resolve from the installed Python package.",
"scope": "resource"
},
"qtForPython.qmllint.options": {
"type": "array",
"default": [],
"items": {
"type": "string"
},
"markdownDescription": "The options passed to `qmllint` executable.",
"scope": "resource"
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions python/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": ["--rcfile=python/.pylintrc"],
"python.linting.mypyEnabled": true,
"qtForPython.qmllint.enabled": true
"python.linting.mypyEnabled": true
}
74 changes: 58 additions & 16 deletions src/qmllint/client.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import * as path from 'node:path'
import type { ExtensionContext } from 'vscode'
import { workspace } from 'vscode'
import type {
Disposable,
DocumentUri,
LanguageClientOptions,
ServerOptions,
} from 'vscode-languageclient/node'
import { LanguageClient, TransportKind } from 'vscode-languageclient/node'
import { URI } from 'vscode-uri'
import { CONFIGURATION_NAMESPACE } from '../constants'
import { resolveScriptCommand } from '../python'
import type { ErrorResult, SuccessResult } from '../result-types'
import { notNil } from '../utils'
import type { QmlLintNotification } from './server/notifications'
import { QmlLintNotificationType } from './server/notifications'
import type { QmlLintCommandResponse } from './server/requests'
import { QmlLintCommandRequestType } from './server/requests'

export async function startClient({
Expand Down Expand Up @@ -41,22 +47,9 @@ export async function startClient({
const disposables = [
client,
client.onNotification(QmlLintNotificationType, onNotification),
client.onRequest(QmlLintCommandRequestType, async ({ resource }) => {
const resolveScriptCommandResult = await resolveScriptCommand({
scriptName: 'qmllint',
extensionPath,
resource,
})
if (resolveScriptCommandResult.kind === 'NotFoundError')
return resolveScriptCommandResult
return {
kind: 'Success',
value: {
command: resolveScriptCommandResult.value,
options: ['--json'],
},
} as const
}),
client.onRequest(QmlLintCommandRequestType, async ({ resource }) =>
resolveQmlLintCommand({ extensionPath, resource }),
),
]

await client.start()
Expand All @@ -79,6 +72,55 @@ type StartClientResult =
| (SuccessResult<LanguageClient> & Disposable)
| ErrorResult<'NotFound'>

async function resolveQmlLintCommand({
extensionPath,
resource,
}: ResolveQmlLintCommandArgs): Promise<QmlLintCommandResponse> {
const qmlLintOptions =
workspace
.getConfiguration(
`${CONFIGURATION_NAMESPACE}.qmllint`,
URI.parse(resource),
)
.get<readonly string[]>('options') ?? []

const qmlLintPath = workspace
.getConfiguration(`${CONFIGURATION_NAMESPACE}.qmllint`, URI.parse(resource))
.get<string>('path')

if (notNil(qmlLintPath) && qmlLintPath.length !== 0) {
return {
kind: 'Success',
value: {
command: [qmlLintPath],
options: qmlLintOptions,
},
}
}

const resolveScriptCommandResult = await resolveScriptCommand({
scriptName: 'qmllint',
extensionPath,
resource,
})

if (resolveScriptCommandResult.kind === 'NotFoundError')
return resolveScriptCommandResult

return {
kind: 'Success',
value: {
command: resolveScriptCommandResult.value,
options: qmlLintOptions,
},
}
}

type ResolveQmlLintCommandArgs = {
readonly extensionPath: string
readonly resource: DocumentUri
}

export async function stopClient(client: LanguageClient) {
return client.stop()
}
2 changes: 1 addition & 1 deletion src/qmllint/server/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type QmlLintCommandRequest = {
readonly resource: DocumentUri
}

type QmlLintCommandResponse =
export type QmlLintCommandResponse =
| SuccessResult<QmlLintCommand>
| ErrorResult<'NotFound'>

Expand Down
8 changes: 7 additions & 1 deletion src/qmllint/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ async function lintQml({ uri, connection }: LintQmlArgs) {
resource: uri,
connection,
})

if (requestIsEnabledResult.kind !== 'Success')
return sendQmlLintNotification({
notification: requestIsEnabledResult,
connection,
})

if (!requestIsEnabledResult.value)
return connection.sendDiagnostics({
uri,
Expand All @@ -69,10 +71,14 @@ async function lintQml({ uri, connection }: LintQmlArgs) {
connection,
})

const options = qmlLintCommandResult.value.options.includes('--json')
? qmlLintCommandResult.value.options
: [...qmlLintCommandResult.value.options, '--json']

const lintResult = await lint({
qmlLintCommand: qmlLintCommandResult.value.command,
documentPath: uriToPathResult.value,
options: qmlLintCommandResult.value.options,
options,
})

if (lintResult.kind === 'Success') {
Expand Down

0 comments on commit e978688

Please sign in to comment.