Allows triggering commands after an assist edit

This commit is contained in:
Jonas Schievink 2022-04-19 18:37:18 +02:00
parent e3ec87730a
commit c6ffffccbd
18 changed files with 58 additions and 16 deletions

View file

@ -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);
@ -140,6 +141,7 @@ mod tests {
is_snippet: false,
},
),
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&apply_in_file_assist);
@ -186,6 +188,7 @@ mod tests {
is_snippet: false,
},
),
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&apply_in_workspace_assist);
@ -225,6 +228,7 @@ mod tests {
),
target: 10..21,
source_change: None,
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&apply_in_file_assist);
@ -244,6 +248,7 @@ mod tests {
),
target: 10..21,
source_change: None,
trigger_signature_help: false,
}
"#]]
.assert_debug_eq(&apply_in_workspace_assist);

View file

@ -193,9 +193,11 @@ 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
@ -203,7 +205,7 @@ impl Assists {
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(())
}
@ -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>,
@ -252,6 +255,7 @@ impl AssistBuilder {
edit: TextEdit::builder(),
file_id,
source_change: SourceChange::default(),
trigger_signature_help: false,
mutated_tree: None,
}
}
@ -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();

View file

@ -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);
}
}
},
)

View file

@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);

View file

@ -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)]

View file

@ -117,6 +117,7 @@ mod baz {}
is_snippet: false,
},
),
trigger_signature_help: false,
},
Assist {
id: AssistId(
@ -143,6 +144,7 @@ mod baz {}
is_snippet: false,
},
),
trigger_signature_help: false,
},
],
),

View file

@ -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,
}
}

View file

@ -318,6 +318,7 @@
"quickfix",
),
),
command: None,
edit: Some(
SnippetWorkspaceEdit {
changes: Some(

View file

@ -165,6 +165,7 @@
"quickfix",
),
),
command: None,
edit: Some(
SnippetWorkspaceEdit {
changes: Some(

View file

@ -165,6 +165,7 @@
"quickfix",
),
),
command: None,
edit: Some(
SnippetWorkspaceEdit {
changes: Some(

View file

@ -165,6 +165,7 @@
"quickfix",
),
),
command: None,
edit: Some(
SnippetWorkspaceEdit {
changes: Some(

View file

@ -328,6 +328,7 @@
"quickfix",
),
),
command: None,
edit: Some(
SnippetWorkspaceEdit {
changes: Some(

View file

@ -204,6 +204,7 @@ fn map_rust_child_diagnostic(
}),
is_preferred: Some(true),
data: None,
command: None,
},
}),
})

View file

@ -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)
}

View file

@ -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")]

View file

@ -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))) => {

View file

@ -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:

View file

@ -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);
}
};
}