2022-04-01 04:52:14 +00:00
|
|
|
pub use lsp_types::*;
|
|
|
|
|
2021-10-21 14:26:37 +00:00
|
|
|
use anyhow::{anyhow, Context, Result};
|
2022-03-01 23:02:04 +00:00
|
|
|
use collections::HashMap;
|
2022-03-28 09:05:55 +00:00
|
|
|
use futures::{channel::oneshot, io::BufWriter, AsyncRead, AsyncWrite};
|
2022-04-01 04:52:14 +00:00
|
|
|
use gpui::{executor, AsyncAppContext, Task};
|
|
|
|
use parking_lot::Mutex;
|
2022-03-09 01:41:52 +00:00
|
|
|
use postage::{barrier, prelude::Stream};
|
2022-03-11 19:44:02 +00:00
|
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
2021-10-25 17:46:33 +00:00
|
|
|
use serde_json::{json, value::RawValue, Value};
|
2021-10-21 14:26:37 +00:00
|
|
|
use smol::{
|
|
|
|
channel,
|
|
|
|
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
|
2022-04-01 04:52:14 +00:00
|
|
|
process,
|
2021-10-21 14:26:37 +00:00
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
io::Write,
|
2022-03-09 11:29:28 +00:00
|
|
|
path::PathBuf,
|
2021-10-25 17:46:33 +00:00
|
|
|
str::FromStr,
|
2021-10-21 14:26:37 +00:00
|
|
|
sync::{
|
2022-01-14 09:20:04 +00:00
|
|
|
atomic::{AtomicUsize, Ordering::SeqCst},
|
2021-10-21 14:26:37 +00:00
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use std::{path::Path, process::Stdio};
|
2022-04-01 04:52:14 +00:00
|
|
|
use util::{ResultExt, TryFutureExt};
|
2021-10-25 22:28:40 +00:00
|
|
|
|
2021-10-21 14:26:37 +00:00
|
|
|
const JSON_RPC_VERSION: &'static str = "2.0";
|
|
|
|
const CONTENT_LEN_HEADER: &'static str = "Content-Length: ";
|
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
type NotificationHandler = Box<dyn Send + FnMut(Option<usize>, &str, AsyncAppContext)>;
|
2021-10-25 16:11:52 +00:00
|
|
|
type ResponseHandler = Box<dyn Send + FnOnce(Result<&str, Error>)>;
|
|
|
|
|
2021-10-21 14:26:37 +00:00
|
|
|
pub struct LanguageServer {
|
2022-03-30 00:24:23 +00:00
|
|
|
server_id: usize,
|
2021-10-21 14:26:37 +00:00
|
|
|
next_id: AtomicUsize,
|
2022-02-25 18:04:57 +00:00
|
|
|
outbound_tx: channel::Sender<Vec<u8>>,
|
2022-03-10 15:45:13 +00:00
|
|
|
name: String,
|
2022-03-09 01:41:52 +00:00
|
|
|
capabilities: ServerCapabilities,
|
2022-04-01 04:52:14 +00:00
|
|
|
notification_handlers: Arc<Mutex<HashMap<&'static str, NotificationHandler>>>,
|
2021-10-21 14:26:37 +00:00
|
|
|
response_handlers: Arc<Mutex<HashMap<usize, ResponseHandler>>>,
|
2021-11-01 18:57:21 +00:00
|
|
|
executor: Arc<executor::Background>,
|
2021-11-02 19:16:25 +00:00
|
|
|
io_tasks: Mutex<Option<(Task<Option<()>>, Task<Option<()>>)>>,
|
|
|
|
output_done_rx: Mutex<Option<barrier::Receiver>>,
|
2022-03-09 11:29:28 +00:00
|
|
|
root_path: PathBuf,
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
pub struct Subscription {
|
|
|
|
method: &'static str,
|
2022-04-01 04:52:14 +00:00
|
|
|
notification_handlers: Arc<Mutex<HashMap<&'static str, NotificationHandler>>>,
|
2021-10-25 16:11:52 +00:00
|
|
|
}
|
2021-10-21 14:26:37 +00:00
|
|
|
|
2021-10-25 22:28:40 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Request<'a, T> {
|
|
|
|
jsonrpc: &'a str,
|
2021-10-21 14:26:37 +00:00
|
|
|
id: usize,
|
2021-10-25 22:28:40 +00:00
|
|
|
method: &'a str,
|
2021-10-21 14:26:37 +00:00
|
|
|
params: T,
|
|
|
|
}
|
|
|
|
|
2021-10-25 22:28:40 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct AnyResponse<'a> {
|
2021-10-21 17:27:10 +00:00
|
|
|
id: usize,
|
|
|
|
#[serde(default)]
|
|
|
|
error: Option<Error>,
|
2021-10-25 16:11:52 +00:00
|
|
|
#[serde(borrow)]
|
2021-11-02 13:58:00 +00:00
|
|
|
result: Option<&'a RawValue>,
|
2021-10-21 17:27:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 19:44:02 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct Response<T> {
|
|
|
|
id: usize,
|
2022-04-01 04:52:14 +00:00
|
|
|
result: Option<T>,
|
|
|
|
error: Option<Error>,
|
2022-03-11 19:44:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 22:28:40 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Notification<'a, T> {
|
|
|
|
#[serde(borrow)]
|
|
|
|
jsonrpc: &'a str,
|
|
|
|
#[serde(borrow)]
|
|
|
|
method: &'a str,
|
2021-10-21 17:27:10 +00:00
|
|
|
params: T,
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2021-10-25 22:28:40 +00:00
|
|
|
struct AnyNotification<'a> {
|
2022-03-11 19:44:02 +00:00
|
|
|
#[serde(default)]
|
|
|
|
id: Option<usize>,
|
2021-10-21 17:27:10 +00:00
|
|
|
#[serde(borrow)]
|
|
|
|
method: &'a str,
|
2021-10-21 14:26:37 +00:00
|
|
|
#[serde(borrow)]
|
|
|
|
params: &'a RawValue,
|
|
|
|
}
|
|
|
|
|
2021-10-25 22:28:40 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-10-21 17:27:10 +00:00
|
|
|
struct Error {
|
|
|
|
message: String,
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LanguageServer {
|
2022-03-09 11:29:28 +00:00
|
|
|
pub fn new(
|
2022-03-30 00:24:23 +00:00
|
|
|
server_id: usize,
|
2021-10-26 19:17:51 +00:00
|
|
|
binary_path: &Path,
|
2022-03-03 21:29:25 +00:00
|
|
|
args: &[&str],
|
2021-10-25 16:11:52 +00:00
|
|
|
root_path: &Path,
|
2022-04-01 04:52:14 +00:00
|
|
|
cx: AsyncAppContext,
|
2022-03-09 11:29:28 +00:00
|
|
|
) -> Result<Self> {
|
2022-03-11 00:06:12 +00:00
|
|
|
let working_dir = if root_path.is_dir() {
|
|
|
|
root_path
|
|
|
|
} else {
|
|
|
|
root_path.parent().unwrap_or(Path::new("/"))
|
|
|
|
};
|
2022-04-01 04:52:14 +00:00
|
|
|
let mut server = process::Command::new(binary_path)
|
2022-03-11 00:06:12 +00:00
|
|
|
.current_dir(working_dir)
|
2022-03-03 21:29:25 +00:00
|
|
|
.args(args)
|
2021-10-21 14:26:37 +00:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::inherit())
|
|
|
|
.spawn()?;
|
2021-10-25 22:28:40 +00:00
|
|
|
let stdin = server.stdin.take().unwrap();
|
|
|
|
let stdout = server.stdout.take().unwrap();
|
2022-03-30 00:24:23 +00:00
|
|
|
let mut server =
|
2022-04-01 04:52:14 +00:00
|
|
|
Self::new_internal(server_id, stdin, stdout, root_path, cx, |notification| {
|
|
|
|
log::info!(
|
|
|
|
"unhandled notification {}:\n{}",
|
|
|
|
notification.method,
|
|
|
|
serde_json::to_string_pretty(
|
|
|
|
&Value::from_str(notification.params.get()).unwrap()
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
});
|
2022-03-10 15:45:13 +00:00
|
|
|
if let Some(name) = binary_path.file_name() {
|
|
|
|
server.name = name.to_string_lossy().to_string();
|
|
|
|
}
|
|
|
|
Ok(server)
|
2021-10-25 22:28:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
fn new_internal<Stdin, Stdout, F>(
|
2022-03-30 00:24:23 +00:00
|
|
|
server_id: usize,
|
2021-10-25 22:28:40 +00:00
|
|
|
stdin: Stdin,
|
|
|
|
stdout: Stdout,
|
2021-10-26 19:17:51 +00:00
|
|
|
root_path: &Path,
|
2022-04-01 04:52:14 +00:00
|
|
|
cx: AsyncAppContext,
|
|
|
|
mut on_unhandled_notification: F,
|
2022-03-09 11:29:28 +00:00
|
|
|
) -> Self
|
2021-10-25 22:28:40 +00:00
|
|
|
where
|
|
|
|
Stdin: AsyncWrite + Unpin + Send + 'static,
|
|
|
|
Stdout: AsyncRead + Unpin + Send + 'static,
|
2022-04-01 04:52:14 +00:00
|
|
|
F: FnMut(AnyNotification) + 'static + Send,
|
2021-10-25 22:28:40 +00:00
|
|
|
{
|
|
|
|
let mut stdin = BufWriter::new(stdin);
|
|
|
|
let mut stdout = BufReader::new(stdout);
|
2021-10-21 14:26:37 +00:00
|
|
|
let (outbound_tx, outbound_rx) = channel::unbounded::<Vec<u8>>();
|
2022-03-01 23:02:04 +00:00
|
|
|
let notification_handlers =
|
2022-04-01 04:52:14 +00:00
|
|
|
Arc::new(Mutex::new(HashMap::<_, NotificationHandler>::default()));
|
2022-03-01 23:02:04 +00:00
|
|
|
let response_handlers = Arc::new(Mutex::new(HashMap::<_, ResponseHandler>::default()));
|
2022-04-01 04:52:14 +00:00
|
|
|
let input_task = cx.spawn(|cx| {
|
|
|
|
let notification_handlers = notification_handlers.clone();
|
|
|
|
let response_handlers = response_handlers.clone();
|
|
|
|
async move {
|
|
|
|
let _clear_response_handlers = ClearResponseHandlers(response_handlers.clone());
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
loop {
|
|
|
|
buffer.clear();
|
|
|
|
stdout.read_until(b'\n', &mut buffer).await?;
|
|
|
|
stdout.read_until(b'\n', &mut buffer).await?;
|
|
|
|
let message_len: usize = std::str::from_utf8(&buffer)?
|
|
|
|
.strip_prefix(CONTENT_LEN_HEADER)
|
|
|
|
.ok_or_else(|| anyhow!("invalid header"))?
|
|
|
|
.trim_end()
|
|
|
|
.parse()?;
|
|
|
|
|
|
|
|
buffer.resize(message_len, 0);
|
|
|
|
stdout.read_exact(&mut buffer).await?;
|
|
|
|
log::trace!("incoming message:{}", String::from_utf8_lossy(&buffer));
|
|
|
|
|
|
|
|
if let Ok(msg) = serde_json::from_slice::<AnyNotification>(&buffer) {
|
|
|
|
if let Some(handler) = notification_handlers.lock().get_mut(msg.method) {
|
|
|
|
handler(msg.id, msg.params.get(), cx.clone());
|
|
|
|
} else {
|
|
|
|
on_unhandled_notification(msg);
|
|
|
|
}
|
|
|
|
} else if let Ok(AnyResponse { id, error, result }) =
|
|
|
|
serde_json::from_slice(&buffer)
|
|
|
|
{
|
|
|
|
if let Some(handler) = response_handlers.lock().remove(&id) {
|
|
|
|
if let Some(error) = error {
|
|
|
|
handler(Err(error));
|
|
|
|
} else if let Some(result) = result {
|
|
|
|
handler(Ok(result.get()));
|
2021-10-25 17:46:33 +00:00
|
|
|
} else {
|
2022-04-01 04:52:14 +00:00
|
|
|
handler(Ok("null"));
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-01 04:52:14 +00:00
|
|
|
} else {
|
|
|
|
return Err(anyhow!(
|
|
|
|
"failed to deserialize message:\n{}",
|
|
|
|
std::str::from_utf8(&buffer)?
|
|
|
|
));
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
2022-04-07 14:30:42 +00:00
|
|
|
|
|
|
|
// Don't starve the main thread when receiving lots of messages at once.
|
|
|
|
smol::future::yield_now().await;
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-01 04:52:14 +00:00
|
|
|
.log_err()
|
|
|
|
});
|
2021-11-01 18:57:21 +00:00
|
|
|
let (output_done_tx, output_done_rx) = barrier::channel();
|
2022-04-01 04:52:14 +00:00
|
|
|
let output_task = cx.background().spawn({
|
2022-03-01 23:02:04 +00:00
|
|
|
let response_handlers = response_handlers.clone();
|
2021-10-21 14:26:37 +00:00
|
|
|
async move {
|
2022-03-01 23:02:04 +00:00
|
|
|
let _clear_response_handlers = ClearResponseHandlers(response_handlers);
|
2021-10-21 14:26:37 +00:00
|
|
|
let mut content_len_buffer = Vec::new();
|
2021-11-01 18:57:21 +00:00
|
|
|
while let Ok(message) = outbound_rx.recv().await {
|
2022-03-31 00:08:40 +00:00
|
|
|
log::trace!("outgoing message:{}", String::from_utf8_lossy(&message));
|
2021-10-25 16:11:52 +00:00
|
|
|
content_len_buffer.clear();
|
2021-10-21 14:26:37 +00:00
|
|
|
write!(content_len_buffer, "{}", message.len()).unwrap();
|
|
|
|
stdin.write_all(CONTENT_LEN_HEADER.as_bytes()).await?;
|
|
|
|
stdin.write_all(&content_len_buffer).await?;
|
|
|
|
stdin.write_all("\r\n\r\n".as_bytes()).await?;
|
|
|
|
stdin.write_all(&message).await?;
|
2021-10-25 22:28:40 +00:00
|
|
|
stdin.flush().await?;
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
2021-11-01 18:57:21 +00:00
|
|
|
drop(output_done_tx);
|
|
|
|
Ok(())
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
2022-03-01 23:02:04 +00:00
|
|
|
.log_err()
|
|
|
|
});
|
2021-10-21 14:26:37 +00:00
|
|
|
|
2022-03-09 11:29:28 +00:00
|
|
|
Self {
|
2022-03-30 00:24:23 +00:00
|
|
|
server_id,
|
2021-10-25 16:11:52 +00:00
|
|
|
notification_handlers,
|
2021-10-21 14:26:37 +00:00
|
|
|
response_handlers,
|
2022-03-10 15:45:13 +00:00
|
|
|
name: Default::default(),
|
2022-03-09 01:41:52 +00:00
|
|
|
capabilities: Default::default(),
|
2021-10-21 14:26:37 +00:00
|
|
|
next_id: Default::default(),
|
2022-02-25 18:04:57 +00:00
|
|
|
outbound_tx,
|
2022-04-01 04:52:14 +00:00
|
|
|
executor: cx.background().clone(),
|
2021-11-02 19:16:25 +00:00
|
|
|
io_tasks: Mutex::new(Some((input_task, output_task))),
|
|
|
|
output_done_rx: Mutex::new(Some(output_done_rx)),
|
2022-03-09 11:29:28 +00:00
|
|
|
root_path: root_path.to_path_buf(),
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 10:29:28 +00:00
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
pub async fn initialize(mut self, options: Option<Value>) -> Result<Arc<Self>> {
|
|
|
|
let root_uri = Url::from_file_path(&self.root_path).unwrap();
|
2022-03-09 11:29:28 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
let params = InitializeParams {
|
|
|
|
process_id: Default::default(),
|
|
|
|
root_path: Default::default(),
|
2022-06-22 23:58:19 +00:00
|
|
|
root_uri: Some(root_uri.clone()),
|
2022-03-09 11:29:28 +00:00
|
|
|
initialization_options: options,
|
|
|
|
capabilities: ClientCapabilities {
|
2022-03-12 01:36:27 +00:00
|
|
|
workspace: Some(WorkspaceClientCapabilities {
|
|
|
|
configuration: Some(true),
|
|
|
|
did_change_configuration: Some(DynamicRegistrationClientCapabilities {
|
|
|
|
dynamic_registration: Some(true),
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2022-03-09 11:29:28 +00:00
|
|
|
text_document: Some(TextDocumentClientCapabilities {
|
|
|
|
definition: Some(GotoCapability {
|
|
|
|
link_support: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
code_action: Some(CodeActionClientCapabilities {
|
|
|
|
code_action_literal_support: Some(CodeActionLiteralSupport {
|
|
|
|
code_action_kind: CodeActionKindLiteralSupport {
|
|
|
|
value_set: vec![
|
|
|
|
CodeActionKind::REFACTOR.as_str().into(),
|
|
|
|
CodeActionKind::QUICKFIX.as_str().into(),
|
2022-04-01 04:52:14 +00:00
|
|
|
CodeActionKind::SOURCE.as_str().into(),
|
2022-03-09 11:29:28 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
data_support: Some(true),
|
|
|
|
resolve_support: Some(CodeActionCapabilityResolveSupport {
|
2022-04-01 04:52:14 +00:00
|
|
|
properties: vec!["edit".to_string(), "command".to_string()],
|
2022-03-09 01:41:52 +00:00
|
|
|
}),
|
2022-03-09 11:29:28 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
completion: Some(CompletionClientCapabilities {
|
|
|
|
completion_item: Some(CompletionItemCapability {
|
|
|
|
snippet_support: Some(true),
|
|
|
|
resolve_support: Some(CompletionItemCapabilityResolveSupport {
|
|
|
|
properties: vec!["additionalTextEdits".to_string()],
|
|
|
|
}),
|
2022-01-31 18:09:29 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
2022-03-09 11:29:28 +00:00
|
|
|
}),
|
2022-04-22 13:43:23 +00:00
|
|
|
rename: Some(RenameClientCapabilities {
|
|
|
|
prepare_support: Some(true),
|
|
|
|
..Default::default()
|
2022-06-01 05:49:47 +00:00
|
|
|
}),
|
|
|
|
hover: Some(HoverClientCapabilities {
|
|
|
|
content_format: Some(vec![MarkupKind::Markdown]),
|
|
|
|
..Default::default()
|
2022-04-22 13:43:23 +00:00
|
|
|
}),
|
2022-03-09 11:29:28 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
experimental: Some(json!({
|
|
|
|
"serverStatusNotification": true,
|
|
|
|
})),
|
|
|
|
window: Some(WindowClientCapabilities {
|
|
|
|
work_done_progress: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
trace: Default::default(),
|
2022-06-22 23:58:19 +00:00
|
|
|
workspace_folders: Some(vec![WorkspaceFolder {
|
|
|
|
uri: root_uri,
|
|
|
|
name: Default::default(),
|
|
|
|
}]),
|
2022-03-09 11:29:28 +00:00
|
|
|
client_info: Default::default(),
|
|
|
|
locale: Default::default(),
|
|
|
|
};
|
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
let response = self.request::<request::Initialize>(params).await?;
|
|
|
|
if let Some(info) = response.server_info {
|
|
|
|
self.name = info.name;
|
2022-03-10 15:45:13 +00:00
|
|
|
}
|
2022-04-01 04:52:14 +00:00
|
|
|
self.capabilities = response.capabilities;
|
|
|
|
|
|
|
|
self.notify::<notification::Initialized>(InitializedParams {})?;
|
|
|
|
Ok(Arc::new(self))
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 18:04:57 +00:00
|
|
|
pub fn shutdown(&self) -> Option<impl 'static + Send + Future<Output = Option<()>>> {
|
2021-11-02 19:16:25 +00:00
|
|
|
if let Some(tasks) = self.io_tasks.lock().take() {
|
|
|
|
let response_handlers = self.response_handlers.clone();
|
|
|
|
let next_id = AtomicUsize::new(self.next_id.load(SeqCst));
|
2022-02-25 18:04:57 +00:00
|
|
|
let outbound_tx = self.outbound_tx.clone();
|
2021-11-02 19:16:25 +00:00
|
|
|
let mut output_done = self.output_done_rx.lock().take().unwrap();
|
2022-02-25 18:04:57 +00:00
|
|
|
let shutdown_request = Self::request_internal::<request::Shutdown>(
|
|
|
|
&next_id,
|
|
|
|
&response_handlers,
|
|
|
|
&outbound_tx,
|
|
|
|
(),
|
|
|
|
);
|
|
|
|
let exit = Self::notify_internal::<notification::Exit>(&outbound_tx, ());
|
|
|
|
outbound_tx.close();
|
|
|
|
Some(
|
|
|
|
async move {
|
2022-03-01 21:36:49 +00:00
|
|
|
log::debug!("language server shutdown started");
|
2022-02-25 18:04:57 +00:00
|
|
|
shutdown_request.await?;
|
2022-03-01 21:36:49 +00:00
|
|
|
response_handlers.lock().clear();
|
2022-02-25 18:04:57 +00:00
|
|
|
exit?;
|
|
|
|
output_done.recv().await;
|
2022-03-01 21:36:49 +00:00
|
|
|
log::debug!("language server shutdown finished");
|
2022-02-25 18:04:57 +00:00
|
|
|
drop(tasks);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
.log_err(),
|
|
|
|
)
|
2021-11-02 19:16:25 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
#[must_use]
|
|
|
|
pub fn on_notification<T, F>(&self, f: F) -> Subscription
|
2021-10-25 16:11:52 +00:00
|
|
|
where
|
2022-01-20 11:10:01 +00:00
|
|
|
T: notification::Notification,
|
2022-04-01 04:52:14 +00:00
|
|
|
F: 'static + Send + FnMut(T::Params, AsyncAppContext),
|
2022-03-11 21:19:10 +00:00
|
|
|
{
|
|
|
|
self.on_custom_notification(T::METHOD, f)
|
|
|
|
}
|
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
#[must_use]
|
|
|
|
pub fn on_request<T, F, Fut>(&self, f: F) -> Subscription
|
2022-03-11 21:19:10 +00:00
|
|
|
where
|
|
|
|
T: request::Request,
|
2022-04-01 04:52:14 +00:00
|
|
|
T::Params: 'static + Send,
|
|
|
|
F: 'static + Send + FnMut(T::Params, AsyncAppContext) -> Fut,
|
|
|
|
Fut: 'static + Future<Output = Result<T::Result>>,
|
2022-03-11 21:19:10 +00:00
|
|
|
{
|
|
|
|
self.on_custom_request(T::METHOD, f)
|
|
|
|
}
|
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
pub fn remove_request_handler<T: request::Request>(&self) {
|
|
|
|
self.notification_handlers.lock().remove(T::METHOD);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub fn on_custom_notification<Params, F>(&self, method: &'static str, mut f: F) -> Subscription
|
2022-03-11 21:19:10 +00:00
|
|
|
where
|
2022-04-01 04:52:14 +00:00
|
|
|
F: 'static + Send + FnMut(Params, AsyncAppContext),
|
2022-03-11 21:19:10 +00:00
|
|
|
Params: DeserializeOwned,
|
2021-10-25 16:11:52 +00:00
|
|
|
{
|
2022-04-01 04:52:14 +00:00
|
|
|
let prev_handler = self.notification_handlers.lock().insert(
|
2022-03-11 21:19:10 +00:00
|
|
|
method,
|
2022-04-01 04:52:14 +00:00
|
|
|
Box::new(move |_, params, cx| {
|
|
|
|
if let Some(params) = serde_json::from_str(params).log_err() {
|
|
|
|
f(params, cx);
|
|
|
|
}
|
2022-03-11 19:44:02 +00:00
|
|
|
}),
|
2021-10-25 16:11:52 +00:00
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
prev_handler.is_none(),
|
2022-03-11 21:19:10 +00:00
|
|
|
"registered multiple handlers for the same LSP method"
|
2021-10-25 16:11:52 +00:00
|
|
|
);
|
|
|
|
Subscription {
|
2022-03-11 21:19:10 +00:00
|
|
|
method,
|
2021-10-25 16:11:52 +00:00
|
|
|
notification_handlers: self.notification_handlers.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
#[must_use]
|
|
|
|
pub fn on_custom_request<Params, Res, Fut, F>(
|
|
|
|
&self,
|
2022-03-11 19:44:02 +00:00
|
|
|
method: &'static str,
|
|
|
|
mut f: F,
|
|
|
|
) -> Subscription
|
|
|
|
where
|
2022-04-01 04:52:14 +00:00
|
|
|
F: 'static + Send + FnMut(Params, AsyncAppContext) -> Fut,
|
|
|
|
Fut: 'static + Future<Output = Result<Res>>,
|
|
|
|
Params: DeserializeOwned + Send + 'static,
|
2022-03-11 21:19:10 +00:00
|
|
|
Res: Serialize,
|
2022-03-11 19:44:02 +00:00
|
|
|
{
|
2022-04-01 04:52:14 +00:00
|
|
|
let outbound_tx = self.outbound_tx.clone();
|
|
|
|
let prev_handler = self.notification_handlers.lock().insert(
|
2022-03-11 19:44:02 +00:00
|
|
|
method,
|
2022-04-01 04:52:14 +00:00
|
|
|
Box::new(move |id, params, cx| {
|
2022-03-11 19:44:02 +00:00
|
|
|
if let Some(id) = id {
|
2022-04-01 04:52:14 +00:00
|
|
|
if let Some(params) = serde_json::from_str(params).log_err() {
|
|
|
|
let response = f(params, cx.clone());
|
|
|
|
cx.foreground()
|
|
|
|
.spawn({
|
|
|
|
let outbound_tx = outbound_tx.clone();
|
|
|
|
async move {
|
|
|
|
let response = match response.await {
|
|
|
|
Ok(result) => Response {
|
|
|
|
id,
|
|
|
|
result: Some(result),
|
|
|
|
error: None,
|
|
|
|
},
|
|
|
|
Err(error) => Response {
|
|
|
|
id,
|
|
|
|
result: None,
|
|
|
|
error: Some(Error {
|
|
|
|
message: error.to_string(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if let Some(response) = serde_json::to_vec(&response).log_err()
|
|
|
|
{
|
|
|
|
outbound_tx.try_send(response).ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.detach();
|
|
|
|
}
|
2022-03-11 19:44:02 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
prev_handler.is_none(),
|
2022-03-11 21:19:10 +00:00
|
|
|
"registered multiple handlers for the same LSP method"
|
2022-03-11 19:44:02 +00:00
|
|
|
);
|
|
|
|
Subscription {
|
|
|
|
method,
|
|
|
|
notification_handlers: self.notification_handlers.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 15:45:13 +00:00
|
|
|
pub fn name<'a>(self: &'a Arc<Self>) -> &'a str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2022-03-09 11:29:28 +00:00
|
|
|
pub fn capabilities<'a>(self: &'a Arc<Self>) -> &'a ServerCapabilities {
|
2022-03-09 01:41:52 +00:00
|
|
|
&self.capabilities
|
2022-02-01 00:50:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 00:24:23 +00:00
|
|
|
pub fn server_id(&self) -> usize {
|
|
|
|
self.server_id
|
|
|
|
}
|
|
|
|
|
2022-01-20 11:10:01 +00:00
|
|
|
pub fn request<T: request::Request>(
|
2022-04-01 04:52:14 +00:00
|
|
|
&self,
|
2021-10-21 14:26:37 +00:00
|
|
|
params: T::Params,
|
|
|
|
) -> impl Future<Output = Result<T::Result>>
|
2021-10-25 10:29:28 +00:00
|
|
|
where
|
|
|
|
T::Result: 'static + Send,
|
|
|
|
{
|
2022-03-09 11:29:28 +00:00
|
|
|
Self::request_internal::<T>(
|
|
|
|
&self.next_id,
|
|
|
|
&self.response_handlers,
|
|
|
|
&self.outbound_tx,
|
|
|
|
params,
|
|
|
|
)
|
2021-10-25 10:29:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:10:01 +00:00
|
|
|
fn request_internal<T: request::Request>(
|
2021-11-01 18:57:21 +00:00
|
|
|
next_id: &AtomicUsize,
|
|
|
|
response_handlers: &Mutex<HashMap<usize, ResponseHandler>>,
|
2022-02-25 18:04:57 +00:00
|
|
|
outbound_tx: &channel::Sender<Vec<u8>>,
|
2021-10-25 10:29:28 +00:00
|
|
|
params: T::Params,
|
2021-11-02 19:16:25 +00:00
|
|
|
) -> impl 'static + Future<Output = Result<T::Result>>
|
2021-10-21 14:26:37 +00:00
|
|
|
where
|
|
|
|
T::Result: 'static + Send,
|
|
|
|
{
|
2021-11-01 18:57:21 +00:00
|
|
|
let id = next_id.fetch_add(1, SeqCst);
|
2021-10-21 14:26:37 +00:00
|
|
|
let message = serde_json::to_vec(&Request {
|
|
|
|
jsonrpc: JSON_RPC_VERSION,
|
|
|
|
id,
|
|
|
|
method: T::METHOD,
|
|
|
|
params,
|
|
|
|
})
|
|
|
|
.unwrap();
|
2022-03-01 23:02:04 +00:00
|
|
|
|
|
|
|
let send = outbound_tx
|
|
|
|
.try_send(message)
|
|
|
|
.context("failed to write to language server's stdin");
|
|
|
|
|
2022-03-01 21:36:49 +00:00
|
|
|
let (tx, rx) = oneshot::channel();
|
2022-03-01 23:02:04 +00:00
|
|
|
response_handlers.lock().insert(
|
2021-10-21 14:26:37 +00:00
|
|
|
id,
|
|
|
|
Box::new(move |result| {
|
|
|
|
let response = match result {
|
|
|
|
Ok(response) => {
|
|
|
|
serde_json::from_str(response).context("failed to deserialize response")
|
|
|
|
}
|
|
|
|
Err(error) => Err(anyhow!("{}", error.message)),
|
|
|
|
};
|
2022-03-01 21:36:49 +00:00
|
|
|
let _ = tx.send(response);
|
2021-10-21 14:26:37 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
async move {
|
2021-11-01 18:57:21 +00:00
|
|
|
send?;
|
2022-03-01 21:36:49 +00:00
|
|
|
rx.await?
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-21 17:27:10 +00:00
|
|
|
|
2022-03-09 01:41:52 +00:00
|
|
|
pub fn notify<T: notification::Notification>(&self, params: T::Params) -> Result<()> {
|
|
|
|
Self::notify_internal::<T>(&self.outbound_tx, params)
|
2021-10-25 10:29:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:10:01 +00:00
|
|
|
fn notify_internal<T: notification::Notification>(
|
2022-02-25 18:04:57 +00:00
|
|
|
outbound_tx: &channel::Sender<Vec<u8>>,
|
2021-10-21 17:27:10 +00:00
|
|
|
params: T::Params,
|
2021-11-01 18:57:21 +00:00
|
|
|
) -> Result<()> {
|
2021-10-25 22:28:40 +00:00
|
|
|
let message = serde_json::to_vec(&Notification {
|
2021-10-21 17:27:10 +00:00
|
|
|
jsonrpc: JSON_RPC_VERSION,
|
|
|
|
method: T::METHOD,
|
|
|
|
params,
|
|
|
|
})
|
|
|
|
.unwrap();
|
2021-11-01 18:57:21 +00:00
|
|
|
outbound_tx.try_send(message)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 10:29:28 +00:00
|
|
|
|
2021-11-01 18:57:21 +00:00
|
|
|
impl Drop for LanguageServer {
|
|
|
|
fn drop(&mut self) {
|
2021-11-02 19:16:25 +00:00
|
|
|
if let Some(shutdown) = self.shutdown() {
|
|
|
|
self.executor.spawn(shutdown).detach();
|
|
|
|
}
|
2021-10-21 17:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 22:28:40 +00:00
|
|
|
impl Subscription {
|
|
|
|
pub fn detach(mut self) {
|
|
|
|
self.method = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
impl Drop for Subscription {
|
|
|
|
fn drop(&mut self) {
|
2022-04-01 04:52:14 +00:00
|
|
|
self.notification_handlers.lock().remove(self.method);
|
2021-10-25 16:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 22:28:40 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-04-01 17:16:26 +00:00
|
|
|
#[derive(Clone)]
|
2021-10-25 22:28:40 +00:00
|
|
|
pub struct FakeLanguageServer {
|
2022-04-01 17:16:26 +00:00
|
|
|
pub server: Arc<LanguageServer>,
|
2022-04-01 04:52:14 +00:00
|
|
|
notifications_rx: channel::Receiver<(String, String)>,
|
2021-10-25 22:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
impl LanguageServer {
|
2022-03-03 23:42:26 +00:00
|
|
|
pub fn full_capabilities() -> ServerCapabilities {
|
|
|
|
ServerCapabilities {
|
|
|
|
document_highlight_provider: Some(OneOf::Left(true)),
|
|
|
|
code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
|
2022-03-04 10:24:18 +00:00
|
|
|
document_formatting_provider: Some(OneOf::Left(true)),
|
|
|
|
document_range_formatting_provider: Some(OneOf::Left(true)),
|
2022-03-03 23:42:26 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 18:27:08 +00:00
|
|
|
pub fn fake(
|
|
|
|
name: String,
|
2022-02-01 00:50:51 +00:00
|
|
|
capabilities: ServerCapabilities,
|
2022-04-01 04:52:14 +00:00
|
|
|
cx: AsyncAppContext,
|
2022-03-09 11:29:28 +00:00
|
|
|
) -> (Self, FakeLanguageServer) {
|
2022-02-11 23:08:56 +00:00
|
|
|
let (stdin_writer, stdin_reader) = async_pipe::pipe();
|
|
|
|
let (stdout_writer, stdout_reader) = async_pipe::pipe();
|
2022-04-01 04:52:14 +00:00
|
|
|
let (notifications_tx, notifications_rx) = channel::unbounded();
|
2021-10-26 01:04:27 +00:00
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
let server = Self::new_internal(
|
|
|
|
0,
|
|
|
|
stdin_writer,
|
|
|
|
stdout_reader,
|
|
|
|
Path::new("/"),
|
|
|
|
cx.clone(),
|
|
|
|
|_| {},
|
|
|
|
);
|
|
|
|
let fake = FakeLanguageServer {
|
|
|
|
server: Arc::new(Self::new_internal(
|
|
|
|
0,
|
|
|
|
stdout_writer,
|
|
|
|
stdin_reader,
|
|
|
|
Path::new("/"),
|
|
|
|
cx.clone(),
|
|
|
|
move |msg| {
|
|
|
|
notifications_tx
|
|
|
|
.try_send((msg.method.to_string(), msg.params.get().to_string()))
|
|
|
|
.ok();
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
notifications_rx,
|
|
|
|
};
|
2022-03-28 08:44:32 +00:00
|
|
|
fake.handle_request::<request::Initialize, _, _>({
|
2022-02-17 00:19:27 +00:00
|
|
|
let capabilities = capabilities.clone();
|
2022-03-28 08:44:32 +00:00
|
|
|
move |_, _| {
|
|
|
|
let capabilities = capabilities.clone();
|
2022-06-21 18:27:08 +00:00
|
|
|
let name = name.clone();
|
2022-03-28 08:44:32 +00:00
|
|
|
async move {
|
2022-04-01 04:52:14 +00:00
|
|
|
Ok(InitializeResult {
|
2022-03-28 08:44:32 +00:00
|
|
|
capabilities,
|
2022-06-21 18:27:08 +00:00
|
|
|
server_info: Some(ServerInfo {
|
|
|
|
name,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2022-03-28 08:44:32 +00:00
|
|
|
..Default::default()
|
2022-04-01 04:52:14 +00:00
|
|
|
})
|
2022-03-28 08:44:32 +00:00
|
|
|
}
|
2022-02-17 00:10:36 +00:00
|
|
|
}
|
2022-02-11 23:08:56 +00:00
|
|
|
});
|
2021-10-26 01:04:27 +00:00
|
|
|
|
2022-03-09 11:29:28 +00:00
|
|
|
(server, fake)
|
2021-10-25 22:28:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
impl FakeLanguageServer {
|
2022-04-01 04:52:14 +00:00
|
|
|
pub fn notify<T: notification::Notification>(&self, params: T::Params) {
|
|
|
|
self.server.notify::<T>(params).ok();
|
2021-10-25 22:28:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:44:43 +00:00
|
|
|
pub async fn request<T>(&self, params: T::Params) -> Result<T::Result>
|
|
|
|
where
|
|
|
|
T: request::Request,
|
|
|
|
T::Result: 'static + Send,
|
|
|
|
{
|
|
|
|
self.server.request::<T>(params).await
|
|
|
|
}
|
|
|
|
|
2022-02-11 23:08:56 +00:00
|
|
|
pub async fn receive_notification<T: notification::Notification>(&mut self) -> T::Params {
|
2022-05-10 01:05:10 +00:00
|
|
|
self.try_receive_notification::<T>().await.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn try_receive_notification<T: notification::Notification>(
|
|
|
|
&mut self,
|
|
|
|
) -> Option<T::Params> {
|
2022-02-11 23:08:56 +00:00
|
|
|
use futures::StreamExt as _;
|
2021-10-25 22:28:40 +00:00
|
|
|
|
2022-01-12 17:01:20 +00:00
|
|
|
loop {
|
2022-05-10 01:05:10 +00:00
|
|
|
let (method, params) = self.notifications_rx.next().await?;
|
2022-04-01 04:52:14 +00:00
|
|
|
if &method == T::METHOD {
|
2022-05-10 01:05:10 +00:00
|
|
|
return Some(serde_json::from_str::<T::Params>(¶ms).unwrap());
|
2022-01-12 17:01:20 +00:00
|
|
|
} else {
|
2022-04-01 04:52:14 +00:00
|
|
|
log::info!("skipping message in fake language server {:?}", params);
|
2022-01-12 17:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-25 22:28:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:44:32 +00:00
|
|
|
pub fn handle_request<T, F, Fut>(
|
2022-04-01 04:52:14 +00:00
|
|
|
&self,
|
2022-02-17 16:26:03 +00:00
|
|
|
mut handler: F,
|
|
|
|
) -> futures::channel::mpsc::UnboundedReceiver<()>
|
2022-02-11 23:08:56 +00:00
|
|
|
where
|
|
|
|
T: 'static + request::Request,
|
2022-04-01 04:52:14 +00:00
|
|
|
T::Params: 'static + Send,
|
2022-03-28 08:44:32 +00:00
|
|
|
F: 'static + Send + FnMut(T::Params, gpui::AsyncAppContext) -> Fut,
|
2022-04-01 04:52:14 +00:00
|
|
|
Fut: 'static + Send + Future<Output = Result<T::Result>>,
|
2022-02-11 23:08:56 +00:00
|
|
|
{
|
2022-02-17 16:26:03 +00:00
|
|
|
let (responded_tx, responded_rx) = futures::channel::mpsc::unbounded();
|
2022-04-01 04:52:14 +00:00
|
|
|
self.server.remove_request_handler::<T>();
|
|
|
|
self.server
|
|
|
|
.on_request::<T, _, _>(move |params, cx| {
|
|
|
|
let result = handler(params, cx.clone());
|
2022-03-28 08:44:32 +00:00
|
|
|
let responded_tx = responded_tx.clone();
|
|
|
|
async move {
|
2022-04-01 04:52:14 +00:00
|
|
|
cx.background().simulate_random_delay().await;
|
2022-03-28 08:44:32 +00:00
|
|
|
let result = result.await;
|
|
|
|
responded_tx.unbounded_send(()).ok();
|
2022-04-01 04:52:14 +00:00
|
|
|
result
|
2022-03-28 08:44:32 +00:00
|
|
|
}
|
2022-04-01 04:52:14 +00:00
|
|
|
})
|
|
|
|
.detach();
|
2022-02-11 23:08:56 +00:00
|
|
|
responded_rx
|
2021-10-25 22:28:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 00:19:27 +00:00
|
|
|
pub fn remove_request_handler<T>(&mut self)
|
|
|
|
where
|
|
|
|
T: 'static + request::Request,
|
|
|
|
{
|
2022-04-01 04:52:14 +00:00
|
|
|
self.server.remove_request_handler::<T>();
|
2022-02-17 00:19:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:44:43 +00:00
|
|
|
pub async fn start_progress(&self, token: impl Into<String>) {
|
|
|
|
let token = token.into();
|
|
|
|
self.request::<request::WorkDoneProgressCreate>(WorkDoneProgressCreateParams {
|
|
|
|
token: NumberOrString::String(token.clone()),
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-01-06 16:32:08 +00:00
|
|
|
self.notify::<notification::Progress>(ProgressParams {
|
2022-06-28 07:44:43 +00:00
|
|
|
token: NumberOrString::String(token),
|
2022-01-06 16:32:08 +00:00
|
|
|
value: ProgressParamsValue::WorkDone(WorkDoneProgress::Begin(Default::default())),
|
2022-03-09 11:29:28 +00:00
|
|
|
});
|
2022-01-06 16:32:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:44:43 +00:00
|
|
|
pub fn end_progress(&self, token: impl Into<String>) {
|
2022-01-06 16:32:08 +00:00
|
|
|
self.notify::<notification::Progress>(ProgressParams {
|
|
|
|
token: NumberOrString::String(token.into()),
|
|
|
|
value: ProgressParamsValue::WorkDone(WorkDoneProgress::End(Default::default())),
|
2022-03-09 11:29:28 +00:00
|
|
|
});
|
2022-01-06 16:32:08 +00:00
|
|
|
}
|
2021-10-25 22:28:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 23:02:04 +00:00
|
|
|
struct ClearResponseHandlers(Arc<Mutex<HashMap<usize, ResponseHandler>>>);
|
2022-03-01 21:36:49 +00:00
|
|
|
|
2022-03-01 23:02:04 +00:00
|
|
|
impl Drop for ClearResponseHandlers {
|
2022-03-01 21:36:49 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.0.lock().clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 17:27:10 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use gpui::TestAppContext;
|
|
|
|
|
2022-02-12 00:37:50 +00:00
|
|
|
#[ctor::ctor]
|
|
|
|
fn init_logger() {
|
|
|
|
if std::env::var("RUST_LOG").is_ok() {
|
|
|
|
env_logger::init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 22:28:40 +00:00
|
|
|
#[gpui::test]
|
2022-03-01 11:01:02 +00:00
|
|
|
async fn test_fake(cx: &mut TestAppContext) {
|
2022-06-21 18:27:08 +00:00
|
|
|
let (server, mut fake) =
|
|
|
|
LanguageServer::fake("the-lsp".to_string(), Default::default(), cx.to_async());
|
2021-10-25 22:28:40 +00:00
|
|
|
|
|
|
|
let (message_tx, message_rx) = channel::unbounded();
|
|
|
|
let (diagnostics_tx, diagnostics_rx) = channel::unbounded();
|
|
|
|
server
|
2022-04-01 04:52:14 +00:00
|
|
|
.on_notification::<notification::ShowMessage, _>(move |params, _| {
|
2021-10-25 22:28:40 +00:00
|
|
|
message_tx.try_send(params).unwrap()
|
|
|
|
})
|
|
|
|
.detach();
|
|
|
|
server
|
2022-04-01 04:52:14 +00:00
|
|
|
.on_notification::<notification::PublishDiagnostics, _>(move |params, _| {
|
2021-10-25 22:28:40 +00:00
|
|
|
diagnostics_tx.try_send(params).unwrap()
|
|
|
|
})
|
|
|
|
.detach();
|
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
let server = server.initialize(None).await.unwrap();
|
2021-10-25 22:28:40 +00:00
|
|
|
server
|
|
|
|
.notify::<notification::DidOpenTextDocument>(DidOpenTextDocumentParams {
|
|
|
|
text_document: TextDocumentItem::new(
|
|
|
|
Url::from_str("file://a/b").unwrap(),
|
|
|
|
"rust".to_string(),
|
|
|
|
0,
|
|
|
|
"".to_string(),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
fake.receive_notification::<notification::DidOpenTextDocument>()
|
|
|
|
.await
|
|
|
|
.text_document
|
|
|
|
.uri
|
|
|
|
.as_str(),
|
|
|
|
"file://a/b"
|
|
|
|
);
|
|
|
|
|
|
|
|
fake.notify::<notification::ShowMessage>(ShowMessageParams {
|
|
|
|
typ: MessageType::ERROR,
|
|
|
|
message: "ok".to_string(),
|
2022-03-09 11:29:28 +00:00
|
|
|
});
|
2021-10-25 22:28:40 +00:00
|
|
|
fake.notify::<notification::PublishDiagnostics>(PublishDiagnosticsParams {
|
|
|
|
uri: Url::from_str("file://b/c").unwrap(),
|
|
|
|
version: Some(5),
|
|
|
|
diagnostics: vec![],
|
2022-03-09 11:29:28 +00:00
|
|
|
});
|
2021-10-25 22:28:40 +00:00
|
|
|
assert_eq!(message_rx.recv().await.unwrap().message, "ok");
|
|
|
|
assert_eq!(
|
|
|
|
diagnostics_rx.recv().await.unwrap().uri.as_str(),
|
|
|
|
"file://b/c"
|
|
|
|
);
|
2021-11-01 18:57:21 +00:00
|
|
|
|
2022-04-01 04:52:14 +00:00
|
|
|
fake.handle_request::<request::Shutdown, _, _>(|_, _| async move { Ok(()) });
|
2022-02-11 23:08:56 +00:00
|
|
|
|
2021-11-01 18:57:21 +00:00
|
|
|
drop(server);
|
2022-01-20 11:10:01 +00:00
|
|
|
fake.receive_notification::<notification::Exit>().await;
|
2021-10-25 22:28:40 +00:00
|
|
|
}
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|