Skip to content

Commit

Permalink
Auto merge of rust-lang#12032 - jonas-schievink:code-action-commands,…
Browse files Browse the repository at this point in the history
… r=jonas-schievink

feat: display signature help when applying "Add `::<>`" assist

Closes rust-lang/rust-analyzer#12031
  • Loading branch information
bors committed Apr 19, 2022
2 parents e3ec877 + c6ffffc commit 5582402
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 16 deletions.
5 changes: 5 additions & 0 deletions crates/ide/src/ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub(crate) fn ssr_assists(
group: Some(GroupLabel("Apply SSR".into())),
target: comment_range,
source_change,
trigger_signature_help: false,
};

ssr_assists.push(assist);
Expand Down Expand Up @@ -140,6 +141,7 @@ mod tests {
is_snippet: false,
},
),
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&apply_in_file_assist);
Expand Down Expand Up @@ -186,6 +188,7 @@ mod tests {
is_snippet: false,
},
),
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&apply_in_workspace_assist);
Expand Down Expand Up @@ -225,6 +228,7 @@ mod tests {
),
target: 10..21,
source_change: None,
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&apply_in_file_assist);
Expand All @@ -244,6 +248,7 @@ mod tests {
),
target: 10..21,
source_change: None,
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&apply_in_workspace_assist);
Expand Down
9 changes: 8 additions & 1 deletion crates/ide_assists/src/assist_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,19 @@ impl Assists {
return None;
}

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

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

Expand All @@ -219,6 +221,7 @@ pub(crate) struct AssistBuilder {
edit: TextEditBuilder,
file_id: FileId,
source_change: SourceChange,
trigger_signature_help: bool,

/// Maps the original, immutable `SyntaxNode` to a `clone_for_update` twin.
mutated_tree: Option<TreeMutator>,
Expand Down Expand Up @@ -252,6 +255,7 @@ impl AssistBuilder {
edit: TextEdit::builder(),
file_id,
source_change: SourceChange::default(),
trigger_signature_help: false,
mutated_tree: None,
}
}
Expand Down Expand Up @@ -332,6 +336,9 @@ impl AssistBuilder {
let file_system_edit = FileSystemEdit::MoveFile { src, dst };
self.source_change.push_file_system_edit(file_system_edit);
}
pub(crate) fn trigger_signature_help(&mut self) {
self.trigger_signature_help = true;
}

fn finish(mut self) -> SourceChange {
self.commit();
Expand Down
21 changes: 12 additions & 9 deletions crates/ide_assists/src/handlers/add_turbo_fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,18 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<(
AssistId("add_turbo_fish", AssistKind::RefactorRewrite),
"Add `::<>`",
ident.text_range(),
|builder| match ctx.config.snippet_cap {
Some(cap) => {
let snip = format!("::<{}>", get_snippet_fish_head(number_of_arguments));
builder.insert_snippet(cap, ident.text_range().end(), snip)
}
None => {
let fish_head = std::iter::repeat("_").take(number_of_arguments).format(", ");
let snip = format!("::<{}>", fish_head);
builder.insert(ident.text_range().end(), snip);
|builder| {
builder.trigger_signature_help();
match ctx.config.snippet_cap {
Some(cap) => {
let snip = format!("::<{}>", get_snippet_fish_head(number_of_arguments));
builder.insert_snippet(cap, ident.text_range().end(), snip)
}
None => {
let fish_head = std::iter::repeat("_").take(number_of_arguments).format(", ");
let snip = format!("::<{}>", fish_head);
builder.insert(ident.text_range().end(), snip);
}
}
},
)
Expand Down
8 changes: 8 additions & 0 deletions crates/ide_assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ pub fn test_some_range(a: int) -> bool {
group: None,
target: 59..60,
source_change: None,
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&extract_into_variable_assist);
Expand All @@ -356,6 +357,7 @@ pub fn test_some_range(a: int) -> bool {
group: None,
target: 59..60,
source_change: None,
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&extract_into_function_assist);
Expand Down Expand Up @@ -385,6 +387,7 @@ pub fn test_some_range(a: int) -> bool {
group: None,
target: 59..60,
source_change: None,
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&extract_into_variable_assist);
Expand All @@ -400,6 +403,7 @@ pub fn test_some_range(a: int) -> bool {
group: None,
target: 59..60,
source_change: None,
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&extract_into_function_assist);
Expand Down Expand Up @@ -450,6 +454,7 @@ pub fn test_some_range(a: int) -> bool {
is_snippet: true,
},
),
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&extract_into_variable_assist);
Expand All @@ -465,6 +470,7 @@ pub fn test_some_range(a: int) -> bool {
group: None,
target: 59..60,
source_change: None,
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&extract_into_function_assist);
Expand Down Expand Up @@ -507,6 +513,7 @@ pub fn test_some_range(a: int) -> bool {
is_snippet: true,
},
),
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&extract_into_variable_assist);
Expand Down Expand Up @@ -543,6 +550,7 @@ pub fn test_some_range(a: int) -> bool {
is_snippet: true,
},
),
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&extract_into_function_assist);
Expand Down
1 change: 1 addition & 0 deletions crates/ide_db/src/assists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
2 changes: 2 additions & 0 deletions crates/ide_diagnostics/src/handlers/unresolved_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ mod baz {}
is_snippet: false,
},
),
trigger_signature_help: false,
},
Assist {
id: AssistId(
Expand All @@ -143,6 +144,7 @@ mod baz {}
is_snippet: false,
},
),
trigger_signature_help: false,
},
],
),
Expand Down
1 change: 1 addition & 0 deletions crates/ide_diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,6 @@ fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
group: None,
target,
source_change: None,
trigger_signature_help: false,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@
"quickfix",
),
),
command: None,
edit: Some(
SnippetWorkspaceEdit {
changes: Some(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
"quickfix",
),
),
command: None,
edit: Some(
SnippetWorkspaceEdit {
changes: Some(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
"quickfix",
),
),
command: None,
edit: Some(
SnippetWorkspaceEdit {
changes: Some(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
"quickfix",
),
),
command: None,
edit: Some(
SnippetWorkspaceEdit {
changes: Some(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@
"quickfix",
),
),
command: None,
edit: Some(
SnippetWorkspaceEdit {
changes: Some(
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/src/diagnostics/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ fn map_rust_child_diagnostic(
}),
is_preferred: Some(true),
data: None,
command: None,
},
}),
})
Expand Down
5 changes: 3 additions & 2 deletions crates/rust-analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,8 +1160,9 @@ pub(crate) fn handle_code_action_resolve(
))
.into());
}
let edit = to_proto::code_action(&snap, assist.clone(), None)?.edit;
code_action.edit = edit;
let ca = to_proto::code_action(&snap, assist.clone(), None)?;
code_action.edit = ca.edit;
code_action.command = ca.command;
Ok(code_action)
}

Expand Down
5 changes: 2 additions & 3 deletions crates/rust-analyzer/src/lsp_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,8 @@ pub struct CodeAction {
pub group: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub kind: Option<CodeActionKind>,
// We don't handle commands on the client-side
// #[serde(skip_serializing_if = "Option::is_none")]
// pub command: Option<lsp_types::Command>,
#[serde(skip_serializing_if = "Option::is_none")]
pub command: Option<lsp_types::Command>,
#[serde(skip_serializing_if = "Option::is_none")]
pub edit: Option<SnippetWorkspaceEdit>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
6 changes: 6 additions & 0 deletions crates/rust-analyzer/src/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,13 @@ pub(crate) fn code_action(
edit: None,
is_preferred: None,
data: None,
command: None,
};

if assist.trigger_signature_help && snap.config.client_commands().trigger_parameter_hints {
res.command = Some(command::trigger_parameter_hints());
}

match (assist.source_change, resolve_data) {
(Some(it), _) => res.edit = Some(snippet_workspace_edit(snap, it)?),
(None, Some((index, code_action_params))) => {
Expand Down
2 changes: 1 addition & 1 deletion docs/dev/lsp-extensions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!---
lsp_ext.rs hash: 326ad62235135223
lsp_ext.rs hash: 7a34bc3f38e2a7d8
If you need to change the above hash to make the test pass, please check if you
need to adjust this doc as well and ping this issue:
Expand Down
3 changes: 3 additions & 0 deletions editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,9 @@ export function resolveCodeAction(ctx: Ctx): Cmd {
const fileSystemEdit = await client.protocol2CodeConverter.asWorkspaceEdit(lcFileSystemEdit);
await vscode.workspace.applyEdit(fileSystemEdit);
await applySnippetWorkspaceEdit(edit);
if (item.command != null) {
await vscode.commands.executeCommand(item.command.command, item.command.arguments);
}
};
}

Expand Down

0 comments on commit 5582402

Please sign in to comment.