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

@ -29,7 +29,16 @@ pub struct Assist {
/// cumbersome, especially if you want to embed an assist into another data
/// structure, such as a diagnostic.
pub source_change: Option<SourceChange>,
pub trigger_signature_help: bool,
/// The command to execute after the assist is applied.
pub command: Option<Command>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Command {
/// Show the parameter hints popup.
TriggerSignatureHelp,
/// Rename the just inserted item.
Rename,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

View file

@ -5,7 +5,7 @@
use std::{collections::hash_map::Entry, iter, mem};
use crate::SnippetCap;
use crate::{assists::Command, SnippetCap};
use base_db::{AnchoredPathBuf, FileId};
use itertools::Itertools;
use nohash_hasher::IntMap;
@ -194,7 +194,7 @@ pub struct SourceChangeBuilder {
pub edit: TextEditBuilder,
pub file_id: FileId,
pub source_change: SourceChange,
pub trigger_signature_help: bool,
pub command: Option<Command>,
/// Maps the original, immutable `SyntaxNode` to a `clone_for_update` twin.
pub mutated_tree: Option<TreeMutator>,
@ -236,7 +236,7 @@ impl SourceChangeBuilder {
edit: TextEdit::builder(),
file_id,
source_change: SourceChange::default(),
trigger_signature_help: false,
command: None,
mutated_tree: None,
snippet_builder: None,
}
@ -304,8 +304,15 @@ impl SourceChangeBuilder {
let file_system_edit = FileSystemEdit::MoveFile { src, dst };
self.source_change.push_file_system_edit(file_system_edit);
}
/// Triggers the parameter hint popup after the assist is applied
pub fn trigger_signature_help(&mut self) {
self.trigger_signature_help = true;
self.command = Some(Command::TriggerSignatureHelp);
}
/// Renames the item at the cursor position after the assist is applied
pub fn rename(&mut self) {
self.command = Some(Command::Rename);
}
/// Adds a tabstop snippet to place the cursor before `node`