Skip to content

Commit

Permalink
Add a format.args setting to the LSP (#258)
Browse files Browse the repository at this point in the history
Analogous to `lint.args`.
  • Loading branch information
charliermarsh authored Oct 2, 2023
1 parent f9eba0c commit 8327c66
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
29 changes: 26 additions & 3 deletions ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ class VersionModified(NamedTuple):
"-",
]

# Arguments that are not allowed to be passed to Ruff.
UNSUPPORTED_ARGS = [
# Arguments that are not allowed to be passed to `ruff check`.
UNSUPPORTED_CHECK_ARGS = [
# Arguments that enforce required behavior. These can be ignored with a warning.
"--force-exclude",
"--no-cache",
Expand Down Expand Up @@ -158,6 +158,22 @@ class VersionModified(NamedTuple):
# "--format",
]

# Arguments that are not allowed to be passed to `ruff format`.
UNSUPPORTED_FORMAT_ARGS = [
# Arguments that enforce required behavior. These can be ignored with a warning.
"--force-exclude",
"--quiet",
# Arguments that contradict the required behavior. These can be ignored with a
# warning.
"-h",
"--help",
"--no-force-exclude",
"--silent",
"--verbose",
# Arguments that are not supported at all, and will error when provided.
# "--stdin-filename",
]


###
# Linting.
Expand Down Expand Up @@ -948,6 +964,7 @@ def _get_global_defaults() -> UserSettings:
"importStrategy": GLOBAL_SETTINGS.get("importStrategy", "fromEnvironment"),
"interpreter": GLOBAL_SETTINGS.get("interpreter", [sys.executable]),
"lint": GLOBAL_SETTINGS.get("lint", {}),
"format": GLOBAL_SETTINGS.get("format", {}),
"logLevel": GLOBAL_SETTINGS.get("logLevel", "error"),
"organizeImports": GLOBAL_SETTINGS.get("organizeImports", True),
"path": GLOBAL_SETTINGS.get("path", []),
Expand Down Expand Up @@ -1150,7 +1167,7 @@ async def _run_check_on_document(
argv: list[str] = CHECK_ARGS + list(extra_args)

for arg in lint_args(settings):
if arg in UNSUPPORTED_ARGS:
if arg in UNSUPPORTED_CHECK_ARGS:
log_to_output(f"Ignoring unsupported argument: {arg}")
else:
argv.append(arg)
Expand Down Expand Up @@ -1195,6 +1212,12 @@ async def _run_format_on_document(document: workspace.Document) -> RunResult | N
document.path,
]

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

return await run_path(
executable.path,
argv,
Expand Down
8 changes: 8 additions & 0 deletions ruff_lsp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class UserSettings(TypedDict, total=False):
lint: Lint
"""Settings specific to lint capabilities."""

format: Format
"""Settings specific to format capabilities."""

# Deprecated: use `lint.args` instead.
args: list[str]
"""Additional command-line arguments to pass to `ruff check`."""
Expand Down Expand Up @@ -73,6 +76,11 @@ class Lint(TypedDict, total=False):
"""Run Ruff on every keystroke (`onType`) or on save (`onSave`)."""


class Format(TypedDict, total=False):
args: list[str]
"""Additional command-line arguments to pass to `ruff format`."""


def lint_args(settings: UserSettings) -> list[str]:
"""Get the `lint.args` setting from the user settings."""
if "lint" in settings and "args" in settings["lint"]:
Expand Down

0 comments on commit 8327c66

Please sign in to comment.