Skip to content

Commit

Permalink
fix: take account 'range' into 'code action'
Browse files Browse the repository at this point in the history
Signed-off-by: shane.xb.qian <[email protected]>
  • Loading branch information
Shane-XB-Qian committed Dec 27, 2023
1 parent 691879b commit 183c282
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
12 changes: 5 additions & 7 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 @@ -301,8 +301,9 @@ def pylsp_code_actions(
),
)

range = converter.structure(range, Range)
checks = run_ruff_check(document=document, settings=settings)
checks_with_fixes = [c for c in checks if c.fix]
checks_with_fixes = [c for c in checks if c.fix and c.location.row - 1 >= range.start.line and c.end_location.row - 1 <= range.end.line]
checks_organize_imports = [c for c in checks_with_fixes if c.code == "I001"]

if not has_organize_imports and checks_organize_imports:
Expand All @@ -320,7 +321,7 @@ def pylsp_code_actions(

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 +401,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 +410,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

0 comments on commit 183c282

Please sign in to comment.