From 691879b56c498287be0e43aba3324b0ab69643e6 Mon Sep 17 00:00:00 2001 From: "shane.xb.qian" Date: Wed, 27 Dec 2023 16:47:23 +0800 Subject: [PATCH 1/7] none: add 'build/' to gitignore Signed-off-by: shane.xb.qian --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9c882b7..1935999 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__/ *.egg-info/ +/build/ From 183c28240394c6ce9d41dba4f7691909ea9d680b Mon Sep 17 00:00:00 2001 From: "shane.xb.qian" Date: Wed, 27 Dec 2023 16:48:32 +0800 Subject: [PATCH 2/7] fix: take account 'range' into 'code action' Signed-off-by: shane.xb.qian --- pylsp_ruff/plugin.py | 12 +++++------- tests/test_code_actions.py | 11 ++++++++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index fa7a98c..9786730 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -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. @@ -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: @@ -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) @@ -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 @@ -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( diff --git a/tests/test_code_actions.py b/tests/test_code_actions.py index f304e7a..75d55c1 100644 --- a/tests/test_code_actions.py +++ b/tests/test_code_actions.py @@ -52,7 +52,6 @@ def f(): codeactions_import = [ "Ruff: Organize imports", - "Ruff: Fix All", "Ruff (I001): Disable for this line", ] @@ -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} @@ -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} From 170599d4e870ef7bea12a8ebf449c0831edeb198 Mon Sep 17 00:00:00 2001 From: "shane.xb.qian" Date: Wed, 27 Dec 2023 18:39:40 +0800 Subject: [PATCH 3/7] none: format code per test req Signed-off-by: shane.xb.qian --- pylsp_ruff/plugin.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index 9786730..0861785 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -303,7 +303,13 @@ 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 and c.location.row - 1 >= range.start.line and c.end_location.row - 1 <= range.end.line] + 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: @@ -321,7 +327,9 @@ def pylsp_code_actions( if checks_with_fixes: code_actions.append( - create_fix_all_code_action(document=document, settings=settings, range=range), + create_fix_all_code_action( + document=document, settings=settings, range=range + ), ) return converter.unstructure(code_actions) From f33a590406d34c7170f008c3e7967c4ab161d664 Mon Sep 17 00:00:00 2001 From: "shane.xb.qian" Date: Wed, 27 Dec 2023 18:56:32 +0800 Subject: [PATCH 4/7] none: fix type err per mypy req Signed-off-by: shane.xb.qian --- pylsp_ruff/plugin.py | 5 ++--- tests/test_code_actions.py | 20 ++++++++------------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index 0861785..61b217a 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -241,7 +241,7 @@ def pylsp_code_actions( config: Config, workspace: Workspace, document: Document, - range: Dict, + range: Range, context: Dict, ) -> List[Dict]: """ @@ -255,7 +255,7 @@ def pylsp_code_actions( Current workspace. document : pylsp.workspace.Document Document to apply ruff on. - range : Dict + range : Range Range argument given by pylsp. context : Dict CodeActionContext given as dict. @@ -301,7 +301,6 @@ def pylsp_code_actions( ), ) - range = converter.structure(range, Range) checks = run_ruff_check(document=document, settings=settings) checks_with_fixes = [ c diff --git a/tests/test_code_actions.py b/tests/test_code_actions.py index 75d55c1..69fef0e 100644 --- a/tests/test_code_actions.py +++ b/tests/test_code_actions.py @@ -73,14 +73,12 @@ def test_ruff_code_actions(workspace): {"plugins": {"ruff": {"select": ["F"], "unsafeFixes": True}}} ) diags = ruff_lint.pylsp_lint(workspace, doc) - range_ = cattrs.unstructure( - Range( - start=Position(line=0, character=0), - end=Position(line=(len(doc.lines) - 1), character=0), - ) + range = 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} + workspace._config, workspace, doc, range=range, context={"diagnostics": diags} ) actions = converter.structure(actions, List[CodeAction]) action_titles = list(map(lambda action: action.title, actions)) @@ -101,14 +99,12 @@ def test_import_action(workspace): _, doc = temp_document(import_str, workspace) diags = ruff_lint.pylsp_lint(workspace, doc) - range_ = cattrs.unstructure( - Range( - start=Position(line=0, character=0), - end=Position(line=(len(doc.lines) - 1), character=0), - ) + range = 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} + workspace._config, workspace, doc, range=range, context={"diagnostics": diags} ) actions = converter.structure(actions, List[CodeAction]) action_titles = list(map(lambda action: action.title, actions)) From 892f080708b0afc00aab0b963e75f68b8791d9a3 Mon Sep 17 00:00:00 2001 From: "shane.xb.qian" Date: Wed, 27 Dec 2023 19:03:22 +0800 Subject: [PATCH 5/7] none: fix warn per ruff req Signed-off-by: shane.xb.qian --- tests/test_code_actions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_code_actions.py b/tests/test_code_actions.py index 69fef0e..7186292 100644 --- a/tests/test_code_actions.py +++ b/tests/test_code_actions.py @@ -6,7 +6,6 @@ from typing import List from unittest.mock import Mock -import cattrs import pytest from lsprotocol.converters import get_converter from lsprotocol.types import CodeAction, Position, Range From 4987f60500c03f8ccab8849cea412b6a19be6ae8 Mon Sep 17 00:00:00 2001 From: "shane.xb.qian" Date: Thu, 28 Dec 2023 19:58:06 +0800 Subject: [PATCH 6/7] none: adjust a bit Signed-off-by: shane.xb.qian --- pylsp_ruff/plugin.py | 19 ++++++++++--------- tests/test_code_actions.py | 21 +++++++++++++-------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index 61b217a..8155b22 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -241,7 +241,7 @@ def pylsp_code_actions( config: Config, workspace: Workspace, document: Document, - range: Range, + range: Dict, context: Dict, ) -> List[Dict]: """ @@ -255,7 +255,7 @@ def pylsp_code_actions( Current workspace. document : pylsp.workspace.Document Document to apply ruff on. - range : Range + range : Dict Range argument given by pylsp. context : Dict CodeActionContext given as dict. @@ -302,13 +302,7 @@ def pylsp_code_actions( ) checks = run_ruff_check(document=document, settings=settings) - 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_with_fixes = [c for c in checks if c.fix] checks_organize_imports = [c for c in checks_with_fixes if c.code == "I001"] if not has_organize_imports and checks_organize_imports: @@ -324,6 +318,13 @@ 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 + and c.end_location.row - 1 <= range.end.line + ] if checks_with_fixes: code_actions.append( create_fix_all_code_action( diff --git a/tests/test_code_actions.py b/tests/test_code_actions.py index 7186292..75d55c1 100644 --- a/tests/test_code_actions.py +++ b/tests/test_code_actions.py @@ -6,6 +6,7 @@ from typing import List from unittest.mock import Mock +import cattrs import pytest from lsprotocol.converters import get_converter from lsprotocol.types import CodeAction, Position, Range @@ -72,12 +73,14 @@ def test_ruff_code_actions(workspace): {"plugins": {"ruff": {"select": ["F"], "unsafeFixes": True}}} ) diags = ruff_lint.pylsp_lint(workspace, doc) - range = Range( - start=Position(line=0, character=0), - end=Position(line=(len(doc.lines) - 1), character=0), + range_ = cattrs.unstructure( + 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} + workspace._config, workspace, doc, range=range_, context={"diagnostics": diags} ) actions = converter.structure(actions, List[CodeAction]) action_titles = list(map(lambda action: action.title, actions)) @@ -98,12 +101,14 @@ def test_import_action(workspace): _, doc = temp_document(import_str, workspace) diags = ruff_lint.pylsp_lint(workspace, doc) - range = Range( - start=Position(line=0, character=0), - end=Position(line=(len(doc.lines) - 1), character=0), + range_ = cattrs.unstructure( + 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} + workspace._config, workspace, doc, range=range_, context={"diagnostics": diags} ) actions = converter.structure(actions, List[CodeAction]) action_titles = list(map(lambda action: action.title, actions)) From 3327a31e2e0c29b6e2ed77574d373331a766aa3f Mon Sep 17 00:00:00 2001 From: "shane.xb.qian" Date: Thu, 28 Dec 2023 20:33:56 +0800 Subject: [PATCH 7/7] none: compliance with mypy Signed-off-by: shane.xb.qian --- pylsp_ruff/plugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index 8155b22..382af80 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -322,8 +322,8 @@ def pylsp_code_actions( checks_with_fixes = [ c for c in checks_with_fixes - if c.location.row - 1 >= range.start.line - and c.end_location.row - 1 <= range.end.line + 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(