|
|
@ -551,6 +551,11 @@ struct CodeActionOrCommandItem {
|
|
|
|
language_server_id: LanguageServerId,
|
|
|
|
language_server_id: LanguageServerId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct CodeActionItem {
|
|
|
|
|
|
|
|
lsp_item: lsp::CodeAction,
|
|
|
|
|
|
|
|
language_server_id: LanguageServerId,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ui::menu::Item for CodeActionOrCommandItem {
|
|
|
|
impl ui::menu::Item for CodeActionOrCommandItem {
|
|
|
|
type Data = ();
|
|
|
|
type Data = ();
|
|
|
|
fn format(&self, _data: &Self::Data) -> Row {
|
|
|
|
fn format(&self, _data: &Self::Data) -> Row {
|
|
|
@ -712,8 +717,36 @@ pub fn code_action(cx: &mut Context) {
|
|
|
|
|
|
|
|
|
|
|
|
// always present here
|
|
|
|
// always present here
|
|
|
|
let action = action.unwrap();
|
|
|
|
let action = action.unwrap();
|
|
|
|
|
|
|
|
let Some(language_server) = editor.language_server_by_id(action.language_server_id)
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
editor.set_error("Language Server disappeared");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let offset_encoding = language_server.offset_encoding();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match &action.lsp_item {
|
|
|
|
|
|
|
|
lsp::CodeActionOrCommand::Command(command) => {
|
|
|
|
|
|
|
|
log::debug!("code action command: {:?}", command);
|
|
|
|
|
|
|
|
execute_lsp_command(editor, action.language_server_id, command.clone());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
lsp::CodeActionOrCommand::CodeAction(code_action) => {
|
|
|
|
|
|
|
|
log::debug!("code action: {:?}", code_action);
|
|
|
|
|
|
|
|
let resolved_code_action =
|
|
|
|
|
|
|
|
resolve_code_action(code_action, language_server);
|
|
|
|
|
|
|
|
|
|
|
|
apply_code_action(editor, action);
|
|
|
|
if let Some(ref workspace_edit) =
|
|
|
|
|
|
|
|
resolved_code_action.as_ref().unwrap_or(code_action).edit
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let _ = editor.apply_workspace_edit(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, action.language_server_id, command.clone());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
picker.move_down(); // pre-select the first item
|
|
|
|
picker.move_down(); // pre-select the first item
|
|
|
|
|
|
|
|
|
|
|
@ -780,7 +813,7 @@ pub fn code_actions_on_save(cx: &mut compositor::Context, doc_id: &DocumentId) {
|
|
|
|
log::debug!("Attempting code action on save {:?}", code_action_kind);
|
|
|
|
log::debug!("Attempting code action on save {:?}", code_action_kind);
|
|
|
|
let doc = doc!(cx.editor, doc_id);
|
|
|
|
let doc = doc!(cx.editor, doc_id);
|
|
|
|
let full_range = Range::new(0, doc.text().len_chars());
|
|
|
|
let full_range = Range::new(0, doc.text().len_chars());
|
|
|
|
let code_actions: Vec<CodeActionOrCommandItem> =
|
|
|
|
let code_actions: Vec<CodeActionItem> =
|
|
|
|
code_actions_for_range(doc, full_range, Some(vec![code_action_kind.clone()]))
|
|
|
|
code_actions_for_range(doc, full_range, Some(vec![code_action_kind.clone()]))
|
|
|
|
.into_iter()
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(future, language_server_id)| {
|
|
|
|
.filter_map(|(future, language_server_id)| {
|
|
|
@ -808,10 +841,15 @@ pub fn code_actions_on_save(cx: &mut compositor::Context, doc_id: &DocumentId) {
|
|
|
|
|
|
|
|
|
|
|
|
// Use the first matching code action
|
|
|
|
// Use the first matching code action
|
|
|
|
if let Some(lsp_item) = actions.first() {
|
|
|
|
if let Some(lsp_item) = actions.first() {
|
|
|
|
return Some(CodeActionOrCommandItem {
|
|
|
|
return match lsp_item {
|
|
|
|
lsp_item: lsp_item.clone(),
|
|
|
|
CodeActionOrCommand::CodeAction(code_action) => {
|
|
|
|
|
|
|
|
Some(CodeActionItem {
|
|
|
|
|
|
|
|
lsp_item: code_action.clone(),
|
|
|
|
language_server_id,
|
|
|
|
language_server_id,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => None,
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -831,31 +869,40 @@ pub fn code_actions_on_save(cx: &mut compositor::Context, doc_id: &DocumentId) {
|
|
|
|
code_action.lsp_item,
|
|
|
|
code_action.lsp_item,
|
|
|
|
code_action.language_server_id
|
|
|
|
code_action.language_server_id
|
|
|
|
);
|
|
|
|
);
|
|
|
|
apply_code_action(cx.editor, &code_action);
|
|
|
|
let Some(language_server) = cx
|
|
|
|
|
|
|
|
.editor
|
|
|
|
|
|
|
|
.language_server_by_id(code_action.language_server_id)
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
log::error!(
|
|
|
|
|
|
|
|
"Language server disappeared {:?}",
|
|
|
|
|
|
|
|
code_action.language_server_id
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let offset_encoding = language_server.offset_encoding();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let resolved_code_action =
|
|
|
|
|
|
|
|
resolve_code_action(&code_action.lsp_item, language_server);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Find a better way to handle this
|
|
|
|
if let Some(ref workspace_edit) = resolved_code_action
|
|
|
|
// Sleep to avoid race condition between next code action/auto-format
|
|
|
|
.as_ref()
|
|
|
|
// and the textDocument/didChange notification
|
|
|
|
.unwrap_or(&code_action.lsp_item)
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
|
|
.edit
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let _ = cx
|
|
|
|
|
|
|
|
.editor
|
|
|
|
|
|
|
|
.apply_workspace_edit_sync(offset_encoding, workspace_edit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn apply_code_action(editor: &mut Editor, action: &CodeActionOrCommandItem) {
|
|
|
|
|
|
|
|
let Some(language_server) = editor.language_server_by_id(action.language_server_id) else {
|
|
|
|
|
|
|
|
editor.set_error("Language Server disappeared");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let offset_encoding = language_server.offset_encoding();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match &action.lsp_item {
|
|
|
|
|
|
|
|
lsp::CodeActionOrCommand::Command(command) => {
|
|
|
|
|
|
|
|
log::debug!("code action command: {:?}", command);
|
|
|
|
|
|
|
|
execute_lsp_command(editor, action.language_server_id, command.clone());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lsp::CodeActionOrCommand::CodeAction(code_action) => {
|
|
|
|
|
|
|
|
log::debug!("code action: {:?}", code_action);
|
|
|
|
pub fn resolve_code_action(
|
|
|
|
|
|
|
|
code_action: &CodeAction,
|
|
|
|
|
|
|
|
language_server: &Client,
|
|
|
|
|
|
|
|
) -> Option<CodeAction> {
|
|
|
|
// we support lsp "codeAction/resolve" for `edit` and `command` fields
|
|
|
|
// we support lsp "codeAction/resolve" for `edit` and `command` fields
|
|
|
|
let mut resolved_code_action = None;
|
|
|
|
let mut resolved_code_action = None;
|
|
|
|
if code_action.edit.is_none() || code_action.command.is_none() {
|
|
|
|
if code_action.edit.is_none() || code_action.command.is_none() {
|
|
|
@ -867,19 +914,7 @@ fn apply_code_action(editor: &mut Editor, action: &CodeActionOrCommandItem) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let resolved_code_action = resolved_code_action.as_ref().unwrap_or(code_action);
|
|
|
|
resolved_code_action
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(ref workspace_edit) = resolved_code_action.edit {
|
|
|
|
|
|
|
|
let _ = editor.apply_workspace_edit(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, action.language_server_id, command.clone());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn execute_lsp_command(
|
|
|
|
pub fn execute_lsp_command(
|
|
|
|