aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/symbol_index.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-02-23 12:17:53 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-02-23 12:17:53 +0000
commite5fb33a94618d45051192d99e859bdd44c3daa36 (patch)
treeb77da2812c973e2270d7abf808634cb9655e2c68 /crates/ra_ide_api/src/symbol_index.rs
parent38add103c3f30493e336a37827a76f48a6e7c584 (diff)
parent40e6cb196b3e4fdb580812a418edfb8df08cf423 (diff)
Merge #879
879: Fixes to goto definition r=vipentti a=vipentti Previously goto definition would fail when the cursor was over the name of the definition. Now we should properly resolve to a `NavigationTarget` when on top of the name of a definition. In addition this adds `name_range` field to `FileSymbol`, this further fixes goto_definition and symbol based navigation by allowing the `NavigationTarget` to actually have a `focus_range`, meaning instead of focusing on the start of the `full_range`, we can have the cursor focus on the name. e.g. goto definition ```rust fn bar() { fn foo() { } foo<|>(); } ``` Previously this would put the cursor at the start of the FN_DEF: ```rust fn bar() { <|>fn foo() { } foo(); } ``` Now when using the symbol based resolving, we'll have a proper focus range and instead put the cursor at the start of the name. ```rust fn bar() { fn <|>foo() { } foo(); } ``` This fixes #877 but doesn't contain the refactoring of the return type for `goto_definition` Co-authored-by: Ville Penttinen <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/symbol_index.rs')
-rw-r--r--crates/ra_ide_api/src/symbol_index.rs22
1 files changed, 16 insertions, 6 deletions
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::{
33 SyntaxKind::{self, *}, 33 SyntaxKind::{self, *},
34 ast::{self, NameOwner}, 34 ast::{self, NameOwner},
35 WalkEvent, 35 WalkEvent,
36 TextRange,
36}; 37};
37use ra_db::{ 38use ra_db::{
38 SourceRootId, SourceDatabase, 39 SourceRootId, SourceDatabase,
@@ -70,7 +71,7 @@ fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc<SymbolIndex>
70 let node = find_covering_node(source_file.syntax(), text_range); 71 let node = find_covering_node(source_file.syntax(), text_range);
71 let ptr = SyntaxNodePtr::new(node); 72 let ptr = SyntaxNodePtr::new(node);
72 // TODO: Should we get container name for macro symbols? 73 // TODO: Should we get container name for macro symbols?
73 symbols.push(FileSymbol { file_id, name, ptr, container_name: None }) 74 symbols.push(FileSymbol { file_id, name, ptr, name_range: None, container_name: None })
74 } 75 }
75 76
76 Arc::new(SymbolIndex::new(symbols)) 77 Arc::new(SymbolIndex::new(symbols))
@@ -207,6 +208,7 @@ pub(crate) struct FileSymbol {
207 pub(crate) file_id: FileId, 208 pub(crate) file_id: FileId,
208 pub(crate) name: SmolStr, 209 pub(crate) name: SmolStr,
209 pub(crate) ptr: SyntaxNodePtr, 210 pub(crate) ptr: SyntaxNodePtr,
211 pub(crate) name_range: Option<TextRange>,
210 pub(crate) container_name: Option<SmolStr>, 212 pub(crate) container_name: Option<SmolStr>,
211} 213}
212 214
@@ -236,12 +238,14 @@ fn source_file_to_file_symbols(source_file: &SourceFile, file_id: FileId) -> Vec
236 symbols 238 symbols
237} 239}
238 240
239fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr)> { 241fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> {
240 fn decl<N: NameOwner>(node: &N) -> Option<(SmolStr, SyntaxNodePtr)> { 242 fn decl<N: NameOwner>(node: &N) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> {
241 let name = node.name()?.text().clone(); 243 let name = node.name()?;
244 let name_range = name.syntax().range();
245 let name = name.text().clone();
242 let ptr = SyntaxNodePtr::new(node.syntax()); 246 let ptr = SyntaxNodePtr::new(node.syntax());
243 247
244 Some((name, ptr)) 248 Some((name, ptr, name_range))
245 } 249 }
246 visitor() 250 visitor()
247 .visit(decl::<ast::FnDef>) 251 .visit(decl::<ast::FnDef>)
@@ -256,5 +260,11 @@ fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr)> {
256} 260}
257 261
258fn to_file_symbol(node: &SyntaxNode, file_id: FileId) -> Option<FileSymbol> { 262fn to_file_symbol(node: &SyntaxNode, file_id: FileId) -> Option<FileSymbol> {
259 to_symbol(node).map(move |(name, ptr)| FileSymbol { name, ptr, file_id, container_name: None }) 263 to_symbol(node).map(move |(name, ptr, name_range)| FileSymbol {
264 name,
265 ptr,
266 file_id,
267 name_range: Some(name_range),
268 container_name: None,
269 })
260} 270}