mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Merge #6017
6017: Don't return any TextEdit if formatting is unchanged r=jonas-schievink a=cuviper 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. Co-authored-by: Josh Stone <cuviper@gmail.com>
This commit is contained in:
commit
d84c18d989
2 changed files with 45 additions and 4 deletions
|
@ -748,10 +748,15 @@ pub(crate) fn handle_formatting(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *file == captured_stdout {
|
||||||
|
// The document is already formatted correctly -- no edits needed.
|
||||||
|
Ok(None)
|
||||||
|
} else {
|
||||||
Ok(Some(vec![lsp_types::TextEdit {
|
Ok(Some(vec![lsp_types::TextEdit {
|
||||||
range: Range::new(Position::new(0, 0), end_position),
|
range: Range::new(Position::new(0, 0), end_position),
|
||||||
new_text: captured_stdout,
|
new_text: captured_stdout,
|
||||||
}]))
|
}]))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_fixes(
|
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]
|
#[test]
|
||||||
fn test_missing_module_code_action() {
|
fn test_missing_module_code_action() {
|
||||||
if skip_slow_tests() {
|
if skip_slow_tests() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue