2022-08-09 03:31:26 +02:00
|
|
|
use crate::compositor::{Component, Context, Event, EventResult};
|
2023-02-13 03:44:31 +01:00
|
|
|
use helix_view::{
|
2023-03-02 21:56:55 +01:00
|
|
|
document::SavePoint,
|
2023-02-13 03:44:31 +01:00
|
|
|
editor::CompleteAction,
|
|
|
|
theme::{Modifier, Style},
|
|
|
|
ViewId,
|
|
|
|
};
|
|
|
|
use tui::{buffer::Buffer as Surface, text::Span};
|
2021-03-27 04:06:40 +01:00
|
|
|
|
2023-03-02 21:56:55 +01:00
|
|
|
use std::{borrow::Cow, sync::Arc};
|
2021-03-27 04:06:40 +01:00
|
|
|
|
2022-03-01 02:45:29 +01:00
|
|
|
use helix_core::{Change, Transaction};
|
2023-02-16 02:15:25 +01:00
|
|
|
use helix_view::{graphics::Rect, Document, Editor};
|
2021-03-27 04:06:40 +01:00
|
|
|
|
2021-04-05 11:23:37 +02:00
|
|
|
use crate::commands;
|
2021-05-22 10:33:42 +02:00
|
|
|
use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};
|
2021-03-27 04:06:40 +01:00
|
|
|
|
2022-05-23 18:10:48 +02:00
|
|
|
use helix_lsp::{lsp, util, OffsetEncoding};
|
2021-03-27 04:06:40 +01:00
|
|
|
|
2021-05-22 10:33:42 +02:00
|
|
|
impl menu::Item for CompletionItem {
|
2022-07-02 13:21:27 +02:00
|
|
|
type Data = ();
|
|
|
|
fn sort_text(&self, data: &Self::Data) -> Cow<str> {
|
|
|
|
self.filter_text(data)
|
2021-08-10 04:19:07 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 13:21:27 +02:00
|
|
|
#[inline]
|
|
|
|
fn filter_text(&self, _data: &Self::Data) -> Cow<str> {
|
2023-04-05 20:25:28 +02:00
|
|
|
self.item
|
|
|
|
.filter_text
|
2022-07-02 13:21:27 +02:00
|
|
|
.as_ref()
|
2023-04-05 20:25:28 +02:00
|
|
|
.unwrap_or(&self.item.label)
|
2022-07-02 13:21:27 +02:00
|
|
|
.as_str()
|
|
|
|
.into()
|
2021-05-22 10:33:42 +02:00
|
|
|
}
|
|
|
|
|
2022-12-25 06:54:09 +01:00
|
|
|
fn format(&self, _data: &Self::Data) -> menu::Row {
|
2023-04-05 20:25:28 +02:00
|
|
|
let deprecated = self.item.deprecated.unwrap_or_default()
|
|
|
|
|| self.item.tags.as_ref().map_or(false, |tags| {
|
2023-02-13 03:44:31 +01:00
|
|
|
tags.contains(&lsp::CompletionItemTag::DEPRECATED)
|
|
|
|
});
|
2022-05-23 18:10:48 +02:00
|
|
|
|
2021-05-22 10:33:42 +02:00
|
|
|
menu::Row::new(vec![
|
2023-02-13 03:44:31 +01:00
|
|
|
menu::Cell::from(Span::styled(
|
2023-04-05 20:25:28 +02:00
|
|
|
self.item.label.as_str(),
|
2023-02-13 03:44:31 +01:00
|
|
|
if deprecated {
|
|
|
|
Style::default().add_modifier(Modifier::CROSSED_OUT)
|
|
|
|
} else {
|
|
|
|
Style::default()
|
|
|
|
},
|
|
|
|
)),
|
2023-04-05 20:25:28 +02:00
|
|
|
menu::Cell::from(match self.item.kind {
|
2021-10-29 05:00:18 +02:00
|
|
|
Some(lsp::CompletionItemKind::TEXT) => "text",
|
|
|
|
Some(lsp::CompletionItemKind::METHOD) => "method",
|
|
|
|
Some(lsp::CompletionItemKind::FUNCTION) => "function",
|
|
|
|
Some(lsp::CompletionItemKind::CONSTRUCTOR) => "constructor",
|
|
|
|
Some(lsp::CompletionItemKind::FIELD) => "field",
|
|
|
|
Some(lsp::CompletionItemKind::VARIABLE) => "variable",
|
|
|
|
Some(lsp::CompletionItemKind::CLASS) => "class",
|
|
|
|
Some(lsp::CompletionItemKind::INTERFACE) => "interface",
|
|
|
|
Some(lsp::CompletionItemKind::MODULE) => "module",
|
|
|
|
Some(lsp::CompletionItemKind::PROPERTY) => "property",
|
|
|
|
Some(lsp::CompletionItemKind::UNIT) => "unit",
|
|
|
|
Some(lsp::CompletionItemKind::VALUE) => "value",
|
|
|
|
Some(lsp::CompletionItemKind::ENUM) => "enum",
|
|
|
|
Some(lsp::CompletionItemKind::KEYWORD) => "keyword",
|
|
|
|
Some(lsp::CompletionItemKind::SNIPPET) => "snippet",
|
|
|
|
Some(lsp::CompletionItemKind::COLOR) => "color",
|
|
|
|
Some(lsp::CompletionItemKind::FILE) => "file",
|
|
|
|
Some(lsp::CompletionItemKind::REFERENCE) => "reference",
|
|
|
|
Some(lsp::CompletionItemKind::FOLDER) => "folder",
|
|
|
|
Some(lsp::CompletionItemKind::ENUM_MEMBER) => "enum_member",
|
|
|
|
Some(lsp::CompletionItemKind::CONSTANT) => "constant",
|
|
|
|
Some(lsp::CompletionItemKind::STRUCT) => "struct",
|
|
|
|
Some(lsp::CompletionItemKind::EVENT) => "event",
|
|
|
|
Some(lsp::CompletionItemKind::OPERATOR) => "operator",
|
|
|
|
Some(lsp::CompletionItemKind::TYPE_PARAMETER) => "type_param",
|
2022-11-08 19:36:01 +01:00
|
|
|
Some(kind) => {
|
|
|
|
log::error!("Received unknown completion item kind: {:?}", kind);
|
|
|
|
""
|
|
|
|
}
|
2021-05-22 10:33:42 +02:00
|
|
|
None => "",
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 20:25:28 +02:00
|
|
|
#[derive(Debug, PartialEq, Default, Clone)]
|
2022-05-23 18:10:48 +02:00
|
|
|
pub struct CompletionItem {
|
|
|
|
pub item: lsp::CompletionItem,
|
|
|
|
pub language_server_id: usize,
|
|
|
|
pub resolved: bool,
|
2023-04-05 20:25:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 04:06:40 +01:00
|
|
|
/// Wraps a Menu.
|
|
|
|
pub struct Completion {
|
2021-06-22 16:26:34 +02:00
|
|
|
popup: Popup<Menu<CompletionItem>>,
|
2021-08-27 03:44:12 +02:00
|
|
|
start_offset: usize,
|
2021-10-10 05:33:22 +02:00
|
|
|
#[allow(dead_code)]
|
2021-03-27 04:06:40 +01:00
|
|
|
trigger_offset: usize,
|
2021-03-31 10:17:01 +02:00
|
|
|
// TODO: maintain a completioncontext with trigger kind & trigger char
|
2021-03-27 04:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Completion {
|
2022-07-19 04:28:24 +02:00
|
|
|
pub const ID: &'static str = "completion";
|
|
|
|
|
2021-04-14 08:30:15 +02:00
|
|
|
pub fn new(
|
2021-08-27 03:44:12 +02:00
|
|
|
editor: &Editor,
|
2023-03-02 21:56:55 +01:00
|
|
|
savepoint: Arc<SavePoint>,
|
2022-05-23 18:10:48 +02:00
|
|
|
mut items: Vec<CompletionItem>,
|
2021-08-27 03:44:12 +02:00
|
|
|
start_offset: usize,
|
2021-04-14 08:30:15 +02:00
|
|
|
trigger_offset: usize,
|
|
|
|
) -> Self {
|
2023-03-09 23:21:02 +01:00
|
|
|
let replace_mode = editor.config().completion_replace;
|
2022-10-27 16:16:55 +02:00
|
|
|
// Sort completion items according to their preselect status (given by the LSP server)
|
2022-05-23 18:10:48 +02:00
|
|
|
items.sort_by_key(|item| !item.item.preselect.unwrap_or(false));
|
2022-10-27 16:16:55 +02:00
|
|
|
|
|
|
|
// Then create the menu
|
2022-10-13 20:10:10 +02:00
|
|
|
let menu = Menu::new(items, (), move |editor: &mut Editor, item, event| {
|
2021-08-10 07:12:57 +02:00
|
|
|
fn item_to_transaction(
|
|
|
|
doc: &Document,
|
2022-12-06 02:29:40 +01:00
|
|
|
view_id: ViewId,
|
2021-08-10 07:12:57 +02:00
|
|
|
item: &CompletionItem,
|
2023-03-20 00:02:41 +01:00
|
|
|
offset_encoding: OffsetEncoding,
|
2021-10-25 04:03:18 +02:00
|
|
|
trigger_offset: usize,
|
2023-02-11 14:20:49 +01:00
|
|
|
include_placeholder: bool,
|
2023-03-09 23:21:02 +01:00
|
|
|
replace_mode: bool,
|
2021-08-10 07:12:57 +02:00
|
|
|
) -> Transaction {
|
2022-10-22 16:52:25 +02:00
|
|
|
use helix_lsp::snippet;
|
2023-03-03 06:41:06 +01:00
|
|
|
let selection = doc.selection(view_id);
|
2023-01-10 20:21:44 +01:00
|
|
|
let text = doc.text().slice(..);
|
|
|
|
let primary_cursor = selection.primary().cursor(text);
|
2022-10-22 16:52:25 +02:00
|
|
|
|
2023-04-05 20:25:28 +02:00
|
|
|
let (edit_offset, new_text) = if let Some(edit) = &item.item.text_edit {
|
2021-08-10 07:12:57 +02:00
|
|
|
let edit = match edit {
|
|
|
|
lsp::CompletionTextEdit::Edit(edit) => edit.clone(),
|
|
|
|
lsp::CompletionTextEdit::InsertAndReplace(item) => {
|
2023-03-09 23:21:02 +01:00
|
|
|
let range = if replace_mode {
|
|
|
|
item.replace
|
|
|
|
} else {
|
|
|
|
item.insert
|
|
|
|
};
|
|
|
|
lsp::TextEdit::new(range, item.new_text.clone())
|
2021-08-10 07:12:57 +02:00
|
|
|
}
|
|
|
|
};
|
2023-03-03 06:41:06 +01:00
|
|
|
|
2023-04-03 03:58:50 +02:00
|
|
|
let Some(range) = util::lsp_range_to_range(doc.text(), edit.range, offset_encoding) else{
|
|
|
|
return Transaction::new(doc.text());
|
|
|
|
};
|
|
|
|
|
|
|
|
let start_offset = range.anchor as i128 - primary_cursor as i128;
|
|
|
|
let end_offset = range.head as i128 - primary_cursor as i128;
|
2023-03-03 06:41:06 +01:00
|
|
|
|
2023-03-09 23:08:55 +01:00
|
|
|
(Some((start_offset, end_offset)), edit.new_text)
|
2021-08-10 07:12:57 +02:00
|
|
|
} else {
|
2023-01-10 20:21:44 +01:00
|
|
|
let new_text = item
|
2023-04-05 20:25:28 +02:00
|
|
|
.item
|
2023-01-10 20:21:44 +01:00
|
|
|
.insert_text
|
|
|
|
.clone()
|
2023-04-05 20:25:28 +02:00
|
|
|
.unwrap_or_else(|| item.item.label.clone());
|
2023-01-10 20:21:44 +01:00
|
|
|
// check that we are still at the correct savepoint
|
|
|
|
// we can still generate a transaction regardless but if the
|
|
|
|
// document changed (and not just the selection) then we will
|
|
|
|
// likely delete the wrong text (same if we applied an edit sent by the LS)
|
|
|
|
debug_assert!(primary_cursor == trigger_offset);
|
2023-03-09 23:21:02 +01:00
|
|
|
(None, new_text)
|
2023-03-03 06:41:06 +01:00
|
|
|
};
|
|
|
|
|
2023-04-05 20:25:28 +02:00
|
|
|
if matches!(item.item.kind, Some(lsp::CompletionItemKind::SNIPPET))
|
2023-03-03 06:41:06 +01:00
|
|
|
|| matches!(
|
2023-04-05 20:25:28 +02:00
|
|
|
item.item.insert_text_format,
|
2023-03-03 06:41:06 +01:00
|
|
|
Some(lsp::InsertTextFormat::SNIPPET)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
match snippet::parse(&new_text) {
|
|
|
|
Ok(snippet) => util::generate_transaction_from_snippet(
|
|
|
|
doc.text(),
|
|
|
|
selection,
|
2023-03-09 23:21:02 +01:00
|
|
|
edit_offset,
|
|
|
|
replace_mode,
|
2023-03-03 06:41:06 +01:00
|
|
|
snippet,
|
|
|
|
doc.line_ending.as_str(),
|
|
|
|
include_placeholder,
|
2023-03-09 23:08:55 +01:00
|
|
|
doc.tab_width(),
|
2023-03-11 03:34:43 +01:00
|
|
|
doc.indent_width(),
|
2023-03-03 06:41:06 +01:00
|
|
|
),
|
|
|
|
Err(err) => {
|
|
|
|
log::error!(
|
|
|
|
"Failed to parse snippet: {:?}, remaining output: {}",
|
|
|
|
&new_text,
|
|
|
|
err
|
|
|
|
);
|
|
|
|
Transaction::new(doc.text())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
util::generate_transaction_from_completion_edit(
|
|
|
|
doc.text(),
|
|
|
|
selection,
|
2023-03-09 23:21:02 +01:00
|
|
|
edit_offset,
|
|
|
|
replace_mode,
|
2023-03-03 06:41:06 +01:00
|
|
|
new_text,
|
|
|
|
)
|
2022-10-22 16:52:25 +02:00
|
|
|
}
|
2022-03-01 02:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn completion_changes(transaction: &Transaction, trigger_offset: usize) -> Vec<Change> {
|
|
|
|
transaction
|
|
|
|
.changes_iter()
|
|
|
|
.filter(|(start, end, _)| (*start..=*end).contains(&trigger_offset))
|
|
|
|
.collect()
|
2021-08-10 07:12:57 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 04:03:18 +02:00
|
|
|
let (view, doc) = current!(editor);
|
|
|
|
|
2023-04-05 18:50:05 +02:00
|
|
|
macro_rules! language_server {
|
|
|
|
($item:expr) => {
|
|
|
|
match editor
|
|
|
|
.language_servers
|
|
|
|
.get_by_id($item.language_server_id)
|
|
|
|
{
|
|
|
|
Some(ls) => ls,
|
|
|
|
None => {
|
|
|
|
editor.set_error("language server disappeared between completion request and application");
|
|
|
|
// TODO close the completion menu somehow,
|
|
|
|
// currently there is no trivial way to access the EditorView to close the completion menu
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-22 10:33:42 +02:00
|
|
|
match event {
|
2023-04-04 23:34:47 +02:00
|
|
|
PromptEvent::Abort => {}
|
2021-08-10 07:12:57 +02:00
|
|
|
PromptEvent::Update => {
|
2023-04-07 14:50:47 +02:00
|
|
|
// Update creates "ghost" transactions which are not sent to the
|
|
|
|
// lsp server to avoid messing up re-requesting completions. Once a
|
|
|
|
// completion has been selected (with tab, c-n or c-p) it's always accepted whenever anything
|
2023-04-04 23:34:47 +02:00
|
|
|
// is typed. The only way to avoid that is to explicitly abort the completion
|
2023-04-07 14:50:47 +02:00
|
|
|
// with c-c. This will remove the "ghost" transaction.
|
2023-04-04 23:34:47 +02:00
|
|
|
//
|
2023-04-07 14:50:47 +02:00
|
|
|
// The ghost transaction is modeled with a transaction that is not sent to the LS.
|
|
|
|
// (apply_temporary) and a savepoint. It's extremely important this savepoint is restored
|
2023-04-04 23:34:47 +02:00
|
|
|
// (also without sending the transaction to the LS) *before any further transaction is applied*.
|
|
|
|
// Otherwise incremental sync breaks (since the state of the LS doesn't match the state the transaction
|
|
|
|
// is applied to).
|
|
|
|
if editor.last_completion.is_none() {
|
|
|
|
editor.last_completion = Some(CompleteAction::Selected {
|
|
|
|
savepoint: doc.savepoint(view),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// if more text was entered, remove it
|
|
|
|
doc.restore(view, &savepoint, false);
|
2021-05-22 10:33:42 +02:00
|
|
|
// always present here
|
|
|
|
let item = item.unwrap();
|
|
|
|
|
2023-03-20 00:02:41 +01:00
|
|
|
let transaction = item_to_transaction(
|
|
|
|
doc,
|
|
|
|
view.id,
|
|
|
|
item,
|
2023-04-05 18:50:05 +02:00
|
|
|
language_server!(item).offset_encoding(),
|
2023-03-20 00:02:41 +01:00
|
|
|
trigger_offset,
|
|
|
|
true,
|
|
|
|
replace_mode,
|
|
|
|
);
|
2023-04-04 23:34:47 +02:00
|
|
|
doc.apply_temporary(&transaction, view.id);
|
2021-08-10 07:12:57 +02:00
|
|
|
}
|
|
|
|
PromptEvent::Validate => {
|
2023-04-04 23:34:47 +02:00
|
|
|
if let Some(CompleteAction::Selected { savepoint }) =
|
|
|
|
editor.last_completion.take()
|
|
|
|
{
|
|
|
|
doc.restore(view, &savepoint, false);
|
|
|
|
}
|
2021-08-10 07:12:57 +02:00
|
|
|
// always present here
|
2023-04-05 20:25:28 +02:00
|
|
|
let mut item = item.unwrap().clone();
|
|
|
|
|
2023-04-05 18:50:05 +02:00
|
|
|
let language_server = language_server!(item);
|
|
|
|
let offset_encoding = language_server.offset_encoding();
|
2023-03-20 00:02:41 +01:00
|
|
|
|
2022-05-23 18:10:48 +02:00
|
|
|
let language_server = editor
|
|
|
|
.language_servers
|
|
|
|
.get_by_id(item.language_server_id)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-04-05 20:25:28 +02:00
|
|
|
// resolve item if not yet resolved
|
|
|
|
if !item.resolved {
|
|
|
|
if let Some(resolved) =
|
2022-05-23 18:10:48 +02:00
|
|
|
Self::resolve_completion_item(language_server, item.item.clone())
|
2023-04-05 20:25:28 +02:00
|
|
|
{
|
|
|
|
item.item = resolved;
|
|
|
|
}
|
2023-04-04 23:35:31 +02:00
|
|
|
};
|
2023-04-04 23:34:47 +02:00
|
|
|
// if more text was entered, remove it
|
|
|
|
doc.restore(view, &savepoint, true);
|
2021-10-25 04:03:18 +02:00
|
|
|
let transaction = item_to_transaction(
|
|
|
|
doc,
|
2022-12-06 02:29:40 +01:00
|
|
|
view.id,
|
2023-04-05 20:25:28 +02:00
|
|
|
&item,
|
2023-03-20 00:02:41 +01:00
|
|
|
offset_encoding,
|
2021-10-25 04:03:18 +02:00
|
|
|
trigger_offset,
|
2023-02-11 14:20:49 +01:00
|
|
|
false,
|
2023-03-09 23:21:02 +01:00
|
|
|
replace_mode,
|
2021-10-25 04:03:18 +02:00
|
|
|
);
|
2023-01-21 19:13:43 +01:00
|
|
|
doc.apply(&transaction, view.id);
|
2021-05-22 10:33:42 +02:00
|
|
|
|
2023-04-04 23:34:47 +02:00
|
|
|
editor.last_completion = Some(CompleteAction::Applied {
|
2022-03-01 02:45:29 +01:00
|
|
|
trigger_offset,
|
|
|
|
changes: completion_changes(&transaction, trigger_offset),
|
|
|
|
});
|
|
|
|
|
2023-04-07 14:50:47 +02:00
|
|
|
// TODO: add additional _edits to completion_changes?
|
2023-04-05 20:25:28 +02:00
|
|
|
if let Some(additional_edits) = item.item.additional_text_edits {
|
2021-05-22 10:33:42 +02:00
|
|
|
if !additional_edits.is_empty() {
|
|
|
|
let transaction = util::generate_transaction_from_edits(
|
|
|
|
doc.text(),
|
2023-04-05 20:25:28 +02:00
|
|
|
additional_edits,
|
2023-03-20 00:02:41 +01:00
|
|
|
offset_encoding, // TODO: should probably transcode in Client
|
2021-05-22 10:33:42 +02:00
|
|
|
);
|
2023-01-21 19:13:43 +01:00
|
|
|
doc.apply(&transaction, view.id);
|
2021-05-17 09:35:34 +02:00
|
|
|
}
|
2021-03-27 04:06:40 +01:00
|
|
|
}
|
2021-05-22 10:33:42 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2023-02-16 02:15:25 +01:00
|
|
|
let popup = Popup::new(Self::ID, menu)
|
|
|
|
.with_scrollbar(false)
|
|
|
|
.ignore_escape_key(true);
|
2021-08-27 03:44:12 +02:00
|
|
|
let mut completion = Self {
|
2021-03-27 04:06:40 +01:00
|
|
|
popup,
|
2021-08-27 03:44:12 +02:00
|
|
|
start_offset,
|
2021-03-27 04:06:40 +01:00
|
|
|
trigger_offset,
|
2021-08-27 03:44:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// need to recompute immediately in case start_offset != trigger_offset
|
|
|
|
completion.recompute_filter(editor);
|
|
|
|
|
|
|
|
completion
|
2021-03-27 04:06:40 +01:00
|
|
|
}
|
2021-04-05 11:23:37 +02:00
|
|
|
|
2021-12-25 11:00:57 +01:00
|
|
|
fn resolve_completion_item(
|
2022-05-23 18:10:48 +02:00
|
|
|
language_server: &helix_lsp::Client,
|
2021-12-25 11:00:57 +01:00
|
|
|
completion_item: lsp::CompletionItem,
|
2023-04-05 20:25:28 +02:00
|
|
|
) -> Option<lsp::CompletionItem> {
|
2022-11-22 03:52:23 +01:00
|
|
|
let future = language_server.resolve_completion_item(completion_item)?;
|
2021-12-25 11:00:57 +01:00
|
|
|
let response = helix_lsp::block_on(future);
|
|
|
|
match response {
|
2022-11-18 19:27:46 +01:00
|
|
|
Ok(value) => serde_json::from_value(value).ok(),
|
2021-12-25 11:00:57 +01:00
|
|
|
Err(err) => {
|
2022-11-22 03:52:23 +01:00
|
|
|
log::error!("Failed to resolve completion item: {}", err);
|
2021-12-25 11:00:57 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 03:44:12 +02:00
|
|
|
pub fn recompute_filter(&mut self, editor: &Editor) {
|
2021-04-05 11:23:37 +02:00
|
|
|
// recompute menu based on matches
|
|
|
|
let menu = self.popup.contents_mut();
|
2021-08-27 03:44:12 +02:00
|
|
|
let (view, doc) = current_ref!(editor);
|
2021-04-05 11:23:37 +02:00
|
|
|
|
|
|
|
// cx.hooks()
|
|
|
|
// cx.add_hook(enum type, ||)
|
|
|
|
// cx.trigger_hook(enum type, &str, ...) <-- there has to be enough to identify doc/view
|
|
|
|
// callback with editor & compositor
|
|
|
|
//
|
|
|
|
// trigger_hook sends event into channel, that's consumed in the global loop and
|
|
|
|
// triggers all registered callbacks
|
|
|
|
// TODO: hooks should get processed immediately so maybe do it after select!(), before
|
|
|
|
// looping?
|
|
|
|
|
2021-07-26 17:40:30 +02:00
|
|
|
let cursor = doc
|
|
|
|
.selection(view.id)
|
|
|
|
.primary()
|
|
|
|
.cursor(doc.text().slice(..));
|
2021-10-26 02:42:37 +02:00
|
|
|
if self.trigger_offset <= cursor {
|
2021-08-27 03:44:12 +02:00
|
|
|
let fragment = doc.text().slice(self.start_offset..cursor);
|
2021-04-05 11:23:37 +02:00
|
|
|
let text = Cow::from(fragment);
|
|
|
|
// TODO: logic is same as ui/picker
|
|
|
|
menu.score(&text);
|
2021-10-16 11:43:07 +02:00
|
|
|
} else {
|
|
|
|
// we backspaced before the start offset, clear the menu
|
|
|
|
// this will cause the editor to remove the completion popup
|
|
|
|
menu.clear();
|
2021-04-05 11:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 03:44:12 +02:00
|
|
|
pub fn update(&mut self, cx: &mut commands::Context) {
|
|
|
|
self.recompute_filter(cx.editor)
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:23:37 +02:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.popup.contents().is_empty()
|
|
|
|
}
|
2022-10-22 03:04:50 +02:00
|
|
|
|
2023-04-05 20:25:28 +02:00
|
|
|
fn replace_item(&mut self, old_item: CompletionItem, new_item: CompletionItem) {
|
2022-11-18 19:27:46 +01:00
|
|
|
self.popup.contents_mut().replace_option(old_item, new_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Asynchronously requests that the currently selection completion item is
|
|
|
|
/// resolved through LSP `completionItem/resolve`.
|
2022-10-22 03:04:50 +02:00
|
|
|
pub fn ensure_item_resolved(&mut self, cx: &mut commands::Context) -> bool {
|
|
|
|
// > If computing full completion items is expensive, servers can additionally provide a
|
|
|
|
// > handler for the completion item resolve request. ...
|
|
|
|
// > A typical use case is for example: the `textDocument/completion` request doesn't fill
|
|
|
|
// > in the `documentation` property for returned completion items since it is expensive
|
|
|
|
// > to compute. When the item is selected in the user interface then a
|
|
|
|
// > 'completionItem/resolve' request is sent with the selected completion item as a parameter.
|
|
|
|
// > The returned completion item should have the documentation property filled in.
|
|
|
|
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_completion
|
2022-11-18 19:27:46 +01:00
|
|
|
let current_item = match self.popup.contents().selection() {
|
2023-04-05 20:25:28 +02:00
|
|
|
Some(item) if !item.resolved => item.clone(),
|
2022-11-18 19:27:46 +01:00
|
|
|
_ => return false,
|
|
|
|
};
|
2023-04-05 18:50:05 +02:00
|
|
|
|
|
|
|
let Some(language_server) = cx.editor.language_server_by_id(current_item.language_server_id) else { return false; };
|
2022-11-18 19:27:46 +01:00
|
|
|
|
|
|
|
// This method should not block the compositor so we handle the response asynchronously.
|
2023-04-05 18:50:05 +02:00
|
|
|
let Some(future) = language_server.resolve_completion_item(current_item.item.clone()) else { return false; };
|
2022-11-18 19:27:46 +01:00
|
|
|
|
|
|
|
cx.callback(
|
|
|
|
future,
|
|
|
|
move |_editor, compositor, response: Option<lsp::CompletionItem>| {
|
|
|
|
let resolved_item = match response {
|
|
|
|
Some(item) => item,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(completion) = &mut compositor
|
|
|
|
.find::<crate::ui::EditorView>()
|
|
|
|
.unwrap()
|
|
|
|
.completion
|
|
|
|
{
|
2022-05-23 18:10:48 +02:00
|
|
|
let resolved_item = CompletionItem {
|
|
|
|
item: resolved_item,
|
|
|
|
language_server_id: current_item.language_server_id,
|
|
|
|
resolved: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
completion.replace_item(current_item, resolved_item);
|
2022-10-22 03:04:50 +02:00
|
|
|
}
|
2022-11-18 19:27:46 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
true
|
2022-10-22 03:04:50 +02:00
|
|
|
}
|
2023-03-30 18:22:51 +02:00
|
|
|
|
|
|
|
pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect {
|
|
|
|
self.popup.area(viewport, editor)
|
|
|
|
}
|
2021-03-27 04:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Completion {
|
2022-08-29 02:48:49 +02:00
|
|
|
fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
|
2021-03-27 04:06:40 +01:00
|
|
|
self.popup.handle_event(event, cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
|
|
|
self.popup.required_size(viewport)
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
2021-05-03 11:22:29 +02:00
|
|
|
self.popup.render(area, surface, cx);
|
|
|
|
|
2021-06-22 16:26:34 +02:00
|
|
|
// if we have a selection, render a markdown popup on top/below with info
|
2023-02-05 22:27:40 +01:00
|
|
|
let option = match self.popup.contents().selection() {
|
|
|
|
Some(option) => option,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
// need to render:
|
|
|
|
// option.detail
|
|
|
|
// ---
|
|
|
|
// option.documentation
|
|
|
|
|
|
|
|
let (view, doc) = current!(cx.editor);
|
|
|
|
let language = doc.language_name().unwrap_or("");
|
|
|
|
let text = doc.text().slice(..);
|
|
|
|
let cursor_pos = doc.selection(view.id).primary().cursor(text);
|
|
|
|
let coords = view
|
|
|
|
.screen_coords_at_pos(doc, text, cursor_pos)
|
|
|
|
.expect("cursor must be in view");
|
|
|
|
let cursor_pos = coords.row as u16;
|
|
|
|
|
2023-02-05 23:18:50 +01:00
|
|
|
let markdowned = |lang: &str, detail: Option<&str>, doc: Option<&str>| {
|
|
|
|
let md = match (detail, doc) {
|
|
|
|
(Some(detail), Some(doc)) => format!("```{lang}\n{detail}\n```\n{doc}"),
|
|
|
|
(Some(detail), None) => format!("```{lang}\n{detail}\n```"),
|
|
|
|
(None, Some(doc)) => doc.to_string(),
|
|
|
|
(None, None) => String::new(),
|
|
|
|
};
|
|
|
|
Markdown::new(md, cx.editor.syn_loader.clone())
|
|
|
|
};
|
|
|
|
|
2023-04-05 20:25:28 +02:00
|
|
|
let mut markdown_doc = match &option.item.documentation {
|
2023-02-05 22:27:40 +01:00
|
|
|
Some(lsp::Documentation::String(contents))
|
|
|
|
| Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
|
|
|
|
kind: lsp::MarkupKind::PlainText,
|
|
|
|
value: contents,
|
|
|
|
})) => {
|
|
|
|
// TODO: convert to wrapped text
|
2023-04-05 20:25:28 +02:00
|
|
|
markdowned(language, option.item.detail.as_deref(), Some(contents))
|
2023-02-05 22:27:40 +01:00
|
|
|
}
|
|
|
|
Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
|
|
|
|
kind: lsp::MarkupKind::Markdown,
|
|
|
|
value: contents,
|
|
|
|
})) => {
|
|
|
|
// TODO: set language based on doc scope
|
2023-04-05 20:25:28 +02:00
|
|
|
markdowned(language, option.item.detail.as_deref(), Some(contents))
|
2023-02-05 22:27:40 +01:00
|
|
|
}
|
2023-04-05 20:25:28 +02:00
|
|
|
None if option.item.detail.is_some() => {
|
2023-02-05 22:27:40 +01:00
|
|
|
// TODO: set language based on doc scope
|
2023-04-05 20:25:28 +02:00
|
|
|
markdowned(language, option.item.detail.as_deref(), None)
|
2023-02-05 22:27:40 +01:00
|
|
|
}
|
|
|
|
None => return,
|
|
|
|
};
|
2021-05-29 17:26:27 +02:00
|
|
|
|
2023-02-06 00:23:17 +01:00
|
|
|
let popup_area = {
|
2023-03-30 18:22:51 +02:00
|
|
|
let (popup_x, popup_y) = self.popup.get_rel_position(area, cx.editor);
|
2023-02-06 00:23:17 +01:00
|
|
|
let (popup_width, popup_height) = self.popup.get_size();
|
|
|
|
Rect::new(popup_x, popup_y, popup_width, popup_height)
|
|
|
|
};
|
|
|
|
|
|
|
|
let doc_width_available = area.width.saturating_sub(popup_area.right());
|
2023-02-05 22:30:52 +01:00
|
|
|
let doc_area = if doc_width_available > 30 {
|
|
|
|
let mut doc_width = doc_width_available;
|
2023-02-06 00:23:17 +01:00
|
|
|
let mut doc_height = area.height.saturating_sub(popup_area.top());
|
|
|
|
let x = popup_area.right();
|
|
|
|
let y = popup_area.top();
|
2023-02-05 22:27:40 +01:00
|
|
|
|
2023-02-05 22:30:52 +01:00
|
|
|
if let Some((rel_width, rel_height)) =
|
|
|
|
markdown_doc.required_size((doc_width, doc_height))
|
|
|
|
{
|
|
|
|
doc_width = rel_width.min(doc_width);
|
|
|
|
doc_height = rel_height.min(doc_height);
|
2023-02-05 22:27:40 +01:00
|
|
|
}
|
2023-02-05 22:30:52 +01:00
|
|
|
Rect::new(x, y, doc_width, doc_height)
|
2023-02-05 22:27:40 +01:00
|
|
|
} else {
|
2023-02-06 00:24:03 +01:00
|
|
|
// Documentation should not cover the cursor or the completion popup
|
|
|
|
// Completion popup could be above or below the current line
|
|
|
|
let avail_height_above = cursor_pos.min(popup_area.top()).saturating_sub(1);
|
|
|
|
let avail_height_below = area
|
|
|
|
.height
|
|
|
|
.saturating_sub(cursor_pos.max(popup_area.bottom()) + 1 /* padding */);
|
|
|
|
let (y, avail_height) = if avail_height_below >= avail_height_above {
|
|
|
|
(
|
|
|
|
area.height.saturating_sub(avail_height_below),
|
|
|
|
avail_height_below,
|
|
|
|
)
|
2021-06-20 06:13:44 +02:00
|
|
|
} else {
|
2023-02-06 00:24:03 +01:00
|
|
|
(0, avail_height_above)
|
2021-09-08 09:33:59 +02:00
|
|
|
};
|
2023-02-06 00:24:03 +01:00
|
|
|
if avail_height <= 1 {
|
|
|
|
return;
|
|
|
|
}
|
2021-05-29 17:26:27 +02:00
|
|
|
|
2023-02-06 00:24:03 +01:00
|
|
|
Rect::new(0, y, area.width, avail_height.min(15))
|
2023-02-05 22:27:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// clear area
|
|
|
|
let background = cx.editor.theme.get("ui.popup");
|
2023-02-05 22:30:52 +01:00
|
|
|
surface.clear_with(doc_area, background);
|
|
|
|
markdown_doc.render(doc_area, surface, cx);
|
2021-03-27 04:06:40 +01:00
|
|
|
}
|
|
|
|
}
|