Advertise link capability in LSP

This commit is contained in:
Antonio Scandurra 2022-01-20 12:10:01 +01:00
parent 6b1f989c2b
commit 11a83d01c2

View file

@ -205,8 +205,7 @@ impl LanguageServer {
output_done_rx: Mutex::new(Some(output_done_rx)), output_done_rx: Mutex::new(Some(output_done_rx)),
}); });
let root_uri = let root_uri = Url::from_file_path(root_path).map_err(|_| anyhow!("invalid root path"))?;
lsp_types::Url::from_file_path(root_path).map_err(|_| anyhow!("invalid root path"))?;
executor executor
.spawn({ .spawn({
let this = this.clone(); let this = this.clone();
@ -220,18 +219,25 @@ impl LanguageServer {
Ok(this) Ok(this)
} }
async fn init(self: Arc<Self>, root_uri: lsp_types::Url) -> Result<()> { async fn init(self: Arc<Self>, root_uri: Url) -> Result<()> {
#[allow(deprecated)] #[allow(deprecated)]
let params = lsp_types::InitializeParams { let params = InitializeParams {
process_id: Default::default(), process_id: Default::default(),
root_path: Default::default(), root_path: Default::default(),
root_uri: Some(root_uri), root_uri: Some(root_uri),
initialization_options: Default::default(), initialization_options: Default::default(),
capabilities: lsp_types::ClientCapabilities { capabilities: ClientCapabilities {
text_document: Some(TextDocumentClientCapabilities {
definition: Some(GotoCapability {
link_support: Some(true),
..Default::default()
}),
..Default::default()
}),
experimental: Some(json!({ experimental: Some(json!({
"serverStatusNotification": true, "serverStatusNotification": true,
})), })),
window: Some(lsp_types::WindowClientCapabilities { window: Some(WindowClientCapabilities {
work_done_progress: Some(true), work_done_progress: Some(true),
..Default::default() ..Default::default()
}), }),
@ -244,16 +250,16 @@ impl LanguageServer {
}; };
let this = self.clone(); let this = self.clone();
let request = Self::request_internal::<lsp_types::request::Initialize>( let request = Self::request_internal::<request::Initialize>(
&this.next_id, &this.next_id,
&this.response_handlers, &this.response_handlers,
this.outbound_tx.read().as_ref(), this.outbound_tx.read().as_ref(),
params, params,
); );
request.await?; request.await?;
Self::notify_internal::<lsp_types::notification::Initialized>( Self::notify_internal::<notification::Initialized>(
this.outbound_tx.read().as_ref(), this.outbound_tx.read().as_ref(),
lsp_types::InitializedParams {}, InitializedParams {},
)?; )?;
Ok(()) Ok(())
} }
@ -265,14 +271,14 @@ impl LanguageServer {
let next_id = AtomicUsize::new(self.next_id.load(SeqCst)); let next_id = AtomicUsize::new(self.next_id.load(SeqCst));
let mut output_done = self.output_done_rx.lock().take().unwrap(); let mut output_done = self.output_done_rx.lock().take().unwrap();
Some(async move { Some(async move {
Self::request_internal::<lsp_types::request::Shutdown>( Self::request_internal::<request::Shutdown>(
&next_id, &next_id,
&response_handlers, &response_handlers,
outbound_tx.as_ref(), outbound_tx.as_ref(),
(), (),
) )
.await?; .await?;
Self::notify_internal::<lsp_types::notification::Exit>(outbound_tx.as_ref(), ())?; Self::notify_internal::<notification::Exit>(outbound_tx.as_ref(), ())?;
drop(outbound_tx); drop(outbound_tx);
output_done.recv().await; output_done.recv().await;
drop(tasks); drop(tasks);
@ -285,7 +291,7 @@ impl LanguageServer {
pub fn on_notification<T, F>(&self, mut f: F) -> Subscription pub fn on_notification<T, F>(&self, mut f: F) -> Subscription
where where
T: lsp_types::notification::Notification, T: notification::Notification,
F: 'static + Send + Sync + FnMut(T::Params), F: 'static + Send + Sync + FnMut(T::Params),
{ {
let prev_handler = self.notification_handlers.write().insert( let prev_handler = self.notification_handlers.write().insert(
@ -309,8 +315,8 @@ impl LanguageServer {
} }
} }
pub fn request<T: lsp_types::request::Request>( pub fn request<T: 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
@ -329,7 +335,7 @@ impl LanguageServer {
} }
} }
fn request_internal<T: lsp_types::request::Request>( fn request_internal<T: request::Request>(
next_id: &AtomicUsize, next_id: &AtomicUsize,
response_handlers: &Mutex<HashMap<usize, ResponseHandler>>, response_handlers: &Mutex<HashMap<usize, ResponseHandler>>,
outbound_tx: Option<&channel::Sender<Vec<u8>>>, outbound_tx: Option<&channel::Sender<Vec<u8>>>,
@ -376,7 +382,7 @@ impl LanguageServer {
} }
} }
pub fn notify<T: lsp_types::notification::Notification>( pub fn notify<T: notification::Notification>(
self: &Arc<Self>, self: &Arc<Self>,
params: T::Params, params: T::Params,
) -> impl Future<Output = Result<()>> { ) -> impl Future<Output = Result<()>> {
@ -388,7 +394,7 @@ impl LanguageServer {
} }
} }
fn notify_internal<T: lsp_types::notification::Notification>( fn notify_internal<T: notification::Notification>(
outbound_tx: Option<&channel::Sender<Vec<u8>>>, outbound_tx: Option<&channel::Sender<Vec<u8>>>,
params: T::Params, params: T::Params,
) -> Result<()> { ) -> Result<()> {
@ -601,8 +607,7 @@ mod tests {
"lib.rs": &lib_source "lib.rs": &lib_source
} }
})); }));
let lib_file_uri = let lib_file_uri = Url::from_file_path(root_dir.path().join("src/lib.rs")).unwrap();
lsp_types::Url::from_file_path(root_dir.path().join("src/lib.rs")).unwrap();
let server = cx.read(|cx| { let server = cx.read(|cx| {
LanguageServer::new( LanguageServer::new(
@ -615,24 +620,22 @@ mod tests {
server.next_idle_notification().await; server.next_idle_notification().await;
server server
.notify::<lsp_types::notification::DidOpenTextDocument>( .notify::<notification::DidOpenTextDocument>(DidOpenTextDocumentParams {
lsp_types::DidOpenTextDocumentParams { text_document: TextDocumentItem::new(
text_document: lsp_types::TextDocumentItem::new( lib_file_uri.clone(),
lib_file_uri.clone(), "rust".to_string(),
"rust".to_string(), 0,
0, lib_source,
lib_source, ),
), })
},
)
.await .await
.unwrap(); .unwrap();
let hover = server let hover = server
.request::<lsp_types::request::HoverRequest>(lsp_types::HoverParams { .request::<request::HoverRequest>(HoverParams {
text_document_position_params: lsp_types::TextDocumentPositionParams { text_document_position_params: TextDocumentPositionParams {
text_document: lsp_types::TextDocumentIdentifier::new(lib_file_uri), text_document: TextDocumentIdentifier::new(lib_file_uri),
position: lsp_types::Position::new(1, 21), position: Position::new(1, 21),
}, },
work_done_progress_params: Default::default(), work_done_progress_params: Default::default(),
}) })
@ -641,8 +644,8 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
hover.contents, hover.contents,
lsp_types::HoverContents::Markup(lsp_types::MarkupContent { HoverContents::Markup(MarkupContent {
kind: lsp_types::MarkupKind::Markdown, kind: MarkupKind::Markdown,
value: "&str".to_string() value: "&str".to_string()
}) })
); );
@ -705,10 +708,9 @@ mod tests {
); );
drop(server); drop(server);
let (shutdown_request, _) = fake.receive_request::<lsp_types::request::Shutdown>().await; let (shutdown_request, _) = fake.receive_request::<request::Shutdown>().await;
fake.respond(shutdown_request, ()).await; fake.respond(shutdown_request, ()).await;
fake.receive_notification::<lsp_types::notification::Exit>() fake.receive_notification::<notification::Exit>().await;
.await;
} }
impl LanguageServer { impl LanguageServer {
@ -726,7 +728,7 @@ mod tests {
pub enum ServerStatusNotification {} pub enum ServerStatusNotification {}
impl lsp_types::notification::Notification for ServerStatusNotification { impl notification::Notification for ServerStatusNotification {
type Params = ServerStatusParams; type Params = ServerStatusParams;
const METHOD: &'static str = "experimental/serverStatus"; const METHOD: &'static str = "experimental/serverStatus";
} }