Skip to content

Commit 3b6166b

Browse files
authored
Add showSyntaxErrors server setting (#454)
## Summary This PR adds a new `showSyntaxErrors` server setting for astral-sh/ruff#12059 in `ruff-lsp`. ## Test Plan ### VS Code Requires: astral-sh/ruff-vscode#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`: <img width="1177" alt="Screenshot 2024-06-27 at 08 34 58" src="https://github.com/astral-sh/ruff/assets/67177269/5d77547a-a908-4a00-8714-7c00784e8679"> And then set it to `false`: <img width="1185" alt="Screenshot 2024-06-27 at 08 35 19" src="https://github.com/astral-sh/ruff/assets/67177269/9720f089-f10c-420b-a2c1-2bbb2245be35"> ### 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`: <img width="1279" alt="Screenshot 2024-06-27 at 08 28 03" src="https://github.com/astral-sh/ruff/assets/67177269/e694e231-91ba-47f8-8e8a-ad2e82b85a45"> And then set it to `false`: <img width="1284" alt="Screenshot 2024-06-27 at 08 28 20" src="https://github.com/astral-sh/ruff/assets/67177269/25b86a57-02b1-44f7-9f65-cf5fdde93b0c">
1 parent e9e6363 commit 3b6166b

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ the following settings are supported:
259259
| logLevel | `error` | Sets the tracing level for the extension: `error`, `warn`, `info`, or `debug`. |
260260
| organizeImports | `true` | Whether to register Ruff as capable of handling `source.organizeImports` actions. |
261261
| path | `[]` | Path to a custom `ruff` executable, e.g., `["/path/to/ruff"]`. |
262+
| showSyntaxErrors | `true` | Whether to show syntax error diagnostics. _New in Ruff v0.5.0_ |
262263

263264
## Development
264265

ruff_lsp/server.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,11 @@ async def _lint_document_impl(
614614
show_error(f"Ruff: Lint failed ({result.stderr.decode('utf-8')})")
615615
return []
616616

617-
return _parse_output(result.stdout) if result.stdout else []
617+
return (
618+
_parse_output(result.stdout, settings.get("showSyntaxErrors", True))
619+
if result.stdout
620+
else []
621+
)
618622

619623

620624
def _parse_fix(content: Fix | LegacyFix | None) -> Fix | None:
@@ -649,7 +653,7 @@ def _parse_fix(content: Fix | LegacyFix | None) -> Fix | None:
649653
return fix
650654

651655

652-
def _parse_output(content: bytes) -> list[Diagnostic]:
656+
def _parse_output(content: bytes, show_syntax_errors: bool) -> list[Diagnostic]:
653657
"""Parse Ruff's JSON output."""
654658
diagnostics: list[Diagnostic] = []
655659

@@ -700,6 +704,8 @@ def _parse_output(content: bytes) -> list[Diagnostic]:
700704
# Cell represents the cell number in a Notebook Document. It is null for normal
701705
# Python files.
702706
for check in json.loads(content):
707+
if not show_syntax_errors and check["code"] is None:
708+
continue
703709
start = Position(
704710
line=max([int(check["location"]["row"]) - 1, 0]),
705711
character=int(check["location"]["column"]) - 1,
@@ -750,6 +756,7 @@ def _get_severity(code: str) -> DiagnosticSeverity:
750756
"F821", # undefined name `name`
751757
"E902", # `IOError`
752758
"E999", # `SyntaxError`
759+
None, # `SyntaxError` as of Ruff v0.5.0
753760
}:
754761
return DiagnosticSeverity.Error
755762
else:

ruff_lsp/settings.py

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class UserSettings(TypedDict, total=False):
4949
ignoreStandardLibrary: bool
5050
"""Whether to ignore files that are inferred to be part of the standard library."""
5151

52+
showSyntaxErrors: bool
53+
"""Whether to show syntax error diagnostics."""
54+
5255
# Deprecated: use `lint.args` instead.
5356
args: list[str]
5457
"""Additional command-line arguments to pass to `ruff check`."""

0 commit comments

Comments
 (0)