Get a basic end-to-end test for rust-analyzer integration working

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-10-25 18:11:52 +02:00
parent 9759f9e947
commit 580bad2042
9 changed files with 194 additions and 74 deletions

2
Cargo.lock generated
View file

@ -2956,12 +2956,14 @@ dependencies = [
"anyhow", "anyhow",
"futures", "futures",
"gpui", "gpui",
"log",
"lsp-types", "lsp-types",
"parking_lot", "parking_lot",
"postage", "postage",
"serde 1.0.125", "serde 1.0.125",
"serde_json 1.0.64", "serde_json 1.0.64",
"smol", "smol",
"unindent",
"util", "util",
] ]

View file

@ -8,6 +8,7 @@ gpui = { path = "../gpui" }
util = { path = "../util" } util = { path = "../util" }
anyhow = "1.0" anyhow = "1.0"
futures = "0.3" futures = "0.3"
log = "0.4"
lsp-types = "0.91" lsp-types = "0.91"
parking_lot = "0.11" parking_lot = "0.11"
postage = { version = "0.4.1", features = ["futures-traits"] } postage = { version = "0.4.1", features = ["futures-traits"] }
@ -17,3 +18,5 @@ smol = "1.2"
[dev-dependencies] [dev-dependencies]
gpui = { path = "../gpui", features = ["test-support"] } gpui = { path = "../gpui", features = ["test-support"] }
util = { path = "../util", features = ["test-support"] }
unindent = "0.1.7"

View file

@ -2,9 +2,9 @@ use std::env;
fn main() { fn main() {
let target = env::var("TARGET").unwrap(); let target = env::var("TARGET").unwrap();
println!("cargo:rustc-env=TARGET={}", target); println!("cargo:rustc-env=ZED_TARGET={}", target);
if let Ok(bundled) = env::var("BUNDLE") { if let Ok(bundled) = env::var("ZED_BUNDLE") {
println!("cargo:rustc-env=BUNDLE={}", bundled); println!("cargo:rustc-env=ZED_BUNDLE={}", bundled);
} }
} }

View file

@ -1,9 +1,9 @@
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use gpui::{executor, AppContext, Task}; use gpui::{executor, AppContext, Task};
use parking_lot::Mutex; use parking_lot::{Mutex, RwLock};
use postage::{barrier, prelude::Stream}; use postage::{barrier, oneshot, prelude::Stream, sink::Sink};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::value::RawValue; use serde_json::{json, value::RawValue};
use smol::{ use smol::{
channel, channel,
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader}, io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
@ -24,16 +24,23 @@ use util::TryFutureExt;
const JSON_RPC_VERSION: &'static str = "2.0"; const JSON_RPC_VERSION: &'static str = "2.0";
const CONTENT_LEN_HEADER: &'static str = "Content-Length: "; const CONTENT_LEN_HEADER: &'static str = "Content-Length: ";
type NotificationHandler = Box<dyn Send + Sync + Fn(&str)>;
type ResponseHandler = Box<dyn Send + FnOnce(Result<&str, Error>)>;
pub struct LanguageServer { pub struct LanguageServer {
next_id: AtomicUsize, next_id: AtomicUsize,
outbound_tx: channel::Sender<Vec<u8>>, outbound_tx: channel::Sender<Vec<u8>>,
notification_handlers: Arc<RwLock<HashMap<&'static str, NotificationHandler>>>,
response_handlers: Arc<Mutex<HashMap<usize, ResponseHandler>>>, response_handlers: Arc<Mutex<HashMap<usize, ResponseHandler>>>,
_input_task: Task<Option<()>>, _input_task: Task<Option<()>>,
_output_task: Task<Option<()>>, _output_task: Task<Option<()>>,
initialized: barrier::Receiver, initialized: barrier::Receiver,
} }
type ResponseHandler = Box<dyn Send + FnOnce(Result<&str, Error>)>; pub struct Subscription {
method: &'static str,
notification_handlers: Arc<RwLock<HashMap<&'static str, NotificationHandler>>>,
}
#[derive(Serialize)] #[derive(Serialize)]
struct Request<T> { struct Request<T> {
@ -48,8 +55,8 @@ struct Response<'a> {
id: usize, id: usize,
#[serde(default)] #[serde(default)]
error: Option<Error>, error: Option<Error>,
#[serde(default, borrow)] #[serde(borrow)]
result: Option<&'a RawValue>, result: &'a RawValue,
} }
#[derive(Serialize)] #[derive(Serialize)]
@ -67,29 +74,33 @@ struct InboundNotification<'a> {
params: &'a RawValue, params: &'a RawValue,
} }
#[derive(Deserialize)] #[derive(Debug, Deserialize)]
struct Error { struct Error {
message: String, message: String,
} }
impl LanguageServer { impl LanguageServer {
pub fn rust(cx: &AppContext) -> Result<Arc<Self>> { pub fn rust(root_path: &Path, cx: &AppContext) -> Result<Arc<Self>> {
const BUNDLE: Option<&'static str> = option_env!("BUNDLE"); const ZED_BUNDLE: Option<&'static str> = option_env!("ZED_BUNDLE");
const TARGET: &'static str = env!("TARGET"); const ZED_TARGET: &'static str = env!("ZED_TARGET");
let rust_analyzer_name = format!("rust-analyzer-{}", TARGET); let rust_analyzer_name = format!("rust-analyzer-{}", ZED_TARGET);
if BUNDLE.map_or(Ok(false), |b| b.parse())? { if ZED_BUNDLE.map_or(Ok(false), |b| b.parse())? {
let rust_analyzer_path = cx let rust_analyzer_path = cx
.platform() .platform()
.path_for_resource(Some(&rust_analyzer_name), None)?; .path_for_resource(Some(&rust_analyzer_name), None)?;
Self::new(&rust_analyzer_path, cx.background()) Self::new(root_path, &rust_analyzer_path, cx.background())
} else { } else {
Self::new(Path::new(&rust_analyzer_name), cx.background()) Self::new(root_path, Path::new(&rust_analyzer_name), cx.background())
} }
} }
pub fn new(path: &Path, background: &executor::Background) -> Result<Arc<Self>> { pub fn new(
let mut server = Command::new(path) root_path: &Path,
server_path: &Path,
background: &executor::Background,
) -> Result<Arc<Self>> {
let mut server = Command::new(server_path)
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
@ -97,9 +108,11 @@ impl LanguageServer {
let mut stdin = server.stdin.take().unwrap(); let mut stdin = server.stdin.take().unwrap();
let mut stdout = BufReader::new(server.stdout.take().unwrap()); let mut stdout = BufReader::new(server.stdout.take().unwrap());
let (outbound_tx, outbound_rx) = channel::unbounded::<Vec<u8>>(); let (outbound_tx, outbound_rx) = channel::unbounded::<Vec<u8>>();
let response_handlers = Arc::new(Mutex::new(HashMap::<usize, ResponseHandler>::new())); let notification_handlers = Arc::new(RwLock::new(HashMap::<_, NotificationHandler>::new()));
let response_handlers = Arc::new(Mutex::new(HashMap::<_, ResponseHandler>::new()));
let _input_task = background.spawn( let _input_task = background.spawn(
{ {
let notification_handlers = notification_handlers.clone();
let response_handlers = response_handlers.clone(); let response_handlers = response_handlers.clone();
async move { async move {
let mut buffer = Vec::new(); let mut buffer = Vec::new();
@ -116,15 +129,21 @@ impl LanguageServer {
buffer.resize(message_len, 0); buffer.resize(message_len, 0);
stdout.read_exact(&mut buffer).await?; stdout.read_exact(&mut buffer).await?;
if let Ok(InboundNotification { .. }) = serde_json::from_slice(&buffer) {
if let Ok(InboundNotification { method, params }) =
serde_json::from_slice(&buffer)
{
if let Some(handler) = notification_handlers.read().get(method) {
handler(params.get());
}
} else if let Ok(Response { id, error, result }) = } else if let Ok(Response { id, error, result }) =
serde_json::from_slice(&buffer) serde_json::from_slice(&buffer)
{ {
if let Some(handler) = response_handlers.lock().remove(&id) { if let Some(handler) = response_handlers.lock().remove(&id) {
if let Some(result) = result { if let Some(error) = error {
handler(Ok(result.get()));
} else if let Some(error) = error {
handler(Err(error)); handler(Err(error));
} else {
handler(Ok(result.get()));
} }
} }
} else { } else {
@ -142,6 +161,8 @@ impl LanguageServer {
async move { async move {
let mut content_len_buffer = Vec::new(); let mut content_len_buffer = Vec::new();
loop { loop {
content_len_buffer.clear();
let message = outbound_rx.recv().await?; let message = outbound_rx.recv().await?;
write!(content_len_buffer, "{}", message.len()).unwrap(); write!(content_len_buffer, "{}", message.len()).unwrap();
stdin.write_all(CONTENT_LEN_HEADER.as_bytes()).await?; stdin.write_all(CONTENT_LEN_HEADER.as_bytes()).await?;
@ -155,6 +176,7 @@ impl LanguageServer {
let (initialized_tx, initialized_rx) = barrier::channel(); let (initialized_tx, initialized_rx) = barrier::channel();
let this = Arc::new(Self { let this = Arc::new(Self {
notification_handlers,
response_handlers, response_handlers,
next_id: Default::default(), next_id: Default::default(),
outbound_tx, outbound_tx,
@ -163,11 +185,13 @@ impl LanguageServer {
initialized: initialized_rx, initialized: initialized_rx,
}); });
let root_uri =
lsp_types::Url::from_file_path(root_path).map_err(|_| anyhow!("invalid root path"))?;
background background
.spawn({ .spawn({
let this = this.clone(); let this = this.clone();
async move { async move {
this.init().log_err().await; this.init(root_uri).log_err().await;
drop(initialized_tx); drop(initialized_tx);
} }
}) })
@ -176,45 +200,74 @@ impl LanguageServer {
Ok(this) Ok(this)
} }
async fn init(self: Arc<Self>) -> Result<()> { async fn init(self: Arc<Self>, root_uri: lsp_types::Url) -> Result<()> {
let res = self self.request_internal::<lsp_types::request::Initialize>(lsp_types::InitializeParams {
.request_internal::<lsp_types::request::Initialize>( process_id: Default::default(),
lsp_types::InitializeParams { root_path: Default::default(),
process_id: Default::default(), root_uri: Some(root_uri),
root_path: Default::default(), initialization_options: Default::default(),
root_uri: Default::default(), capabilities: lsp_types::ClientCapabilities {
initialization_options: Default::default(), experimental: Some(json!({
capabilities: Default::default(), "serverStatusNotification": true,
trace: Default::default(), })),
workspace_folders: Default::default(), ..Default::default()
client_info: Default::default(), },
locale: Default::default(), trace: Default::default(),
}, workspace_folders: Default::default(),
false, client_info: Default::default(),
) locale: Default::default(),
.await?; })
.await?;
self.notify_internal::<lsp_types::notification::Initialized>( self.notify_internal::<lsp_types::notification::Initialized>(
lsp_types::InitializedParams {}, lsp_types::InitializedParams {},
false,
) )
.await?; .await?;
Ok(()) Ok(())
} }
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(),
}
}
pub fn request<T: lsp_types::request::Request>( pub fn request<T: lsp_types::request::Request>(
self: &Arc<Self>, self: Arc<Self>,
params: T::Params, params: T::Params,
) -> impl Future<Output = Result<T::Result>> ) -> impl Future<Output = Result<T::Result>>
where where
T::Result: 'static + Send, T::Result: 'static + Send,
{ {
self.request_internal::<T>(params, true) let this = self.clone();
async move {
this.initialized.clone().recv().await;
this.request_internal::<T>(params).await
}
} }
fn request_internal<T: lsp_types::request::Request>( fn request_internal<T: lsp_types::request::Request>(
self: &Arc<Self>, self: &Arc<Self>,
params: T::Params, params: T::Params,
wait_for_initialization: bool,
) -> impl Future<Output = Result<T::Result>> ) -> impl Future<Output = Result<T::Result>>
where where
T::Result: 'static + Send, T::Result: 'static + Send,
@ -228,7 +281,7 @@ impl LanguageServer {
}) })
.unwrap(); .unwrap();
let mut response_handlers = self.response_handlers.lock(); let mut response_handlers = self.response_handlers.lock();
let (tx, rx) = smol::channel::bounded(1); let (mut tx, mut rx) = oneshot::channel();
response_handlers.insert( response_handlers.insert(
id, id,
Box::new(move |result| { Box::new(move |result| {
@ -238,17 +291,14 @@ impl LanguageServer {
} }
Err(error) => Err(anyhow!("{}", error.message)), Err(error) => Err(anyhow!("{}", error.message)),
}; };
let _ = smol::block_on(tx.send(response)); let _ = tx.try_send(response);
}), }),
); );
let this = self.clone(); let this = self.clone();
async move { async move {
if wait_for_initialization {
this.initialized.clone().recv().await;
}
this.outbound_tx.send(message).await?; this.outbound_tx.send(message).await?;
rx.recv().await? rx.recv().await.unwrap()
} }
} }
@ -256,13 +306,16 @@ impl LanguageServer {
self: &Arc<Self>, self: &Arc<Self>,
params: T::Params, params: T::Params,
) -> impl Future<Output = Result<()>> { ) -> impl Future<Output = Result<()>> {
self.notify_internal::<T>(params, true) let this = self.clone();
async move {
this.initialized.clone().recv().await;
this.notify_internal::<T>(params).await
}
} }
fn notify_internal<T: lsp_types::notification::Notification>( fn notify_internal<T: lsp_types::notification::Notification>(
self: &Arc<Self>, self: &Arc<Self>,
params: T::Params, params: T::Params,
wait_for_initialization: bool,
) -> impl Future<Output = Result<()>> { ) -> impl Future<Output = Result<()>> {
let message = serde_json::to_vec(&OutboundNotification { let message = serde_json::to_vec(&OutboundNotification {
jsonrpc: JSON_RPC_VERSION, jsonrpc: JSON_RPC_VERSION,
@ -273,22 +326,90 @@ impl LanguageServer {
let this = self.clone(); let this = self.clone();
async move { async move {
if wait_for_initialization {
this.initialized.clone().recv().await;
}
this.outbound_tx.send(message).await?; this.outbound_tx.send(message).await?;
Ok(()) Ok(())
} }
} }
} }
impl Drop for Subscription {
fn drop(&mut self) {
self.notification_handlers.write().remove(self.method);
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use gpui::TestAppContext; use gpui::TestAppContext;
use unindent::Unindent;
use util::test::temp_tree;
#[gpui::test] #[gpui::test]
async fn test_basic(cx: TestAppContext) { async fn test_basic(cx: TestAppContext) {
let server = cx.read(|cx| LanguageServer::rust(cx).unwrap()); 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,
} }
} }

View file

@ -7,8 +7,7 @@ use buffer::LanguageRegistry;
use client::Client; use client::Client;
use futures::Future; use futures::Future;
use fuzzy::{PathMatch, PathMatchCandidate, PathMatchCandidateSet}; use fuzzy::{PathMatch, PathMatchCandidate, PathMatchCandidateSet};
use gpui::{executor, AppContext, Entity, ModelContext, ModelHandle, Task}; use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task};
use lsp::LanguageServer;
use std::{ use std::{
path::Path, path::Path,
sync::{atomic::AtomicBool, Arc}, sync::{atomic::AtomicBool, Arc},
@ -24,7 +23,6 @@ pub struct Project {
languages: Arc<LanguageRegistry>, languages: Arc<LanguageRegistry>,
client: Arc<client::Client>, client: Arc<client::Client>,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
language_server: Arc<LanguageServer>,
} }
pub enum Event { pub enum Event {
@ -45,19 +43,13 @@ pub struct ProjectEntry {
} }
impl Project { impl Project {
pub fn new( pub fn new(languages: Arc<LanguageRegistry>, rpc: Arc<Client>, fs: Arc<dyn Fs>) -> Self {
languages: Arc<LanguageRegistry>,
rpc: Arc<Client>,
fs: Arc<dyn Fs>,
cx: &AppContext,
) -> Self {
Self { Self {
worktrees: Default::default(), worktrees: Default::default(),
active_entry: None, active_entry: None,
languages, languages,
client: rpc, client: rpc,
fs, fs,
language_server: LanguageServer::rust(cx).unwrap(),
} }
} }
@ -416,6 +408,6 @@ mod tests {
let languages = Arc::new(LanguageRegistry::new()); let languages = Arc::new(LanguageRegistry::new());
let fs = Arc::new(RealFs); let fs = Arc::new(RealFs);
let rpc = client::Client::new(); let rpc = client::Client::new();
cx.add_model(|cx| Project::new(languages, rpc, fs, cx)) cx.add_model(|_| Project::new(languages, rpc, fs))
} }
} }

View file

@ -14,6 +14,7 @@ use gpui::{
Task, UpgradeModelHandle, WeakModelHandle, Task, UpgradeModelHandle, WeakModelHandle,
}; };
use lazy_static::lazy_static; use lazy_static::lazy_static;
use lsp::LanguageServer;
use parking_lot::Mutex; use parking_lot::Mutex;
use postage::{ use postage::{
prelude::{Sink as _, Stream as _}, prelude::{Sink as _, Stream as _},
@ -684,6 +685,7 @@ pub struct LocalWorktree {
queued_operations: Vec<(u64, Operation)>, queued_operations: Vec<(u64, Operation)>,
rpc: Arc<Client>, rpc: Arc<Client>,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
language_server: Arc<LanguageServer>,
} }
#[derive(Default, Deserialize)] #[derive(Default, Deserialize)]
@ -721,6 +723,7 @@ impl LocalWorktree {
let (scan_states_tx, scan_states_rx) = smol::channel::unbounded(); let (scan_states_tx, scan_states_rx) = smol::channel::unbounded();
let (mut last_scan_state_tx, last_scan_state_rx) = watch::channel_with(ScanState::Scanning); let (mut last_scan_state_tx, last_scan_state_rx) = watch::channel_with(ScanState::Scanning);
let tree = cx.add_model(move |cx: &mut ModelContext<Worktree>| { let tree = cx.add_model(move |cx: &mut ModelContext<Worktree>| {
let language_server = LanguageServer::rust(&abs_path, cx).unwrap();
let mut snapshot = Snapshot { let mut snapshot = Snapshot {
id: cx.model_id(), id: cx.model_id(),
scan_id: 0, scan_id: 0,
@ -796,6 +799,7 @@ impl LocalWorktree {
languages, languages,
rpc, rpc,
fs, fs,
language_server,
}; };
cx.spawn_weak(|this, mut cx| async move { cx.spawn_weak(|this, mut cx| async move {

View file

@ -617,12 +617,11 @@ mod tests {
) )
.await; .await;
let project = cx.add_model(|cx| { let project = cx.add_model(|_| {
Project::new( Project::new(
params.languages.clone(), params.languages.clone(),
params.client.clone(), params.client.clone(),
params.fs.clone(), params.fs.clone(),
cx,
) )
}); });
let root1 = project let root1 = project

View file

@ -322,12 +322,11 @@ pub struct Workspace {
impl Workspace { impl Workspace {
pub fn new(params: &WorkspaceParams, cx: &mut ViewContext<Self>) -> Self { pub fn new(params: &WorkspaceParams, cx: &mut ViewContext<Self>) -> Self {
let project = cx.add_model(|cx| { let project = cx.add_model(|_| {
Project::new( Project::new(
params.languages.clone(), params.languages.clone(),
params.client.clone(), params.client.clone(),
params.fs.clone(), params.fs.clone(),
cx,
) )
}); });
cx.observe(&project, |_, _, cx| cx.notify()).detach(); cx.observe(&project, |_, _, cx| cx.notify()).detach();

View file

@ -2,7 +2,7 @@
set -e set -e
export BUNDLE=true export ZED_BUNDLE=true
# Install cargo-bundle 0.5.0 if it's not already installed # Install cargo-bundle 0.5.0 if it's not already installed
cargo install cargo-bundle --version 0.5.0 cargo install cargo-bundle --version 0.5.0