aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/imp.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-12-08 18:18:29 +0000
committerAleksey Kladov <[email protected]>2018-12-08 18:18:29 +0000
commit7fd6a41127dc9a60efe703f7d588f8555b8bffc6 (patch)
tree91695d49c0dd47ce0543161b57b8986fa37eeb6b /crates/ra_analysis/src/imp.rs
parent7a79cde107ec71abbc7c715e933f29f7a1fb2b95 (diff)
Refactor symbol resolve API
Introduce ReferenceResolution to avoid nesting to many non-nominal types.
Diffstat (limited to 'crates/ra_analysis/src/imp.rs')
-rw-r--r--crates/ra_analysis/src/imp.rs27
1 files changed, 14 insertions, 13 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index 99bcf0666..03d17de0d 100644
--- a/crates/ra_analysis/src/imp.rs
+++ b/crates/ra_analysis/src/imp.rs
@@ -27,6 +27,7 @@ use crate::{
27 symbol_index::{SymbolIndex, SymbolsDatabase}, 27 symbol_index::{SymbolIndex, SymbolsDatabase},
28 AnalysisChange, Cancelable, CrateId, Diagnostic, FileId, 28 AnalysisChange, Cancelable, CrateId, Diagnostic, FileId,
29 FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit, 29 FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit,
30 ReferenceResolution,
30}; 31};
31 32
32#[derive(Debug, Default)] 33#[derive(Debug, Default)]
@@ -206,10 +207,11 @@ impl AnalysisImpl {
206 pub fn approximately_resolve_symbol( 207 pub fn approximately_resolve_symbol(
207 &self, 208 &self,
208 position: FilePosition, 209 position: FilePosition,
209 ) -> Cancelable<Option<(TextRange, Vec<(FileId, FileSymbol)>)>> { 210 ) -> Cancelable<Option<ReferenceResolution>> {
210 let file = self.db.source_file(position.file_id); 211 let file = self.db.source_file(position.file_id);
211 let syntax = file.syntax(); 212 let syntax = file.syntax();
212 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) { 213 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
214 let mut rr = ReferenceResolution::new(name_ref.syntax().range());
213 if let Some(fn_descr) = source_binder::function_from_child_node( 215 if let Some(fn_descr) = source_binder::function_from_child_node(
214 &*self.db, 216 &*self.db,
215 position.file_id, 217 position.file_id,
@@ -218,24 +220,25 @@ impl AnalysisImpl {
218 let scope = fn_descr.scope(&*self.db); 220 let scope = fn_descr.scope(&*self.db);
219 // First try to resolve the symbol locally 221 // First try to resolve the symbol locally
220 if let Some(entry) = scope.resolve_local_name(name_ref) { 222 if let Some(entry) = scope.resolve_local_name(name_ref) {
221 let vec = vec![( 223 rr.add_resolution(
222 position.file_id, 224 position.file_id,
223 FileSymbol { 225 FileSymbol {
224 name: entry.name().clone(), 226 name: entry.name().clone(),
225 node_range: entry.ptr().range(), 227 node_range: entry.ptr().range(),
226 kind: NAME, 228 kind: NAME,
227 }, 229 },
228 )]; 230 );
229 return Ok(Some((name_ref.syntax().range(), vec))); 231 return Ok(Some(rr));
230 }; 232 };
231 } 233 }
232 // If that fails try the index based approach. 234 // If that fails try the index based approach.
233 return Ok(Some(( 235 for (file_id, symbol) in self.index_resolve(name_ref)? {
234 name_ref.syntax().range(), 236 rr.add_resolution(file_id, symbol);
235 self.index_resolve(name_ref)?, 237 }
236 ))); 238 return Ok(Some(rr));
237 } 239 }
238 if let Some(name) = find_node_at_offset::<ast::Name>(syntax, position.offset) { 240 if let Some(name) = find_node_at_offset::<ast::Name>(syntax, position.offset) {
241 let mut rr = ReferenceResolution::new(name.syntax().range());
239 if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { 242 if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
240 if module.has_semi() { 243 if module.has_semi() {
241 let parent_module = 244 let parent_module =
@@ -250,7 +253,8 @@ impl AnalysisImpl {
250 node_range: TextRange::offset_len(0.into(), 0.into()), 253 node_range: TextRange::offset_len(0.into(), 0.into()),
251 kind: MODULE, 254 kind: MODULE,
252 }; 255 };
253 return Ok(Some((name.syntax().range(), vec![(file_id, symbol)]))); 256 rr.add_resolution(file_id, symbol);
257 return Ok(Some(rr));
254 } 258 }
255 } 259 }
256 _ => (), 260 _ => (),
@@ -258,10 +262,7 @@ impl AnalysisImpl {
258 } 262 }
259 } 263 }
260 } 264 }
261 let range = 265 Ok(None)
262 ctry!(ra_syntax::algo::find_leaf_at_offset(syntax, position.offset).left_biased())
263 .range();
264 Ok(Some((range, vec![])))
265 } 266 }
266 267
267 pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> { 268 pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {