diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index 6628e91..86a9c21 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -125,6 +125,9 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator source = document.source settings = load_settings(workspace=workspace, document_path=document.path) + if not settings.format_enabled: + return + new_text = run_ruff_format( settings=settings, document_path=document.path, document_source=source ) @@ -720,6 +723,7 @@ def load_settings(workspace: Workspace, document_path: str) -> PluginSettings: # Leave config to pyproject.toml return PluginSettings( enabled=plugin_settings.enabled, + format_enabled=plugin_settings.format_enabled, executable=plugin_settings.executable, unsafe_fixes=plugin_settings.unsafe_fixes, extend_ignore=plugin_settings.extend_ignore, diff --git a/pylsp_ruff/settings.py b/pylsp_ruff/settings.py index 2345ce1..27af16f 100644 --- a/pylsp_ruff/settings.py +++ b/pylsp_ruff/settings.py @@ -8,6 +8,7 @@ @dataclass class PluginSettings: enabled: bool = True + format_enabled: bool = True executable: Optional[str] = None config: Optional[str] = None line_length: Optional[int] = None diff --git a/tests/test_ruff_format.py b/tests/test_ruff_format.py index 817d7be..3d7ef85 100644 --- a/tests/test_ruff_format.py +++ b/tests/test_ruff_format.py @@ -86,7 +86,7 @@ def force_result(self, r): if result.result: return result.result[0]["newText"] - return pytest.fail() + return "" def test_ruff_format_only(workspace): @@ -97,6 +97,15 @@ def test_ruff_format_only(workspace): assert want == got +def test_ruff_format_disabled(workspace): + _, doc = temp_document(_UNFORMATTED_CODE, workspace) + workspace._config.update( + {"plugins": {"ruff": {"format": ["I001"], "formatEnabled": False}}} + ) + got = run_plugin_format(workspace, doc) + assert got == "" + + def test_ruff_format_and_sort_imports(workspace): txt = f"{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}" want = f"{_SORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n"