1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-06-13 08:56:07 +02:00

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

This commit is contained in:
Blaž Hrastnik 2021-05-06 15:08:59 +09:00
parent 1ad0581ddd
commit d00414f81a
3 changed files with 38 additions and 44 deletions

View File

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

View File

@ -1790,12 +1790,12 @@ pub fn completion(cx: &mut Context) {
);
// 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();
cx.callback(
res,
future,
move |editor: &mut Editor,
compositor: &mut Compositor,
response: Option<lsp::CompletionResponse>| {

View File

@ -131,14 +131,12 @@ pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Erro
.map(ToOwned::to_owned)
.unwrap_or_default();
let rt = tokio::runtime::Handle::current();
rt.block_on(language_server.text_document_did_open(
tokio::spawn(language_server.text_document_did_open(
doc.url().unwrap(),
doc.version(),
doc.text(),
language_id,
))
.unwrap();
));
}
let id = self.documents.insert(doc);
@ -162,9 +160,7 @@ pub fn close(&mut self, id: ViewId) {
.and_then(|language| language_servers.get(language));
if let Some(language_server) = language_server {
let rt = tokio::runtime::Handle::current();
rt.block_on(language_server.text_document_did_close(doc.identifier()))
.unwrap();
tokio::spawn(language_server.text_document_did_close(doc.identifier()));
}
// remove selection