aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/rust-analyzer/src/handlers.rs7
-rw-r--r--crates/rust-analyzer/tests/rust-analyzer/main.rs143
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(
448 let old_name = from_path.file_stem()?.to_str()?; 448 let old_name = from_path.file_stem()?.to_str()?;
449 let new_name = to_path.file_stem()?.to_str()?; 449 let new_name = to_path.file_stem()?.to_str()?;
450 match (old_name, new_name) { 450 match (old_name, new_name) {
451 ("mod", "mod") => { 451 ("mod", _) => None,
452 Some((snap.url_to_file_id(&from).ok()?, new_name.to_string())) 452 (_, "mod") => None,
453 } 453 _ => Some((snap.url_to_file_id(&from).ok()?, new_name.to_string())),
454 _ => None,
455 } 454 }
456 } 455 }
457 } 456 }
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};
15 15
16use lsp_types::{ 16use lsp_types::{
17 notification::DidOpenTextDocument, 17 notification::DidOpenTextDocument,
18 request::{CodeActionRequest, Completion, Formatting, GotoTypeDefinition, HoverRequest}, 18 request::{
19 CodeActionRequest, Completion, Formatting, GotoTypeDefinition, HoverRequest,
20 WillRenameFiles,
21 },
19 CodeActionContext, CodeActionParams, CompletionParams, DidOpenTextDocumentParams, 22 CodeActionContext, CodeActionParams, CompletionParams, DidOpenTextDocumentParams,
20 DocumentFormattingParams, FormattingOptions, GotoDefinitionParams, HoverParams, 23 DocumentFormattingParams, FileRename, FormattingOptions, GotoDefinitionParams, HoverParams,
21 PartialResultParams, Position, Range, TextDocumentItem, TextDocumentPositionParams, 24 PartialResultParams, Position, Range, RenameFilesParams, TextDocumentItem,
22 WorkDoneProgressParams, 25 TextDocumentPositionParams, WorkDoneProgressParams,
23}; 26};
24use rust_analyzer::lsp_ext::{OnEnter, Runnables, RunnablesParams}; 27use rust_analyzer::lsp_ext::{OnEnter, Runnables, RunnablesParams};
25use serde_json::json; 28use serde_json::json;
@@ -733,3 +736,135 @@ pub fn foo(_input: TokenStream) -> TokenStream {
733 let value = res.get("contents").unwrap().get("value").unwrap().to_string(); 736 let value = res.get("contents").unwrap().get("value").unwrap().to_string();
734 assert_eq!(value, r#""\n```rust\nfoo::Bar\n```\n\n```rust\nfn bar()\n```""#) 737 assert_eq!(value, r#""\n```rust\nfoo::Bar\n```\n\n```rust\nfn bar()\n```""#)
735} 738}
739
740#[test]
741fn test_will_rename_files_same_level() {
742 // if skip_slow_tests() {
743 // return;
744 // }
745 let tmp_dir = TestDir::new();
746 let tmp_dir_path = tmp_dir.path().to_owned();
747 let tmp_dir_path = tmp_dir_path.to_str().unwrap();
748 let base_path = PathBuf::from(format!("file://{}", tmp_dir_path));
749
750 let code = r#"
751//- /Cargo.toml
752[package]
753name = "foo"
754version = "0.0.0"
755
756//- /src/lib.rs
757mod old_file;
758mod from_mod;
759mod to_mod;
760mod old_folder;
761fn main() {}
762
763//- /src/old_file.rs
764
765//- /src/old_folder/mod.rs
766
767//- /src/from_mod/mod.rs
768
769//- /src/to_mod/foo.rs
770
771"#;
772 let server =
773 Project::with_fixture(&code).tmp_dir(tmp_dir).server().wait_until_workspace_is_loaded();
774
775 //rename same level file
776 server.request::<WillRenameFiles>(
777 RenameFilesParams {
778 files: vec![FileRename {
779 old_uri: base_path.join("src/old_file.rs").to_str().unwrap().to_string(),
780 new_uri: base_path.join("src/new_file.rs").to_str().unwrap().to_string(),
781 }],
782 },
783 json!({
784 "documentChanges": [
785 {
786 "textDocument": {
787 "uri": format!("file://{}/src/lib.rs", tmp_dir_path),
788 "version": null
789 },
790 "edits": [
791 {
792 "range": {
793 "start": {
794 "line": 0,
795 "character": 4
796 },
797 "end": {
798 "line": 0,
799 "character": 12
800 }
801 },
802 "newText": "new_file"
803 }
804 ]
805 }
806 ]
807 }),
808 );
809
810 //rename file from mod.rs to foo.rs
811 server.request::<WillRenameFiles>(
812 RenameFilesParams {
813 files: vec![FileRename {
814 old_uri: base_path.join("src/from_mod/mod.rs").to_str().unwrap().to_string(),
815 new_uri: base_path.join("src/from_mod/foo.rs").to_str().unwrap().to_string(),
816 }],
817 },
818 json!({
819 "documentChanges": []
820 }),
821 );
822
823 //rename file from foo.rs to mod.rs
824 server.request::<WillRenameFiles>(
825 RenameFilesParams {
826 files: vec![FileRename {
827 old_uri: base_path.join("src/to_mod/foo.rs").to_str().unwrap().to_string(),
828 new_uri: base_path.join("src/to_mod/mod.rs").to_str().unwrap().to_string(),
829 }],
830 },
831 json!({
832 "documentChanges": []
833 }),
834 );
835
836 //rename same level file
837 server.request::<WillRenameFiles>(
838 RenameFilesParams {
839 files: vec![FileRename {
840 old_uri: base_path.join("src/old_folder").to_str().unwrap().to_string(),
841 new_uri: base_path.join("src/new_folder").to_str().unwrap().to_string(),
842 }],
843 },
844 json!({
845 "documentChanges": [
846 {
847 "textDocument": {
848 "uri": format!("file://{}/src/lib.rs", tmp_dir_path),
849 "version": null
850 },
851 "edits": [
852 {
853 "range": {
854 "start": {
855 "line": 3,
856 "character": 4
857 },
858 "end": {
859 "line": 3,
860 "character": 14
861 }
862 },
863 "newText": "new_folder"
864 }
865 ]
866 }
867 ]
868 }),
869 );
870}