Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support returning non-hierarchical symbols #4113

Merged
merged 2 commits into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub enum RustfmtConfig {
pub struct ClientCapsConfig {
pub location_link: bool,
pub line_folding_only: bool,
pub hierarchical_symbols: bool,
}

impl Default for Config {
Expand Down Expand Up @@ -215,6 +216,11 @@ impl Config {
if let Some(value) = caps.folding_range.as_ref().and_then(|it| it.line_folding_only) {
self.client_caps.line_folding_only = value
}
if let Some(value) =
caps.document_symbol.as_ref().and_then(|it| it.hierarchical_document_symbol_support)
{
self.client_caps.hierarchical_symbols = value
}
self.completion.allow_snippets(false);
if let Some(completion) = &caps.completion {
if let Some(completion_item) = &completion.completion_item {
Expand Down
37 changes: 33 additions & 4 deletions crates/rust-analyzer/src/main_loop/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use lsp_types::{
Hover, HoverContents, Location, MarkupContent, MarkupKind, Position, PrepareRenameResponse,
Range, RenameParams, SemanticTokensParams, SemanticTokensRangeParams,
SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, TextDocumentIdentifier,
TextEdit, WorkspaceEdit,
TextEdit, Url, WorkspaceEdit,
};
use ra_ide::{
Assist, AssistId, FileId, FilePosition, FileRange, Query, RangeInfo, Runnable, RunnableKind,
Expand Down Expand Up @@ -219,6 +219,7 @@ pub fn handle_document_symbol(
let _p = profile("handle_document_symbol");
let file_id = params.text_document.try_conv_with(&world)?;
let line_index = world.analysis().file_line_index(file_id)?;
let url = file_id.try_conv_with(&world)?;

let mut parents: Vec<(DocumentSymbol, Option<usize>)> = Vec::new();

Expand All @@ -234,10 +235,10 @@ pub fn handle_document_symbol(
};
parents.push((doc_symbol, symbol.parent));
}
let mut res = Vec::new();
let mut document_symbols = Vec::new();
while let Some((node, parent)) = parents.pop() {
match parent {
None => res.push(node),
None => document_symbols.push(node),
Some(i) => {
let children = &mut parents[i].0.children;
if children.is_none() {
Expand All @@ -248,7 +249,35 @@ pub fn handle_document_symbol(
}
}

Ok(Some(res.into()))
if world.config.client_caps.hierarchical_symbols {
Ok(Some(document_symbols.into()))
} else {
let mut symbol_information = Vec::<SymbolInformation>::new();
for symbol in document_symbols {
flatten_document_symbol(&symbol, None, &url, &mut symbol_information);
}

Ok(Some(symbol_information.into()))
}
}

fn flatten_document_symbol(
symbol: &DocumentSymbol,
container_name: Option<String>,
url: &Url,
res: &mut Vec<SymbolInformation>,
) {
res.push(SymbolInformation {
name: symbol.name.clone(),
kind: symbol.kind,
deprecated: symbol.deprecated,
location: Location::new(url.clone(), symbol.range),
container_name: container_name,
});

for child in symbol.children.iter().flatten() {
flatten_document_symbol(child, Some(symbol.name.clone()), url, res);
}
}

pub fn handle_workspace_symbol(
Expand Down