Skip to content

Commit

Permalink
Add showSyntaxErrors server setting (#454)
Browse files Browse the repository at this point in the history
## 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">
  • Loading branch information
dhruvmanila authored Jun 27, 2024
1 parent e9e6363 commit 3b6166b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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] = []

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions ruff_lsp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`."""
Expand Down

0 comments on commit 3b6166b

Please # to comment.