on master help sign in
Start moving more LSP calls into callbacks/futures without capturing self
d00414fBlaž Hrastnik
helix-lsp/src/client.rs+33−35
@@ -112,49 +112,49 @@ impl Client {
112 R::Result: core::fmt::Debug, // TODO: temporary
112 R::Result: core::fmt::Debug, // TODO: temporary
113 {
113 {
114 // a future that resolves into the response
114 // a future that resolves into the response
115− let future = self.call::<R>(params).await?;
115+ let json = self.call::<R>(params).await?;
116− let json = future.await?;
117 let response = serde_json::from_value(json)?;
116 let response = serde_json::from_value(json)?;
118 Ok(response)
117 Ok(response)
119 }
118 }
120
119
121 /// Execute a RPC request on the language server.
120 /// 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>(
123 &self,
122 &self,
124 params: R::Params,
123 params: R::Params,
125− ) -> Result<impl Future<Output = Result<Value>>>
124+ ) -> impl Future<Output = Result<Value>>
126 where
125 where
127 R::Params: serde::Serialize,
126 R::Params: serde::Serialize,
128 {
127 {
129− let params = serde_json::to_value(params)?;
128+ let outgoing = self.outgoing.clone();
129+ let id = self.next_request_id();
130
130
131− let request = jsonrpc::MethodCall {
131+ async move {
132− jsonrpc: Some(jsonrpc::Version::V2),
132+ let params = serde_json::to_value(params)?;
133− id: self.next_request_id(),
134− method: R::METHOD.to_string(),
135− params: Self::value_into_params(params),
136− };
137
133
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+ };
139
140
140− self.outgoing
141+ let (tx, mut rx) = channel::<Result<Value>>(1);
141− .send(Payload::Request {
142− chan: tx,
143− value: request,
144− })
145− .map_err(|e| Error::Other(e.into()))?;
146
142
143+ outgoing
144+ .send(Payload::Request {
145+ chan: tx,
146+ value: request,
147+ })
148+ .map_err(|e| Error::Other(e.into()))?;
149+
147− use std::time::Duration;
150+ use std::time::Duration;
148− use tokio::time::timeout;
151+ use tokio::time::timeout;
149
152
150− let future = async move {
151 timeout(Duration::from_secs(2), rx.recv())
153 timeout(Duration::from_secs(2), rx.recv())
152 .await
154 .await
153 .map_err(|e| Error::Timeout)? // return Timeout
155 .map_err(|e| Error::Timeout)? // return Timeout
154 .unwrap() // TODO: None if channel closed
156 .unwrap() // TODO: None if channel closed
155− };
157+ }
156−
157− Ok(future)
158 }
158 }
159
159
160 /// Send a RPC notification to the language server.
160 /// Send a RPC notification to the language server.
@@ -269,21 +269,21 @@ impl Client {
269 self.request::<lsp::request::Shutdown>(()).await
269 self.request::<lsp::request::Shutdown>(()).await
270 }
270 }
271
271
272− pub async fn exit(&self) -> Result<()> {
272+ pub fn exit(&self) -> impl Future<Output = Result<()>> {
273− self.notify::<lsp::notification::Exit>(()).await
273+ self.notify::<lsp::notification::Exit>(())
274 }
274 }
275
275
276 // -------------------------------------------------------------------------------------------
276 // -------------------------------------------------------------------------------------------
277 // Text document
277 // Text document
278 // -------------------------------------------------------------------------------------------
278 // -------------------------------------------------------------------------------------------
279
279
280− pub async fn text_document_did_open(
280+ pub fn text_document_did_open(
281 &self,
281 &self,
282 uri: lsp::Url,
282 uri: lsp::Url,
283 version: i32,
283 version: i32,
284 doc: &Rope,
284 doc: &Rope,
285 language_id: String,
285 language_id: String,
286− ) -> Result<()> {
286+ ) -> impl Future<Output = Result<()>> {
287 self.notify::<lsp::notification::DidOpenTextDocument>(lsp::DidOpenTextDocumentParams {
287 self.notify::<lsp::notification::DidOpenTextDocument>(lsp::DidOpenTextDocumentParams {
288 text_document: lsp::TextDocumentItem {
288 text_document: lsp::TextDocumentItem {
289 uri,
289 uri,
@@ -292,7 +292,6 @@ impl Client {
292 text: String::from(doc),
292 text: String::from(doc),
293 },
293 },
294 })
294 })
295− .await
296 }
295 }
297
296
298 pub fn changeset_to_changes(
297 pub fn changeset_to_changes(
@@ -435,14 +434,13 @@ impl Client {
435 ))
434 ))
436 }
435 }
437
436
438− pub async fn text_document_did_close(
437+ pub fn text_document_did_close(
439 &self,
438 &self,
440 text_document: lsp::TextDocumentIdentifier,
439 text_document: lsp::TextDocumentIdentifier,
441− ) -> Result<()> {
440+ ) -> impl Future<Output = Result<()>> {
442 self.notify::<lsp::notification::DidCloseTextDocument>(lsp::DidCloseTextDocumentParams {
441 self.notify::<lsp::notification::DidCloseTextDocument>(lsp::DidCloseTextDocumentParams {
443 text_document,
442 text_document,
444 })
443 })
445− .await
446 }
444 }
447
445
448 // will_save / will_save_wait_until
446 // will_save / will_save_wait_until
@@ -477,11 +475,11 @@ impl Client {
477 .await
475 .await
478 }
476 }
479
477
480− pub async fn completion(
478+ pub fn completion(
481 &self,
479 &self,
482 text_document: lsp::TextDocumentIdentifier,
480 text_document: lsp::TextDocumentIdentifier,
483 position: lsp::Position,
481 position: lsp::Position,
484− ) -> Result<impl Future<Output = Result<Value>>> {
482+ ) -> impl Future<Output = Result<Value>> {
485 // ) -> Result<Vec<lsp::CompletionItem>> {
483 // ) -> Result<Vec<lsp::CompletionItem>> {
486 let params = lsp::CompletionParams {
484 let params = lsp::CompletionParams {
487 text_document_position: lsp::TextDocumentPositionParams {
485 text_document_position: lsp::TextDocumentPositionParams {
@@ -499,7 +497,7 @@ impl Client {
499 // lsp::CompletionContext { trigger_kind: , trigger_character: Some(), }
497 // lsp::CompletionContext { trigger_kind: , trigger_character: Some(), }
500 };
498 };
501
499
502− self.call::<lsp::request::Completion>(params).await
500+ self.call::<lsp::request::Completion>(params)
503 }
501 }
504
502
505 pub async fn text_document_signature_help(
503 pub async fn text_document_signature_help(
helix-term/src/commands.rs+2−2
@@ -1790,12 +1790,12 @@ pub fn completion(cx: &mut Context) {
1790 );
1790 );
1791
1791
1792 // TODO: handle fails
1792 // TODO: handle fails
1793− let res = block_on(language_server.completion(doc.identifier(), pos)).unwrap();
1793+ let future = language_server.completion(doc.identifier(), pos);
1794
1794
1795 let trigger_offset = doc.selection(view.id).cursor();
1795 let trigger_offset = doc.selection(view.id).cursor();
1796
1796
1797 cx.callback(
1797 cx.callback(
1798− res,
1798+ future,
1799 move |editor: &mut Editor,
1799 move |editor: &mut Editor,
1800 compositor: &mut Compositor,
1800 compositor: &mut Compositor,
1801 response: Option<lsp::CompletionResponse>| {
1801 response: Option<lsp::CompletionResponse>| {
helix-view/src/editor.rs+3−7
@@ -131,14 +131,12 @@ impl Editor {
131 .map(ToOwned::to_owned)
131 .map(ToOwned::to_owned)
132 .unwrap_or_default();
132 .unwrap_or_default();
133
133
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(
136 doc.url().unwrap(),
135 doc.url().unwrap(),
137 doc.version(),
136 doc.version(),
138 doc.text(),
137 doc.text(),
139 language_id,
138 language_id,
140− ))
139+ ));
141− .unwrap();
142 }
140 }
143
141
144 let id = self.documents.insert(doc);
142 let id = self.documents.insert(doc);
@@ -162,9 +160,7 @@ impl Editor {
162 .and_then(|language| language_servers.get(language));
160 .and_then(|language| language_servers.get(language));
163
161
164 if let Some(language_server) = language_server {
162 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()))
163+ tokio::spawn(language_server.text_document_did_close(doc.identifier()));
167− .unwrap();
168 }
164 }
169
165
170 // remove selection
166 // remove selection