From f034d0ebaa4f903abb73323275357fb839612807 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 1 Oct 2023 22:58:20 -0400 Subject: [PATCH] Move args and run settings to lint.args and lint.run --- README.md | 22 ++++++++++----------- ruff_lsp/server.py | 32 +++++++++++++++++++----------- ruff_lsp/settings.py | 47 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 73 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index a437e28..d8cc20e 100644 --- a/README.md +++ b/README.md @@ -197,17 +197,17 @@ 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 | `[]` | 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. | -| run | `onType` | Run Ruff on every keystroke (`onType`) or on save (`onSave`). | -| 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. | -| codeAction.fixViolation.enable | `true` | Whether to display Quick Fix actions to autofix violations. | -| codeAction.disableRuleComment.enable | `true` | Whether to display Quick Fix actions to disable rules via `noqa` suppression comments. | +| Settings | Default | Description | +|--------------------------------------| -------- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| lint.args | `[]` | Additional command-line arguments to pass to `ruff check`, 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`. | +| lint.run | `onType` | Run Ruff on every keystroke (`onType`) or on save (`onSave`). | +| 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. | +| 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. | +| codeAction.fixViolation.enable | `true` | Whether to display Quick Fix actions to autofix violations. | +| logLevel | `error` | Sets the tracing level for the extension: `error`, `warn`, `info`, or `debug`. | +| codeAction.disableRuleComment.enable | `true` | Whether to display Quick Fix actions to disable rules via `noqa` suppression comments. | ## Development diff --git a/ruff_lsp/server.py b/ruff_lsp/server.py index 828d5fb..37d6b58 100755 --- a/ruff_lsp/server.py +++ b/ruff_lsp/server.py @@ -57,7 +57,12 @@ from typing_extensions import TypedDict from ruff_lsp import __version__, utils -from ruff_lsp.settings import UserSettings, WorkspaceSettings +from ruff_lsp.settings import ( + lint_args, + lint_run, + UserSettings, + WorkspaceSettings, +) from ruff_lsp.utils import RunResult logger = logging.getLogger(__name__) @@ -179,7 +184,7 @@ def did_close(params: DidCloseTextDocumentParams) -> None: async def did_save(params: DidSaveTextDocumentParams) -> None: """LSP handler for textDocument/didSave request.""" document = LSP_SERVER.workspace.get_document(params.text_document.uri) - if _get_settings_by_document(document).get("run", "onSave") == "onSave": + if lint_run(_get_settings_by_document(document), "onSave") == "onSave": diagnostics: list[Diagnostic] = await _lint_document_impl(document) LSP_SERVER.publish_diagnostics(document.uri, diagnostics) @@ -188,7 +193,7 @@ async def did_save(params: DidSaveTextDocumentParams) -> None: async def did_change(params: DidChangeTextDocumentParams) -> None: """LSP handler for textDocument/didChange request.""" document = LSP_SERVER.workspace.get_document(params.text_document.uri) - if _get_settings_by_document(document).get("run", "onType") == "onType": + if lint_run(_get_settings_by_document(document), "onType") == "onType": diagnostics: list[Diagnostic] = await _lint_document_impl(document) LSP_SERVER.publish_diagnostics(document.uri, diagnostics) @@ -938,15 +943,18 @@ def _supports_code_action_resolve(capabilities: ClientCapabilities) -> bool: def _get_global_defaults() -> UserSettings: return { + "codeAction": GLOBAL_SETTINGS.get("codeAction", {}), + "fixAll": GLOBAL_SETTINGS.get("fixAll", True), + "importStrategy": GLOBAL_SETTINGS.get("importStrategy", "fromEnvironment"), + "interpreter": GLOBAL_SETTINGS.get("interpreter", [sys.executable]), + "lint": GLOBAL_SETTINGS.get("lint", {}), "logLevel": GLOBAL_SETTINGS.get("logLevel", "error"), - "args": GLOBAL_SETTINGS.get("args", []), + "organizeImports": GLOBAL_SETTINGS.get("organizeImports", True), "path": GLOBAL_SETTINGS.get("path", []), - "interpreter": GLOBAL_SETTINGS.get("interpreter", [sys.executable]), - "importStrategy": GLOBAL_SETTINGS.get("importStrategy", "fromEnvironment"), + # Deprecated: use `lint.args` instead. + "args": GLOBAL_SETTINGS.get("args", []), + # Deprecated: use `lint.run` instead. "run": GLOBAL_SETTINGS.get("run", "onType"), - "codeAction": GLOBAL_SETTINGS.get("codeAction", {}), - "organizeImports": GLOBAL_SETTINGS.get("organizeImports", True), - "fixAll": GLOBAL_SETTINGS.get("fixAll", True), } @@ -993,7 +1001,9 @@ def _get_document_key(document: workspace.Document) -> str | None: return None -def _get_settings_by_document(document: workspace.Document | None) -> WorkspaceSettings: +def _get_settings_by_document( + document: workspace.Document | None, +) -> WorkspaceSettings: if document is None or document.path is None: return list(WORKSPACE_SETTINGS.values())[0] @@ -1133,7 +1143,7 @@ async def _run_check_on_document( executable = _find_ruff_binary(settings, VERSION_REQUIREMENT_LINTER) argv: list[str] = CHECK_ARGS + list(extra_args) - for arg in settings["args"]: + for arg in lint_args(settings["args"]): if arg in UNSUPPORTED_ARGS: log_to_output(f"Ignoring unsupported argument: {arg}") else: diff --git a/ruff_lsp/settings.py b/ruff_lsp/settings.py index 5be2103..e11953a 100644 --- a/ruff_lsp/settings.py +++ b/ruff_lsp/settings.py @@ -2,6 +2,8 @@ from typing_extensions import Literal, TypedDict +Run = Literal["onSave", "onType"] + class UserSettings(TypedDict, total=False): """Settings for the Ruff Language Server.""" @@ -9,9 +11,6 @@ class UserSettings(TypedDict, total=False): logLevel: Literal["error", "warning", "info", "debug"] """The log level for the Ruff server. Defaults to "error".""" - args: list[str] - """Additional command-line arguments to pass to `ruff`.""" - path: list[str] """Path to a custom `ruff` executable.""" @@ -21,9 +20,6 @@ class UserSettings(TypedDict, total=False): importStrategy: Literal["fromEnvironment", "useBundled"] """Strategy for loading the `ruff` executable.""" - run: Literal["onSave", "onType"] - """Run Ruff on every keystroke (`onType`) or on save (`onSave`).""" - codeAction: CodeActions """Settings for the `source.codeAction` capability.""" @@ -33,6 +29,17 @@ class UserSettings(TypedDict, total=False): fixAll: bool """Whether to register Ruff as capable of handling `source.fixAll`.""" + lint: Lint + """Settings specific to lint capabilities.""" + + # Deprecated: use `lint.args` instead. + args: list[str] + """Additional command-line arguments to pass to `ruff check`.""" + + # Deprecated: use `lint.run` instead. + run: Run + """Run Ruff on every keystroke (`onType`) or on save (`onSave`).""" + class WorkspaceSettings(TypedDict, UserSettings): cwd: str | None @@ -56,3 +63,31 @@ class CodeActions(TypedDict, total=False): class CodeAction(TypedDict, total=False): enable: bool """Whether to enable the code action.""" + + +class Lint(TypedDict, total=False): + args: list[str] + """Additional command-line arguments to pass to `ruff check`.""" + + run: Run + """Run Ruff on every keystroke (`onType`) or on save (`onSave`).""" + + +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"]: + return settings["lint"]["args"] + elif "args" in settings: + return settings["args"] + else: + return [] + + +def lint_run(settings: UserSettings, default: Run) -> Run: + """Get the `lint.run` setting from the user settings.""" + if "lint" in settings and "run" in settings["lint"]: + return settings["lint"]["run"] + elif "run" in settings: + return settings["run"] + else: + return default