diff --git a/lsp/nls/src/requests/formatting.rs b/lsp/nls/src/requests/formatting.rs index c3dc31f9cb..e4d555d120 100644 --- a/lsp/nls/src/requests/formatting.rs +++ b/lsp/nls/src/requests/formatting.rs @@ -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 = Vec::new(); nickel_lang_core::format::format(text.as_bytes(), &mut formatted).map_err(|err| { @@ -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, diff --git a/lsp/nls/src/requests/formatting_external.rs b/lsp/nls/src/requests/formatting_external.rs index b7a4ea7974..842e027adf 100644 --- a/lsp/nls/src/requests/formatting_external.rs +++ b/lsp/nls/src/requests/formatting_external.rs @@ -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..]) @@ -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, diff --git a/lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__formatting.ncl.snap b/lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__formatting.ncl.snap index 225ef795ac..254180371d 100644 --- a/lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__formatting.ncl.snap +++ b/lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__formatting.ncl.snap @@ -2,7 +2,7 @@ source: lsp/nls/tests/main.rs expression: output --- -[<0:0-1:8> { +[<0:0-2:0> { foo = "bar", baz = 7 }