From c565ec2d6e736c90b8c5a6b89795022d1cc1c1a3 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sat, 23 Feb 2019 13:05:45 +0200 Subject: Add name_range field to FileSymbol This contains the syntax range of the name itself, allowing NavigationTarget to properly set the focus_range. This should make it so that when using symbol based navigation, we should always focus on the name, instead of the full range. --- crates/ra_ide_api/src/navigation_target.rs | 2 +- crates/ra_ide_api/src/symbol_index.rs | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) (limited to 'crates/ra_ide_api/src') diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs index 2c9ec970a..ce5ae0363 100644 --- a/crates/ra_ide_api/src/navigation_target.rs +++ b/crates/ra_ide_api/src/navigation_target.rs @@ -67,7 +67,7 @@ impl NavigationTarget { name: symbol.name.clone(), kind: symbol.ptr.kind(), full_range: symbol.ptr.range(), - focus_range: None, + focus_range: symbol.name_range, container_name: symbol.container_name.clone(), } } diff --git a/crates/ra_ide_api/src/symbol_index.rs b/crates/ra_ide_api/src/symbol_index.rs index afb10fa92..93bdf05d8 100644 --- a/crates/ra_ide_api/src/symbol_index.rs +++ b/crates/ra_ide_api/src/symbol_index.rs @@ -33,6 +33,7 @@ use ra_syntax::{ SyntaxKind::{self, *}, ast::{self, NameOwner}, WalkEvent, + TextRange, }; use ra_db::{ SourceRootId, SourceDatabase, @@ -70,7 +71,7 @@ fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc let node = find_covering_node(source_file.syntax(), text_range); let ptr = SyntaxNodePtr::new(node); // TODO: Should we get container name for macro symbols? - symbols.push(FileSymbol { file_id, name, ptr, container_name: None }) + symbols.push(FileSymbol { file_id, name, ptr, name_range: None, container_name: None }) } Arc::new(SymbolIndex::new(symbols)) @@ -207,6 +208,7 @@ pub(crate) struct FileSymbol { pub(crate) file_id: FileId, pub(crate) name: SmolStr, pub(crate) ptr: SyntaxNodePtr, + pub(crate) name_range: Option, pub(crate) container_name: Option, } @@ -236,12 +238,14 @@ fn source_file_to_file_symbols(source_file: &SourceFile, file_id: FileId) -> Vec symbols } -fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr)> { - fn decl(node: &N) -> Option<(SmolStr, SyntaxNodePtr)> { - let name = node.name()?.text().clone(); +fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> { + fn decl(node: &N) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> { + let name = node.name()?; + let name_range = name.syntax().range(); + let name = name.text().clone(); let ptr = SyntaxNodePtr::new(node.syntax()); - Some((name, ptr)) + Some((name, ptr, name_range)) } visitor() .visit(decl::) @@ -256,5 +260,11 @@ fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr)> { } fn to_file_symbol(node: &SyntaxNode, file_id: FileId) -> Option { - to_symbol(node).map(move |(name, ptr)| FileSymbol { name, ptr, file_id, container_name: None }) + to_symbol(node).map(move |(name, ptr, name_range)| FileSymbol { + name, + ptr, + file_id, + name_range: Some(name_range), + container_name: None, + }) } -- cgit v1.2.3