|
|
@ -452,43 +452,83 @@ pub fn code_action(cx: &mut Context) {
|
|
|
|
cx.callback(
|
|
|
|
cx.callback(
|
|
|
|
future,
|
|
|
|
future,
|
|
|
|
move |editor, compositor, response: Option<lsp::CodeActionResponse>| {
|
|
|
|
move |editor, compositor, response: Option<lsp::CodeActionResponse>| {
|
|
|
|
let actions = match response {
|
|
|
|
let mut actions = match response {
|
|
|
|
Some(a) => a,
|
|
|
|
Some(a) => a,
|
|
|
|
None => return,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if actions.is_empty() {
|
|
|
|
if actions.is_empty() {
|
|
|
|
editor.set_status("No code actions available");
|
|
|
|
editor.set_status("No code actions available");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let mut picker = ui::Menu::new(actions, (), move |editor, code_action, event| {
|
|
|
|
// sort by CodeActionKind
|
|
|
|
if event != PromptEvent::Validate {
|
|
|
|
// this ensures that the most relevant codeactions (quickfix) show up first
|
|
|
|
return;
|
|
|
|
// while more situational commands (like refactors) show up later
|
|
|
|
}
|
|
|
|
// this behaviour is modeled after the behaviour of vscode (editor/contrib/codeAction/browser/codeActionWidget.ts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut categories = vec![Vec::new(); 8];
|
|
|
|
|
|
|
|
for action in actions.drain(..) {
|
|
|
|
|
|
|
|
let category = match &action {
|
|
|
|
|
|
|
|
lsp::CodeActionOrCommand::CodeAction(lsp::CodeAction {
|
|
|
|
|
|
|
|
kind: Some(kind),
|
|
|
|
|
|
|
|
..
|
|
|
|
|
|
|
|
}) => {
|
|
|
|
|
|
|
|
let mut components = kind.as_str().split('.');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match components.next() {
|
|
|
|
|
|
|
|
Some("quickfix") => 0,
|
|
|
|
|
|
|
|
Some("refactor") => match components.next() {
|
|
|
|
|
|
|
|
Some("extract") => 1,
|
|
|
|
|
|
|
|
Some("inline") => 2,
|
|
|
|
|
|
|
|
Some("rewrite") => 3,
|
|
|
|
|
|
|
|
Some("move") => 4,
|
|
|
|
|
|
|
|
Some("surround") => 5,
|
|
|
|
|
|
|
|
_ => 7,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Some("source") => 6,
|
|
|
|
|
|
|
|
_ => 7,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => 7,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
categories[category].push(action);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// always present here
|
|
|
|
for category in categories {
|
|
|
|
let code_action = code_action.unwrap();
|
|
|
|
actions.extend(category.into_iter())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
match code_action {
|
|
|
|
let mut picker =
|
|
|
|
lsp::CodeActionOrCommand::Command(command) => {
|
|
|
|
ui::Menu::new(actions, false, (), move |editor, code_action, event| {
|
|
|
|
log::debug!("code action command: {:?}", command);
|
|
|
|
if event != PromptEvent::Validate {
|
|
|
|
execute_lsp_command(editor, command.clone());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lsp::CodeActionOrCommand::CodeAction(code_action) => {
|
|
|
|
|
|
|
|
log::debug!("code action: {:?}", code_action);
|
|
|
|
|
|
|
|
if let Some(ref workspace_edit) = code_action.edit {
|
|
|
|
|
|
|
|
log::debug!("edit: {:?}", workspace_edit);
|
|
|
|
|
|
|
|
apply_workspace_edit(editor, offset_encoding, workspace_edit);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if code action provides both edit and command first the edit
|
|
|
|
// always present here
|
|
|
|
// should be applied and then the command
|
|
|
|
let code_action = code_action.unwrap();
|
|
|
|
if let Some(command) = &code_action.command {
|
|
|
|
|
|
|
|
|
|
|
|
match code_action {
|
|
|
|
|
|
|
|
lsp::CodeActionOrCommand::Command(command) => {
|
|
|
|
|
|
|
|
log::debug!("code action command: {:?}", command);
|
|
|
|
execute_lsp_command(editor, command.clone());
|
|
|
|
execute_lsp_command(editor, command.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lsp::CodeActionOrCommand::CodeAction(code_action) => {
|
|
|
|
|
|
|
|
log::debug!("code action: {:?}", code_action);
|
|
|
|
|
|
|
|
if let Some(ref workspace_edit) = code_action.edit {
|
|
|
|
|
|
|
|
log::debug!("edit: {:?}", workspace_edit);
|
|
|
|
|
|
|
|
apply_workspace_edit(editor, offset_encoding, workspace_edit);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if code action provides both edit and command first the edit
|
|
|
|
|
|
|
|
// should be applied and then the command
|
|
|
|
|
|
|
|
if let Some(command) = &code_action.command {
|
|
|
|
|
|
|
|
execute_lsp_command(editor, command.clone());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
picker.move_down(); // pre-select the first item
|
|
|
|
picker.move_down(); // pre-select the first item
|
|
|
|
|
|
|
|
|
|
|
|
let popup = Popup::new("code-action", picker);
|
|
|
|
let popup = Popup::new("code-action", picker);
|
|
|
|