Skip to content
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

Allow setting pylsp.plugins.ruff.format to ["ALL"] #101

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pylsp_ruff/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator
# specifying `format = ["I"]` to get import sorting as part of formatting.
new_text = run_ruff(
settings=PluginSettings(
ignore=["ALL"],
ignore=["ALL"] if "ALL" not in settings.format else [],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is admittedly a bit of a quick and dirty patch; would it be preferred to track this condition when the settings are parsed?

select=settings.format,
executable=settings.executable,
),
Expand Down
33 changes: 29 additions & 4 deletions tests/test_ruff_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"""
).strip()

_UNUSED_IMPORTS = tw.dedent(
"""
import unused_import
"""
).strip()

_SORTED_IMPORTS = tw.dedent(
"""
import asyncio
Expand All @@ -30,19 +36,19 @@

_UNFORMATTED_CODE = tw.dedent(
"""
def foo(): pass
def bar(): pass
def foo(): return asyncio.subprocess.DEVNULL
def bar(): return x(io.DEFAULT_BUFFER_SIZE)
"""
).strip()

_FORMATTED_CODE = tw.dedent(
"""
def foo():
pass
return asyncio.subprocess.DEVNULL


def bar():
pass
return x(io.DEFAULT_BUFFER_SIZE)
"""
).strip()

Expand Down Expand Up @@ -121,3 +127,22 @@ def test_ruff_format_and_sort_imports(workspace):
)
got = run_plugin_format(workspace, doc)
assert want == got


def test_ruff_format_via_all(workspace):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat of a fragile thing to test since it's at the mercy of whatever rules ruff might add. The tested text might be reasonably robust, but it also might be best to just leave this out.

# If `pylsp.plugins.ruff.format` contains "ALL", formatting should apply
# the automatic fixes for unsorted-imports (I001) and unused-import (F401)
txt = f"{_UNUSED_IMPORTS}\n{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}"
want = f"{_SORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n"
_, doc = temp_document(txt, workspace)
workspace._config.update(
{
"plugins": {
"ruff": {
"format": ["ALL"],
}
}
}
)
got = run_plugin_format(workspace, doc)
assert want == got
Loading