Start moving more LSP calls into callbacks/futures without capturing self

pull/11/head
Blaž Hrastnik 4 years ago
parent 1ad0581ddd
commit d00414f81a

@ -112,32 +112,35 @@ impl Client {
R::Result: core::fmt::Debug, // TODO: temporary R::Result: core::fmt::Debug, // TODO: temporary
{ {
// a future that resolves into the response // a future that resolves into the response
let future = self.call::<R>(params).await?; let json = self.call::<R>(params).await?;
let json = future.await?;
let response = serde_json::from_value(json)?; let response = serde_json::from_value(json)?;
Ok(response) Ok(response)
} }
/// Execute a RPC request on the language server. /// Execute a RPC request on the language server.
pub async fn call<R: lsp::request::Request>( pub fn call<R: lsp::request::Request>(
&self, &self,
params: R::Params, params: R::Params,
) -> Result<impl Future<Output = Result<Value>>> ) -> impl Future<Output = Result<Value>>
where where
R::Params: serde::Serialize, R::Params: serde::Serialize,
{ {
let outgoing = self.outgoing.clone();
let id = self.next_request_id();
async move {
let params = serde_json::to_value(params)?; let params = serde_json::to_value(params)?;
let request = jsonrpc::MethodCall { let request = jsonrpc::MethodCall {
jsonrpc: Some(jsonrpc::Version::V2), jsonrpc: Some(jsonrpc::Version::V2),
id: self.next_request_id(), id,
method: R::METHOD.to_string(), method: R::METHOD.to_string(),
params: Self::value_into_params(params), params: Self::value_into_params(params),
}; };
let (tx, mut rx) = channel::<Result<Value>>(1); let (tx, mut rx) = channel::<Result<Value>>(1);
self.outgoing outgoing
.send(Payload::Request { .send(Payload::Request {
chan: tx, chan: tx,
value: request, value: request,
@ -147,14 +150,11 @@ impl Client {
use std::time::Duration; use std::time::Duration;
use tokio::time::timeout; use tokio::time::timeout;
let future = async move {
timeout(Duration::from_secs(2), rx.recv()) timeout(Duration::from_secs(2), rx.recv())
.await .await
.map_err(|e| Error::Timeout)? // return Timeout .map_err(|e| Error::Timeout)? // return Timeout
.unwrap() // TODO: None if channel closed .unwrap() // TODO: None if channel closed
}; }
Ok(future)
} }
/// Send a RPC notification to the language server. /// Send a RPC notification to the language server.
@ -269,21 +269,21 @@ impl Client {
self.request::<lsp::request::Shutdown>(()).await self.request::<lsp::request::Shutdown>(()).await
} }
pub async fn exit(&self) -> Result<()> { pub fn exit(&self) -> impl Future<Output = Result<()>> {
self.notify::<lsp::notification::Exit>(()).await self.notify::<lsp::notification::Exit>(())
} }
// ------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------
// Text document // Text document
// ------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------
pub async fn text_document_did_open( pub fn text_document_did_open(
&self, &self,
uri: lsp::Url, uri: lsp::Url,
version: i32, version: i32,
doc: &Rope, doc: &Rope,
language_id: String, language_id: String,
) -> Result<()> { ) -> impl Future<Output = Result<()>> {
self.notify::<lsp::notification::DidOpenTextDocument>(lsp::DidOpenTextDocumentParams { self.notify::<lsp::notification::DidOpenTextDocument>(lsp::DidOpenTextDocumentParams {
text_document: lsp::TextDocumentItem { text_document: lsp::TextDocumentItem {
uri, uri,
@ -292,7 +292,6 @@ impl Client {
text: String::from(doc), text: String::from(doc),
}, },
}) })
.await
} }
pub fn changeset_to_changes( pub fn changeset_to_changes(
@ -435,14 +434,13 @@ impl Client {
)) ))
} }
pub async fn text_document_did_close( pub fn text_document_did_close(
&self, &self,
text_document: lsp::TextDocumentIdentifier, text_document: lsp::TextDocumentIdentifier,
) -> Result<()> { ) -> impl Future<Output = Result<()>> {
self.notify::<lsp::notification::DidCloseTextDocument>(lsp::DidCloseTextDocumentParams { self.notify::<lsp::notification::DidCloseTextDocument>(lsp::DidCloseTextDocumentParams {
text_document, text_document,
}) })
.await
} }
// will_save / will_save_wait_until // will_save / will_save_wait_until
@ -477,11 +475,11 @@ impl Client {
.await .await
} }
pub async fn completion( pub fn completion(
&self, &self,
text_document: lsp::TextDocumentIdentifier, text_document: lsp::TextDocumentIdentifier,
position: lsp::Position, position: lsp::Position,
) -> Result<impl Future<Output = Result<Value>>> { ) -> impl Future<Output = Result<Value>> {
// ) -> Result<Vec<lsp::CompletionItem>> { // ) -> Result<Vec<lsp::CompletionItem>> {
let params = lsp::CompletionParams { let params = lsp::CompletionParams {
text_document_position: lsp::TextDocumentPositionParams { text_document_position: lsp::TextDocumentPositionParams {
@ -499,7 +497,7 @@ impl Client {
// lsp::CompletionContext { trigger_kind: , trigger_character: Some(), } // lsp::CompletionContext { trigger_kind: , trigger_character: Some(), }
}; };
self.call::<lsp::request::Completion>(params).await self.call::<lsp::request::Completion>(params)
} }
pub async fn text_document_signature_help( pub async fn text_document_signature_help(

@ -1790,12 +1790,12 @@ pub fn completion(cx: &mut Context) {
); );
// TODO: handle fails // TODO: handle fails
let res = block_on(language_server.completion(doc.identifier(), pos)).unwrap(); let future = language_server.completion(doc.identifier(), pos);
let trigger_offset = doc.selection(view.id).cursor(); let trigger_offset = doc.selection(view.id).cursor();
cx.callback( cx.callback(
res, future,
move |editor: &mut Editor, move |editor: &mut Editor,
compositor: &mut Compositor, compositor: &mut Compositor,
response: Option<lsp::CompletionResponse>| { response: Option<lsp::CompletionResponse>| {

@ -131,14 +131,12 @@ impl Editor {
.map(ToOwned::to_owned) .map(ToOwned::to_owned)
.unwrap_or_default(); .unwrap_or_default();
let rt = tokio::runtime::Handle::current(); tokio::spawn(language_server.text_document_did_open(
rt.block_on(language_server.text_document_did_open(
doc.url().unwrap(), doc.url().unwrap(),
doc.version(), doc.version(),
doc.text(), doc.text(),
language_id, language_id,
)) ));
.unwrap();
} }
let id = self.documents.insert(doc); let id = self.documents.insert(doc);
@ -162,9 +160,7 @@ impl Editor {
.and_then(|language| language_servers.get(language)); .and_then(|language| language_servers.get(language));
if let Some(language_server) = language_server { if let Some(language_server) = language_server {
let rt = tokio::runtime::Handle::current(); tokio::spawn(language_server.text_document_did_close(doc.identifier()));
rt.block_on(language_server.text_document_did_close(doc.identifier()))
.unwrap();
} }
// remove selection // remove selection

Loading…
Cancel
Save