summaryrefslogtreecommitdiff
path: root/cli/lsp/language_server.rs
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2023-08-26 01:50:47 +0100
committerGitHub <noreply@github.com>2023-08-26 02:50:47 +0200
commit6f077ebb07740446dba26ef2b3f9fb35fa0d9d1d (patch)
treea1e4b10f473828e5d8b53c17447fe7089393cb5d /cli/lsp/language_server.rs
parenta526cff0a9888a475d5b542efda443fe720a93d0 (diff)
feat(lsp): update imports on file rename (#20245)
Closes https://github.com/denoland/vscode_deno/issues/410.
Diffstat (limited to 'cli/lsp/language_server.rs')
-rw-r--r--cli/lsp/language_server.rs87
1 files changed, 67 insertions, 20 deletions
diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs
index c29817919..9137092e1 100644
--- a/cli/lsp/language_server.rs
+++ b/cli/lsp/language_server.rs
@@ -94,6 +94,7 @@ use crate::factory::CliFactory;
use crate::file_fetcher::FileFetcher;
use crate::graph_util;
use crate::http_util::HttpClient;
+use crate::lsp::tsc::file_text_changes_to_workspace_edit;
use crate::lsp::urls::LspUrlKind;
use crate::npm::create_npm_fs_resolver;
use crate::npm::CliNpmRegistryApi;
@@ -2060,13 +2061,7 @@ impl Inner {
action_data.action_name,
)
.await?;
- code_action.edit = refactor_edit_info
- .to_workspace_edit(self)
- .await
- .map_err(|err| {
- error!("Unable to convert changes to edits: {}", err);
- LspError::internal_error()
- })?;
+ code_action.edit = refactor_edit_info.to_workspace_edit(self).await?;
code_action
} else {
// The code action doesn't need to be resolved
@@ -2934,6 +2929,37 @@ impl Inner {
}
}
+ async fn will_rename_files(
+ &self,
+ params: RenameFilesParams,
+ ) -> LspResult<Option<WorkspaceEdit>> {
+ let mut changes = vec![];
+ for rename in params.files {
+ changes.extend(
+ self
+ .ts_server
+ .get_edits_for_file_rename(
+ self.snapshot(),
+ self.url_map.normalize_url(
+ &resolve_url(&rename.old_uri).unwrap(),
+ LspUrlKind::File,
+ ),
+ self.url_map.normalize_url(
+ &resolve_url(&rename.new_uri).unwrap(),
+ LspUrlKind::File,
+ ),
+ (&self.fmt_options.options).into(),
+ tsc::UserPreferences {
+ allow_text_changes_in_new_files: Some(true),
+ ..Default::default()
+ },
+ )
+ .await?,
+ );
+ }
+ file_text_changes_to_workspace_edit(&changes, self)
+ }
+
async fn symbol(
&self,
params: WorkspaceSymbolParams,
@@ -3004,7 +3030,7 @@ impl tower_lsp::LanguageServer for LanguageServer {
}
async fn initialized(&self, _: InitializedParams) {
- let mut maybe_registration = None;
+ let mut registrations = Vec::with_capacity(2);
let client = {
let mut ls = self.0.write().await;
if ls
@@ -3015,19 +3041,33 @@ impl tower_lsp::LanguageServer for LanguageServer {
// we are going to watch all the JSON files in the workspace, and the
// notification handler will pick up any of the changes of those files we
// are interested in.
- let watch_registration_options =
- DidChangeWatchedFilesRegistrationOptions {
- watchers: vec![FileSystemWatcher {
- glob_pattern: "**/*.{json,jsonc,lock}".to_string(),
- kind: Some(WatchKind::Change),
- }],
- };
- maybe_registration = Some(Registration {
+ let options = DidChangeWatchedFilesRegistrationOptions {
+ watchers: vec![FileSystemWatcher {
+ glob_pattern: "**/*.{json,jsonc,lock}".to_string(),
+ kind: Some(WatchKind::Change),
+ }],
+ };
+ registrations.push(Registration {
id: "workspace/didChangeWatchedFiles".to_string(),
method: "workspace/didChangeWatchedFiles".to_string(),
- register_options: Some(
- serde_json::to_value(watch_registration_options).unwrap(),
- ),
+ register_options: Some(serde_json::to_value(options).unwrap()),
+ });
+ }
+ if ls.config.client_capabilities.workspace_will_rename_files {
+ let options = FileOperationRegistrationOptions {
+ filters: vec![FileOperationFilter {
+ scheme: Some("file".to_string()),
+ pattern: FileOperationPattern {
+ glob: "**/*".to_string(),
+ matches: None,
+ options: None,
+ },
+ }],
+ };
+ registrations.push(Registration {
+ id: "workspace/willRenameFiles".to_string(),
+ method: "workspace/willRenameFiles".to_string(),
+ register_options: Some(serde_json::to_value(options).unwrap()),
});
}
@@ -3042,7 +3082,7 @@ impl tower_lsp::LanguageServer for LanguageServer {
ls.client.clone()
};
- if let Some(registration) = maybe_registration {
+ for registration in registrations {
if let Err(err) = client
.when_outside_lsp_lock()
.register_capability(vec![registration])
@@ -3376,6 +3416,13 @@ impl tower_lsp::LanguageServer for LanguageServer {
self.0.read().await.signature_help(params).await
}
+ async fn will_rename_files(
+ &self,
+ params: RenameFilesParams,
+ ) -> LspResult<Option<WorkspaceEdit>> {
+ self.0.read().await.will_rename_files(params).await
+ }
+
async fn symbol(
&self,
params: WorkspaceSymbolParams,