Skip to content
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__/
*.egg-info/
/build/
18 changes: 12 additions & 6 deletions pylsp_ruff/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def pylsp_code_actions(
document : pylsp.workspace.Document
Document to apply ruff on.
range : Dict
Range argument given by pylsp. Not used here.
Range argument given by pylsp.
context : Dict
CodeActionContext given as dict.

Expand Down Expand Up @@ -318,9 +318,18 @@ def pylsp_code_actions(
]
)

range = converter.structure(range, Range)
checks_with_fixes = [
c
for c in checks_with_fixes
if c.location.row - 1 >= range.start.line # type: ignore
and c.end_location.row - 1 <= range.end.line # type: ignore
]
if checks_with_fixes:
code_actions.append(
create_fix_all_code_action(document=document, settings=settings),
create_fix_all_code_action(
document=document, settings=settings, range=range
),
)

return converter.unstructure(code_actions)
Expand Down Expand Up @@ -400,6 +409,7 @@ def create_organize_imports_code_action(
def create_fix_all_code_action(
document: Document,
settings: PluginSettings,
range: Range,
) -> CodeAction:
title = "Ruff: Fix All"
kind = CodeActionKind.SourceFixAll
Expand All @@ -408,10 +418,6 @@ def create_fix_all_code_action(
settings.unsafe_fixes = False

new_text = run_ruff_fix(document=document, settings=settings)
range = Range(
start=Position(line=0, character=0),
end=Position(line=len(document.lines), character=0),
)
text_edit = TextEdit(range=range, new_text=new_text)
workspace_edit = WorkspaceEdit(changes={document.uri: [text_edit]})
return CodeAction(
Expand Down
11 changes: 8 additions & 3 deletions tests/test_code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def f():

codeactions_import = [
"Ruff: Organize imports",
"Ruff: Fix All",
"Ruff (I001): Disable for this line",
]

Expand All @@ -75,7 +74,10 @@ def test_ruff_code_actions(workspace):
)
diags = ruff_lint.pylsp_lint(workspace, doc)
range_ = cattrs.unstructure(
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
Range(
start=Position(line=0, character=0),
end=Position(line=(len(doc.lines) - 1), character=0),
)
)
actions = ruff_lint.pylsp_code_actions(
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
Expand All @@ -100,7 +102,10 @@ def test_import_action(workspace):

diags = ruff_lint.pylsp_lint(workspace, doc)
range_ = cattrs.unstructure(
Range(start=Position(line=0, character=0), end=Position(line=0, character=0))
Range(
start=Position(line=0, character=0),
end=Position(line=(len(doc.lines) - 1), character=0),
)
)
actions = ruff_lint.pylsp_code_actions(
workspace._config, workspace, doc, range=range_, context={"diagnostics": diags}
Expand Down