Skip to content

Commit 1572254

Browse files
committed
Make import sorting opt-in
1 parent 077f739 commit 1572254

File tree

3 files changed

+67
-47
lines changed

3 files changed

+67
-47
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ the valid configuration keys:
7272
- `pylsp.plugins.ruff.perFileIgnores`: File-specific error codes to be ignored.
7373
- `pylsp.plugins.ruff.select`: List of error codes to enable.
7474
- `pylsp.plugins.ruff.extendSelect`: Same as select, but append to existing error codes.
75-
- `pylsp.plugins.ruff.format`: List of error codes to fix during formatting. The default is `["I"]`, any additional codes are appended to this list.
75+
- `pylsp.plugins.ruff.format`: List of error codes to fix during formatting. Empty by default, use `["I"]` here to get import sorting as part of formatting.
7676
- `pylsp.plugins.ruff.unsafeFixes`: boolean that enables/disables fixes that are marked "unsafe" by `ruff`. `false` by default.
7777
- `pylsp.plugins.ruff.severities`: Dictionary of custom severity levels for specific codes, see [below](#custom-severities).
7878

pylsp_ruff/plugin.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
from pathlib import PurePath
77
from subprocess import PIPE, Popen
8-
from typing import Dict, Final, Generator, List, Optional
8+
from typing import Dict, Generator, List, Optional
99

1010
if sys.version_info >= (3, 11):
1111
import tomllib
@@ -62,8 +62,6 @@
6262
"H": DiagnosticSeverity.Hint,
6363
}
6464

65-
ISORT_FIXES: Final = "I"
66-
6765

6866
class Subcommand(str, enum.Enum):
6967
CHECK = "check"
@@ -130,15 +128,16 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator
130128
settings=settings, document_path=document.path, document_source=source
131129
)
132130

133-
settings.select = [ISORT_FIXES]
134131
if settings.format:
135-
settings.select.extend(settings.format)
136-
new_text = run_ruff(
137-
settings=settings,
138-
document_path=document.path,
139-
document_source=new_text,
140-
fix=True,
141-
)
132+
# A second pass on the document via `ruff check` with only the rules
133+
# enabled via the format config. This allows for things liek specifying
134+
# `format = ["I"]` to get import sorting as part of formatting.
135+
new_text = run_ruff(
136+
settings=PluginSettings(ignore=["ALL"], select=settings.format),
137+
document_path=document.path,
138+
document_source=new_text,
139+
fix=True,
140+
)
142141

143142
# Avoid applying empty text edit
144143
if not new_text or new_text == source:

tests/test_ruff_format.py

+56-35
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@
1111

1212
import pylsp_ruff.plugin as plugin
1313

14+
_UNSORTED_IMPORTS = tw.dedent(
15+
"""
16+
from thirdparty import x
17+
import io
18+
import asyncio
19+
"""
20+
).strip()
21+
22+
_SORTED_IMPORTS = tw.dedent(
23+
"""
24+
import asyncio
25+
import io
26+
27+
from thirdparty import x
28+
"""
29+
).strip()
30+
31+
_UNFORMATTED_CODE = tw.dedent(
32+
"""
33+
def foo(): pass
34+
def bar(): pass
35+
"""
36+
).strip()
37+
38+
_FORMATTED_CODE = tw.dedent(
39+
"""
40+
def foo():
41+
pass
42+
43+
44+
def bar():
45+
pass
46+
"""
47+
).strip()
48+
1449

1550
@pytest.fixture()
1651
def workspace(tmp_path):
@@ -54,40 +89,26 @@ def force_result(self, r):
5489
return pytest.fail()
5590

5691

57-
def test_ruff_format(workspace):
58-
# imports incorrectly ordered,
59-
# body of foo has a line that's too long
60-
# def bar() line missing whitespace above
61-
txt = tw.dedent(
62-
"""
63-
from thirdparty import x
64-
import io
65-
import asyncio
66-
67-
def foo():
68-
print("this is a looooooooooooooooooooooooooooooooooooooooooooong line that should exceed the usual line-length limit which is normally eighty-eight columns")
69-
def bar():
70-
pass
71-
""" # noqa: E501
72-
).lstrip()
73-
want = tw.dedent(
74-
"""
75-
import asyncio
76-
import io
77-
78-
from thirdparty import x
79-
80-
81-
def foo():
82-
print(
83-
"this is a looooooooooooooooooooooooooooooooooooooooooooong line that should exceed the usual line-length limit which is normally eighty-eight columns"
84-
)
85-
86-
87-
def bar():
88-
pass
89-
""" # noqa: E501
90-
).lstrip()
92+
def test_ruff_format_only(workspace):
93+
txt = f"{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}"
94+
want = f"{_UNSORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n"
95+
_, doc = temp_document(txt, workspace)
96+
got = run_plugin_format(workspace, doc)
97+
assert want == got
98+
99+
100+
def test_ruff_format_and_sort_imports(workspace):
101+
txt = f"{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}"
102+
want = f"{_SORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n"
91103
_, doc = temp_document(txt, workspace)
104+
workspace._config.update(
105+
{
106+
"plugins": {
107+
"ruff": {
108+
"format": ["I001"],
109+
}
110+
}
111+
}
112+
)
92113
got = run_plugin_format(workspace, doc)
93-
assert want == got, f"want:\n{want}\n\ngot:\n{got}"
114+
assert want == got

0 commit comments

Comments
 (0)