2021-07-01 19:41:20 +02:00
|
|
|
use crate::compositor::{Component, Context, EventResult};
|
|
|
|
use crossterm::event::{Event, KeyCode, KeyEvent};
|
2021-06-25 05:58:15 +02:00
|
|
|
use tui::buffer::Buffer as Surface;
|
2021-03-27 04:06:40 +01:00
|
|
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2021-07-01 19:41:20 +02:00
|
|
|
use helix_core::Transaction;
|
|
|
|
use helix_view::{graphics::Rect, 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
|
|
|
|
2021-07-01 19:41:20 +02:00
|
|
|
use helix_lsp::{lsp, util};
|
2021-03-27 04:06:40 +01:00
|
|
|
use lsp::CompletionItem;
|
|
|
|
|
2021-05-22 10:33:42 +02:00
|
|
|
impl menu::Item for CompletionItem {
|
|
|
|
fn filter_text(&self) -> &str {
|
2021-05-27 17:00:51 +02:00
|
|
|
self.filter_text.as_ref().unwrap_or(&self.label).as_str()
|
2021-05-22 10:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn label(&self) -> &str {
|
|
|
|
self.label.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn row(&self) -> menu::Row {
|
|
|
|
menu::Row::new(vec![
|
|
|
|
menu::Cell::from(self.label.as_str()),
|
|
|
|
menu::Cell::from(match self.kind {
|
|
|
|
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::EnumMember) => "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::TypeParameter) => "type_param",
|
|
|
|
None => "",
|
|
|
|
}),
|
|
|
|
// self.detail.as_deref().unwrap_or("")
|
|
|
|
// self.label_details
|
|
|
|
// .as_ref()
|
|
|
|
// .or(self.detail())
|
|
|
|
// .as_str(),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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 {
|
2021-04-14 08:30:15 +02:00
|
|
|
pub fn new(
|
|
|
|
items: Vec<CompletionItem>,
|
|
|
|
offset_encoding: helix_lsp::OffsetEncoding,
|
|
|
|
trigger_offset: usize,
|
|
|
|
) -> Self {
|
2021-03-27 04:06:40 +01:00
|
|
|
// let items: Vec<CompletionItem> = Vec::new();
|
2021-07-01 20:57:12 +02:00
|
|
|
let menu = Menu::new(items, move |editor: &mut Editor, item, event| {
|
2021-05-22 10:33:42 +02:00
|
|
|
match event {
|
2021-06-22 16:26:34 +02:00
|
|
|
PromptEvent::Abort => {}
|
2021-05-22 10:33:42 +02:00
|
|
|
PromptEvent::Validate => {
|
2021-06-18 00:09:10 +02:00
|
|
|
let (view, doc) = current!(editor);
|
2021-05-22 10:33:42 +02:00
|
|
|
|
|
|
|
// always present here
|
|
|
|
let item = item.unwrap();
|
|
|
|
|
|
|
|
// if more text was entered, remove it
|
|
|
|
let cursor = doc.selection(view.id).cursor();
|
|
|
|
if trigger_offset < cursor {
|
|
|
|
let remove = Transaction::change(
|
2021-04-14 08:30:15 +02:00
|
|
|
doc.text(),
|
2021-05-22 10:33:42 +02:00
|
|
|
vec![(trigger_offset, cursor, None)].into_iter(),
|
2021-04-14 08:30:15 +02:00
|
|
|
);
|
2021-05-22 10:33:42 +02:00
|
|
|
doc.apply(&remove, view.id);
|
|
|
|
}
|
|
|
|
|
2021-06-07 19:01:07 +02:00
|
|
|
let transaction = if let Some(edit) = &item.text_edit {
|
|
|
|
let edit = match edit {
|
|
|
|
lsp::CompletionTextEdit::Edit(edit) => edit.clone(),
|
|
|
|
lsp::CompletionTextEdit::InsertAndReplace(item) => {
|
|
|
|
unimplemented!("completion: insert_and_replace {:?}", item)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
util::generate_transaction_from_edits(
|
|
|
|
doc.text(),
|
|
|
|
vec![edit],
|
|
|
|
offset_encoding, // TODO: should probably transcode in Client
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let text = item.insert_text.as_ref().unwrap_or(&item.label);
|
|
|
|
let cursor = doc.selection(view.id).cursor();
|
|
|
|
Transaction::change(
|
|
|
|
doc.text(),
|
|
|
|
vec![(cursor, cursor, Some(text.as_str().into()))].into_iter(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2021-05-22 10:33:42 +02:00
|
|
|
doc.apply(&transaction, view.id);
|
|
|
|
|
|
|
|
if let Some(additional_edits) = &item.additional_text_edits {
|
|
|
|
// gopls uses this to add extra imports
|
|
|
|
if !additional_edits.is_empty() {
|
|
|
|
let transaction = util::generate_transaction_from_edits(
|
|
|
|
doc.text(),
|
|
|
|
additional_edits.clone(),
|
|
|
|
offset_encoding, // TODO: should probably transcode in Client
|
|
|
|
);
|
|
|
|
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
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
});
|
2021-03-27 04:06:40 +01:00
|
|
|
let popup = Popup::new(menu);
|
|
|
|
Self {
|
|
|
|
popup,
|
|
|
|
trigger_offset,
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 11:23:37 +02:00
|
|
|
|
|
|
|
pub fn update(&mut self, cx: &mut commands::Context) {
|
|
|
|
// recompute menu based on matches
|
|
|
|
let menu = self.popup.contents_mut();
|
2021-06-18 00:09:10 +02:00
|
|
|
let (view, doc) = current!(cx.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?
|
|
|
|
|
|
|
|
let cursor = doc.selection(view.id).cursor();
|
|
|
|
if self.trigger_offset <= cursor {
|
2021-04-06 08:34:33 +02:00
|
|
|
let fragment = doc.text().slice(self.trigger_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.popup.contents().is_empty()
|
|
|
|
}
|
2021-03-27 04:06:40 +01:00
|
|
|
}
|
|
|
|
|
2021-04-05 11:23:37 +02:00
|
|
|
// need to:
|
|
|
|
// - trigger on the right trigger char
|
|
|
|
// - detect previous open instance and recycle
|
|
|
|
// - update after input, but AFTER the document has changed
|
|
|
|
// - if no more matches, need to auto close
|
|
|
|
//
|
|
|
|
// missing bits:
|
|
|
|
// - a more robust hook system: emit to a channel, process in main loop
|
|
|
|
// - a way to find specific layers in compositor
|
|
|
|
// - components register for hooks, then unregister when terminated
|
|
|
|
// ... since completion is a special case, maybe just build it into doc/render?
|
|
|
|
|
2021-03-27 04:06:40 +01:00
|
|
|
impl Component for Completion {
|
|
|
|
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
2021-05-29 03:37:47 +02:00
|
|
|
// let the Editor handle Esc instead
|
|
|
|
if let Event::Key(KeyEvent {
|
|
|
|
code: KeyCode::Esc, ..
|
|
|
|
}) = event
|
|
|
|
{
|
|
|
|
return EventResult::Ignored;
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(&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
|
2021-05-03 11:22:29 +02:00
|
|
|
if let Some(option) = self.popup.contents().selection() {
|
|
|
|
// need to render:
|
|
|
|
// option.detail
|
|
|
|
// ---
|
|
|
|
// option.documentation
|
2021-05-29 17:26:27 +02:00
|
|
|
|
2021-06-18 00:09:10 +02:00
|
|
|
let (view, doc) = current!(cx.editor);
|
2021-05-30 10:13:02 +02:00
|
|
|
let language = doc
|
|
|
|
.language()
|
|
|
|
.and_then(|scope| scope.strip_prefix("source."))
|
|
|
|
.unwrap_or("");
|
2021-06-20 06:13:44 +02:00
|
|
|
let cursor_pos = doc.selection(view.id).cursor();
|
2021-06-20 07:15:08 +02:00
|
|
|
let cursor_pos = (helix_core::coords_at_pos(doc.text().slice(..), cursor_pos).row
|
|
|
|
- view.first_line) as u16;
|
2021-05-30 10:13:02 +02:00
|
|
|
|
2021-05-29 17:26:27 +02:00
|
|
|
let doc = match &option.documentation {
|
|
|
|
Some(lsp::Documentation::String(contents))
|
2021-05-03 11:22:29 +02:00
|
|
|
| Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
|
|
|
|
kind: lsp::MarkupKind::PlainText,
|
2021-05-29 17:26:27 +02:00
|
|
|
value: contents,
|
2021-05-03 11:22:29 +02:00
|
|
|
})) => {
|
|
|
|
// TODO: convert to wrapped text
|
2021-06-19 13:26:52 +02:00
|
|
|
Markdown::new(
|
|
|
|
format!(
|
|
|
|
"```{}\n{}\n```\n{}",
|
|
|
|
language,
|
|
|
|
option.detail.as_deref().unwrap_or_default(),
|
|
|
|
contents.clone()
|
|
|
|
),
|
|
|
|
cx.editor.syn_loader.clone(),
|
|
|
|
)
|
2021-05-03 11:22:29 +02:00
|
|
|
}
|
|
|
|
Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
|
|
|
|
kind: lsp::MarkupKind::Markdown,
|
|
|
|
value: contents,
|
|
|
|
})) => {
|
2021-05-29 17:26:27 +02:00
|
|
|
// TODO: set language based on doc scope
|
2021-06-19 13:26:52 +02:00
|
|
|
Markdown::new(
|
|
|
|
format!(
|
|
|
|
"```{}\n{}\n```\n{}",
|
|
|
|
language,
|
|
|
|
option.detail.as_deref().unwrap_or_default(),
|
|
|
|
contents.clone()
|
|
|
|
),
|
|
|
|
cx.editor.syn_loader.clone(),
|
|
|
|
)
|
2021-05-29 17:26:27 +02:00
|
|
|
}
|
|
|
|
None if option.detail.is_some() => {
|
|
|
|
// TODO: copied from above
|
2021-05-03 11:22:29 +02:00
|
|
|
|
2021-05-29 17:26:27 +02:00
|
|
|
// TODO: set language based on doc scope
|
2021-06-19 13:26:52 +02:00
|
|
|
Markdown::new(
|
|
|
|
format!(
|
|
|
|
"```{}\n{}\n```",
|
|
|
|
language,
|
|
|
|
option.detail.as_deref().unwrap_or_default(),
|
|
|
|
),
|
|
|
|
cx.editor.syn_loader.clone(),
|
|
|
|
)
|
2021-05-03 11:22:29 +02:00
|
|
|
}
|
2021-05-29 17:26:27 +02:00
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let half = area.height / 2;
|
|
|
|
let height = 15.min(half);
|
2021-06-20 12:35:12 +02:00
|
|
|
// we want to make sure the cursor is visible (not hidden behind the documentation)
|
2021-06-20 07:15:08 +02:00
|
|
|
let y = if cursor_pos + view.area.y
|
2021-06-20 12:35:12 +02:00
|
|
|
>= (cx.editor.tree.area().height - height - 2/* statusline + commandline */)
|
2021-06-20 07:15:08 +02:00
|
|
|
{
|
2021-06-20 06:13:44 +02:00
|
|
|
0
|
|
|
|
} else {
|
|
|
|
// -2 to subtract command line + statusline. a bit of a hack, because of splits.
|
|
|
|
area.height.saturating_sub(height).saturating_sub(2)
|
|
|
|
};
|
|
|
|
|
|
|
|
let area = Rect::new(0, y, area.width, height);
|
2021-05-29 17:26:27 +02:00
|
|
|
|
|
|
|
// clear area
|
|
|
|
let background = cx.editor.theme.get("ui.popup");
|
|
|
|
surface.clear_with(area, background);
|
|
|
|
doc.render(area, surface, cx);
|
2021-05-03 11:22:29 +02:00
|
|
|
}
|
2021-03-27 04:06:40 +01:00
|
|
|
}
|
|
|
|
}
|