Skip to content

Commit a9af4a7

Browse files
Add '(safe fixes)' to Fix All code action (#86)
Additional cleanup: - Updated .gitignore - Formatting --------- Co-authored-by: Julian Hoßbach <[email protected]>
1 parent dd42768 commit a9af4a7

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
__pycache__/
22
*.egg-info/
3+
4+
*.swp
5+
tags
6+
/build/

pylsp_ruff/plugin.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ def pylsp_settings():
106106

107107
@hookimpl(hookwrapper=True)
108108
def pylsp_format_document(workspace: Workspace, document: Document) -> Generator:
109-
"""
110-
Provide formatting through ruff.
109+
"""Provide formatting through ruff.
111110
112111
Parameters
113112
----------
114113
workspace : pylsp.workspace.Workspace
115114
Current workspace.
116115
document : pylsp.workspace.Document
117116
Document to apply ruff on.
117+
118118
"""
119119
log.debug(f"textDocument/formatting: {document}")
120120
outcome = yield
@@ -158,8 +158,7 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator
158158

159159
@hookimpl
160160
def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
161-
"""
162-
Register ruff as the linter.
161+
"""Register ruff as the linter.
163162
164163
Parameters
165164
----------
@@ -171,6 +170,7 @@ def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
171170
Returns
172171
-------
173172
List of dicts containing the diagnostics.
173+
174174
"""
175175
settings = load_settings(workspace, document.path)
176176
checks = run_ruff_check(document=document, settings=settings)
@@ -179,8 +179,7 @@ def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
179179

180180

181181
def create_diagnostic(check: RuffCheck, settings: PluginSettings) -> Diagnostic:
182-
"""
183-
Create a LSP diagnostic based on the given RuffCheck object.
182+
"""Create a LSP diagnostic based on the given RuffCheck object.
184183
185184
Parameters
186185
----------
@@ -192,6 +191,7 @@ def create_diagnostic(check: RuffCheck, settings: PluginSettings) -> Diagnostic:
192191
Returns
193192
-------
194193
Diagnostic
194+
195195
"""
196196
# Adapt range to LSP specification (zero-based)
197197
range = Range(
@@ -248,8 +248,7 @@ def pylsp_code_actions(
248248
range: Dict,
249249
context: Dict,
250250
) -> List[Dict]:
251-
"""
252-
Provide code actions through ruff.
251+
"""Provide code actions through ruff.
253252
254253
Parameters
255254
----------
@@ -267,6 +266,7 @@ def pylsp_code_actions(
267266
Returns
268267
-------
269268
List of dicts containing the code actions.
269+
270270
"""
271271
log.debug(f"textDocument/codeAction: {document} {range} {context}")
272272

@@ -322,7 +322,7 @@ def pylsp_code_actions(
322322
]
323323
)
324324

325-
if checks_with_fixes:
325+
if any([c.fix.applicability == "safe" for c in checks_with_fixes]): # type: ignore
326326
code_actions.append(
327327
create_fix_all_code_action(document=document, settings=settings),
328328
)
@@ -405,7 +405,7 @@ def create_fix_all_code_action(
405405
document: Document,
406406
settings: PluginSettings,
407407
) -> CodeAction:
408-
title = "Ruff: Fix All"
408+
title = "Ruff: Fix All (safe fixes)"
409409
kind = CodeActionKind.SourceFixAll
410410

411411
# No unsafe fixes for 'Fix all', see https://github.com/python-lsp/python-lsp-ruff/issues/55
@@ -487,8 +487,7 @@ def run_ruff(
487487
fix: bool = False,
488488
extra_arguments: Optional[List[str]] = None,
489489
) -> str:
490-
"""
491-
Run ruff on the given document and the given arguments.
490+
"""Run ruff on the given document and the given arguments.
492491
493492
Parameters
494493
----------
@@ -509,6 +508,7 @@ def run_ruff(
509508
Returns
510509
-------
511510
String containing the result in json format.
511+
512512
"""
513513
executable = settings.executable
514514

@@ -545,8 +545,7 @@ def build_check_arguments(
545545
fix: bool = False,
546546
extra_arguments: Optional[List[str]] = None,
547547
) -> List[str]:
548-
"""
549-
Build arguments for ruff check.
548+
"""Build arguments for ruff check.
550549
551550
Parameters
552551
----------
@@ -562,6 +561,7 @@ def build_check_arguments(
562561
Returns
563562
-------
564563
List containing the arguments.
564+
565565
"""
566566
args = []
567567
# Suppress update announcements
@@ -631,8 +631,7 @@ def build_format_arguments(
631631
settings: PluginSettings,
632632
extra_arguments: Optional[List[str]] = None,
633633
) -> List[str]:
634-
"""
635-
Build arguments for ruff format.
634+
"""Build arguments for ruff format.
636635
637636
Parameters
638637
----------
@@ -646,6 +645,7 @@ def build_format_arguments(
646645
Returns
647646
-------
648647
List containing the arguments.
648+
649649
"""
650650
args = []
651651
# Suppress update announcements
@@ -681,8 +681,7 @@ def build_format_arguments(
681681

682682

683683
def load_settings(workspace: Workspace, document_path: str) -> PluginSettings:
684-
"""
685-
Load settings from pyproject.toml file in the project path.
684+
"""Load settings from pyproject.toml file in the project path.
686685
687686
Parameters
688687
----------
@@ -694,6 +693,7 @@ def load_settings(workspace: Workspace, document_path: str) -> PluginSettings:
694693
Returns
695694
-------
696695
PluginSettings read via lsp.
696+
697697
"""
698698
config = workspace._config
699699
_plugin_settings = config.plugin_settings("ruff", document_path=document_path)

pylsp_ruff/ruff.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import List, Union
2+
from typing import List, Optional
33

44

55
@dataclass
@@ -29,4 +29,4 @@ class Check:
2929
filename: str
3030
location: Location
3131
end_location: Location
32-
fix: Union[Fix, None] = None
32+
fix: Optional[Fix] = None

tests/test_code_actions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ def f():
4747
"Ruff (F401): Disable for this line",
4848
"Ruff (F841): Remove assignment to unused variable `a` (unsafe)",
4949
"Ruff (F841): Disable for this line",
50-
"Ruff: Fix All",
50+
"Ruff: Fix All (safe fixes)",
5151
]
5252

5353
codeactions_import = [
5454
"Ruff: Organize imports",
55-
"Ruff: Fix All",
55+
"Ruff: Fix All (safe fixes)",
5656
"Ruff (I001): Disable for this line",
5757
]
5858

0 commit comments

Comments
 (0)