|
|
@ -10,7 +10,7 @@ use helix_core::{
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
use crate::{
|
|
|
|
compositor::{Callback, Compositor},
|
|
|
|
compositor::{Callback, Component, Compositor},
|
|
|
|
ui::{self, Picker, Popup, Prompt, PromptEvent},
|
|
|
|
ui::{self, Picker, Popup, Prompt, PromptEvent},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -26,14 +26,20 @@ use crossterm::event::{KeyCode, KeyEvent};
|
|
|
|
|
|
|
|
|
|
|
|
use helix_lsp::lsp;
|
|
|
|
use helix_lsp::lsp;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use crate::application::{LspCallbackWrapper, LspCallbacks};
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Context<'a> {
|
|
|
|
pub struct Context<'a> {
|
|
|
|
pub count: usize,
|
|
|
|
pub count: usize,
|
|
|
|
pub editor: &'a mut Editor,
|
|
|
|
pub editor: &'a mut Editor,
|
|
|
|
|
|
|
|
|
|
|
|
pub callback: Option<crate::compositor::Callback>,
|
|
|
|
pub callback: Option<crate::compositor::Callback>,
|
|
|
|
pub on_next_key_callback: Option<Box<dyn FnOnce(&mut Context, KeyEvent)>>,
|
|
|
|
pub on_next_key_callback: Option<Box<dyn FnOnce(&mut Context, KeyEvent)>>,
|
|
|
|
|
|
|
|
pub callbacks: &'a mut LspCallbacks,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use futures_util::FutureExt;
|
|
|
|
|
|
|
|
use std::future::Future;
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> Context<'a> {
|
|
|
|
impl<'a> Context<'a> {
|
|
|
|
#[inline]
|
|
|
|
#[inline]
|
|
|
|
pub fn view(&mut self) -> &mut View {
|
|
|
|
pub fn view(&mut self) -> &mut View {
|
|
|
@ -47,7 +53,7 @@ impl<'a> Context<'a> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Push a new component onto the compositor.
|
|
|
|
/// Push a new component onto the compositor.
|
|
|
|
pub fn push_layer(&mut self, mut component: Box<dyn crate::compositor::Component>) {
|
|
|
|
pub fn push_layer(&mut self, mut component: Box<dyn Component>) {
|
|
|
|
self.callback = Some(Box::new(
|
|
|
|
self.callback = Some(Box::new(
|
|
|
|
|compositor: &mut Compositor, editor: &mut Editor| {
|
|
|
|
|compositor: &mut Compositor, editor: &mut Editor| {
|
|
|
|
let size = compositor.size();
|
|
|
|
let size = compositor.size();
|
|
|
@ -65,6 +71,27 @@ impl<'a> Context<'a> {
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
self.on_next_key_callback = Some(Box::new(on_next_key_callback));
|
|
|
|
self.on_next_key_callback = Some(Box::new(on_next_key_callback));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
|
|
pub fn callback<T, F>(
|
|
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
|
|
call: impl Future<Output = helix_lsp::Result<serde_json::Value>> + 'static + Send,
|
|
|
|
|
|
|
|
callback: F,
|
|
|
|
|
|
|
|
) where
|
|
|
|
|
|
|
|
T: for<'de> serde::Deserialize<'de> + Send + 'static,
|
|
|
|
|
|
|
|
F: FnOnce(&mut Editor, &mut Compositor, T) + Send + 'static,
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let callback = Box::pin(async move {
|
|
|
|
|
|
|
|
let json = call.await?;
|
|
|
|
|
|
|
|
let response = serde_json::from_value(json)?;
|
|
|
|
|
|
|
|
let call: LspCallbackWrapper =
|
|
|
|
|
|
|
|
Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
|
|
|
|
|
|
|
|
callback(editor, compositor, response)
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(call)
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
self.callbacks.push(callback);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// A command is a function that takes the current state and a count, and does a side-effect on the
|
|
|
|
/// A command is a function that takes the current state and a count, and does a side-effect on the
|
|
|
@ -1564,6 +1591,24 @@ pub fn save(cx: &mut Context) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn completion(cx: &mut Context) {
|
|
|
|
pub fn completion(cx: &mut Context) {
|
|
|
|
|
|
|
|
// trigger on trigger char, or if user calls it
|
|
|
|
|
|
|
|
// (or on word char typing??)
|
|
|
|
|
|
|
|
// after it's triggered, if response marked is_incomplete, update on every subsequent keypress
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// lsp calls are done via a callback: it sends a request and doesn't block.
|
|
|
|
|
|
|
|
// when we get the response similarly to notification, trigger a call to the completion popup
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// language_server.completion(params, |cx: &mut Context, _meta, response| {
|
|
|
|
|
|
|
|
// // called at response time
|
|
|
|
|
|
|
|
// // compositor, lookup completion layer
|
|
|
|
|
|
|
|
// // downcast dyn Component to Completion component
|
|
|
|
|
|
|
|
// // emit response to completion (completion.complete/handle(response))
|
|
|
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
// async {
|
|
|
|
|
|
|
|
// let (response, callback) = response.await?;
|
|
|
|
|
|
|
|
// callback(response)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
let doc = cx.doc();
|
|
|
|
let doc = cx.doc();
|
|
|
|
|
|
|
|
|
|
|
|
let language_server = match doc.language_server() {
|
|
|
|
let language_server = match doc.language_server() {
|
|
|
@ -1576,91 +1621,119 @@ pub fn completion(cx: &mut Context) {
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: handle fails
|
|
|
|
// TODO: handle fails
|
|
|
|
|
|
|
|
|
|
|
|
let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
|
|
|
|
let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: if no completion, show some message or something
|
|
|
|
cx.callback(
|
|
|
|
if !res.is_empty() {
|
|
|
|
res,
|
|
|
|
// let snapshot = doc.state.clone();
|
|
|
|
|editor: &mut Editor,
|
|
|
|
let mut menu = ui::Menu::new(
|
|
|
|
compositor: &mut Compositor,
|
|
|
|
res,
|
|
|
|
response: Option<lsp::CompletionResponse>| {
|
|
|
|
|item| {
|
|
|
|
let items = match response {
|
|
|
|
// format_fn
|
|
|
|
Some(lsp::CompletionResponse::Array(items)) => items,
|
|
|
|
item.label.as_str().into()
|
|
|
|
// TODO: do something with is_incomplete
|
|
|
|
|
|
|
|
Some(lsp::CompletionResponse::List(lsp::CompletionList {
|
|
|
|
// TODO: use item.filter_text for filtering
|
|
|
|
is_incomplete: _is_incomplete,
|
|
|
|
},
|
|
|
|
items,
|
|
|
|
move |editor: &mut Editor, item, event| {
|
|
|
|
})) => items,
|
|
|
|
match event {
|
|
|
|
None => Vec::new(),
|
|
|
|
PromptEvent::Abort => {
|
|
|
|
};
|
|
|
|
// revert state
|
|
|
|
|
|
|
|
// let id = editor.view().doc;
|
|
|
|
// TODO: if no completion, show some message or something
|
|
|
|
// let doc = &mut editor.documents[id];
|
|
|
|
if !items.is_empty() {
|
|
|
|
// doc.state = snapshot.clone();
|
|
|
|
// let snapshot = doc.state.clone();
|
|
|
|
}
|
|
|
|
let mut menu = ui::Menu::new(
|
|
|
|
PromptEvent::Validate => {
|
|
|
|
items,
|
|
|
|
let id = editor.view().doc;
|
|
|
|
|item| {
|
|
|
|
let doc = &mut editor.documents[id];
|
|
|
|
// format_fn
|
|
|
|
|
|
|
|
item.label.as_str().into()
|
|
|
|
// revert state to what it was before the last update
|
|
|
|
|
|
|
|
// doc.state = snapshot.clone();
|
|
|
|
// TODO: use item.filter_text for filtering
|
|
|
|
|
|
|
|
},
|
|
|
|
// extract as fn(doc, item):
|
|
|
|
move |editor: &mut Editor, item, event| {
|
|
|
|
|
|
|
|
match event {
|
|
|
|
// TODO: need to apply without composing state...
|
|
|
|
PromptEvent::Abort => {
|
|
|
|
// TODO: need to update lsp on accept/cancel by diffing the snapshot with
|
|
|
|
// revert state
|
|
|
|
// the final state?
|
|
|
|
// let id = editor.view().doc;
|
|
|
|
// -> on update simply update the snapshot, then on accept redo the call,
|
|
|
|
// let doc = &mut editor.documents[id];
|
|
|
|
// finally updating doc.changes + notifying lsp.
|
|
|
|
// doc.state = snapshot.clone();
|
|
|
|
//
|
|
|
|
}
|
|
|
|
// or we could simply use doc.undo + apply when changing between options
|
|
|
|
PromptEvent::Validate => {
|
|
|
|
|
|
|
|
let id = editor.view().doc;
|
|
|
|
// always present here
|
|
|
|
let doc = &mut editor.documents[id];
|
|
|
|
let item = item.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
// revert state to what it was before the last update
|
|
|
|
use helix_lsp::{lsp, util};
|
|
|
|
// doc.state = snapshot.clone();
|
|
|
|
// determine what to insert: text_edit | insert_text | label
|
|
|
|
|
|
|
|
let edit = if let Some(edit) = &item.text_edit {
|
|
|
|
// extract as fn(doc, item):
|
|
|
|
match edit {
|
|
|
|
|
|
|
|
lsp::CompletionTextEdit::Edit(edit) => edit.clone(),
|
|
|
|
// TODO: need to apply without composing state...
|
|
|
|
lsp::CompletionTextEdit::InsertAndReplace(item) => {
|
|
|
|
// TODO: need to update lsp on accept/cancel by diffing the snapshot with
|
|
|
|
unimplemented!("completion: insert_and_replace {:?}", item)
|
|
|
|
// the final state?
|
|
|
|
|
|
|
|
// -> on update simply update the snapshot, then on accept redo the call,
|
|
|
|
|
|
|
|
// finally updating doc.changes + notifying lsp.
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// or we could simply use doc.undo + apply when changing between options
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// always present here
|
|
|
|
|
|
|
|
let item = item.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use helix_lsp::{lsp, util};
|
|
|
|
|
|
|
|
// determine what to insert: text_edit | insert_text | label
|
|
|
|
|
|
|
|
let edit = if let Some(edit) = &item.text_edit {
|
|
|
|
|
|
|
|
match edit {
|
|
|
|
|
|
|
|
lsp::CompletionTextEdit::Edit(edit) => edit.clone(),
|
|
|
|
|
|
|
|
lsp::CompletionTextEdit::InsertAndReplace(item) => {
|
|
|
|
|
|
|
|
unimplemented!(
|
|
|
|
|
|
|
|
"completion: insert_and_replace {:?}",
|
|
|
|
|
|
|
|
item
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
item.insert_text.as_ref().unwrap_or(&item.label);
|
|
|
|
|
|
|
|
unimplemented!();
|
|
|
|
|
|
|
|
// lsp::TextEdit::new(); TODO: calculate a TextEdit from insert_text
|
|
|
|
|
|
|
|
// and we insert at position.
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: merge edit with additional_text_edits
|
|
|
|
|
|
|
|
if let Some(additional_edits) = &item.additional_text_edits {
|
|
|
|
|
|
|
|
if !additional_edits.is_empty() {
|
|
|
|
|
|
|
|
unimplemented!(
|
|
|
|
|
|
|
|
"completion: additional_text_edits: {:?}",
|
|
|
|
|
|
|
|
additional_edits
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let transaction =
|
|
|
|
|
|
|
|
util::generate_transaction_from_edits(doc.text(), vec![edit]);
|
|
|
|
|
|
|
|
doc.apply(&transaction);
|
|
|
|
|
|
|
|
// TODO: doc.append_changes_to_history(); if not in insert mode?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_ => (),
|
|
|
|
item.insert_text.as_ref().unwrap_or(&item.label);
|
|
|
|
|
|
|
|
unimplemented!();
|
|
|
|
|
|
|
|
// lsp::TextEdit::new(); TODO: calculate a TextEdit from insert_text
|
|
|
|
|
|
|
|
// and we insert at position.
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: merge edit with additional_text_edits
|
|
|
|
let popup = Popup::new(Box::new(menu));
|
|
|
|
if let Some(additional_edits) = &item.additional_text_edits {
|
|
|
|
let mut component: Box<dyn Component> = Box::new(popup);
|
|
|
|
if !additional_edits.is_empty() {
|
|
|
|
|
|
|
|
unimplemented!(
|
|
|
|
|
|
|
|
"completion: additional_text_edits: {:?}",
|
|
|
|
|
|
|
|
additional_edits
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: <-- if state has changed by further input, transaction will panic on len
|
|
|
|
// Server error: content modified
|
|
|
|
let transaction =
|
|
|
|
|
|
|
|
util::generate_transaction_from_edits(doc.text(), vec![edit]);
|
|
|
|
|
|
|
|
doc.apply(&transaction);
|
|
|
|
|
|
|
|
// TODO: doc.append_changes_to_history(); if not in insert mode?
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let popup = Popup::new(Box::new(menu));
|
|
|
|
// TODO: this is shared with cx.push_layer
|
|
|
|
cx.push_layer(Box::new(popup));
|
|
|
|
let size = compositor.size();
|
|
|
|
|
|
|
|
// trigger required_size on init
|
|
|
|
|
|
|
|
component.required_size((size.width, size.height));
|
|
|
|
|
|
|
|
compositor.push(component);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO!: when iterating over items, show the docs in popup
|
|
|
|
// // TODO!: when iterating over items, show the docs in popup
|
|
|
|
|
|
|
|
|
|
|
|
// language server client needs to be accessible via a registry of some sort
|
|
|
|
// // language server client needs to be accessible via a registry of some sort
|
|
|
|
}
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn hover(cx: &mut Context) {
|
|
|
|
pub fn hover(cx: &mut Context) {
|
|
|
|