From 55a170c4719a7ab25a2cbd2984409f335f5404b0 Mon Sep 17 00:00:00 2001 From: willjow <24803216+willjow@users.noreply.github.com> Date: Thu, 24 Oct 2024 16:29:24 -0700 Subject: [PATCH 1/2] Don't ignore the "ALL" rule if it is in format In this case, the ignore will take precedence and setting `pylsp.plugins.ruff.format` to `["ALL"]` will do nothing. --- pylsp_ruff/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index 23af27e..26f5fa0 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -142,7 +142,7 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator # specifying `format = ["I"]` to get import sorting as part of formatting. new_text = run_ruff( settings=PluginSettings( - ignore=["ALL"], + ignore=["ALL"] if "ALL" not in settings.format else [], select=settings.format, executable=settings.executable, ), From fb54915c8abeaf9bdc831d3b31d2bcbfa211d597 Mon Sep 17 00:00:00 2001 From: willjow <24803216+willjow@users.noreply.github.com> Date: Thu, 24 Oct 2024 16:29:52 -0700 Subject: [PATCH 2/2] Add a test case to `test_ruff_format.py` Update existing test text to be compatible with the case that a format is run with `format` set to `["ALL"]`. --- tests/test_ruff_format.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/tests/test_ruff_format.py b/tests/test_ruff_format.py index 3d7ef85..76bb165 100644 --- a/tests/test_ruff_format.py +++ b/tests/test_ruff_format.py @@ -19,6 +19,12 @@ """ ).strip() +_UNUSED_IMPORTS = tw.dedent( + """ + import unused_import + """ +).strip() + _SORTED_IMPORTS = tw.dedent( """ import asyncio @@ -30,19 +36,19 @@ _UNFORMATTED_CODE = tw.dedent( """ - def foo(): pass - def bar(): pass + def foo(): return asyncio.subprocess.DEVNULL + def bar(): return x(io.DEFAULT_BUFFER_SIZE) """ ).strip() _FORMATTED_CODE = tw.dedent( """ def foo(): - pass + return asyncio.subprocess.DEVNULL def bar(): - pass + return x(io.DEFAULT_BUFFER_SIZE) """ ).strip() @@ -121,3 +127,22 @@ def test_ruff_format_and_sort_imports(workspace): ) got = run_plugin_format(workspace, doc) assert want == got + + +def test_ruff_format_via_all(workspace): + # If `pylsp.plugins.ruff.format` contains "ALL", formatting should apply + # the automatic fixes for unsorted-imports (I001) and unused-import (F401) + txt = f"{_UNUSED_IMPORTS}\n{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}" + want = f"{_SORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n" + _, doc = temp_document(txt, workspace) + workspace._config.update( + { + "plugins": { + "ruff": { + "format": ["ALL"], + } + } + } + ) + got = run_plugin_format(workspace, doc) + assert want == got