Skip to content

Commit

Permalink
Sort code actions by specificity (#245)
Browse files Browse the repository at this point in the history
## Summary

It changes the sort order sent to clients. This way code actions for
specific diagnostic codes are suggested before more general ones like
'Fix: All' or 'Organize Imports'.

See issue #210 for discussions
  • Loading branch information
MrGreenTea authored Sep 27, 2023
1 parent 160e936 commit b12a277
Showing 1 changed file with 62 additions and 62 deletions.
124 changes: 62 additions & 62 deletions ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,68 +487,6 @@ async def code_action(params: CodeActionParams) -> list[CodeAction] | None:

actions: list[CodeAction] = []

if settings["organizeImports"]:
# Add "Ruff: Organize Imports" as a supported action.
if not params.context.only or (
CodeActionKind.SourceOrganizeImports in params.context.only
):
if CLIENT_CAPABILITIES[CODE_ACTION_RESOLVE]:
actions.append(
CodeAction(
title="Ruff: Organize Imports",
kind=CodeActionKind.SourceOrganizeImports,
data=params.text_document.uri,
edit=None,
diagnostics=[],
),
)
else:
edits = await _fix_document_impl(document, only="I001")
if edits:
actions.append(
CodeAction(
title="Ruff: Organize Imports",
kind=CodeActionKind.SourceOrganizeImports,
data=params.text_document.uri,
edit=_create_workspace_edits(document, edits),
diagnostics=[],
),
)

if settings["fixAll"]:
# Add "Ruff: Fix All" as a supported action.
if not params.context.only or (
CodeActionKind.SourceFixAll in params.context.only
):
if CLIENT_CAPABILITIES[CODE_ACTION_RESOLVE]:
actions.append(
CodeAction(
title="Ruff: Fix All",
kind=CodeActionKind.SourceFixAll,
data=params.text_document.uri,
edit=None,
diagnostics=[],
),
)
else:
edits = await _fix_document_impl(document)
if edits:
actions.append(
CodeAction(
title="Ruff: Fix All",
kind=CodeActionKind.SourceFixAll,
data=params.text_document.uri,
edit=_create_workspace_edits(document, edits),
diagnostics=[
diagnostic
for diagnostic in params.context.diagnostics
if diagnostic.source == "Ruff"
and cast(DiagnosticData, diagnostic.data).get("fix")
is not None
],
),
)

# Add "Ruff: Autofix" for every fixable diagnostic.
if settings.get("codeAction", {}).get("fixViolation", {}).get("enable", True):
if not params.context.only or CodeActionKind.QuickFix in params.context.only:
Expand Down Expand Up @@ -629,6 +567,68 @@ async def code_action(params: CodeActionParams) -> list[CodeAction] | None:
),
)

if settings["organizeImports"]:
# Add "Ruff: Organize Imports" as a supported action.
if not params.context.only or (
CodeActionKind.SourceOrganizeImports in params.context.only
):
if CLIENT_CAPABILITIES[CODE_ACTION_RESOLVE]:
actions.append(
CodeAction(
title="Ruff: Organize Imports",
kind=CodeActionKind.SourceOrganizeImports,
data=params.text_document.uri,
edit=None,
diagnostics=[],
),
)
else:
edits = await _fix_document_impl(document, only="I001")
if edits:
actions.append(
CodeAction(
title="Ruff: Organize Imports",
kind=CodeActionKind.SourceOrganizeImports,
data=params.text_document.uri,
edit=_create_workspace_edits(document, edits),
diagnostics=[],
),
)

if settings["fixAll"]:
# Add "Ruff: Fix All" as a supported action.
if not params.context.only or (
CodeActionKind.SourceFixAll in params.context.only
):
if CLIENT_CAPABILITIES[CODE_ACTION_RESOLVE]:
actions.append(
CodeAction(
title="Ruff: Fix All",
kind=CodeActionKind.SourceFixAll,
data=params.text_document.uri,
edit=None,
diagnostics=[],
),
)
else:
edits = await _fix_document_impl(document)
if edits:
actions.append(
CodeAction(
title="Ruff: Fix All",
kind=CodeActionKind.SourceFixAll,
data=params.text_document.uri,
edit=_create_workspace_edits(document, edits),
diagnostics=[
diagnostic
for diagnostic in params.context.diagnostics
if diagnostic.source == "Ruff"
and cast(DiagnosticData, diagnostic.data).get("fix")
is not None
],
),
)

return actions if actions else None


Expand Down

0 comments on commit b12a277

Please sign in to comment.