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

Commit

Permalink
feat: show error notifications generated from Python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwu1105 committed Aug 18, 2022
1 parent d060b76 commit 90a6a8f
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 49 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
- [x] `qmllint` language server
- [ ] Support ini file
- [x] Show errors on notification
- [ ] Handle exceptions from Python scripts
- [x] Handle exceptions from Python scripts
- [x] Drop support to install Python dependencies
- [ ] Auto bump Python project version
- [ ] Support multi-root projects
- [ ] Support spaces in paths
- [ ] Unit tests for ALL TypeScript scripts
Expand Down
4 changes: 3 additions & 1 deletion python/.coveragerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[run]
omit = **/test_*.py
omit =
**/test_*.py
scripts/qmllint.py

[report]
fail_under = 100
Expand Down
1 change: 0 additions & 1 deletion python/scripts/__init__.py

This file was deleted.

6 changes: 5 additions & 1 deletion python/scripts/qmllint.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import re
import sys

from PySide6.scripts.pyside_tool import qmllint
from utils import is_installed

if __name__ == "__main__":
if is_installed("PySide6"):
from PySide6.scripts.pyside_tool import qmllint
else:
sys.exit("No qmllint can be found in current Python environment.")
sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0])
sys.exit(qmllint())
6 changes: 6 additions & 0 deletions python/scripts/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import importlib.util
import sys


def is_installed(name: str) -> bool:
return name in sys.modules or importlib.util.find_spec(name) is not None
6 changes: 0 additions & 6 deletions python/tests/test_qmllint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
import subprocess
import typing

import scripts.qmllint

from tests import ASSETS_DIR, SCRIPTS_DIR


def test_qmllint_is_callable():
assert callable(scripts.qmllint.qmllint)


def test_qmllint_version():
result = invoke_qmllint_py(["-v"])
assert result.returncode == 0
Expand Down
5 changes: 0 additions & 5 deletions python/tests/test_scripts.py

This file was deleted.

6 changes: 6 additions & 0 deletions python/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from scripts.utils import is_installed


def test_is_installed():
assert is_installed("not_exist_module") == False
assert is_installed("PySide6") == True
14 changes: 8 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// TODO: 1. odd behavior when checking is QmlLintType!!!
// TODO: 2. it seems that qmllint does not support whole file content
// TODO: 3. make onNotification work

import type { ExtensionContext } from 'vscode'
import { window } from 'vscode'
import type { LanguageClient } from 'vscode-languageclient/node'
Expand Down Expand Up @@ -37,8 +33,14 @@ export async function activate({

function onNotification(n: QmlLintNotification) {
switch (n.kind) {
case 'Error':
window.showErrorMessage(n.message)
case 'ParseError':
return window.showErrorMessage(n.message)
case 'ExecError':
return window.showErrorMessage(
`${n.stderr}\n${n.stdout}\n${n.error.message ?? ''}`,
)
case 'StdErrError':
return window.showErrorMessage(`${n.stderr}\n${n.stdout}`)
}
}

Expand Down
39 changes: 12 additions & 27 deletions src/qmllint/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {

import { fileURLToPath, pathToFileURL } from 'node:url'
import { TextDocument } from 'vscode-languageserver-textdocument'
import type { ErrorResult } from '../../result-types'
import type { ExecError, StdErrError } from '../../run'
import { toDiagnostic } from '../converters'
import { lint } from '../lint'

Expand All @@ -32,28 +34,16 @@ export function startServer() {
options: ['--json'],
})

if (result.kind === 'StdErrError') {
const n: QmlLintNotification = { kind: 'Error', message: result.stderr }
return connection.sendNotification(QmlLintNotification, n)
}
if (result.kind === 'ParseError') {
const n: QmlLintNotification = { kind: 'Error', message: result.message }
return connection.sendNotification(QmlLintNotification, n)
}
if (result.kind === 'ExecError') {
const n: QmlLintNotification = {
kind: 'Error',
message: result.error.message,
}
return connection.sendNotification(QmlLintNotification, n)
}
if (result.kind === 'Success')
return result.value.files.forEach(file =>
connection.sendDiagnostics({
uri: pathToFileURL(file.filename).href,
diagnostics: file.warnings.map(w => toDiagnostic(w)),
}),
)

return result.value.files.forEach(file =>
connection.sendDiagnostics({
uri: pathToFileURL(file.filename).href,
diagnostics: file.warnings.map(w => toDiagnostic(w)),
}),
)
const notification: QmlLintNotification = result
return connection.sendNotification(QmlLintNotification, notification)
}

documents.listen(connection)
Expand All @@ -64,9 +54,4 @@ export function startServer() {
export type InitializationOptions = { readonly qmlLintCommand: string[] }

export const QmlLintNotification = 'QmlLintNotification'
export type QmlLintNotification = ErrorNotification

type ErrorNotification = {
readonly kind: 'Error'
readonly message: string
}
export type QmlLintNotification = ErrorResult<'Parse'> | ExecError | StdErrError
2 changes: 1 addition & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type RunResult = SuccessResult<string> | ExecError | StdErrError

export type ExecError = {
readonly kind: 'ExecError'
readonly error: ExecException
readonly error: Partial<ExecException>
readonly stdout: string
readonly stderr: string
}
Expand Down

0 comments on commit 90a6a8f

Please sign in to comment.