wip: Async async. Delay response handling with a callback.

pull/11/head
Blaž Hrastnik 4 years ago
parent ad3325db8e
commit 2a3910c1d9

2
Cargo.lock generated

@ -549,6 +549,8 @@ dependencies = [
"num_cpus", "num_cpus",
"once_cell", "once_cell",
"pulldown-cmark", "pulldown-cmark",
"serde",
"serde_json",
"smol", "smol",
"smol-timeout", "smol-timeout",
"toml", "toml",

@ -1,13 +1,12 @@
use crate::{ use crate::{
transport::{Payload, Transport}, transport::{Payload, Transport},
Call, Error, Call, Error, Result,
}; };
type Result<T> = core::result::Result<T, Error>;
use helix_core::{ChangeSet, Rope}; use helix_core::{ChangeSet, Rope};
// use std::collections::HashMap; // use std::collections::HashMap;
use std::future::Future;
use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::atomic::{AtomicU64, Ordering};
use jsonrpc_core as jsonrpc; use jsonrpc_core as jsonrpc;
@ -96,6 +95,21 @@ impl Client {
where where
R::Params: serde::Serialize, R::Params: serde::Serialize,
R::Result: core::fmt::Debug, // TODO: temporary R::Result: core::fmt::Debug, // TODO: temporary
{
// a future that resolves into the response
let future = self.call::<R>(params).await?;
let json = future.await?;
let response = serde_json::from_value(json)?;
Ok(response)
}
/// Execute a RPC request on the language server.
pub async fn call<R: lsp::request::Request>(
&self,
params: R::Params,
) -> Result<impl Future<Output = Result<Value>>>
where
R::Params: serde::Serialize,
{ {
let params = serde_json::to_value(params)?; let params = serde_json::to_value(params)?;
@ -119,15 +133,15 @@ impl Client {
use smol_timeout::TimeoutExt; use smol_timeout::TimeoutExt;
use std::time::Duration; use std::time::Duration;
let response = match rx.recv().timeout(Duration::from_secs(2)).await { let future = async move {
Some(response) => response, rx.recv()
None => return Err(Error::Timeout), .timeout(Duration::from_secs(2))
} .await
.map_err(|e| Error::Other(e.into()))??; .ok_or(Error::Timeout)? // return Timeout
.map_err(|e| Error::Other(e.into()))?
let response = serde_json::from_value(response)?; };
Ok(response) Ok(future)
} }
/// Send a RPC notification to the language server. /// Send a RPC notification to the language server.
@ -447,7 +461,8 @@ impl Client {
&self, &self,
text_document: lsp::TextDocumentIdentifier, text_document: lsp::TextDocumentIdentifier,
position: lsp::Position, position: lsp::Position,
) -> Result<Vec<lsp::CompletionItem>> { ) -> Result<impl Future<Output = Result<Value>>> {
// ) -> Result<Vec<lsp::CompletionItem>> {
let params = lsp::CompletionParams { let params = lsp::CompletionParams {
text_document_position: lsp::TextDocumentPositionParams { text_document_position: lsp::TextDocumentPositionParams {
text_document, text_document,
@ -464,19 +479,7 @@ impl Client {
// lsp::CompletionContext { trigger_kind: , trigger_character: Some(), } // lsp::CompletionContext { trigger_kind: , trigger_character: Some(), }
}; };
let response = self.request::<lsp::request::Completion>(params).await?; self.call::<lsp::request::Completion>(params).await
let items = match response {
Some(lsp::CompletionResponse::Array(items)) => items,
// TODO: do something with is_incomplete
Some(lsp::CompletionResponse::List(lsp::CompletionList {
is_incomplete: _is_incomplete,
items,
})) => items,
None => Vec::new(),
};
Ok(items)
} }
pub async fn text_document_signature_help( pub async fn text_document_signature_help(

@ -8,6 +8,8 @@ pub use lsp_types as lsp;
pub use client::Client; pub use client::Client;
pub use lsp::{Position, Url}; pub use lsp::{Position, Url};
pub type Result<T> = core::result::Result<T, Error>;
use helix_core::syntax::LanguageConfiguration; use helix_core::syntax::LanguageConfiguration;
use thiserror::Error; use thiserror::Error;

@ -44,3 +44,6 @@ pulldown-cmark = { version = "0.8", default-features = false }
# config # config
toml = "0.5" toml = "0.5"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }

@ -24,11 +24,23 @@ use crossterm::{
use tui::layout::Rect; use tui::layout::Rect;
// use futures_util::future::BoxFuture;
use futures_util::stream::FuturesUnordered;
use std::pin::Pin;
type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
pub type LspCallback =
BoxFuture<Result<Box<dyn FnOnce(&mut Editor, &mut Compositor) + Send>, anyhow::Error>>;
pub type LspCallbacks = FuturesUnordered<LspCallback>;
pub type LspCallbackWrapper = Box<dyn FnOnce(&mut Editor, &mut Compositor) + Send>;
pub struct Application { pub struct Application {
compositor: Compositor, compositor: Compositor,
editor: Editor, editor: Editor,
executor: &'static smol::Executor<'static>, executor: &'static smol::Executor<'static>,
callbacks: LspCallbacks,
} }
impl Application { impl Application {
@ -50,6 +62,7 @@ impl Application {
editor, editor,
executor, executor,
callbacks: FuturesUnordered::new(),
}; };
Ok(app) Ok(app)
@ -59,10 +72,12 @@ impl Application {
let executor = &self.executor; let executor = &self.executor;
let editor = &mut self.editor; let editor = &mut self.editor;
let compositor = &mut self.compositor; let compositor = &mut self.compositor;
let callbacks = &mut self.callbacks;
let mut cx = crate::compositor::Context { let mut cx = crate::compositor::Context {
editor, editor,
executor, executor,
callbacks,
scroll: None, scroll: None,
}; };
@ -87,14 +102,28 @@ impl Application {
call = self.editor.language_servers.incoming.next().fuse() => { call = self.editor.language_servers.incoming.next().fuse() => {
self.handle_language_server_message(call).await self.handle_language_server_message(call).await
} }
callback = self.callbacks.next().fuse() => {
self.handle_language_server_callback(callback)
}
} }
} }
} }
pub fn handle_language_server_callback(
&mut self,
callback: Option<Result<LspCallbackWrapper, anyhow::Error>>,
) {
if let Some(Ok(callback)) = callback {
// TODO: handle Err()
callback(&mut self.editor, &mut self.compositor);
self.render();
}
}
pub fn handle_terminal_events(&mut self, event: Option<Result<Event, crossterm::ErrorKind>>) { pub fn handle_terminal_events(&mut self, event: Option<Result<Event, crossterm::ErrorKind>>) {
let mut cx = crate::compositor::Context { let mut cx = crate::compositor::Context {
editor: &mut self.editor, editor: &mut self.editor,
executor: &self.executor, executor: &self.executor,
callbacks: &mut self.callbacks,
scroll: None, scroll: None,
}; };
// Handle key events // Handle key events

@ -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,13 +1621,28 @@ 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();
cx.callback(
res,
|editor: &mut Editor,
compositor: &mut Compositor,
response: Option<lsp::CompletionResponse>| {
let items = match response {
Some(lsp::CompletionResponse::Array(items)) => items,
// TODO: do something with is_incomplete
Some(lsp::CompletionResponse::List(lsp::CompletionList {
is_incomplete: _is_incomplete,
items,
})) => items,
None => Vec::new(),
};
// TODO: if no completion, show some message or something // TODO: if no completion, show some message or something
if !res.is_empty() { if !items.is_empty() {
// let snapshot = doc.state.clone(); // let snapshot = doc.state.clone();
let mut menu = ui::Menu::new( let mut menu = ui::Menu::new(
res, items,
|item| { |item| {
// format_fn // format_fn
item.label.as_str().into() item.label.as_str().into()
@ -1623,7 +1683,10 @@ pub fn completion(cx: &mut Context) {
match edit { match edit {
lsp::CompletionTextEdit::Edit(edit) => edit.clone(), lsp::CompletionTextEdit::Edit(edit) => edit.clone(),
lsp::CompletionTextEdit::InsertAndReplace(item) => { lsp::CompletionTextEdit::InsertAndReplace(item) => {
unimplemented!("completion: insert_and_replace {:?}", item) unimplemented!(
"completion: insert_and_replace {:?}",
item
)
} }
} }
} else { } else {
@ -1643,7 +1706,6 @@ pub fn completion(cx: &mut Context) {
} }
} }
// TODO: <-- if state has changed by further input, transaction will panic on len
let transaction = let transaction =
util::generate_transaction_from_edits(doc.text(), vec![edit]); util::generate_transaction_from_edits(doc.text(), vec![edit]);
doc.apply(&transaction); doc.apply(&transaction);
@ -1655,12 +1717,23 @@ pub fn completion(cx: &mut Context) {
); );
let popup = Popup::new(Box::new(menu)); let popup = Popup::new(Box::new(menu));
cx.push_layer(Box::new(popup)); let mut component: Box<dyn Component> = Box::new(popup);
// TODO!: when iterating over items, show the docs in popup // Server error: content modified
// language server client needs to be accessible via a registry of some sort // TODO: this is shared with cx.push_layer
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
// // 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) {

@ -25,10 +25,13 @@ pub enum EventResult {
use helix_view::{Editor, View}; use helix_view::{Editor, View};
use crate::application::LspCallbacks;
pub struct Context<'a> { pub struct Context<'a> {
pub editor: &'a mut Editor, pub editor: &'a mut Editor,
pub executor: &'static smol::Executor<'static>, pub executor: &'static smol::Executor<'static>,
pub scroll: Option<usize>, pub scroll: Option<usize>,
pub callbacks: &'a mut LspCallbacks,
} }
pub trait Component { pub trait Component {

@ -439,6 +439,7 @@ impl Component for EditorView {
editor: &mut cx.editor, editor: &mut cx.editor,
count: 1, count: 1,
callback: None, callback: None,
callbacks: cx.callbacks,
on_next_key_callback: None, on_next_key_callback: None,
}; };

Loading…
Cancel
Save