|
5 | 5 | import sys |
6 | 6 | from pathlib import PurePath |
7 | 7 | from subprocess import PIPE, Popen |
8 | | -from typing import Dict, Final, Generator, List, Optional |
| 8 | +from typing import Dict, Generator, List, Optional |
9 | 9 |
|
10 | 10 | if sys.version_info >= (3, 11): |
11 | 11 | import tomllib |
|
62 | 62 | "H": DiagnosticSeverity.Hint, |
63 | 63 | } |
64 | 64 |
|
65 | | -ISORT_FIXES: Final = "I" |
66 | | - |
67 | 65 |
|
68 | 66 | class Subcommand(str, enum.Enum): |
69 | 67 | CHECK = "check" |
@@ -130,15 +128,20 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator |
130 | 128 | settings=settings, document_path=document.path, document_source=source |
131 | 129 | ) |
132 | 130 |
|
133 | | - settings.select = [ISORT_FIXES] |
134 | 131 | 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 | + fmtselect = settings.format |
| 136 | + settings = PluginSettings() |
| 137 | + settings.ignore = ["ALL"] |
| 138 | + settings.select = fmtselect |
| 139 | + new_text = run_ruff( |
| 140 | + settings=settings, |
| 141 | + document_path=document.path, |
| 142 | + document_source=new_text, |
| 143 | + fix=True, |
| 144 | + ) |
142 | 145 |
|
143 | 146 | # Avoid applying empty text edit |
144 | 147 | if not new_text or new_text == source: |
|
0 commit comments