Skip to content

Commit 66fd174

Browse files
author
Junfeng Li
committed
Implement cquery extensions.
Close #293.
1 parent 30ecd65 commit 66fd174

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed

doc/LanguageClient.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,22 @@ Example status line making use of LanguageClient_serverStatusMessage.
311311

312312
Apply a workspace edit.
313313

314+
4.16 LanguageClient_cquery_base() *LanguageClient_cquery_base*
315+
316+
Call $cquery/base.
317+
318+
4.17 LanguageClient_cquery_derived() *LanguageClient_cquery_derived*
319+
320+
Call $cquery/derived.
321+
322+
4.18 LanguageClient_cquery_callers() *LanguageClient_cquery_callers*
323+
324+
Call $cquery/callers.
325+
326+
4.19 LanguageClient_cquery_vars() *LanguageClient_cquery_vars*
327+
328+
Call $cquery/vars.
329+
314330
==============================================================================
315331
5. Events *LanguageClientEvents*
316332

plugin/LanguageClient.vim

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,62 @@ function! LanguageClient_statusLine() abort
647647
return '[' . g:LanguageClient_serverStatusMessage . ']'
648648
endfunction
649649

650+
function! LanguageClient_cquery_base(...) abort
651+
let l:params = {
652+
\ 'buftype': &buftype,
653+
\ 'languageId': &filetype,
654+
\ 'filename': s:Expand('%:p'),
655+
\ 'line': line('.') - 1,
656+
\ 'character': col('.') - 1,
657+
\ 'handle': v:true,
658+
\ }
659+
call extend(l:params, a:0 >= 1 ? a:1 : {})
660+
let l:callback = a:0 >= 2 ? a:2 : v:null
661+
return LanguageClient#Call('$cquery/base', l:params, l:callback)
662+
endfunction
663+
664+
function! LanguageClient_cquery_derived(...) abort
665+
let l:params = {
666+
\ 'buftype': &buftype,
667+
\ 'languageId': &filetype,
668+
\ 'filename': s:Expand('%:p'),
669+
\ 'line': line('.') - 1,
670+
\ 'character': col('.') - 1,
671+
\ 'handle': v:true,
672+
\ }
673+
call extend(l:params, a:0 >= 1 ? a:1 : {})
674+
let l:callback = a:0 >= 2 ? a:2 : v:null
675+
return LanguageClient#Call('$cquery/derived', l:params, l:callback)
676+
endfunction
677+
678+
function! LanguageClient_cquery_callers(...) abort
679+
let l:params = {
680+
\ 'buftype': &buftype,
681+
\ 'languageId': &filetype,
682+
\ 'filename': s:Expand('%:p'),
683+
\ 'line': line('.') - 1,
684+
\ 'character': col('.') - 1,
685+
\ 'handle': v:true,
686+
\ }
687+
call extend(l:params, a:0 >= 1 ? a:1 : {})
688+
let l:callback = a:0 >= 2 ? a:2 : v:null
689+
return LanguageClient#Call('$cquery/callers', l:params, l:callback)
690+
endfunction
691+
692+
function! LanguageClient_cquery_vars(...) abort
693+
let l:params = {
694+
\ 'buftype': &buftype,
695+
\ 'languageId': &filetype,
696+
\ 'filename': s:Expand('%:p'),
697+
\ 'line': line('.') - 1,
698+
\ 'character': col('.') - 1,
699+
\ 'handle': v:true,
700+
\ }
701+
call extend(l:params, a:0 >= 1 ? a:1 : {})
702+
let l:callback = a:0 >= 2 ? a:2 : v:null
703+
return LanguageClient#Call('$cquery/vars', l:params, l:callback)
704+
endfunction
705+
650706
" When editing a [No Name] file, neovim reports filename as "", while vim reports null.
651707
function! s:Expand(exp) abort
652708
let l:result = expand(a:exp)

src/languageclient.rs

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ pub trait ILanguageClient {
8282
fn rust_handleDiagnosticsBegin(&self, params: &Option<Params>) -> Result<()>;
8383
fn rust_handleDiagnosticsEnd(&self, params: &Option<Params>) -> Result<()>;
8484
fn cquery_handleProgress(&self, params: &Option<Params>) -> Result<()>;
85+
fn cquery_base(&self, params: &Option<Params>) -> Result<Value>;
86+
fn cquery_derived(&self, params: &Option<Params>) -> Result<Value>;
87+
fn cquery_callers(&self, params: &Option<Params>) -> Result<Value>;
88+
fn cquery_vars(&self, params: &Option<Params>) -> Result<Value>;
8589
}
8690

8791
impl ILanguageClient for Arc<Mutex<State>> {
@@ -2446,6 +2450,170 @@ impl ILanguageClient for Arc<Mutex<State>> {
24462450
Ok(())
24472451
}
24482452

2453+
fn cquery_base(&self, params: &Option<Params>) -> Result<Value> {
2454+
info!("Begin {}", REQUEST__CqueryBase);
2455+
2456+
let (buftype, languageId, filename, line, character, handle): (String, String, String, u64, u64, bool) =
2457+
self.gather_args(
2458+
&[
2459+
VimVar::Buftype,
2460+
VimVar::LanguageId,
2461+
VimVar::Filename,
2462+
VimVar::Line,
2463+
VimVar::Character,
2464+
VimVar::Handle,
2465+
],
2466+
params,
2467+
)?;
2468+
if !buftype.is_empty() || languageId.is_empty() {
2469+
return Ok(Value::Null);
2470+
}
2471+
2472+
let result = self.call(
2473+
Some(&languageId),
2474+
REQUEST__CqueryBase,
2475+
TextDocumentPositionParams {
2476+
text_document: TextDocumentIdentifier {
2477+
uri: filename.to_url()?,
2478+
},
2479+
position: Position { line, character },
2480+
},
2481+
)?;
2482+
2483+
if !handle {
2484+
return Ok(result);
2485+
}
2486+
2487+
let locations: Vec<Location> = serde_json::from_value(result.clone())?;
2488+
self.display_locations(&locations, &languageId)?;
2489+
2490+
info!("End {}", REQUEST__CqueryBase);
2491+
Ok(result)
2492+
}
2493+
2494+
fn cquery_derived(&self, params: &Option<Params>) -> Result<Value> {
2495+
info!("Begin {}", REQUEST__CqueryDerived);
2496+
2497+
let (buftype, languageId, filename, line, character, handle): (String, String, String, u64, u64, bool) =
2498+
self.gather_args(
2499+
&[
2500+
VimVar::Buftype,
2501+
VimVar::LanguageId,
2502+
VimVar::Filename,
2503+
VimVar::Line,
2504+
VimVar::Character,
2505+
VimVar::Handle,
2506+
],
2507+
params,
2508+
)?;
2509+
if !buftype.is_empty() || languageId.is_empty() {
2510+
return Ok(Value::Null);
2511+
}
2512+
2513+
let result = self.call(
2514+
Some(&languageId),
2515+
REQUEST__CqueryDerived,
2516+
TextDocumentPositionParams {
2517+
text_document: TextDocumentIdentifier {
2518+
uri: filename.to_url()?,
2519+
},
2520+
position: Position { line, character },
2521+
},
2522+
)?;
2523+
2524+
if !handle {
2525+
return Ok(result);
2526+
}
2527+
2528+
let locations: Vec<Location> = serde_json::from_value(result.clone())?;
2529+
self.display_locations(&locations, &languageId)?;
2530+
2531+
info!("End {}", REQUEST__CqueryDerived);
2532+
Ok(result)
2533+
}
2534+
2535+
fn cquery_callers(&self, params: &Option<Params>) -> Result<Value> {
2536+
info!("Begin {}", REQUEST__CqueryCallers);
2537+
2538+
let (buftype, languageId, filename, line, character, handle): (String, String, String, u64, u64, bool) =
2539+
self.gather_args(
2540+
&[
2541+
VimVar::Buftype,
2542+
VimVar::LanguageId,
2543+
VimVar::Filename,
2544+
VimVar::Line,
2545+
VimVar::Character,
2546+
VimVar::Handle,
2547+
],
2548+
params,
2549+
)?;
2550+
if !buftype.is_empty() || languageId.is_empty() {
2551+
return Ok(Value::Null);
2552+
}
2553+
2554+
let result = self.call(
2555+
Some(&languageId),
2556+
REQUEST__CqueryCallers,
2557+
TextDocumentPositionParams {
2558+
text_document: TextDocumentIdentifier {
2559+
uri: filename.to_url()?,
2560+
},
2561+
position: Position { line, character },
2562+
},
2563+
)?;
2564+
2565+
if !handle {
2566+
return Ok(result);
2567+
}
2568+
2569+
let locations: Vec<Location> = serde_json::from_value(result.clone())?;
2570+
self.display_locations(&locations, &languageId)?;
2571+
2572+
info!("End {}", REQUEST__CqueryCallers);
2573+
Ok(result)
2574+
}
2575+
2576+
fn cquery_vars(&self, params: &Option<Params>) -> Result<Value> {
2577+
info!("Begin {}", REQUEST__CqueryVars);
2578+
2579+
let (buftype, languageId, filename, line, character, handle): (String, String, String, u64, u64, bool) =
2580+
self.gather_args(
2581+
&[
2582+
VimVar::Buftype,
2583+
VimVar::LanguageId,
2584+
VimVar::Filename,
2585+
VimVar::Line,
2586+
VimVar::Character,
2587+
VimVar::Handle,
2588+
],
2589+
params,
2590+
)?;
2591+
if !buftype.is_empty() || languageId.is_empty() {
2592+
return Ok(Value::Null);
2593+
}
2594+
2595+
let result = self.call(
2596+
Some(&languageId),
2597+
REQUEST__CqueryVars,
2598+
TextDocumentPositionParams {
2599+
text_document: TextDocumentIdentifier {
2600+
uri: filename.to_url()?,
2601+
},
2602+
position: Position { line, character },
2603+
},
2604+
)?;
2605+
2606+
if !handle {
2607+
return Ok(result);
2608+
}
2609+
2610+
let locations: Vec<Location> = serde_json::from_value(result.clone())?;
2611+
self.display_locations(&locations, &languageId)?;
2612+
2613+
info!("End {}", REQUEST__CqueryVars);
2614+
Ok(result)
2615+
}
2616+
24492617
fn cquery_handleProgress(&self, params: &Option<Params>) -> Result<()> {
24502618
info!("Begin {}", NOTIFICATION__CqueryProgress);
24512619
let params: CqueryProgressParams = serde_json::from_value(params.clone().to_value())?;

src/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ pub const REQUEST__RustImplementations: &str = "rustDocument/implementations";
3030
pub const NOTIFICATION__RustBeginBuild: &str = "rustDocument/beginBuild";
3131
pub const NOTIFICATION__RustDiagnosticsBegin: &str = "rustDocument/diagnosticsBegin";
3232
pub const NOTIFICATION__RustDiagnosticsEnd: &str = "rustDocument/diagnosticsEnd";
33+
pub const REQUEST__CqueryBase: &str = "$cquery/base";
34+
pub const REQUEST__CqueryCallers: &str = "$cquery/callers";
35+
pub const REQUEST__CqueryDerived: &str = "$cquery/derived";
36+
pub const REQUEST__CqueryVars: &str = "$cquery/vars";
3337
pub const NOTIFICATION__CqueryProgress: &str = "$cquery/progress";
3438
pub const NOTIFICATION__LanguageStatus: &str = "language/status";
3539

0 commit comments

Comments
 (0)