Skip to content

Commit e58ae8f

Browse files
autozimuJunfeng Li
authored and
Junfeng Li
committed
Make call() generic over return type.
1 parent e99fe18 commit e58ae8f

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/languageclient.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub trait ILanguageClient: IVim {
231231
lines.pop();
232232
}
233233
self.command("1,$d")?;
234-
if self.call(None, "setline", json!([1, lines]))? != 0 {
234+
if self.call::<_, i64>(None, "setline", json!([1, lines]))? != 0 {
235235
bail!("Failed to set preview buffer content!");
236236
}
237237
debug!("End apply TextEdits");
@@ -313,10 +313,14 @@ pub trait ILanguageClient: IVim {
313313

314314
match self.get(|state| Ok(state.diagnosticsList.clone()))? {
315315
DiagnosticsList::Quickfix => {
316-
self.call(None, "setqflist", [qflist])?;
316+
if self.call::<_, i64>(None, "setqflist", [qflist])? != 0 {
317+
bail!("Failed to set quickfix list!");
318+
}
317319
}
318320
DiagnosticsList::Location => {
319-
self.call(None, "setloclist", json!([0, qflist]))?;
321+
if self.call::<_, i64>(None, "setloclist", json!([0, qflist]))? != 0 {
322+
bail!("Failed to set location list!");
323+
}
320324
}
321325
}
322326

@@ -342,7 +346,7 @@ pub trait ILanguageClient: IVim {
342346

343347
// Highlight.
344348
// TODO: Optimize.
345-
self.call(None, "nvim_buf_clear_highlight", json!([0, source, 1, -1]))?;
349+
self.notify(None, "nvim_buf_clear_highlight", json!([0, source, 1, -1]))?;
346350
for dn in diagnostics.iter() {
347351
let severity = dn.severity.unwrap_or(DiagnosticSeverity::Information);
348352
let hl_group = diagnosticsDisplay
@@ -542,7 +546,7 @@ pub trait ILanguageClient: IVim {
542546
.ok_or_else(|| err_msg("No highlight source"))
543547
});
544548
if let Ok(hlsource) = hlsource {
545-
self.call(
549+
self.notify(
546550
None,
547551
"nvim_buf_clear_highlight",
548552
json!([0, hlsource, 1, -1]),
@@ -568,7 +572,7 @@ pub trait ILanguageClient: IVim {
568572
if self.get(|state| Ok(state.is_nvim))? {
569573
let bufnr: u64 = serde_json::from_value(self.call(None, "bufnr", bufname)?)?;
570574
self.notify(None, "nvim_buf_set_lines", json!([bufnr, 0, -1, 0, lines]))?;
571-
} else if self.call(None, "setbufline", json!([bufname, 1, lines]))? != 0 {
575+
} else if self.call::<_, i64>(None, "setbufline", json!([bufname, 1, lines]))? != 0 {
572576
bail!("Failed to set preview buffer content!");
573577
// TODO: removing existing bottom lines.
574578
}
@@ -625,7 +629,7 @@ pub trait ILanguageClient: IVim {
625629

626630
let trace = self.get(|state| Ok(state.trace.clone()))?;
627631

628-
let result = self.call(
632+
let result: Value = self.call(
629633
Some(&languageId),
630634
lsp::request::Initialize::METHOD,
631635
InitializeParams {

src/vim.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,12 @@ pub trait IVim {
104104
}
105105

106106
/// RPC method call.
107-
/// TODO: make return value generic.
108-
fn call<P: Serialize>(
107+
fn call<P: Serialize, V: DeserializeOwned>(
109108
&self,
110109
languageId: Option<&str>,
111110
method: &str,
112111
params: P,
113-
) -> Result<Value> {
112+
) -> Result<V> {
114113
let id = self.update(|state| {
115114
state.id += 1;
116115
Ok(state.id)
@@ -133,7 +132,8 @@ pub trait IVim {
133132
info!("=> {}", message);
134133
self.write(languageId, &message)?;
135134

136-
cx.recv_timeout(std::time::Duration::from_secs(60 * 5))?
135+
let value = cx.recv_timeout(std::time::Duration::from_secs(60 * 5))??;
136+
Ok(serde_json::from_value(value)?)
137137
}
138138

139139
/// RPC notification.

0 commit comments

Comments
 (0)