From 3b6166b3199fd20dabd96eb1f512de559bc6ee6d Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 27 Jun 2024 12:43:48 +0530 Subject: [PATCH] Add `showSyntaxErrors` server setting (#454) ## Summary This PR adds a new `showSyntaxErrors` server setting for https://github.com/astral-sh/ruff/pull/12059 in `ruff-lsp`. ## Test Plan ### VS Code Requires: https://github.com/astral-sh/ruff-vscode/pull/504 Verified that the VS Code extension is using the bundled `ruff-lsp` and the debug build of `ruff`: ``` 2024-06-27 08:47:49.567 [info] Server run command: /Users/dhruv/work/astral/ruff-vscode/.venv/bin/python /Users/dhruv/work/astral/ruff-vscode/bundled/tool/server.py 2024-06-27 08:47:49.820 [info] Using 'path' setting: /Users/dhruv/work/astral/ruff/target/debug/ruff 2024-06-27 08:47:49.827 [info] Inferred version 0.4.10 for: /Users/dhruv/work/astral/ruff/target/debug/ruff 2024-06-27 08:47:49.828 [info] Found ruff 0.4.10 at /Users/dhruv/work/astral/ruff/target/debug/ruff ``` Using the following VS Code config: ```json { "ruff.nativeServer": false, "ruff.path": ["/Users/dhruv/work/astral/ruff/target/debug/ruff"], "ruff.showSyntaxErrors": false } ``` First, set `ruff.showSyntaxErrors` to `true`: Screenshot 2024-06-27 at 08 34 58 And then set it to `false`: Screenshot 2024-06-27 at 08 35 19 ### Neovim Using the following Ruff server config: ```lua require('lspconfig').ruff_lsp.setup { cmd = { '/Users/dhruv/work/astral/ruff-lsp/.venv/bin/ruff-lsp' }, init_options = { settings = { path = { '/Users/dhruv/work/astral/ruff/target/debug/ruff' }, showSyntaxErrors = true, }, }, } ``` First, set `showSyntaxErrors` to `true`: Screenshot 2024-06-27 at 08 28 03 And then set it to `false`: Screenshot 2024-06-27 at 08 28 20 --- README.md | 1 + ruff_lsp/server.py | 11 +++++++++-- ruff_lsp/settings.py | 3 +++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9ed11ee..8a2f851 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,7 @@ the following settings are supported: | logLevel | `error` | Sets the tracing level for the extension: `error`, `warn`, `info`, or `debug`. | | organizeImports | `true` | Whether to register Ruff as capable of handling `source.organizeImports` actions. | | path | `[]` | Path to a custom `ruff` executable, e.g., `["/path/to/ruff"]`. | +| showSyntaxErrors | `true` | Whether to show syntax error diagnostics. _New in Ruff v0.5.0_ | ## Development diff --git a/ruff_lsp/server.py b/ruff_lsp/server.py index 1a7268d..b1febb0 100755 --- a/ruff_lsp/server.py +++ b/ruff_lsp/server.py @@ -614,7 +614,11 @@ async def _lint_document_impl( show_error(f"Ruff: Lint failed ({result.stderr.decode('utf-8')})") return [] - return _parse_output(result.stdout) if result.stdout else [] + return ( + _parse_output(result.stdout, settings.get("showSyntaxErrors", True)) + if result.stdout + else [] + ) def _parse_fix(content: Fix | LegacyFix | None) -> Fix | None: @@ -649,7 +653,7 @@ def _parse_fix(content: Fix | LegacyFix | None) -> Fix | None: return fix -def _parse_output(content: bytes) -> list[Diagnostic]: +def _parse_output(content: bytes, show_syntax_errors: bool) -> list[Diagnostic]: """Parse Ruff's JSON output.""" diagnostics: list[Diagnostic] = [] @@ -700,6 +704,8 @@ def _parse_output(content: bytes) -> list[Diagnostic]: # Cell represents the cell number in a Notebook Document. It is null for normal # Python files. for check in json.loads(content): + if not show_syntax_errors and check["code"] is None: + continue start = Position( line=max([int(check["location"]["row"]) - 1, 0]), character=int(check["location"]["column"]) - 1, @@ -750,6 +756,7 @@ def _get_severity(code: str) -> DiagnosticSeverity: "F821", # undefined name `name` "E902", # `IOError` "E999", # `SyntaxError` + None, # `SyntaxError` as of Ruff v0.5.0 }: return DiagnosticSeverity.Error else: diff --git a/ruff_lsp/settings.py b/ruff_lsp/settings.py index 9ed8007..0adda9a 100644 --- a/ruff_lsp/settings.py +++ b/ruff_lsp/settings.py @@ -49,6 +49,9 @@ class UserSettings(TypedDict, total=False): ignoreStandardLibrary: bool """Whether to ignore files that are inferred to be part of the standard library.""" + showSyntaxErrors: bool + """Whether to show syntax error diagnostics.""" + # Deprecated: use `lint.args` instead. args: list[str] """Additional command-line arguments to pass to `ruff check`."""