Skip to content

Commit

Permalink
Pass settings to format document (#364)
Browse files Browse the repository at this point in the history
Just more consistent with the lint signature.
  • Loading branch information
charliermarsh authored Jan 25, 2024
1 parent 54f2ad1 commit 2c0492f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 7 additions & 5 deletions ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
Expand All @@ -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(
Expand All @@ -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}")
Expand All @@ -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

0 comments on commit 2c0492f

Please sign in to comment.