Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass settings to format document #364

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading