Skip to content

Commit

Permalink
Auto merge of #17587 - joshka:jm/edit-name-after-refactor, r=Veykril
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jul 15, 2024
2 parents 5784915 + bfa6a5c commit e961b1a
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 36 deletions.
6 changes: 3 additions & 3 deletions crates/ide-assists/src/assist_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,19 @@ impl Assists {
return None;
}

let mut trigger_signature_help = false;
let mut command = None;
let source_change = if self.resolve.should_resolve(&id) {
let mut builder = SourceChangeBuilder::new(self.file);
f(&mut builder);
trigger_signature_help = builder.trigger_signature_help;
command = builder.command.take();
Some(builder.finish())
} else {
None
};

let label = Label::new(label);
let group = group.cloned();
self.buf.push(Assist { id, label, group, target, source_change, trigger_signature_help });
self.buf.push(Assist { id, label, group, target, source_change, command });
Some(())
}

Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/extract_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
block.indent(indent_to);
}
}
edit.rename();
},
)
}
Expand Down
20 changes: 12 additions & 8 deletions crates/ide-assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ pub fn test_some_range(a: int) -> bool {
group: None,
target: 59..60,
source_change: None,
trigger_signature_help: false,
command: None,
}
"#]]
.assert_debug_eq(&extract_into_variable_assist);
Expand All @@ -470,7 +470,7 @@ pub fn test_some_range(a: int) -> bool {
group: None,
target: 59..60,
source_change: None,
trigger_signature_help: false,
command: None,
}
"#]]
.assert_debug_eq(&extract_into_function_assist);
Expand Down Expand Up @@ -500,7 +500,7 @@ pub fn test_some_range(a: int) -> bool {
group: None,
target: 59..60,
source_change: None,
trigger_signature_help: false,
command: None,
}
"#]]
.assert_debug_eq(&extract_into_variable_assist);
Expand All @@ -516,7 +516,7 @@ pub fn test_some_range(a: int) -> bool {
group: None,
target: 59..60,
source_change: None,
trigger_signature_help: false,
command: None,
}
"#]]
.assert_debug_eq(&extract_into_function_assist);
Expand Down Expand Up @@ -587,7 +587,9 @@ pub fn test_some_range(a: int) -> bool {
is_snippet: true,
},
),
trigger_signature_help: false,
command: Some(
Rename,
),
}
"#]]
.assert_debug_eq(&extract_into_variable_assist);
Expand All @@ -603,7 +605,7 @@ pub fn test_some_range(a: int) -> bool {
group: None,
target: 59..60,
source_change: None,
trigger_signature_help: false,
command: None,
}
"#]]
.assert_debug_eq(&extract_into_function_assist);
Expand Down Expand Up @@ -666,7 +668,9 @@ pub fn test_some_range(a: int) -> bool {
is_snippet: true,
},
),
trigger_signature_help: false,
command: Some(
Rename,
),
}
"#]]
.assert_debug_eq(&extract_into_variable_assist);
Expand Down Expand Up @@ -715,7 +719,7 @@ pub fn test_some_range(a: int) -> bool {
is_snippet: true,
},
),
trigger_signature_help: false,
command: None,
}
"#]]
.assert_debug_eq(&extract_into_function_assist);
Expand Down
11 changes: 10 additions & 1 deletion crates/ide-db/src/assists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
15 changes: 11 additions & 4 deletions crates/ide-db/src/source_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn quickfix_for_redundant_assoc_item(
group: None,
target: range,
source_change: Some(source_change_builder.finish()),
trigger_signature_help: false,
command: None,
}])
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ide-diagnostics/src/handlers/typed_hole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::TypedHole) -> Option<Vec<Assist>
original_range.file_id,
TextEdit::replace(original_range.range, code),
)),
trigger_signature_help: false,
command: None,
})
.collect();

Expand Down
8 changes: 4 additions & 4 deletions crates/ide-diagnostics/src/handlers/unresolved_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn add_variant_to_union(
group: None,
target: error_range.range,
source_change: Some(src_change_builder.finish()),
trigger_signature_help: false,
command: None,
})
}

Expand Down Expand Up @@ -173,7 +173,7 @@ fn add_field_to_struct_fix(
group: None,
target: error_range.range,
source_change: Some(src_change_builder.finish()),
trigger_signature_help: false,
command: None,
})
}
None => {
Expand Down Expand Up @@ -204,7 +204,7 @@ fn add_field_to_struct_fix(
group: None,
target: error_range.range,
source_change: Some(src_change_builder.finish()),
trigger_signature_help: false,
command: None,
})
}
Some(FieldList::TupleFieldList(_tuple)) => {
Expand Down Expand Up @@ -266,7 +266,7 @@ fn method_fix(
file_id,
TextEdit::insert(range.end(), "()".to_owned()),
)),
trigger_signature_help: false,
command: None,
})
}
#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-diagnostics/src/handlers/unresolved_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn field_fix(
(file_id, TextEdit::insert(range.start(), "(".to_owned())),
(file_id, TextEdit::insert(range.end(), ")".to_owned())),
])),
trigger_signature_help: false,
command: None,
})
}

Expand Down Expand Up @@ -191,7 +191,7 @@ fn assoc_func_fix(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedMethodCall) -
file_id,
TextEdit::replace(range, assoc_func_call_expr_string),
)),
trigger_signature_help: false,
command: None,
})
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-diagnostics/src/handlers/unused_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn fixes(
diagnostic_range.file_id,
TextEdit::replace(name_range, format!("_{}", var_name.display(db))),
)),
trigger_signature_help: false,
command: None,
}])
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ide-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
group: None,
target,
source_change: None,
trigger_signature_help: false,
command: None,
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/ide/src/ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) fn ssr_assists(
group: Some(GroupLabel("Apply SSR".into())),
target: comment_range,
source_change,
trigger_signature_help: false,
command: None,
};

ssr_assists.push(assist);
Expand Down Expand Up @@ -143,7 +143,7 @@ mod tests {
is_snippet: false,
},
),
trigger_signature_help: false,
command: None,
}
"#]]
.assert_debug_eq(&apply_in_file_assist);
Expand Down Expand Up @@ -196,7 +196,7 @@ mod tests {
is_snippet: false,
},
),
trigger_signature_help: false,
command: None,
}
"#]]
.assert_debug_eq(&apply_in_workspace_assist);
Expand Down Expand Up @@ -236,7 +236,7 @@ mod tests {
),
target: 10..21,
source_change: None,
trigger_signature_help: false,
command: None,
}
"#]]
.assert_debug_eq(&apply_in_file_assist);
Expand All @@ -256,7 +256,7 @@ mod tests {
),
target: 10..21,
source_change: None,
trigger_signature_help: false,
command: None,
}
"#]]
.assert_debug_eq(&apply_in_workspace_assist);
Expand Down
4 changes: 3 additions & 1 deletion crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,13 +1128,14 @@ pub struct WorkspaceSymbolConfig {
/// How many items are returned at most.
pub search_limit: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ClientCommandsConfig {
pub run_single: bool,
pub debug_single: bool,
pub show_reference: bool,
pub goto_location: bool,
pub trigger_parameter_hints: bool,
pub rename: bool,
}

#[derive(Debug)]
Expand Down Expand Up @@ -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"),
}
}

Expand Down
21 changes: 17 additions & 4 deletions crates/rust-analyzer/src/lsp/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1336,9 +1336,14 @@ pub(crate) fn code_action(
command: None,
};

if assist.trigger_signature_help && snap.config.client_commands().trigger_parameter_hints {
res.command = Some(command::trigger_parameter_hints());
}
let commands = snap.config.client_commands();
res.command = match assist.command {
Some(assists::Command::TriggerSignatureHelp) if commands.trigger_parameter_hints => {
Some(command::trigger_parameter_hints())
}
Some(assists::Command::Rename) if commands.rename => Some(command::rename()),
_ => None,
};

match (assist.source_change, resolve_data) {
(Some(it), _) => res.edit = Some(snippet_workspace_edit(snap, it)?),
Expand Down Expand Up @@ -1715,6 +1720,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 {
Expand Down
1 change: 1 addition & 0 deletions editors/code/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ class ExperimentalFeatures implements lc.StaticFeature {
"rust-analyzer.showReferences",
"rust-analyzer.gotoLocation",
"editor.action.triggerParameterHints",
"editor.action.rename",
],
},
...capabilities.experimental,
Expand Down
6 changes: 6 additions & 0 deletions editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ export function triggerParameterHints(_: CtxInit): Cmd {
};
}

export function rename(_: CtxInit): Cmd {
return async () => {
await vscode.commands.executeCommand("editor.action.rename");
};
}

export function openLogs(ctx: CtxInit): Cmd {
return async () => {
if (ctx.client.outputChannel) {
Expand Down
1 change: 1 addition & 0 deletions editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function createCommands(): Record<string, CommandFactory> {
runSingle: { enabled: commands.runSingle },
showReferences: { enabled: commands.showReferences },
triggerParameterHints: { enabled: commands.triggerParameterHints },
rename: { enabled: commands.rename },
openLogs: { enabled: commands.openLogs },
revealDependency: { enabled: commands.revealDependency },
};
Expand Down

0 comments on commit e961b1a

Please sign in to comment.