Skip to content

Commit

Permalink
Automatically ignore unsupported command-line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 16, 2023
1 parent 5b7d837 commit bda5435
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,20 @@ Upon successful installation, you should see errors surfaced directly in your ed

![](https://user-images.githubusercontent.com/1309177/209418462-ae106d1f-dbc3-4d53-bae2-66bfccc3e841.png)


### Example: Kate

To use `ruff-lsp` with [Kate](https://kate-editor.org/), add something like the following to
the LSP client's `settings.json`:

```json
{
"servers": {
"python": {
"command": ["ruff-lsp"],
"url": "https://github.com/charliermarsh/ruff-lsp",
"highlightingModeRegex": "^Python$"
}
"servers": {
"python": {
"command": ["ruff-lsp"],
"url": "https://github.com/charliermarsh/ruff-lsp",
"highlightingModeRegex": "^Python$"
}
}
}
```

Expand All @@ -181,15 +180,15 @@ the LSP client's `settings.json`:
The exact mechanism by which settings will be passed to `ruff-lsp` will vary by editor. However,
the following settings are supported:

| Settings | Default | Description |
| ---------------- | ------- | ---------------------------------------------------------------------------------------- |
| args | `[]` | Custom arguments passed to `ruff`. E.g `"args": ["--config=/path/to/pyproject.toml"]`. |
| logLevel | `error` | Sets the tracing level for the extension. |
| path | `[]` | Setting to provide custom `ruff` executables, to try in order. E.g. `["/path/to/ruff"]`. |
| interpreter | `[]` | Path to a Python interpreter to use to run the linter server. |
| showNotification | `off` | Setting to control when a notification is shown. |
| organizeImports | `true` | Whether to register Ruff as capable of handling `source.organizeImports` actions. |
| fixAll | `true` | Whether to register Ruff as capable of handling `source.fixAll` actions. |
| Settings | Default | Description |
| ---------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| args | `[]` | Additional command-line arguments to pass to `ruff`, e.g., `"args": ["--config=/path/to/pyproject.toml"]`. Supports a subset of Ruff's command-line arguments, ignoring those that are required to operate the LSP, like `--force-exclude` and `--verbose`. |
| logLevel | `error` | Sets the tracing level for the extension: `error`, `warn`, `info`, or `debug`. |
| path | `[]` | Path to a custom `ruff` executable, e.g., `["/path/to/ruff"]`. |
| interpreter | `[]` | Path to a Python interpreter to use to run the linter server. |
| showNotification | `off` | Setting to control when a notification is shown. |
| organizeImports | `true` | Whether to register Ruff as capable of handling `source.organizeImports` actions. |
| fixAll | `true` | Whether to register Ruff as capable of handling `source.fixAll` actions. |

## Development

Expand Down
37 changes: 36 additions & 1 deletion ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@

TOOL_MODULE = "ruff.exe" if platform.system() == "Windows" else "ruff"
TOOL_DISPLAY = "Ruff"

# Arguments provided to every Ruff invocation.
TOOL_ARGS = [
"--force-exclude",
"--no-cache",
Expand All @@ -99,6 +101,33 @@
"-",
]

# Arguments that are not allowed to be passed to Ruff.
TOOL_NON_ARGS = [
# Arguments that enforce required behavior.
"--force-exclude",
"--no-cache",
"--no-fix",
"--quiet",
# Arguments that contradict the required behavior.
"--diff",
"--exit-non-zero-on-fix",
"-e",
"--exit-zero",
"--fix",
"--fix-only",
"-h",
"--help",
"--no-force-exclude",
"--show-files",
"--show-fixes",
"--show-settings",
"--show-source",
"--silent",
"--statistics",
"--verbose",
"-w",
"--watch",
]

###
# Linting.
Expand Down Expand Up @@ -1017,7 +1046,13 @@ def _run_tool_on_document(
settings = _get_settings_by_document(document)

executable = _executable_path(settings)
argv: list[str] = [executable] + TOOL_ARGS + settings["args"] + list(extra_args)
argv: list[str] = [executable] + TOOL_ARGS + list(extra_args)

for arg in settings["args"]:
if arg in TOOL_NON_ARGS:
log_to_output(f"Ignoring unsupported argument: {arg}")
else:
argv.append(arg)

# If we're trying to run a single rule, add it to the command line, and disable
# all other rules (if the Ruff version is sufficiently recent).
Expand Down

0 comments on commit bda5435

Please sign in to comment.