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, ), 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