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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# 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).

# 0.1.1

Expand Down
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