]> git.lizzy.rs Git - rust.git/commitdiff
Merge #7263
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Wed, 13 Jan 2021 18:35:08 +0000 (18:35 +0000)
committerGitHub <noreply@github.com>
Wed, 13 Jan 2021 18:35:08 +0000 (18:35 +0000)
7263: Use upstream TextSize API r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
crates/rust-analyzer/src/caps.rs
crates/rust-analyzer/src/handlers.rs
crates/rust-analyzer/tests/rust-analyzer/main.rs

index 3db0d55c5a8e0c4df964a39751ac79737552ea04..f148521a2cf887cfc1a130f92dd7bebdfacfb758 100644 (file)
@@ -75,14 +75,24 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti
                 will_create: None,
                 did_rename: None,
                 will_rename: Some(FileOperationRegistrationOptions {
-                    filters: vec![FileOperationFilter {
-                        scheme: Some(String::from("file")),
-                        pattern: FileOperationPattern {
-                            glob: String::from("**/*.rs"),
-                            matches: Some(FileOperationPatternKind::File),
-                            options: None,
+                    filters: vec![
+                        FileOperationFilter {
+                            scheme: Some(String::from("file")),
+                            pattern: FileOperationPattern {
+                                glob: String::from("**/*.rs"),
+                                matches: Some(FileOperationPatternKind::File),
+                                options: None,
+                            },
                         },
-                    }],
+                        FileOperationFilter {
+                            scheme: Some(String::from("file")),
+                            pattern: FileOperationPattern {
+                                glob: String::from("**"),
+                                matches: Some(FileOperationPatternKind::Folder),
+                                options: None,
+                            },
+                        },
+                    ],
                 }),
                 did_delete: None,
                 will_delete: None,
index 70dd519110037dbdd62109a2ee55acee214d8027..dc81f55d6c421815226ea065fff3496a0aa1b341 100644 (file)
@@ -432,9 +432,27 @@ pub(crate) fn handle_will_rename_files(
             // Limit to single-level moves for now.
             match (from_path.parent(), to_path.parent()) {
                 (Some(p1), Some(p2)) if p1 == p2 => {
-                    let new_name = to_path.file_stem()?;
-                    let new_name = new_name.to_str()?;
-                    Some((snap.url_to_file_id(&from).ok()?, new_name.to_string()))
+                    if from_path.is_dir() {
+                        // add '/' to end of url -- from `file://path/to/folder` to `file://path/to/folder/`
+                        let mut old_folder_name = from_path.file_stem()?.to_str()?.to_string();
+                        old_folder_name.push('/');
+                        let from_with_trailing_slash = from.join(&old_folder_name).ok()?;
+
+                        let imitate_from_url = from_with_trailing_slash.join("mod.rs").ok()?;
+                        let new_file_name = to_path.file_name()?.to_str()?;
+                        Some((
+                            snap.url_to_file_id(&imitate_from_url).ok()?,
+                            new_file_name.to_string(),
+                        ))
+                    } else {
+                        let old_name = from_path.file_stem()?.to_str()?;
+                        let new_name = to_path.file_stem()?.to_str()?;
+                        match (old_name, new_name) {
+                            ("mod", _) => None,
+                            (_, "mod") => None,
+                            _ => Some((snap.url_to_file_id(&from).ok()?, new_name.to_string())),
+                        }
+                    }
                 }
                 _ => None,
             }
index f5c5c63e5bd3c593e0d6085c65e979036cb71501..80bde29b9e68a5a9f558f55cd9540ee84142bd2e 100644 (file)
 use expect_test::expect;
 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;
@@ -745,3 +748,136 @@ fn bar()
         ```"#]]
     .assert_eq(&value);
 }
+
+#[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_str = tmp_dir_path.to_str().unwrap();
+    let base_path = PathBuf::from(format!("file://{}", tmp_dir_str));
+
+    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://{}", tmp_dir_path.join("src").join("lib.rs").to_str().unwrap().to_string().replace("C:\\", "/c:/").replace("\\", "/")),
+                "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://{}", tmp_dir_path.join("src").join("lib.rs").to_str().unwrap().to_string().replace("C:\\", "/c:/").replace("\\", "/")),
+                "version": null
+              },
+              "edits": [
+                {
+                  "range": {
+                    "start": {
+                      "line": 3,
+                      "character": 4
+                    },
+                    "end": {
+                      "line": 3,
+                      "character": 14
+                    }
+                  },
+                  "newText": "new_folder"
+                }
+              ]
+            }
+          ]
+        }),
+    );
+}