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

Fix an off-by-one error in the LSP formatter #1605

Merged
merged 2 commits into from
Sep 14, 2023
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
8 changes: 5 additions & 3 deletions lsp/nls/src/requests/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub fn handle_format_document(
let file_id = server.cache.id_of(&SourcePath::Path(path)).unwrap();
let text = server.cache.files().source(file_id).clone();
let document_length = text.lines().count() as u32;
let last_line_length = text.lines().next_back().unwrap().len() as u32;

let mut formatted: Vec<u8> = Vec::new();
nickel_lang_core::format::format(text.as_bytes(), &mut formatted).map_err(|err| {
Expand All @@ -39,9 +38,12 @@ pub fn handle_format_document(
line: 0,
character: 0,
},
// The end position is exclusive. Since we want to replace the
// entire document, we specify the beginning of the line after the
// last line in the document.
end: Position {
line: document_length - 1,
character: last_line_length,
line: document_length,
character: 0,
},
},
new_text: formatted,
Expand Down
8 changes: 5 additions & 3 deletions lsp/nls/src/requests/formatting_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub fn handle_format_document(
let file_id = server.cache.id_of(&SourcePath::Path(path)).unwrap();
let text = server.cache.files().source(file_id).clone();
let document_length = text.lines().count() as u32;
let last_line_length = text.lines().next_back().unwrap().len() as u32;

let Ok(mut topiary) = process::Command::new(FORMATTING_COMMAND[0])
.args(&FORMATTING_COMMAND[1..])
Expand Down Expand Up @@ -62,9 +61,12 @@ pub fn handle_format_document(
line: 0,
character: 0,
},
// The end position is exclusive. Since we want to replace the
// entire document, we specify the beginning of the line after the
// last line in the document.
end: Position {
line: document_length - 1,
character: last_line_length,
line: document_length,
character: 0,
},
},
new_text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: lsp/nls/tests/main.rs
expression: output
---
[<0:0-1:8> {
[<0:0-2:0> {
foo = "bar",
baz = 7
}
Expand Down