on master help sign in
Start moving more LSP calls into callbacks/futures without capturing self
commitd00414f81a49501277642868c2cf76eefeb0db6b authorBlaž Hrastnik <blaz@mxxn.io> dateThu 6 May 2021, 15:08 (5 years ago) parent1ad0581
3 files changed+38−44
helix-lsp/src/client.rs+33−35 helix-term/src/commands.rs+2−2 helix-view/src/editor.rs+3−7
helix-lsp/src/client.rs+33−35
@@ -112,49 +112,49 @@ impl Client {
112112 R::Result: core::fmt::Debug, // TODO: temporary
113113 {
114114 // a future that resolves into the response
115− let future = self.call::<R>(params).await?;
116− let json = future.await?;
115+ let json = self.call::<R>(params).await?;
117116 let response = serde_json::from_value(json)?;
118117 Ok(response)
119118 }
120119
121120 /// Execute a RPC request on the language server.
122− pub async fn call<R: lsp::request::Request>(
121+ pub fn call<R: lsp::request::Request>(
123122 &self,
124123 params: R::Params,
125− ) -> Result<impl Future<Output = Result<Value>>>
124+ ) -> impl Future<Output = Result<Value>>
126125 where
127126 R::Params: serde::Serialize,
128127 {
129− let params = serde_json::to_value(params)?;
128+ let outgoing = self.outgoing.clone();
129+ let id = self.next_request_id();
130130
131− let request = jsonrpc::MethodCall {
132− jsonrpc: Some(jsonrpc::Version::V2),
133− id: self.next_request_id(),
134− method: R::METHOD.to_string(),
135− params: Self::value_into_params(params),
136− };
131+ async move {
132+ let params = serde_json::to_value(params)?;
137133
138− let (tx, mut rx) = channel::<Result<Value>>(1);
134+ let request = jsonrpc::MethodCall {
135+ jsonrpc: Some(jsonrpc::Version::V2),
136+ id,
137+ method: R::METHOD.to_string(),
138+ params: Self::value_into_params(params),
139+ };
139140
140− self.outgoing
141− .send(Payload::Request {
142− chan: tx,
143− value: request,
144− })
145− .map_err(|e| Error::Other(e.into()))?;
141+ let (tx, mut rx) = channel::<Result<Value>>(1);
146142
147− use std::time::Duration;
148− use tokio::time::timeout;
143+ outgoing
144+ .send(Payload::Request {
145+ chan: tx,
146+ value: request,
147+ })
148+ .map_err(|e| Error::Other(e.into()))?;
149+
150+ use std::time::Duration;
151+ use tokio::time::timeout;
149152
150− let future = async move {
151153 timeout(Duration::from_secs(2), rx.recv())
152154 .await
153155 .map_err(|e| Error::Timeout)? // return Timeout
154156 .unwrap() // TODO: None if channel closed
155− };
156−
157− Ok(future)
157+ }
158158 }
159159
160160 /// Send a RPC notification to the language server.
@@ -269,21 +269,21 @@ impl Client {
269269 self.request::<lsp::request::Shutdown>(()).await
270270 }
271271
272− pub async fn exit(&self) -> Result<()> {
273− self.notify::<lsp::notification::Exit>(()).await
272+ pub fn exit(&self) -> impl Future<Output = Result<()>> {
273+ self.notify::<lsp::notification::Exit>(())
274274 }
275275
276276 // -------------------------------------------------------------------------------------------
277277 // Text document
278278 // -------------------------------------------------------------------------------------------
279279
280− pub async fn text_document_did_open(
280+ pub fn text_document_did_open(
281281 &self,
282282 uri: lsp::Url,
283283 version: i32,
284284 doc: &Rope,
285285 language_id: String,
286− ) -> Result<()> {
286+ ) -> impl Future<Output = Result<()>> {
287287 self.notify::<lsp::notification::DidOpenTextDocument>(lsp::DidOpenTextDocumentParams {
288288 text_document: lsp::TextDocumentItem {
289289 uri,
@@ -292,7 +292,6 @@ impl Client {
292292 text: String::from(doc),
293293 },
294294 })
295− .await
296295 }
297296
298297 pub fn changeset_to_changes(
@@ -435,14 +434,13 @@ impl Client {
435434 ))
436435 }
437436
438− pub async fn text_document_did_close(
437+ pub fn text_document_did_close(
439438 &self,
440439 text_document: lsp::TextDocumentIdentifier,
441− ) -> Result<()> {
440+ ) -> impl Future<Output = Result<()>> {
442441 self.notify::<lsp::notification::DidCloseTextDocument>(lsp::DidCloseTextDocumentParams {
443442 text_document,
444443 })
445− .await
446444 }
447445
448446 // will_save / will_save_wait_until
@@ -477,11 +475,11 @@ impl Client {
477475 .await
478476 }
479477
480− pub async fn completion(
478+ pub fn completion(
481479 &self,
482480 text_document: lsp::TextDocumentIdentifier,
483481 position: lsp::Position,
484− ) -> Result<impl Future<Output = Result<Value>>> {
482+ ) -> impl Future<Output = Result<Value>> {
485483 // ) -> Result<Vec<lsp::CompletionItem>> {
486484 let params = lsp::CompletionParams {
487485 text_document_position: lsp::TextDocumentPositionParams {
@@ -499,7 +497,7 @@ impl Client {
499497 // lsp::CompletionContext { trigger_kind: , trigger_character: Some(), }
500498 };
501499
502− self.call::<lsp::request::Completion>(params).await
500+ self.call::<lsp::request::Completion>(params)
503501 }
504502
505503 pub async fn text_document_signature_help(
helix-term/src/commands.rs+2−2
@@ -1790,12 +1790,12 @@ pub fn completion(cx: &mut Context) {
17901790 );
17911791
17921792 // TODO: handle fails
1793− let res = block_on(language_server.completion(doc.identifier(), pos)).unwrap();
1793+ let future = language_server.completion(doc.identifier(), pos);
17941794
17951795 let trigger_offset = doc.selection(view.id).cursor();
17961796
17971797 cx.callback(
1798− res,
1798+ future,
17991799 move |editor: &mut Editor,
18001800 compositor: &mut Compositor,
18011801 response: Option<lsp::CompletionResponse>| {
helix-view/src/editor.rs+3−7
@@ -131,14 +131,12 @@ impl Editor {
131131 .map(ToOwned::to_owned)
132132 .unwrap_or_default();
133133
134− let rt = tokio::runtime::Handle::current();
135− rt.block_on(language_server.text_document_did_open(
134+ tokio::spawn(language_server.text_document_did_open(
136135 doc.url().unwrap(),
137136 doc.version(),
138137 doc.text(),
139138 language_id,
140− ))
141− .unwrap();
139+ ));
142140 }
143141
144142 let id = self.documents.insert(doc);
@@ -162,9 +160,7 @@ impl Editor {
162160 .and_then(|language| language_servers.get(language));
163161
164162 if let Some(language_server) = language_server {
165− let rt = tokio::runtime::Handle::current();
166− rt.block_on(language_server.text_document_did_close(doc.identifier()))
167− .unwrap();
163+ tokio::spawn(language_server.text_document_did_close(doc.identifier()));
168164 }
169165
170166 // remove selection