feat(lsp): update imports on file rename (#20245)

Closes https://github.com/denoland/vscode_deno/issues/410.
This commit is contained in:
Nayeem Rahman 2023-08-26 01:50:47 +01:00 committed by GitHub
parent a526cff0a9
commit 6f077ebb07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 221 additions and 31 deletions

View file

@ -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,