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

Log a warning on parse errors, but don't return an LSP error #120

Merged
merged 2 commits into from
Jan 8, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# Development version

- Parse errors in your document no longer trigger an LSP error when you request
document or range formatting (which typically would show up as an annoying
toast notification in your code editor) (#120).

- `air format` is now faster on Windows when nothing changes (#90).

- `air format --check` now works correctly with Windows line endings (#123).
Expand Down
18 changes: 17 additions & 1 deletion crates/lsp/src/handlers_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ pub(crate) fn document_formatting(
let format_options = settings.format.to_format_options(&doc.contents);

if doc.parse.has_errors() {
return Err(anyhow::anyhow!("Can't format when there are parse errors."));
// Refuse to format in the face of parse errors, but only log a warning
// rather than returning an LSP error, as toast notifications here are distracting.
tracing::warn!(
"Failed to format {uri}. Can't format when there are parse errors.",
uri = params.text_document.uri
);
return Ok(None);
}

let formatted = format_node(format_options, &doc.parse.syntax())?;
Expand All @@ -49,6 +55,16 @@ pub(crate) fn document_range_formatting(
) -> anyhow::Result<Option<Vec<lsp_types::TextEdit>>> {
let doc = state.get_document(&params.text_document.uri)?;

if doc.parse.has_errors() {
// Refuse to format in the face of parse errors, but only log a warning
// rather than returning an LSP error, as toast notifications here are distracting.
tracing::warn!(
"Failed to format {uri}. Can't format when there are parse errors.",
uri = params.text_document.uri
);
return Ok(None);
}

let range =
from_proto::text_range(&doc.line_index.index, params.range, doc.line_index.encoding)?;

Expand Down
Loading