1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use deno_core::anyhow::anyhow;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_core::serde_json::Value;
use std::cell::RefCell;
use tokio::sync::mpsc::channel;
use tokio::sync::mpsc::unbounded_channel;
use tokio::sync::mpsc::Receiver;
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.
pub fn rustyline_channel(
) -> (RustylineSyncMessageSender, RustylineSyncMessageHandler) {
let (message_tx, message_rx) = channel(1);
let (response_tx, response_rx) = unbounded_channel();
(
RustylineSyncMessageSender {
message_tx,
response_rx: RefCell::new(response_rx),
},
RustylineSyncMessageHandler {
response_tx,
message_rx,
},
)
}
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>,
response_rx: RefCell<UnboundedReceiver<RustylineSyncResponse>>,
}
impl RustylineSyncMessageSender {
pub fn post_message<T: serde::Serialize>(
&self,
method: &str,
params: Option<T>,
) -> Result<Value, AnyError> {
if let Err(err) =
self
.message_tx
.blocking_send(RustylineSyncMessage::PostMessage {
method: method.to_string(),
params: params
.map(|params| serde_json::to_value(params))
.transpose()?,
})
{
Err(anyhow!("{}", err))
} else {
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!(),
}
}
}
}
pub struct RustylineSyncMessageHandler {
message_rx: Receiver<RustylineSyncMessage>,
response_tx: UnboundedSender<RustylineSyncResponse>,
}
impl RustylineSyncMessageHandler {
pub async fn recv(&mut self) -> Option<RustylineSyncMessage> {
self.message_rx.recv().await
}
pub fn send(&self, response: RustylineSyncResponse) -> Result<(), AnyError> {
self
.response_tx
.send(response)
.map_err(|err| anyhow!("{}", err))
}
}
|