diff --git a/CHANGELOG.md b/CHANGELOG.md index e089eea6..a2b23f3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/crates/lsp/src/handlers_format.rs b/crates/lsp/src/handlers_format.rs index 3a635843..0655075e 100644 --- a/crates/lsp/src/handlers_format.rs +++ b/crates/lsp/src/handlers_format.rs @@ -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())?; @@ -49,6 +55,16 @@ pub(crate) fn document_range_formatting( ) -> anyhow::Result>> { let doc = state.get_document(¶ms.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)?;