Skip to content

Commit 871fa62

Browse files
committed
Generic find_locations.
Close #454.
1 parent 62c4d01 commit 871fa62

File tree

1 file changed

+18
-75
lines changed

1 file changed

+18
-75
lines changed

src/languageclient.rs

Lines changed: 18 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,40 +1007,33 @@ impl State {
10071007
pub fn find_locations(&mut self, method_name: &str, params: &Option<Params>) -> Result<Value> {
10081008
self.textDocument_didChange(params)?;
10091009
info!("Begin {}", method_name);
1010-
let (buftype, languageId, filename, line, character, goto_cmd, handle): (
1011-
String,
1010+
let (languageId, filename, line, character, handle, goto_cmd): (
10121011
String,
10131012
String,
10141013
u64,
10151014
u64,
1016-
Option<String>,
10171015
bool,
1016+
Option<String>,
10181017
) = self.gather_args(
10191018
&[
1020-
VimVar::Buftype,
10211019
VimVar::LanguageId,
10221020
VimVar::Filename,
10231021
VimVar::Line,
10241022
VimVar::Character,
1025-
VimVar::GotoCmd,
10261023
VimVar::Handle,
1024+
VimVar::GotoCmd,
10271025
],
10281026
params,
10291027
)?;
1030-
if !buftype.is_empty() || languageId.is_empty() {
1031-
return Ok(Value::Null);
1032-
}
10331028

1034-
let result = self.call(
1035-
Some(&languageId),
1036-
method_name,
1037-
TextDocumentPositionParams {
1038-
text_document: TextDocumentIdentifier {
1039-
uri: filename.to_url()?,
1040-
},
1041-
position: Position { line, character },
1029+
let params = serde_json::to_value(TextDocumentPositionParams {
1030+
text_document: TextDocumentIdentifier {
1031+
uri: filename.to_url()?,
10421032
},
1043-
)?;
1033+
position: Position { line, character },
1034+
})?.combine(serde_json::to_value(params)?);
1035+
1036+
let result = self.call(Some(&languageId), method_name, &params.to_params()?)?;
10441037

10451038
if !handle {
10461039
return Ok(result);
@@ -1421,71 +1414,21 @@ impl State {
14211414
}
14221415

14231416
pub fn textDocument_references(&mut self, params: &Option<Params>) -> Result<Value> {
1424-
self.textDocument_didChange(params)?;
14251417
info!("Begin {}", lsp::request::References::METHOD);
14261418

1427-
let (buftype, languageId, filename, line, character, goto_cmd, handle, include_declaration): (
1428-
String,
1429-
String,
1430-
String,
1431-
u64,
1432-
u64,
1433-
Option<String>,
1434-
bool,
1435-
bool,
1436-
) = self.gather_args(
1437-
&[
1438-
VimVar::Buftype,
1439-
VimVar::LanguageId,
1440-
VimVar::Filename,
1441-
VimVar::Line,
1442-
VimVar::Character,
1443-
VimVar::GotoCmd,
1444-
VimVar::Handle,
1445-
VimVar::IncludeDeclaration,
1446-
],
1447-
params,
1448-
)?;
1449-
if !buftype.is_empty() || languageId.is_empty() {
1419+
let (buftype, include_declaration): (String, bool) =
1420+
self.gather_args(&[VimVar::Buftype, VimVar::IncludeDeclaration], params)?;
1421+
if !buftype.is_empty() {
14501422
return Ok(Value::Null);
14511423
}
14521424

1453-
let result = self.call(
1454-
Some(&languageId),
1455-
lsp::request::References::METHOD,
1456-
ReferenceParams {
1457-
text_document: TextDocumentIdentifier {
1458-
uri: filename.to_url()?,
1459-
},
1460-
position: Position { line, character },
1461-
context: ReferenceContext {
1425+
let params = serde_json::to_value(params)?.combine(json!({
1426+
"context": ReferenceContext {
14621427
include_declaration,
1463-
},
1464-
},
1465-
)?;
1466-
1467-
if !handle {
1468-
return Ok(result);
1469-
}
1428+
}
1429+
}));
14701430

1471-
let locations: Option<Vec<Location>> = serde_json::from_value(result.clone())?;
1472-
match locations {
1473-
None => self.echowarn("Not found!")?,
1474-
Some(ref arr) if arr.is_empty() => self.echowarn("Not found!")?,
1475-
Some(ref arr) if arr.len() == 1 => {
1476-
let loc = arr.get(0).ok_or_else(|| err_msg("Not found!"))?;
1477-
self.edit(&goto_cmd, loc.uri.filepath()?)?;
1478-
self.cursor(loc.range.start.line + 1, loc.range.start.character + 1)?;
1479-
let cur_file: String = self.eval("expand('%')")?;
1480-
self.echomsg_ellipsis(format!(
1481-
"[LC]: {} {}:{}",
1482-
cur_file,
1483-
loc.range.start.line + 1,
1484-
loc.range.start.character + 1
1485-
))?
1486-
}
1487-
Some(ref arr) => self.display_locations(arr)?,
1488-
}
1431+
let result = self.find_locations(lsp::request::References::METHOD, &params.to_params()?)?;
14891432

14901433
info!("End {}", lsp::request::References::METHOD);
14911434
Ok(result)

0 commit comments

Comments
 (0)