Trigger VSCode to rename after extract variable assist is applied

When the user applies the "Extract Variable" assist, the cursor is
positioned at the newly inserted variable. This commit adds a command
to the assist that triggers the rename action in VSCode. This way, the
user can quickly rename the variable after applying the assist.

Fixes part of: #17579
This commit is contained in:
bors 2024-07-11 08:55:34 +00:00 committed by Josh McKinney
parent 5577e4e3b1
commit 8efe8a8528
No known key found for this signature in database
GPG key ID: 722287396A903BC5
17 changed files with 80 additions and 33 deletions

View file

@ -1135,6 +1135,7 @@ pub struct ClientCommandsConfig {
pub show_reference: bool,
pub goto_location: bool,
pub trigger_parameter_hints: bool,
pub rename: bool,
}
#[derive(Debug)]
@ -1901,6 +1902,7 @@ impl Config {
show_reference: get("rust-analyzer.showReferences"),
goto_location: get("rust-analyzer.gotoLocation"),
trigger_parameter_hints: get("editor.action.triggerParameterHints"),
rename: get("editor.action.rename"),
}
}

View file

@ -13,7 +13,7 @@ use ide::{
NavigationTarget, ReferenceCategory, RenameError, Runnable, Severity, SignatureHelp,
SnippetEdit, SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
};
use ide_db::{rust_doc::format_docs, FxHasher};
use ide_db::{assists, rust_doc::format_docs, FxHasher};
use itertools::Itertools;
use paths::{Utf8Component, Utf8Prefix};
use semver::VersionReq;
@ -1336,8 +1336,14 @@ pub(crate) fn code_action(
command: None,
};
if assist.trigger_signature_help && snap.config.client_commands().trigger_parameter_hints {
if assist.command == Some(assists::Command::TriggerSignatureHelp)
&& snap.config.client_commands().trigger_parameter_hints
{
res.command = Some(command::trigger_parameter_hints());
} else if assist.command == Some(assists::Command::Rename)
&& snap.config.client_commands().rename
{
res.command = Some(command::rename());
}
match (assist.source_change, resolve_data) {
@ -1715,6 +1721,14 @@ pub(crate) mod command {
arguments: None,
}
}
pub(crate) fn rename() -> lsp_types::Command {
lsp_types::Command {
title: "rename".into(),
command: "rust-analyzer.rename".into(),
arguments: None,
}
}
}
pub(crate) fn implementation_title(count: usize) -> String {