2021-10-21 14:26:37 +00:00
|
|
|
use anyhow::{anyhow, Context, Result};
|
2021-10-25 09:02:35 +00:00
|
|
|
use gpui::{executor, AppContext, Task};
|
2021-10-25 16:11:52 +00:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
|
|
|
use postage::{barrier, oneshot, prelude::Stream, sink::Sink};
|
2021-10-21 14:26:37 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-10-25 16:11:52 +00:00
|
|
|
use serde_json::{json, value::RawValue};
|
2021-10-21 14:26:37 +00:00
|
|
|
use smol::{
|
|
|
|
channel,
|
|
|
|
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
|
|
|
|
process::Command,
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
future::Future,
|
|
|
|
io::Write,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicUsize, Ordering::SeqCst},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use std::{path::Path, process::Stdio};
|
|
|
|
use util::TryFutureExt;
|
|
|
|
|
|
|
|
const JSON_RPC_VERSION: &'static str = "2.0";
|
|
|
|
const CONTENT_LEN_HEADER: &'static str = "Content-Length: ";
|
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
type NotificationHandler = Box<dyn Send + Sync + Fn(&str)>;
|
|
|
|
type ResponseHandler = Box<dyn Send + FnOnce(Result<&str, Error>)>;
|
|
|
|
|
2021-10-21 14:26:37 +00:00
|
|
|
pub struct LanguageServer {
|
|
|
|
next_id: AtomicUsize,
|
|
|
|
outbound_tx: channel::Sender<Vec<u8>>,
|
2021-10-25 16:11:52 +00:00
|
|
|
notification_handlers: Arc<RwLock<HashMap<&'static str, NotificationHandler>>>,
|
2021-10-21 14:26:37 +00:00
|
|
|
response_handlers: Arc<Mutex<HashMap<usize, ResponseHandler>>>,
|
|
|
|
_input_task: Task<Option<()>>,
|
|
|
|
_output_task: Task<Option<()>>,
|
2021-10-25 10:29:28 +00:00
|
|
|
initialized: barrier::Receiver,
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
pub struct Subscription {
|
|
|
|
method: &'static str,
|
|
|
|
notification_handlers: Arc<RwLock<HashMap<&'static str, NotificationHandler>>>,
|
|
|
|
}
|
2021-10-21 14:26:37 +00:00
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct Request<T> {
|
|
|
|
jsonrpc: &'static str,
|
|
|
|
id: usize,
|
|
|
|
method: &'static str,
|
|
|
|
params: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2021-10-21 17:27:10 +00:00
|
|
|
struct Response<'a> {
|
|
|
|
id: usize,
|
|
|
|
#[serde(default)]
|
|
|
|
error: Option<Error>,
|
2021-10-25 16:11:52 +00:00
|
|
|
#[serde(borrow)]
|
|
|
|
result: &'a RawValue,
|
2021-10-21 17:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct OutboundNotification<T> {
|
|
|
|
jsonrpc: &'static str,
|
|
|
|
method: &'static str,
|
|
|
|
params: T,
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2021-10-21 17:27:10 +00:00
|
|
|
struct InboundNotification<'a> {
|
|
|
|
#[serde(borrow)]
|
|
|
|
method: &'a str,
|
2021-10-21 14:26:37 +00:00
|
|
|
#[serde(borrow)]
|
|
|
|
params: &'a RawValue,
|
|
|
|
}
|
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
2021-10-21 17:27:10 +00:00
|
|
|
struct Error {
|
|
|
|
message: String,
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LanguageServer {
|
2021-10-25 16:11:52 +00:00
|
|
|
pub fn rust(root_path: &Path, cx: &AppContext) -> Result<Arc<Self>> {
|
|
|
|
const ZED_BUNDLE: Option<&'static str> = option_env!("ZED_BUNDLE");
|
|
|
|
const ZED_TARGET: &'static str = env!("ZED_TARGET");
|
2021-10-25 09:02:35 +00:00
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
let rust_analyzer_name = format!("rust-analyzer-{}", ZED_TARGET);
|
|
|
|
if ZED_BUNDLE.map_or(Ok(false), |b| b.parse())? {
|
2021-10-25 09:02:35 +00:00
|
|
|
let rust_analyzer_path = cx
|
|
|
|
.platform()
|
|
|
|
.path_for_resource(Some(&rust_analyzer_name), None)?;
|
2021-10-25 16:11:52 +00:00
|
|
|
Self::new(root_path, &rust_analyzer_path, cx.background())
|
2021-10-25 09:02:35 +00:00
|
|
|
} else {
|
2021-10-25 16:11:52 +00:00
|
|
|
Self::new(root_path, Path::new(&rust_analyzer_name), cx.background())
|
2021-10-25 09:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
pub fn new(
|
|
|
|
root_path: &Path,
|
|
|
|
server_path: &Path,
|
|
|
|
background: &executor::Background,
|
|
|
|
) -> Result<Arc<Self>> {
|
|
|
|
let mut server = Command::new(server_path)
|
2021-10-21 14:26:37 +00:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::inherit())
|
|
|
|
.spawn()?;
|
|
|
|
let mut stdin = server.stdin.take().unwrap();
|
|
|
|
let mut stdout = BufReader::new(server.stdout.take().unwrap());
|
|
|
|
let (outbound_tx, outbound_rx) = channel::unbounded::<Vec<u8>>();
|
2021-10-25 16:11:52 +00:00
|
|
|
let notification_handlers = Arc::new(RwLock::new(HashMap::<_, NotificationHandler>::new()));
|
|
|
|
let response_handlers = Arc::new(Mutex::new(HashMap::<_, ResponseHandler>::new()));
|
2021-10-21 14:26:37 +00:00
|
|
|
let _input_task = background.spawn(
|
|
|
|
{
|
2021-10-25 16:11:52 +00:00
|
|
|
let notification_handlers = notification_handlers.clone();
|
2021-10-21 14:26:37 +00:00
|
|
|
let response_handlers = response_handlers.clone();
|
|
|
|
async move {
|
|
|
|
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?;
|
2021-10-25 16:11:52 +00:00
|
|
|
|
|
|
|
if let Ok(InboundNotification { method, params }) =
|
|
|
|
serde_json::from_slice(&buffer)
|
|
|
|
{
|
|
|
|
if let Some(handler) = notification_handlers.read().get(method) {
|
|
|
|
handler(params.get());
|
|
|
|
}
|
2021-10-21 14:26:37 +00:00
|
|
|
} else if let Ok(Response { id, error, result }) =
|
|
|
|
serde_json::from_slice(&buffer)
|
|
|
|
{
|
|
|
|
if let Some(handler) = response_handlers.lock().remove(&id) {
|
2021-10-25 16:11:52 +00:00
|
|
|
if let Some(error) = error {
|
2021-10-21 14:26:37 +00:00
|
|
|
handler(Err(error));
|
2021-10-25 16:11:52 +00:00
|
|
|
} else {
|
|
|
|
handler(Ok(result.get()));
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(anyhow!(
|
|
|
|
"failed to deserialize message:\n{}",
|
|
|
|
std::str::from_utf8(&buffer)?
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.log_err(),
|
|
|
|
);
|
|
|
|
let _output_task = background.spawn(
|
|
|
|
async move {
|
|
|
|
let mut content_len_buffer = Vec::new();
|
|
|
|
loop {
|
2021-10-25 16:11:52 +00:00
|
|
|
content_len_buffer.clear();
|
|
|
|
|
2021-10-21 14:26:37 +00:00
|
|
|
let message = outbound_rx.recv().await?;
|
|
|
|
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?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.log_err(),
|
|
|
|
);
|
|
|
|
|
2021-10-25 10:29:28 +00:00
|
|
|
let (initialized_tx, initialized_rx) = barrier::channel();
|
2021-10-21 14:26:37 +00:00
|
|
|
let this = Arc::new(Self {
|
2021-10-25 16:11:52 +00:00
|
|
|
notification_handlers,
|
2021-10-21 14:26:37 +00:00
|
|
|
response_handlers,
|
|
|
|
next_id: Default::default(),
|
|
|
|
outbound_tx,
|
|
|
|
_input_task,
|
|
|
|
_output_task,
|
2021-10-25 10:29:28 +00:00
|
|
|
initialized: initialized_rx,
|
2021-10-21 14:26:37 +00:00
|
|
|
});
|
2021-10-25 10:29:28 +00:00
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
let root_uri =
|
|
|
|
lsp_types::Url::from_file_path(root_path).map_err(|_| anyhow!("invalid root path"))?;
|
2021-10-25 10:29:28 +00:00
|
|
|
background
|
|
|
|
.spawn({
|
|
|
|
let this = this.clone();
|
|
|
|
async move {
|
2021-10-25 16:11:52 +00:00
|
|
|
this.init(root_uri).log_err().await;
|
2021-10-25 10:29:28 +00:00
|
|
|
drop(initialized_tx);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.detach();
|
2021-10-21 14:26:37 +00:00
|
|
|
|
|
|
|
Ok(this)
|
|
|
|
}
|
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
async fn init(self: Arc<Self>, root_uri: lsp_types::Url) -> Result<()> {
|
|
|
|
self.request_internal::<lsp_types::request::Initialize>(lsp_types::InitializeParams {
|
|
|
|
process_id: Default::default(),
|
|
|
|
root_path: Default::default(),
|
|
|
|
root_uri: Some(root_uri),
|
|
|
|
initialization_options: Default::default(),
|
|
|
|
capabilities: lsp_types::ClientCapabilities {
|
|
|
|
experimental: Some(json!({
|
|
|
|
"serverStatusNotification": true,
|
|
|
|
})),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
trace: Default::default(),
|
|
|
|
workspace_folders: Default::default(),
|
|
|
|
client_info: Default::default(),
|
|
|
|
locale: Default::default(),
|
|
|
|
})
|
|
|
|
.await?;
|
2021-10-25 10:29:28 +00:00
|
|
|
self.notify_internal::<lsp_types::notification::Initialized>(
|
|
|
|
lsp_types::InitializedParams {},
|
|
|
|
)
|
2021-10-21 17:27:10 +00:00
|
|
|
.await?;
|
2021-10-21 14:26:37 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
pub fn on_notification<T, F>(&self, f: F) -> Subscription
|
|
|
|
where
|
|
|
|
T: lsp_types::notification::Notification,
|
|
|
|
F: 'static + Send + Sync + Fn(T::Params),
|
|
|
|
{
|
|
|
|
let prev_handler = self.notification_handlers.write().insert(
|
|
|
|
T::METHOD,
|
|
|
|
Box::new(
|
|
|
|
move |notification| match serde_json::from_str(notification) {
|
|
|
|
Ok(notification) => f(notification),
|
|
|
|
Err(err) => log::error!("error parsing notification {}: {}", T::METHOD, err),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
prev_handler.is_none(),
|
|
|
|
"registered multiple handlers for the same notification"
|
|
|
|
);
|
|
|
|
|
|
|
|
Subscription {
|
|
|
|
method: T::METHOD,
|
|
|
|
notification_handlers: self.notification_handlers.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:26:37 +00:00
|
|
|
pub fn request<T: lsp_types::request::Request>(
|
2021-10-25 16:11:52 +00:00
|
|
|
self: Arc<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,
|
|
|
|
{
|
2021-10-25 16:11:52 +00:00
|
|
|
let this = self.clone();
|
|
|
|
async move {
|
|
|
|
this.initialized.clone().recv().await;
|
|
|
|
this.request_internal::<T>(params).await
|
|
|
|
}
|
2021-10-25 10:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn request_internal<T: lsp_types::request::Request>(
|
|
|
|
self: &Arc<Self>,
|
|
|
|
params: T::Params,
|
|
|
|
) -> impl Future<Output = Result<T::Result>>
|
2021-10-21 14:26:37 +00:00
|
|
|
where
|
|
|
|
T::Result: 'static + Send,
|
|
|
|
{
|
|
|
|
let id = self.next_id.fetch_add(1, SeqCst);
|
|
|
|
let message = serde_json::to_vec(&Request {
|
|
|
|
jsonrpc: JSON_RPC_VERSION,
|
|
|
|
id,
|
|
|
|
method: T::METHOD,
|
|
|
|
params,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let mut response_handlers = self.response_handlers.lock();
|
2021-10-25 16:11:52 +00:00
|
|
|
let (mut tx, mut rx) = oneshot::channel();
|
2021-10-21 14:26:37 +00:00
|
|
|
response_handlers.insert(
|
|
|
|
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)),
|
|
|
|
};
|
2021-10-25 16:11:52 +00:00
|
|
|
let _ = tx.try_send(response);
|
2021-10-21 14:26:37 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2021-10-25 10:29:28 +00:00
|
|
|
let this = self.clone();
|
2021-10-21 14:26:37 +00:00
|
|
|
async move {
|
2021-10-25 10:29:28 +00:00
|
|
|
this.outbound_tx.send(message).await?;
|
2021-10-25 16:11:52 +00:00
|
|
|
rx.recv().await.unwrap()
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-21 17:27:10 +00:00
|
|
|
|
|
|
|
pub fn notify<T: lsp_types::notification::Notification>(
|
2021-10-25 10:29:28 +00:00
|
|
|
self: &Arc<Self>,
|
|
|
|
params: T::Params,
|
|
|
|
) -> impl Future<Output = Result<()>> {
|
2021-10-25 16:11:52 +00:00
|
|
|
let this = self.clone();
|
|
|
|
async move {
|
|
|
|
this.initialized.clone().recv().await;
|
|
|
|
this.notify_internal::<T>(params).await
|
|
|
|
}
|
2021-10-25 10:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn notify_internal<T: lsp_types::notification::Notification>(
|
|
|
|
self: &Arc<Self>,
|
2021-10-21 17:27:10 +00:00
|
|
|
params: T::Params,
|
2021-10-25 10:29:28 +00:00
|
|
|
) -> impl Future<Output = Result<()>> {
|
2021-10-21 17:27:10 +00:00
|
|
|
let message = serde_json::to_vec(&OutboundNotification {
|
|
|
|
jsonrpc: JSON_RPC_VERSION,
|
|
|
|
method: T::METHOD,
|
|
|
|
params,
|
|
|
|
})
|
|
|
|
.unwrap();
|
2021-10-25 10:29:28 +00:00
|
|
|
|
|
|
|
let this = self.clone();
|
|
|
|
async move {
|
|
|
|
this.outbound_tx.send(message).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-21 17:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 16:11:52 +00:00
|
|
|
impl Drop for Subscription {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.notification_handlers.write().remove(self.method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 17:27:10 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use gpui::TestAppContext;
|
2021-10-25 16:11:52 +00:00
|
|
|
use unindent::Unindent;
|
|
|
|
use util::test::temp_tree;
|
2021-10-21 17:27:10 +00:00
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_basic(cx: TestAppContext) {
|
2021-10-25 16:11:52 +00:00
|
|
|
let root_dir = temp_tree(json!({
|
|
|
|
"Cargo.toml": r#"
|
|
|
|
[package]
|
|
|
|
name = "temp"
|
|
|
|
version = "0.1.0"
|
|
|
|
edition = "2018"
|
|
|
|
"#.unindent(),
|
|
|
|
"src": {
|
|
|
|
"lib.rs": r#"
|
|
|
|
fn fun() {
|
|
|
|
let hello = "world";
|
|
|
|
}
|
|
|
|
"#.unindent()
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
let server = cx.read(|cx| LanguageServer::rust(root_dir.path(), cx).unwrap());
|
|
|
|
server.next_idle_notification().await;
|
|
|
|
|
|
|
|
let hover = server
|
|
|
|
.request::<lsp_types::request::HoverRequest>(lsp_types::HoverParams {
|
|
|
|
text_document_position_params: lsp_types::TextDocumentPositionParams {
|
|
|
|
text_document: lsp_types::TextDocumentIdentifier::new(
|
|
|
|
lsp_types::Url::from_file_path(root_dir.path().join("src/lib.rs")).unwrap(),
|
|
|
|
),
|
|
|
|
position: lsp_types::Position::new(1, 21),
|
|
|
|
},
|
|
|
|
work_done_progress_params: Default::default(),
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
hover.contents,
|
|
|
|
lsp_types::HoverContents::Markup(lsp_types::MarkupContent {
|
|
|
|
kind: lsp_types::MarkupKind::Markdown,
|
|
|
|
value: "&str".to_string()
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LanguageServer {
|
|
|
|
async fn next_idle_notification(self: &Arc<Self>) {
|
|
|
|
let (tx, rx) = channel::unbounded();
|
|
|
|
let _subscription =
|
|
|
|
self.on_notification::<ServerStatusNotification, _>(move |params| {
|
|
|
|
if params.quiescent {
|
|
|
|
tx.try_send(()).unwrap();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let _ = rx.recv().await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ServerStatusNotification {}
|
|
|
|
|
|
|
|
impl lsp_types::notification::Notification for ServerStatusNotification {
|
|
|
|
type Params = ServerStatusParams;
|
|
|
|
const METHOD: &'static str = "experimental/serverStatus";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, PartialEq, Eq, Clone)]
|
|
|
|
pub struct ServerStatusParams {
|
|
|
|
pub quiescent: bool,
|
2021-10-21 17:27:10 +00:00
|
|
|
}
|
2021-10-21 14:26:37 +00:00
|
|
|
}
|