mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 01:17:27 +00:00
Don't return any TextEdit if formatting is unchanged
I found that `textDocument/formatting` was always returning a full `TextEdit` replacement, even when there are no changes, which caused Vim (w/ vim-lsp) to always indicate a modified buffer after formatting. We can easily compare whether there were changes and return `null` if not, so the client knows there's nothing to do.
This commit is contained in:
parent
b14bf68ce6
commit
29c3421391
2 changed files with 45 additions and 4 deletions
|
@ -748,10 +748,15 @@ pub(crate) fn handle_formatting(
|
|||
}
|
||||
}
|
||||
|
||||
Ok(Some(vec![lsp_types::TextEdit {
|
||||
range: Range::new(Position::new(0, 0), end_position),
|
||||
new_text: captured_stdout,
|
||||
}]))
|
||||
if *file == captured_stdout {
|
||||
// The document is already formatted correctly -- no edits needed.
|
||||
Ok(None)
|
||||
} else {
|
||||
Ok(Some(vec![lsp_types::TextEdit {
|
||||
range: Range::new(Position::new(0, 0), end_position),
|
||||
new_text: captured_stdout,
|
||||
}]))
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_fixes(
|
||||
|
|
|
@ -259,6 +259,42 @@ pub use std::collections::HashMap;
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_document_unchanged() {
|
||||
if skip_slow_tests() {
|
||||
return;
|
||||
}
|
||||
|
||||
let server = project(
|
||||
r#"
|
||||
//- /Cargo.toml
|
||||
[package]
|
||||
name = "foo"
|
||||
version = "0.0.0"
|
||||
|
||||
//- /src/lib.rs
|
||||
fn main() {}
|
||||
"#,
|
||||
)
|
||||
.wait_until_workspace_is_loaded();
|
||||
|
||||
server.request::<Formatting>(
|
||||
DocumentFormattingParams {
|
||||
text_document: server.doc_id("src/lib.rs"),
|
||||
options: FormattingOptions {
|
||||
tab_size: 4,
|
||||
insert_spaces: false,
|
||||
insert_final_newline: None,
|
||||
trim_final_newlines: None,
|
||||
trim_trailing_whitespace: None,
|
||||
properties: HashMap::new(),
|
||||
},
|
||||
work_done_progress_params: WorkDoneProgressParams::default(),
|
||||
},
|
||||
json!(null),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_missing_module_code_action() {
|
||||
if skip_slow_tests() {
|
||||
|
|
Loading…
Reference in a new issue