diff options
Diffstat (limited to 'cli/tools/repl/channel.rs')
-rw-r--r-- | cli/tools/repl/channel.rs | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/cli/tools/repl/channel.rs b/cli/tools/repl/channel.rs index 54ec6869d..a02f88523 100644 --- a/cli/tools/repl/channel.rs +++ b/cli/tools/repl/channel.rs @@ -11,6 +11,8 @@ use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::UnboundedReceiver; use tokio::sync::mpsc::UnboundedSender; +use crate::lsp::ReplCompletionItem; + /// Rustyline uses synchronous methods in its interfaces, but we need to call /// async methods. To get around this, we communicate with async code by using /// a channel and blocking on the result. @@ -31,8 +33,21 @@ pub fn rustyline_channel( ) } -pub type RustylineSyncMessage = (String, Option<Value>); -pub type RustylineSyncResponse = Result<Value, AnyError>; +pub enum RustylineSyncMessage { + PostMessage { + method: String, + params: Option<Value>, + }, + LspCompletions { + line_text: String, + position: usize, + }, +} + +pub enum RustylineSyncResponse { + PostMessage(Result<Value, AnyError>), + LspCompletions(Vec<ReplCompletionItem>), +} pub struct RustylineSyncMessageSender { message_tx: Sender<RustylineSyncMessage>, @@ -46,11 +61,41 @@ impl RustylineSyncMessageSender { params: Option<Value>, ) -> Result<Value, AnyError> { if let Err(err) = - self.message_tx.blocking_send((method.to_string(), params)) + self + .message_tx + .blocking_send(RustylineSyncMessage::PostMessage { + method: method.to_string(), + params, + }) { Err(anyhow!("{}", err)) } else { - self.response_rx.borrow_mut().blocking_recv().unwrap() + match self.response_rx.borrow_mut().blocking_recv().unwrap() { + RustylineSyncResponse::PostMessage(result) => result, + RustylineSyncResponse::LspCompletions(_) => unreachable!(), + } + } + } + + pub fn lsp_completions( + &self, + line_text: &str, + position: usize, + ) -> Vec<ReplCompletionItem> { + if self + .message_tx + .blocking_send(RustylineSyncMessage::LspCompletions { + line_text: line_text.to_string(), + position, + }) + .is_err() + { + Vec::new() + } else { + match self.response_rx.borrow_mut().blocking_recv().unwrap() { + RustylineSyncResponse::LspCompletions(result) => result, + RustylineSyncResponse::PostMessage(_) => unreachable!(), + } } } } |