fix and add tests.

This commit is contained in:
ShuiRuTian 2021-01-13 00:30:49 +08:00
parent cf3042f881
commit e1c9c9b604
2 changed files with 142 additions and 8 deletions

View File

@ -448,10 +448,9 @@ pub(crate) fn handle_will_rename_files(
let old_name = from_path.file_stem()?.to_str()?;
let new_name = to_path.file_stem()?.to_str()?;
match (old_name, new_name) {
("mod", "mod") => {
Some((snap.url_to_file_id(&from).ok()?, new_name.to_string()))
}
_ => None,
("mod", _) => None,
(_, "mod") => None,
_ => Some((snap.url_to_file_id(&from).ok()?, new_name.to_string())),
}
}
}

View File

@ -15,11 +15,14 @@ use std::{collections::HashMap, path::PathBuf, time::Instant};
use lsp_types::{
notification::DidOpenTextDocument,
request::{CodeActionRequest, Completion, Formatting, GotoTypeDefinition, HoverRequest},
request::{
CodeActionRequest, Completion, Formatting, GotoTypeDefinition, HoverRequest,
WillRenameFiles,
},
CodeActionContext, CodeActionParams, CompletionParams, DidOpenTextDocumentParams,
DocumentFormattingParams, FormattingOptions, GotoDefinitionParams, HoverParams,
PartialResultParams, Position, Range, TextDocumentItem, TextDocumentPositionParams,
WorkDoneProgressParams,
DocumentFormattingParams, FileRename, FormattingOptions, GotoDefinitionParams, HoverParams,
PartialResultParams, Position, Range, RenameFilesParams, TextDocumentItem,
TextDocumentPositionParams, WorkDoneProgressParams,
};
use rust_analyzer::lsp_ext::{OnEnter, Runnables, RunnablesParams};
use serde_json::json;
@ -733,3 +736,135 @@ pub fn foo(_input: TokenStream) -> TokenStream {
let value = res.get("contents").unwrap().get("value").unwrap().to_string();
assert_eq!(value, r#""\n```rust\nfoo::Bar\n```\n\n```rust\nfn bar()\n```""#)
}
#[test]
fn test_will_rename_files_same_level() {
// if skip_slow_tests() {
// return;
// }
let tmp_dir = TestDir::new();
let tmp_dir_path = tmp_dir.path().to_owned();
let tmp_dir_path = tmp_dir_path.to_str().unwrap();
let base_path = PathBuf::from(format!("file://{}", tmp_dir_path));
let code = r#"
//- /Cargo.toml
[package]
name = "foo"
version = "0.0.0"
//- /src/lib.rs
mod old_file;
mod from_mod;
mod to_mod;
mod old_folder;
fn main() {}
//- /src/old_file.rs
//- /src/old_folder/mod.rs
//- /src/from_mod/mod.rs
//- /src/to_mod/foo.rs
"#;
let server =
Project::with_fixture(&code).tmp_dir(tmp_dir).server().wait_until_workspace_is_loaded();
//rename same level file
server.request::<WillRenameFiles>(
RenameFilesParams {
files: vec![FileRename {
old_uri: base_path.join("src/old_file.rs").to_str().unwrap().to_string(),
new_uri: base_path.join("src/new_file.rs").to_str().unwrap().to_string(),
}],
},
json!({
"documentChanges": [
{
"textDocument": {
"uri": format!("file://{}/src/lib.rs", tmp_dir_path),
"version": null
},
"edits": [
{
"range": {
"start": {
"line": 0,
"character": 4
},
"end": {
"line": 0,
"character": 12
}
},
"newText": "new_file"
}
]
}
]
}),
);
//rename file from mod.rs to foo.rs
server.request::<WillRenameFiles>(
RenameFilesParams {
files: vec![FileRename {
old_uri: base_path.join("src/from_mod/mod.rs").to_str().unwrap().to_string(),
new_uri: base_path.join("src/from_mod/foo.rs").to_str().unwrap().to_string(),
}],
},
json!({
"documentChanges": []
}),
);
//rename file from foo.rs to mod.rs
server.request::<WillRenameFiles>(
RenameFilesParams {
files: vec![FileRename {
old_uri: base_path.join("src/to_mod/foo.rs").to_str().unwrap().to_string(),
new_uri: base_path.join("src/to_mod/mod.rs").to_str().unwrap().to_string(),
}],
},
json!({
"documentChanges": []
}),
);
//rename same level file
server.request::<WillRenameFiles>(
RenameFilesParams {
files: vec![FileRename {
old_uri: base_path.join("src/old_folder").to_str().unwrap().to_string(),
new_uri: base_path.join("src/new_folder").to_str().unwrap().to_string(),
}],
},
json!({
"documentChanges": [
{
"textDocument": {
"uri": format!("file://{}/src/lib.rs", tmp_dir_path),
"version": null
},
"edits": [
{
"range": {
"start": {
"line": 3,
"character": 4
},
"end": {
"line": 3,
"character": 14
}
},
"newText": "new_folder"
}
]
}
]
}),
);
}