diff --git a/ruff_lsp/server.py b/ruff_lsp/server.py index a046a99..4210776 100755 --- a/ruff_lsp/server.py +++ b/ruff_lsp/server.py @@ -1190,8 +1190,9 @@ async def apply_organize_imports(arguments: tuple[TextDocument]): async def apply_format(arguments: tuple[TextDocument]): uri = arguments[0]["uri"] document = Document.from_uri(uri) + settings = _get_settings_by_document(document.path) - result = await _run_format_on_document(document) + result = await _run_format_on_document(document, settings) if result is None: return None @@ -1214,8 +1215,9 @@ async def format_document(params: DocumentFormattingParams) -> list[TextEdit] | # represented as a text document. The "Notebook: Format notebook" action calls # this request for every cell. document = Document.from_cell_or_text_uri(params.text_document.uri) + settings = _get_settings_by_document(document.path) - result = await _run_format_on_document(document) + result = await _run_format_on_document(document, settings) if result is None: return None @@ -1820,10 +1822,10 @@ async def _run_check_on_document( ) -async def _run_format_on_document(document: Document) -> ExecutableResult | None: +async def _run_format_on_document( + document: Document, settings: WorkspaceSettings +) -> ExecutableResult | None: """Runs the Ruff `format` subcommand on the given document source.""" - settings = _get_settings_by_document(document.path) - if settings.get("ignoreStandardLibrary", True) and document.is_stdlib_file(): log_warning(f"Skipping standard library file: {document.path}") return None diff --git a/tests/test_format.py b/tests/test_format.py index dd13cbe..09a94b9 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -10,6 +10,7 @@ VERSION_REQUIREMENT_FORMATTER, Document, _fixed_source_to_edits, + _get_settings_by_document, _run_format_on_document, ) from tests.client import utils @@ -30,6 +31,7 @@ async def test_format(tmp_path, ruff_version: Version): workspace = Workspace(str(tmp_path)) document = Document.from_text_document(workspace.get_text_document(uri)) + settings = _get_settings_by_document(document.path) handle_unsupported = ( pytest.raises(RuntimeError, match=f"Ruff .* required, but found {ruff_version}") @@ -38,7 +40,7 @@ async def test_format(tmp_path, ruff_version: Version): ) with handle_unsupported: - result = await _run_format_on_document(document) + result = await _run_format_on_document(document, settings) assert result is not None assert result.exit_code == 0 [edit] = _fixed_source_to_edits( @@ -59,6 +61,7 @@ async def test_format_code_with_syntax_error(tmp_path, ruff_version: Version): workspace = Workspace(str(tmp_path)) document = Document.from_text_document(workspace.get_text_document(uri)) + settings = _get_settings_by_document(document.path) handle_unsupported = ( pytest.raises(RuntimeError, match=f"Ruff .* required, but found {ruff_version}") @@ -67,6 +70,6 @@ async def test_format_code_with_syntax_error(tmp_path, ruff_version: Version): ) with handle_unsupported: - result = await _run_format_on_document(document) + result = await _run_format_on_document(document, settings) assert result is not None assert result.exit_code == 2