Skip to content

Commit 06eba1b

Browse files
authored
Add unfixable config option (#109)
1 parent 5928f5f commit 06eba1b

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pylsp = {
5959
format = { "I" }, -- Rules that are marked as fixable by ruff that should be fixed when running textDocument/formatting
6060
severities = { ["D212"] = "I" }, -- Optional table of rules where a custom severity is desired
6161
unsafeFixes = false, -- Whether or not to offer unsafe fixes as code actions. Ignored with the "Fix All" action
62+
unfixable = { "F401" }, -- Rules that are excluded when checking the code actions (including the "Fix All" action)
6263

6364
-- Rules that are ignored when a pyproject.toml or ruff.toml is present:
6465
lineLength = 88, -- Line length to pass to ruff checking and formatting
@@ -93,6 +94,7 @@ pylsp = {
9394
"D212": "I"
9495
},
9596
"unsafeFixes": false,
97+
"unfixable": [ "F401" ],
9698
"lineLength": 88,
9799
"exclude": ["__about__.py"],
98100
"select": ["F"],
@@ -143,7 +145,7 @@ The `Fix all` code action *only* consideres safe fixes.
143145
The log level can be set via the `cmd` option of `pylsp`:
144146

145147
```lua
146-
lspconfig.pylsp.setup {
148+
vim.lsp.config("pylsp", {
147149
cmd = {"pylsp", "-vvv", "--log-file", "/tmp/lsp.log"},
148150
settings = {
149151
pylsp = {
@@ -154,5 +156,5 @@ lspconfig.pylsp.setup {
154156
}
155157
}
156158
}
157-
}
159+
})
158160
```

pylsp_ruff/plugin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,9 @@ def build_check_arguments(
628628
if settings.unsafe_fixes:
629629
args.append("--unsafe-fixes")
630630

631+
if settings.unfixable:
632+
args.append(f"--unfixable={','.join(settings.unfixable)}")
633+
631634
if settings.exclude:
632635
args.append(f"--exclude={','.join(settings.exclude)}")
633636

@@ -730,8 +733,8 @@ def load_settings(workspace: Workspace, document_path: str) -> PluginSettings:
730733
731734
"""
732735
config = workspace._config
733-
_plugin_settings = config.plugin_settings("ruff", document_path=document_path)
734-
plugin_settings = converter.structure(_plugin_settings, PluginSettings)
736+
plugin_settings = config.plugin_settings("ruff", document_path=document_path)
737+
plugin_settings = converter.structure(plugin_settings, PluginSettings)
735738

736739
pyproject_file = find_parents(
737740
workspace.root_path, document_path, ["pyproject.toml"]
@@ -764,6 +767,7 @@ def load_settings(workspace: Workspace, document_path: str) -> PluginSettings:
764767
extend_select=plugin_settings.extend_select,
765768
format=plugin_settings.format,
766769
severities=plugin_settings.severities,
770+
unfixable=plugin_settings.unfixable,
767771
)
768772

769773
return plugin_settings

pylsp_ruff/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class PluginSettings:
2727
preview: bool = False
2828
unsafe_fixes: bool = False
2929

30+
unfixable: Optional[List[str]] = None
31+
3032
severities: Optional[Dict[str, str]] = None
3133

3234
target_version: Optional[str] = None

tests/test_code_actions.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ def f():
5050
"Ruff: Fix All (safe fixes)",
5151
]
5252

53+
codeactions_unfixable = [
54+
"Ruff (F401): Remove unused import: `os`",
55+
"Ruff (F401): Disable for this line",
56+
# "Ruff (F841): Remove assignment to unused variable `a` (unsafe)",
57+
"Ruff (F841): Disable for this line",
58+
"Ruff: Fix All (safe fixes)",
59+
]
60+
5361
codeactions_import = [
5462
"Ruff: Organize imports",
5563
"Ruff: Fix All (safe fixes)",
@@ -85,6 +93,24 @@ def test_ruff_code_actions(workspace):
8593
assert sorted(codeactions) == sorted(action_titles)
8694

8795

96+
def test_ruff_code_actions_unfixable(workspace):
97+
_, doc = temp_document(codeaction_str, workspace)
98+
99+
workspace._config.update(
100+
{"plugins": {"ruff": {"select": ["F"], "unfixable": ["F841"]}}}
101+
)
102+
diags = ruff_lint.pylsp_lint(workspace, doc)
103+
range_ = cattrs.unstructure(
104+
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
105+
)
106+
actions = ruff_lint.pylsp_code_actions(
107+
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
108+
)
109+
actions = converter.structure(actions, List[CodeAction])
110+
action_titles = list(map(lambda action: action.title, actions))
111+
assert sorted(codeactions_unfixable) == sorted(action_titles)
112+
113+
88114
def test_import_action(workspace):
89115
workspace._config.update(
90116
{

0 commit comments

Comments
 (0)