Skip to content

Commit 94927e7

Browse files
committed
add option to disable formatter
1 parent 335c288 commit 94927e7

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

pylsp_ruff/plugin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator
125125
source = document.source
126126

127127
settings = load_settings(workspace=workspace, document_path=document.path)
128+
if not settings.format_enabled:
129+
return
130+
128131
new_text = run_ruff_format(
129132
settings=settings, document_path=document.path, document_source=source
130133
)
@@ -720,6 +723,7 @@ def load_settings(workspace: Workspace, document_path: str) -> PluginSettings:
720723
# Leave config to pyproject.toml
721724
return PluginSettings(
722725
enabled=plugin_settings.enabled,
726+
format_enabled=plugin_settings.format_enabled,
723727
executable=plugin_settings.executable,
724728
unsafe_fixes=plugin_settings.unsafe_fixes,
725729
extend_ignore=plugin_settings.extend_ignore,

pylsp_ruff/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@dataclass
99
class PluginSettings:
1010
enabled: bool = True
11+
format_enabled: bool = True
1112
executable: Optional[str] = None
1213
config: Optional[str] = None
1314
line_length: Optional[int] = None

tests/test_ruff_format.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def force_result(self, r):
8686

8787
if result.result:
8888
return result.result[0]["newText"]
89-
return pytest.fail()
89+
return ""
9090

9191

9292
def test_ruff_format_only(workspace):
@@ -97,6 +97,15 @@ def test_ruff_format_only(workspace):
9797
assert want == got
9898

9999

100+
def test_ruff_format_disabled(workspace):
101+
_, doc = temp_document(_UNFORMATTED_CODE, workspace)
102+
workspace._config.update(
103+
{"plugins": {"ruff": {"format": ["I001"], "formatEnabled": False}}}
104+
)
105+
got = run_plugin_format(workspace, doc)
106+
assert got == ""
107+
108+
100109
def test_ruff_format_and_sort_imports(workspace):
101110
txt = f"{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}"
102111
want = f"{_SORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n"

0 commit comments

Comments
 (0)