Skip to content

Commit 836e600

Browse files
author
Junfeng Li
committed
Reduce duplications of various goto.
1 parent d74db2e commit 836e600

File tree

2 files changed

+10
-199
lines changed

2 files changed

+10
-199
lines changed

src/languageclient.rs

+5-192
Original file line numberDiff line numberDiff line change
@@ -703,9 +703,10 @@ pub trait ILanguageClient: IVim {
703703
Ok(result)
704704
}
705705

706-
fn textDocument_definition(&self, params: &Option<Params>) -> Result<Value> {
706+
/// Generic find locations, e.g, definitions, references.
707+
fn find_locations(&self, method_name: &str, params: &Option<Params>) -> Result<Value> {
707708
self.textDocument_didChange(params)?;
708-
info!("Begin {}", lsp::request::GotoDefinition::METHOD);
709+
info!("Begin {}", method_name);
709710
let (buftype, languageId, filename, line, character, goto_cmd, handle): (
710711
String,
711712
String,
@@ -732,7 +733,7 @@ pub trait ILanguageClient: IVim {
732733

733734
let result = self.call(
734735
Some(&languageId),
735-
lsp::request::GotoDefinition::METHOD,
736+
method_name,
736737
TextDocumentPositionParams {
737738
text_document: TextDocumentIdentifier {
738739
uri: filename.to_url()?,
@@ -775,7 +776,7 @@ pub trait ILanguageClient: IVim {
775776
},
776777
};
777778

778-
info!("End {}", lsp::request::GotoDefinition::METHOD);
779+
info!("End {}", method_name);
779780
Ok(result)
780781
}
781782

@@ -2160,194 +2161,6 @@ pub trait ILanguageClient: IVim {
21602161
info!("End {}", NOTIFICATION__CqueryProgress);
21612162
Ok(())
21622163
}
2163-
2164-
fn cquery_base(&self, params: &Option<Params>) -> Result<Value> {
2165-
info!("Begin {}", REQUEST__CqueryBase);
2166-
2167-
let (buftype, languageId, filename, line, character, handle): (
2168-
String,
2169-
String,
2170-
String,
2171-
u64,
2172-
u64,
2173-
bool,
2174-
) = self.gather_args(
2175-
&[
2176-
VimVar::Buftype,
2177-
VimVar::LanguageId,
2178-
VimVar::Filename,
2179-
VimVar::Line,
2180-
VimVar::Character,
2181-
VimVar::Handle,
2182-
],
2183-
params,
2184-
)?;
2185-
if !buftype.is_empty() || languageId.is_empty() {
2186-
return Ok(Value::Null);
2187-
}
2188-
2189-
let result = self.call(
2190-
Some(&languageId),
2191-
REQUEST__CqueryBase,
2192-
TextDocumentPositionParams {
2193-
text_document: TextDocumentIdentifier {
2194-
uri: filename.to_url()?,
2195-
},
2196-
position: Position { line, character },
2197-
},
2198-
)?;
2199-
2200-
if !handle {
2201-
return Ok(result);
2202-
}
2203-
2204-
let locations: Vec<Location> = serde_json::from_value(result.clone())?;
2205-
self.display_locations(&locations, &languageId)?;
2206-
2207-
info!("End {}", REQUEST__CqueryBase);
2208-
Ok(result)
2209-
}
2210-
2211-
fn cquery_derived(&self, params: &Option<Params>) -> Result<Value> {
2212-
info!("Begin {}", REQUEST__CqueryDerived);
2213-
2214-
let (buftype, languageId, filename, line, character, handle): (
2215-
String,
2216-
String,
2217-
String,
2218-
u64,
2219-
u64,
2220-
bool,
2221-
) = self.gather_args(
2222-
&[
2223-
VimVar::Buftype,
2224-
VimVar::LanguageId,
2225-
VimVar::Filename,
2226-
VimVar::Line,
2227-
VimVar::Character,
2228-
VimVar::Handle,
2229-
],
2230-
params,
2231-
)?;
2232-
if !buftype.is_empty() || languageId.is_empty() {
2233-
return Ok(Value::Null);
2234-
}
2235-
2236-
let result = self.call(
2237-
Some(&languageId),
2238-
REQUEST__CqueryDerived,
2239-
TextDocumentPositionParams {
2240-
text_document: TextDocumentIdentifier {
2241-
uri: filename.to_url()?,
2242-
},
2243-
position: Position { line, character },
2244-
},
2245-
)?;
2246-
2247-
if !handle {
2248-
return Ok(result);
2249-
}
2250-
2251-
let locations: Vec<Location> = serde_json::from_value(result.clone())?;
2252-
self.display_locations(&locations, &languageId)?;
2253-
2254-
info!("End {}", REQUEST__CqueryDerived);
2255-
Ok(result)
2256-
}
2257-
2258-
fn cquery_callers(&self, params: &Option<Params>) -> Result<Value> {
2259-
info!("Begin {}", REQUEST__CqueryCallers);
2260-
2261-
let (buftype, languageId, filename, line, character, handle): (
2262-
String,
2263-
String,
2264-
String,
2265-
u64,
2266-
u64,
2267-
bool,
2268-
) = self.gather_args(
2269-
&[
2270-
VimVar::Buftype,
2271-
VimVar::LanguageId,
2272-
VimVar::Filename,
2273-
VimVar::Line,
2274-
VimVar::Character,
2275-
VimVar::Handle,
2276-
],
2277-
params,
2278-
)?;
2279-
if !buftype.is_empty() || languageId.is_empty() {
2280-
return Ok(Value::Null);
2281-
}
2282-
2283-
let result = self.call(
2284-
Some(&languageId),
2285-
REQUEST__CqueryCallers,
2286-
TextDocumentPositionParams {
2287-
text_document: TextDocumentIdentifier {
2288-
uri: filename.to_url()?,
2289-
},
2290-
position: Position { line, character },
2291-
},
2292-
)?;
2293-
2294-
if !handle {
2295-
return Ok(result);
2296-
}
2297-
2298-
let locations: Vec<Location> = serde_json::from_value(result.clone())?;
2299-
self.display_locations(&locations, &languageId)?;
2300-
2301-
info!("End {}", REQUEST__CqueryCallers);
2302-
Ok(result)
2303-
}
2304-
2305-
fn cquery_vars(&self, params: &Option<Params>) -> Result<Value> {
2306-
info!("Begin {}", REQUEST__CqueryVars);
2307-
2308-
let (buftype, languageId, filename, line, character, handle): (
2309-
String,
2310-
String,
2311-
String,
2312-
u64,
2313-
u64,
2314-
bool,
2315-
) = self.gather_args(
2316-
&[
2317-
VimVar::Buftype,
2318-
VimVar::LanguageId,
2319-
VimVar::Filename,
2320-
VimVar::Line,
2321-
VimVar::Character,
2322-
VimVar::Handle,
2323-
],
2324-
params,
2325-
)?;
2326-
if !buftype.is_empty() || languageId.is_empty() {
2327-
return Ok(Value::Null);
2328-
}
2329-
2330-
let result = self.call(
2331-
Some(&languageId),
2332-
REQUEST__CqueryVars,
2333-
TextDocumentPositionParams {
2334-
text_document: TextDocumentIdentifier {
2335-
uri: filename.to_url()?,
2336-
},
2337-
position: Position { line, character },
2338-
},
2339-
)?;
2340-
2341-
if !handle {
2342-
return Ok(result);
2343-
}
2344-
2345-
let locations: Vec<Location> = serde_json::from_value(result.clone())?;
2346-
self.display_locations(&locations, &languageId)?;
2347-
2348-
info!("End {}", REQUEST__CqueryVars);
2349-
Ok(result)
2350-
}
23512164
}
23522165

23532166
impl ILanguageClient for Arc<RwLock<State>> {

src/rpchandler.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ impl IRpcHandler for Arc<RwLock<State>> {
3434

3535
match method_call.method.as_str() {
3636
lsp::request::HoverRequest::METHOD => self.textDocument_hover(&method_call.params),
37-
lsp::request::GotoDefinition::METHOD => {
38-
self.textDocument_definition(&method_call.params)
39-
}
37+
m @ lsp::request::GotoDefinition::METHOD
38+
| m @ REQUEST__CqueryBase
39+
| m @ REQUEST__CqueryCallers
40+
| m @ REQUEST__CqueryDerived
41+
| m @ REQUEST__CqueryVars => self.find_locations(m, &method_call.params),
4042
lsp::request::Rename::METHOD => self.textDocument_rename(&method_call.params),
4143
lsp::request::DocumentSymbol::METHOD => {
4244
self.textDocument_documentSymbol(&method_call.params)
@@ -75,10 +77,6 @@ impl IRpcHandler for Arc<RwLock<State>> {
7577
REQUEST__RegisterHandlers => self.languageClient_registerHandlers(&method_call.params),
7678
REQUEST__NCMRefresh => self.NCM_refresh(&method_call.params),
7779
REQUEST__OmniComplete => self.languageClient_omniComplete(&method_call.params),
78-
REQUEST__CqueryBase => self.cquery_base(&method_call.params),
79-
REQUEST__CqueryCallers => self.cquery_callers(&method_call.params),
80-
REQUEST__CqueryDerived => self.cquery_derived(&method_call.params),
81-
REQUEST__CqueryVars => self.cquery_vars(&method_call.params),
8280

8381
_ => {
8482
if languageId.is_none() {

0 commit comments

Comments
 (0)