Skip to content

Commit

Permalink
Add '(safe fixes)' to Fix All code action (#86)
Browse files Browse the repository at this point in the history
Additional cleanup:
 - Updated .gitignore
 - Formatting

---------

Co-authored-by: Julian Hoßbach <[email protected]>
  • Loading branch information
Shane-XB-Qian and jhossbach authored Mar 16, 2024
1 parent dd42768 commit a9af4a7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
__pycache__/
*.egg-info/

*.swp
tags
/build/
36 changes: 18 additions & 18 deletions pylsp_ruff/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ def pylsp_settings():

@hookimpl(hookwrapper=True)
def pylsp_format_document(workspace: Workspace, document: Document) -> Generator:
"""
Provide formatting through ruff.
"""Provide formatting through ruff.
Parameters
----------
workspace : pylsp.workspace.Workspace
Current workspace.
document : pylsp.workspace.Document
Document to apply ruff on.
"""
log.debug(f"textDocument/formatting: {document}")
outcome = yield
Expand Down Expand Up @@ -158,8 +158,7 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator

@hookimpl
def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
"""
Register ruff as the linter.
"""Register ruff as the linter.
Parameters
----------
Expand All @@ -171,6 +170,7 @@ def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
Returns
-------
List of dicts containing the diagnostics.
"""
settings = load_settings(workspace, document.path)
checks = run_ruff_check(document=document, settings=settings)
Expand All @@ -179,8 +179,7 @@ def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:


def create_diagnostic(check: RuffCheck, settings: PluginSettings) -> Diagnostic:
"""
Create a LSP diagnostic based on the given RuffCheck object.
"""Create a LSP diagnostic based on the given RuffCheck object.
Parameters
----------
Expand All @@ -192,6 +191,7 @@ def create_diagnostic(check: RuffCheck, settings: PluginSettings) -> Diagnostic:
Returns
-------
Diagnostic
"""
# Adapt range to LSP specification (zero-based)
range = Range(
Expand Down Expand Up @@ -248,8 +248,7 @@ def pylsp_code_actions(
range: Dict,
context: Dict,
) -> List[Dict]:
"""
Provide code actions through ruff.
"""Provide code actions through ruff.
Parameters
----------
Expand All @@ -267,6 +266,7 @@ def pylsp_code_actions(
Returns
-------
List of dicts containing the code actions.
"""
log.debug(f"textDocument/codeAction: {document} {range} {context}")

Expand Down Expand Up @@ -322,7 +322,7 @@ def pylsp_code_actions(
]
)

if checks_with_fixes:
if any([c.fix.applicability == "safe" for c in checks_with_fixes]): # type: ignore
code_actions.append(
create_fix_all_code_action(document=document, settings=settings),
)
Expand Down Expand Up @@ -405,7 +405,7 @@ def create_fix_all_code_action(
document: Document,
settings: PluginSettings,
) -> CodeAction:
title = "Ruff: Fix All"
title = "Ruff: Fix All (safe fixes)"
kind = CodeActionKind.SourceFixAll

# No unsafe fixes for 'Fix all', see https://github.com/python-lsp/python-lsp-ruff/issues/55
Expand Down Expand Up @@ -487,8 +487,7 @@ def run_ruff(
fix: bool = False,
extra_arguments: Optional[List[str]] = None,
) -> str:
"""
Run ruff on the given document and the given arguments.
"""Run ruff on the given document and the given arguments.
Parameters
----------
Expand All @@ -509,6 +508,7 @@ def run_ruff(
Returns
-------
String containing the result in json format.
"""
executable = settings.executable

Expand Down Expand Up @@ -545,8 +545,7 @@ def build_check_arguments(
fix: bool = False,
extra_arguments: Optional[List[str]] = None,
) -> List[str]:
"""
Build arguments for ruff check.
"""Build arguments for ruff check.
Parameters
----------
Expand All @@ -562,6 +561,7 @@ def build_check_arguments(
Returns
-------
List containing the arguments.
"""
args = []
# Suppress update announcements
Expand Down Expand Up @@ -631,8 +631,7 @@ def build_format_arguments(
settings: PluginSettings,
extra_arguments: Optional[List[str]] = None,
) -> List[str]:
"""
Build arguments for ruff format.
"""Build arguments for ruff format.
Parameters
----------
Expand All @@ -646,6 +645,7 @@ def build_format_arguments(
Returns
-------
List containing the arguments.
"""
args = []
# Suppress update announcements
Expand Down Expand Up @@ -681,8 +681,7 @@ def build_format_arguments(


def load_settings(workspace: Workspace, document_path: str) -> PluginSettings:
"""
Load settings from pyproject.toml file in the project path.
"""Load settings from pyproject.toml file in the project path.
Parameters
----------
Expand All @@ -694,6 +693,7 @@ def load_settings(workspace: Workspace, document_path: str) -> PluginSettings:
Returns
-------
PluginSettings read via lsp.
"""
config = workspace._config
_plugin_settings = config.plugin_settings("ruff", document_path=document_path)
Expand Down
4 changes: 2 additions & 2 deletions pylsp_ruff/ruff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import List, Union
from typing import List, Optional


@dataclass
Expand Down Expand Up @@ -29,4 +29,4 @@ class Check:
filename: str
location: Location
end_location: Location
fix: Union[Fix, None] = None
fix: Optional[Fix] = None
4 changes: 2 additions & 2 deletions tests/test_code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ def f():
"Ruff (F401): Disable for this line",
"Ruff (F841): Remove assignment to unused variable `a` (unsafe)",
"Ruff (F841): Disable for this line",
"Ruff: Fix All",
"Ruff: Fix All (safe fixes)",
]

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

Expand Down

0 comments on commit a9af4a7

Please sign in to comment.