Add new LSP extension for workspace symbol lookup

The new extension allows filtering of workspace symbool lookup
results by search scope or search kind.

Filtering can be configured in 3 different ways:

 - The '#' or '*' markers can be added inline with the symbol lookup
   query.

   The '#' marker means symbols should be looked up in the current
   workspace and any dependencies. If not specified, only current
   workspace is considered.

   The '*' marker means all kinds of symbols should be looked up
   (types, functions, etc). If not specified, only type symbols are
   returned.

 - Each LSP request can take an optional search_scope or search_kind
   argument query parameter.

 - Finally there are 2 global config options that can be set for all
   requests served by the active RA instance.

Add support for setting the global config options to the VSCode
extension.
The extension does not use the per-request way, but it's useful for
other IDEs.

The latest version of VSCode filters out the inline markers, so
currently the only reasonable way to use the new functionality is
via the global config.
This commit is contained in:
alcroito
2021-02-23 14:03:31 +02:00
parent c04eaa1f37
commit 1f7d2a6c22
8 changed files with 220 additions and 8 deletions

View File

@@ -38,7 +38,7 @@ use crate::{
from_proto,
global_state::{GlobalState, GlobalStateSnapshot},
line_index::LineEndings,
lsp_ext::{self, InlayHint, InlayHintsParams},
lsp_ext::{self, InlayHint, InlayHintsParams, WorkspaceSymbolParams},
lsp_utils::all_edits_are_disjoint,
to_proto, LspError, Result,
};
@@ -380,11 +380,12 @@ pub(crate) fn handle_document_symbol(
pub(crate) fn handle_workspace_symbol(
snap: GlobalStateSnapshot,
params: lsp_types::WorkspaceSymbolParams,
params: WorkspaceSymbolParams,
) -> Result<Option<Vec<SymbolInformation>>> {
let _p = profile::span("handle_workspace_symbol");
let all_symbols = params.query.contains('#');
let libs = params.query.contains('*');
let (all_symbols, libs) = decide_search_scope_and_kind(&params, &snap);
let query = {
let query: String = params.query.chars().filter(|&c| c != '#' && c != '*').collect();
let mut q = Query::new(query);
@@ -406,6 +407,45 @@ pub(crate) fn handle_workspace_symbol(
return Ok(Some(res));
fn decide_search_scope_and_kind(
params: &WorkspaceSymbolParams,
snap: &GlobalStateSnapshot,
) -> (bool, bool) {
// Support old-style parsing of markers in the query.
let mut all_symbols = params.query.contains('#');
let mut libs = params.query.contains('*');
let config = snap.config.workspace_symbol();
// If no explicit marker was set, check request params. If that's also empty
// use global config.
if !all_symbols {
let search_kind = if let Some(ref search_kind) = params.search_kind {
search_kind
} else {
&config.search_kind
};
all_symbols = match search_kind {
lsp_ext::WorkspaceSymbolSearchKind::OnlyTypes => false,
lsp_ext::WorkspaceSymbolSearchKind::AllSymbols => true,
}
}
if !libs {
let search_scope = if let Some(ref search_scope) = params.search_scope {
search_scope
} else {
&config.search_scope
};
libs = match search_scope {
lsp_ext::WorkspaceSymbolSearchScope::Workspace => false,
lsp_ext::WorkspaceSymbolSearchScope::WorkspaceAndDependencies => true,
}
}
(all_symbols, libs)
}
fn exec_query(snap: &GlobalStateSnapshot, query: Query) -> Result<Vec<SymbolInformation>> {
let mut res = Vec::new();
for nav in snap.analysis.symbol_search(query)? {