diff --git a/ruff_lsp/server.py b/ruff_lsp/server.py index ebce915..02fd656 100755 --- a/ruff_lsp/server.py +++ b/ruff_lsp/server.py @@ -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", @@ -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. @@ -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", []), @@ -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) @@ -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, diff --git a/ruff_lsp/settings.py b/ruff_lsp/settings.py index c5c3983..164ba6a 100644 --- a/ruff_lsp/settings.py +++ b/ruff_lsp/settings.py @@ -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`.""" @@ -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"]: