From e1c9c9b6049d9610d2e4454ffb7c337cb5c69d5b Mon Sep 17 00:00:00 2001 From: ShuiRuTian <158983297@qq.com> Date: Wed, 13 Jan 2021 00:30:49 +0800 Subject: fix and add tests. --- crates/rust-analyzer/src/handlers.rs | 7 +- crates/rust-analyzer/tests/rust-analyzer/main.rs | 143 ++++++++++++++++++++++- 2 files changed, 142 insertions(+), 8 deletions(-) diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 53e64f106..bfd92159c 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -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())), } } } diff --git a/crates/rust-analyzer/tests/rust-analyzer/main.rs b/crates/rust-analyzer/tests/rust-analyzer/main.rs index 84db0856d..787239385 100644 --- a/crates/rust-analyzer/tests/rust-analyzer/main.rs +++ b/crates/rust-analyzer/tests/rust-analyzer/main.rs @@ -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::( + 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::( + 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::( + 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::( + 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" + } + ] + } + ] + }), + ); +} -- cgit v1.2.3