Skip to content

Commit e961b1a

Browse files
committed
Auto merge of #17587 - joshka:jm/edit-name-after-refactor, r=Veykril
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 https://github.com/user-attachments/assets/4cf38740-ab22-4b94-b0f1-eddd51c26c29 I haven't yet looked at the module or function extraction assists yet.
2 parents 5784915 + bfa6a5c commit e961b1a

File tree

17 files changed

+80
-36
lines changed

17 files changed

+80
-36
lines changed

crates/ide-assists/src/assist_context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,19 @@ impl Assists {
185185
return None;
186186
}
187187

188-
let mut trigger_signature_help = false;
188+
let mut command = None;
189189
let source_change = if self.resolve.should_resolve(&id) {
190190
let mut builder = SourceChangeBuilder::new(self.file);
191191
f(&mut builder);
192-
trigger_signature_help = builder.trigger_signature_help;
192+
command = builder.command.take();
193193
Some(builder.finish())
194194
} else {
195195
None
196196
};
197197

198198
let label = Label::new(label);
199199
let group = group.cloned();
200-
self.buf.push(Assist { id, label, group, target, source_change, trigger_signature_help });
200+
self.buf.push(Assist { id, label, group, target, source_change, command });
201201
Some(())
202202
}
203203

crates/ide-assists/src/handlers/extract_variable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
197197
block.indent(indent_to);
198198
}
199199
}
200+
edit.rename();
200201
},
201202
)
202203
}

crates/ide-assists/src/tests.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ pub fn test_some_range(a: int) -> bool {
454454
group: None,
455455
target: 59..60,
456456
source_change: None,
457-
trigger_signature_help: false,
457+
command: None,
458458
}
459459
"#]]
460460
.assert_debug_eq(&extract_into_variable_assist);
@@ -470,7 +470,7 @@ pub fn test_some_range(a: int) -> bool {
470470
group: None,
471471
target: 59..60,
472472
source_change: None,
473-
trigger_signature_help: false,
473+
command: None,
474474
}
475475
"#]]
476476
.assert_debug_eq(&extract_into_function_assist);
@@ -500,7 +500,7 @@ pub fn test_some_range(a: int) -> bool {
500500
group: None,
501501
target: 59..60,
502502
source_change: None,
503-
trigger_signature_help: false,
503+
command: None,
504504
}
505505
"#]]
506506
.assert_debug_eq(&extract_into_variable_assist);
@@ -516,7 +516,7 @@ pub fn test_some_range(a: int) -> bool {
516516
group: None,
517517
target: 59..60,
518518
source_change: None,
519-
trigger_signature_help: false,
519+
command: None,
520520
}
521521
"#]]
522522
.assert_debug_eq(&extract_into_function_assist);
@@ -587,7 +587,9 @@ pub fn test_some_range(a: int) -> bool {
587587
is_snippet: true,
588588
},
589589
),
590-
trigger_signature_help: false,
590+
command: Some(
591+
Rename,
592+
),
591593
}
592594
"#]]
593595
.assert_debug_eq(&extract_into_variable_assist);
@@ -603,7 +605,7 @@ pub fn test_some_range(a: int) -> bool {
603605
group: None,
604606
target: 59..60,
605607
source_change: None,
606-
trigger_signature_help: false,
608+
command: None,
607609
}
608610
"#]]
609611
.assert_debug_eq(&extract_into_function_assist);
@@ -666,7 +668,9 @@ pub fn test_some_range(a: int) -> bool {
666668
is_snippet: true,
667669
},
668670
),
669-
trigger_signature_help: false,
671+
command: Some(
672+
Rename,
673+
),
670674
}
671675
"#]]
672676
.assert_debug_eq(&extract_into_variable_assist);
@@ -715,7 +719,7 @@ pub fn test_some_range(a: int) -> bool {
715719
is_snippet: true,
716720
},
717721
),
718-
trigger_signature_help: false,
722+
command: None,
719723
}
720724
"#]]
721725
.assert_debug_eq(&extract_into_function_assist);

crates/ide-db/src/assists.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ pub struct Assist {
2929
/// cumbersome, especially if you want to embed an assist into another data
3030
/// structure, such as a diagnostic.
3131
pub source_change: Option<SourceChange>,
32-
pub trigger_signature_help: bool,
32+
/// The command to execute after the assist is applied.
33+
pub command: Option<Command>,
34+
}
35+
36+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37+
pub enum Command {
38+
/// Show the parameter hints popup.
39+
TriggerSignatureHelp,
40+
/// Rename the just inserted item.
41+
Rename,
3342
}
3443

3544
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

crates/ide-db/src/source_change.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use std::{collections::hash_map::Entry, iter, mem};
77

8-
use crate::SnippetCap;
8+
use crate::{assists::Command, SnippetCap};
99
use base_db::{AnchoredPathBuf, FileId};
1010
use itertools::Itertools;
1111
use nohash_hasher::IntMap;
@@ -194,7 +194,7 @@ pub struct SourceChangeBuilder {
194194
pub edit: TextEditBuilder,
195195
pub file_id: FileId,
196196
pub source_change: SourceChange,
197-
pub trigger_signature_help: bool,
197+
pub command: Option<Command>,
198198

199199
/// Maps the original, immutable `SyntaxNode` to a `clone_for_update` twin.
200200
pub mutated_tree: Option<TreeMutator>,
@@ -236,7 +236,7 @@ impl SourceChangeBuilder {
236236
edit: TextEdit::builder(),
237237
file_id,
238238
source_change: SourceChange::default(),
239-
trigger_signature_help: false,
239+
command: None,
240240
mutated_tree: None,
241241
snippet_builder: None,
242242
}
@@ -304,8 +304,15 @@ impl SourceChangeBuilder {
304304
let file_system_edit = FileSystemEdit::MoveFile { src, dst };
305305
self.source_change.push_file_system_edit(file_system_edit);
306306
}
307+
308+
/// Triggers the parameter hint popup after the assist is applied
307309
pub fn trigger_signature_help(&mut self) {
308-
self.trigger_signature_help = true;
310+
self.command = Some(Command::TriggerSignatureHelp);
311+
}
312+
313+
/// Renames the item at the cursor position after the assist is applied
314+
pub fn rename(&mut self) {
315+
self.command = Some(Command::Rename);
309316
}
310317

311318
/// Adds a tabstop snippet to place the cursor before `node`

crates/ide-diagnostics/src/handlers/trait_impl_redundant_assoc_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn quickfix_for_redundant_assoc_item(
9898
group: None,
9999
target: range,
100100
source_change: Some(source_change_builder.finish()),
101-
trigger_signature_help: false,
101+
command: None,
102102
}])
103103
}
104104

crates/ide-diagnostics/src/handlers/typed_hole.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::TypedHole) -> Option<Vec<Assist>
8282
original_range.file_id,
8383
TextEdit::replace(original_range.range, code),
8484
)),
85-
trigger_signature_help: false,
85+
command: None,
8686
})
8787
.collect();
8888

crates/ide-diagnostics/src/handlers/unresolved_field.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn add_variant_to_union(
130130
group: None,
131131
target: error_range.range,
132132
source_change: Some(src_change_builder.finish()),
133-
trigger_signature_help: false,
133+
command: None,
134134
})
135135
}
136136

@@ -173,7 +173,7 @@ fn add_field_to_struct_fix(
173173
group: None,
174174
target: error_range.range,
175175
source_change: Some(src_change_builder.finish()),
176-
trigger_signature_help: false,
176+
command: None,
177177
})
178178
}
179179
None => {
@@ -204,7 +204,7 @@ fn add_field_to_struct_fix(
204204
group: None,
205205
target: error_range.range,
206206
source_change: Some(src_change_builder.finish()),
207-
trigger_signature_help: false,
207+
command: None,
208208
})
209209
}
210210
Some(FieldList::TupleFieldList(_tuple)) => {
@@ -266,7 +266,7 @@ fn method_fix(
266266
file_id,
267267
TextEdit::insert(range.end(), "()".to_owned()),
268268
)),
269-
trigger_signature_help: false,
269+
command: None,
270270
})
271271
}
272272
#[cfg(test)]

crates/ide-diagnostics/src/handlers/unresolved_method.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn field_fix(
108108
(file_id, TextEdit::insert(range.start(), "(".to_owned())),
109109
(file_id, TextEdit::insert(range.end(), ")".to_owned())),
110110
])),
111-
trigger_signature_help: false,
111+
command: None,
112112
})
113113
}
114114

@@ -191,7 +191,7 @@ fn assoc_func_fix(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedMethodCall) -
191191
file_id,
192192
TextEdit::replace(range, assoc_func_call_expr_string),
193193
)),
194-
trigger_signature_help: false,
194+
command: None,
195195
})
196196
} else {
197197
None

crates/ide-diagnostics/src/handlers/unused_variables.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn fixes(
7373
diagnostic_range.file_id,
7474
TextEdit::replace(name_range, format!("_{}", var_name.display(db))),
7575
)),
76-
trigger_signature_help: false,
76+
command: None,
7777
}])
7878
}
7979

crates/ide-diagnostics/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
613613
group: None,
614614
target,
615615
source_change: None,
616-
trigger_signature_help: false,
616+
command: None,
617617
}
618618
}
619619

crates/ide/src/ssr.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub(crate) fn ssr_assists(
4545
group: Some(GroupLabel("Apply SSR".into())),
4646
target: comment_range,
4747
source_change,
48-
trigger_signature_help: false,
48+
command: None,
4949
};
5050

5151
ssr_assists.push(assist);
@@ -143,7 +143,7 @@ mod tests {
143143
is_snippet: false,
144144
},
145145
),
146-
trigger_signature_help: false,
146+
command: None,
147147
}
148148
"#]]
149149
.assert_debug_eq(&apply_in_file_assist);
@@ -196,7 +196,7 @@ mod tests {
196196
is_snippet: false,
197197
},
198198
),
199-
trigger_signature_help: false,
199+
command: None,
200200
}
201201
"#]]
202202
.assert_debug_eq(&apply_in_workspace_assist);
@@ -236,7 +236,7 @@ mod tests {
236236
),
237237
target: 10..21,
238238
source_change: None,
239-
trigger_signature_help: false,
239+
command: None,
240240
}
241241
"#]]
242242
.assert_debug_eq(&apply_in_file_assist);
@@ -256,7 +256,7 @@ mod tests {
256256
),
257257
target: 10..21,
258258
source_change: None,
259-
trigger_signature_help: false,
259+
command: None,
260260
}
261261
"#]]
262262
.assert_debug_eq(&apply_in_workspace_assist);

crates/rust-analyzer/src/config.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1128,13 +1128,14 @@ pub struct WorkspaceSymbolConfig {
11281128
/// How many items are returned at most.
11291129
pub search_limit: usize,
11301130
}
1131-
1131+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11321132
pub struct ClientCommandsConfig {
11331133
pub run_single: bool,
11341134
pub debug_single: bool,
11351135
pub show_reference: bool,
11361136
pub goto_location: bool,
11371137
pub trigger_parameter_hints: bool,
1138+
pub rename: bool,
11381139
}
11391140

11401141
#[derive(Debug)]
@@ -1901,6 +1902,7 @@ impl Config {
19011902
show_reference: get("rust-analyzer.showReferences"),
19021903
goto_location: get("rust-analyzer.gotoLocation"),
19031904
trigger_parameter_hints: get("editor.action.triggerParameterHints"),
1905+
rename: get("editor.action.rename"),
19041906
}
19051907
}
19061908

crates/rust-analyzer/src/lsp/to_proto.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ide::{
1313
NavigationTarget, ReferenceCategory, RenameError, Runnable, Severity, SignatureHelp,
1414
SnippetEdit, SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
1515
};
16-
use ide_db::{rust_doc::format_docs, FxHasher};
16+
use ide_db::{assists, rust_doc::format_docs, FxHasher};
1717
use itertools::Itertools;
1818
use paths::{Utf8Component, Utf8Prefix};
1919
use semver::VersionReq;
@@ -1336,9 +1336,14 @@ pub(crate) fn code_action(
13361336
command: None,
13371337
};
13381338

1339-
if assist.trigger_signature_help && snap.config.client_commands().trigger_parameter_hints {
1340-
res.command = Some(command::trigger_parameter_hints());
1341-
}
1339+
let commands = snap.config.client_commands();
1340+
res.command = match assist.command {
1341+
Some(assists::Command::TriggerSignatureHelp) if commands.trigger_parameter_hints => {
1342+
Some(command::trigger_parameter_hints())
1343+
}
1344+
Some(assists::Command::Rename) if commands.rename => Some(command::rename()),
1345+
_ => None,
1346+
};
13421347

13431348
match (assist.source_change, resolve_data) {
13441349
(Some(it), _) => res.edit = Some(snippet_workspace_edit(snap, it)?),
@@ -1715,6 +1720,14 @@ pub(crate) mod command {
17151720
arguments: None,
17161721
}
17171722
}
1723+
1724+
pub(crate) fn rename() -> lsp_types::Command {
1725+
lsp_types::Command {
1726+
title: "rename".into(),
1727+
command: "rust-analyzer.rename".into(),
1728+
arguments: None,
1729+
}
1730+
}
17181731
}
17191732

17201733
pub(crate) fn implementation_title(count: usize) -> String {

editors/code/src/client.ts

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ class ExperimentalFeatures implements lc.StaticFeature {
348348
"rust-analyzer.showReferences",
349349
"rust-analyzer.gotoLocation",
350350
"editor.action.triggerParameterHints",
351+
"editor.action.rename",
351352
],
352353
},
353354
...capabilities.experimental,

editors/code/src/commands.ts

+6
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ export function triggerParameterHints(_: CtxInit): Cmd {
118118
};
119119
}
120120

121+
export function rename(_: CtxInit): Cmd {
122+
return async () => {
123+
await vscode.commands.executeCommand("editor.action.rename");
124+
};
125+
}
126+
121127
export function openLogs(ctx: CtxInit): Cmd {
122128
return async () => {
123129
if (ctx.client.outputChannel) {

editors/code/src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ function createCommands(): Record<string, CommandFactory> {
190190
runSingle: { enabled: commands.runSingle },
191191
showReferences: { enabled: commands.showReferences },
192192
triggerParameterHints: { enabled: commands.triggerParameterHints },
193+
rename: { enabled: commands.rename },
193194
openLogs: { enabled: commands.openLogs },
194195
revealDependency: { enabled: commands.revealDependency },
195196
};

0 commit comments

Comments
 (0)