Skip to content

Add option to disable formatter #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pylsp_ruff/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pylsp_ruff/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion tests/test_ruff_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"
Expand Down