Skip to content

Commit

Permalink
Avoid writing empty source on excluded files (#317)
Browse files Browse the repository at this point in the history
Right now, if the user tries to fix or format an excluded file, we don't
write anything to `stdout`. As a result, the LSP then overwrites the
file with the empty string.

This is a compromise fix whereby we avoid writing empty fixes in the
LSP. It's a "compromise" in that it'll do the wrong thing in some cases,
e.g., if the file is _just_ `import os`, we won't remove that when the
user runs "Fix all". However, I think we already had this behavior in
the LSP in the past, and we can fix it for newer versions by changing
Ruff to output the unchanged file.

## Test Plan

Ran the debug extension.

Opened a first-party file, and ensured that formatting and linting work
as before (with the exception of the `import os` case).

Opened a third-party file, and ensured that formatting and linting had
no effect (and didn't delete the file).
  • Loading branch information
charliermarsh authored Nov 10, 2023
1 parent 7c42733 commit 5e03217
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,10 @@ async def format_document(params: DocumentFormattingParams) -> list[TextEdit] |
document = Document.from_cell_or_text_uri(params.text_document.uri)

result = await _run_format_on_document(document)
if result is None or result.exit_code != 0:
if result is None or result.exit_code:
return None

if not result.stdout and document.source.strip():
return None

if document.kind is DocumentKind.Cell:
Expand Down Expand Up @@ -1145,6 +1148,9 @@ def _result_to_workspace_edit(
if result is None:
return None

if not result.stdout and document.source.strip():
return None

if document.kind is DocumentKind.Text:
edits = _fixed_source_to_edits(
original_source=document.source, fixed_source=result.stdout.decode("utf-8")
Expand Down

0 comments on commit 5e03217

Please sign in to comment.