From 94cf92a98f205c76c8a26c0b0aa4efe01a786a36 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Thu, 7 Mar 2024 17:27:24 +0000 Subject: [PATCH] fix(lsp): don't apply renames to remote modules (#22765) --- cli/lsp/tsc.rs | 9 +++++ tests/integration/lsp_tests.rs | 63 ++++++++++++++++++++++++++++++++ tests/testdata/subdir/exports.ts | 1 + 3 files changed, 73 insertions(+) create mode 100644 tests/testdata/subdir/exports.ts diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index ce848c210b..1040241672 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -2115,8 +2115,13 @@ impl RenameLocations { LspClientUrl, lsp::TextDocumentEdit, > = HashMap::new(); + let mut includes_non_files = false; for location in self.locations.iter() { let specifier = resolve_url(&location.document_span.file_name)?; + if specifier.scheme() != "file" { + includes_non_files = true; + continue; + } let uri = language_server.url_map.normalize_specifier(&specifier)?; let asset_or_doc = language_server.get_asset_or_document(&specifier)?; @@ -2146,6 +2151,10 @@ impl RenameLocations { })); } + if includes_non_files { + language_server.client.show_message(lsp::MessageType::WARNING, "The renamed symbol had references in non-file schemed modules. These have not been modified."); + } + Ok(lsp::WorkspaceEdit { change_annotations: None, changes: None, diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs index 9300413b53..8165cc86a2 100644 --- a/tests/integration/lsp_tests.rs +++ b/tests/integration/lsp_tests.rs @@ -2461,6 +2461,69 @@ fn lsp_hover_deps_preserved_when_invalid_parse() { client.shutdown(); } +// Regression test for https://github.com/denoland/vscode_deno/issues/1068. +#[test] +fn lsp_rename_synbol_file_scheme_edits_only() { + let context = TestContextBuilder::new() + .use_http_server() + .use_temp_cwd() + .build(); + let temp_dir = context.temp_dir(); + let mut client = context.new_lsp_command().build(); + client.initialize_default(); + client.did_open(json!({ + "textDocument": { + "uri": temp_dir.uri().join("file.ts").unwrap(), + "languageId": "typescript", + "version": 1, + "text": r#" + import { SEPARATOR } from "http://localhost:4545/subdir/exports.ts"; + console.log(SEPARATOR); + "#, + }, + })); + let res = client.write_request( + "textDocument/rename", + json!({ + "textDocument": { + "uri": temp_dir.uri().join("file.ts").unwrap(), + }, + "position": { "line": 1, "character": 17 }, + "newName": "PATH_SEPARATOR", + }), + ); + assert_eq!( + res, + json!({ + "documentChanges": [ + { + "textDocument": { + "uri": temp_dir.uri().join("file.ts").unwrap(), + "version": 1, + }, + "edits": [ + { + "range": { + "start": { "line": 1, "character": 17 }, + "end": { "line": 1, "character": 26 }, + }, + "newText": "PATH_SEPARATOR", + }, + { + "range": { + "start": { "line": 2, "character": 20 }, + "end": { "line": 2, "character": 29 }, + }, + "newText": "PATH_SEPARATOR", + }, + ], + } + ], + }) + ); + client.shutdown(); +} + #[test] fn lsp_hover_typescript_types() { let context = TestContextBuilder::new() diff --git a/tests/testdata/subdir/exports.ts b/tests/testdata/subdir/exports.ts new file mode 100644 index 0000000000..d5f8be3422 --- /dev/null +++ b/tests/testdata/subdir/exports.ts @@ -0,0 +1 @@ +export const SEPARATOR = "/";