Skip to content

Commit

Permalink
Log a warning on parse errors, but don't return an LSP error
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Dec 20, 2024
1 parent d61f962 commit 6a85179
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions crates/lsp/src/handlers_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ pub(crate) fn document_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 line_width = LineWidth::try_from(80).map_err(|err| anyhow::anyhow!("{err}"))?;

// TODO: Handle FormattingOptions
let options = RFormatOptions::default()
.with_indent_style(IndentStyle::Space)
.with_line_width(line_width);

if doc.parse.has_errors() {
return Err(anyhow::anyhow!("Can't format when there are parse errors."));
}

let formatted = format_node(options.clone(), &doc.parse.syntax())?;
let output = formatted.print()?.into_code();

Expand All @@ -51,6 +57,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 line_width = LineWidth::try_from(80).map_err(|err| anyhow::anyhow!("{err}"))?;
let range =
from_proto::text_range(&doc.line_index.index, params.range, doc.line_index.encoding)?;
Expand Down

0 comments on commit 6a85179

Please sign in to comment.