From 0192c116f78b3a81ef15587eecac29a2676ae00c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 5 Nov 2018 14:57:41 +0300 Subject: Use FilePosition everywhere --- crates/ra_analysis/src/imp.rs | 74 ++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 40 deletions(-) (limited to 'crates/ra_analysis/src/imp.rs') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 86d9cecd2..f2482559f 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -220,16 +220,13 @@ impl AnalysisImpl { let source_root = self.db.file_source_root(file_id); self.db.module_tree(source_root) } - pub fn parent_module( - &self, - file_id: FileId, - offset: TextUnit, - ) -> Cancelable> { - let module_tree = self.module_tree(file_id)?; - let file = self.db.file_syntax(file_id); - let module_source = match find_node_at_offset::(file.syntax(), offset) { - Some(m) if !m.has_semi() => ModuleSource::new_inline(file_id, m), - _ => ModuleSource::File(file_id), + pub fn parent_module(&self, position: FilePosition) -> Cancelable> { + let module_tree = self.module_tree(position.file_id)?; + let file = self.db.file_syntax(position.file_id); + let module_source = match find_node_at_offset::(file.syntax(), position.offset) + { + Some(m) if !m.has_semi() => ModuleSource::new_inline(position.file_id, m), + _ => ModuleSource::File(position.file_id), }; let res = module_tree @@ -269,18 +266,14 @@ impl AnalysisImpl { pub fn crate_root(&self, crate_id: CrateId) -> FileId { self.db.crate_graph().crate_roots[&crate_id] } - pub fn completions( - &self, - file_id: FileId, - offset: TextUnit, - ) -> Cancelable>> { + pub fn completions(&self, position: FilePosition) -> Cancelable>> { let mut res = Vec::new(); let mut has_completions = false; - if let Some(scope_based) = scope_completion(&self.db, file_id, offset) { + if let Some(scope_based) = scope_completion(&self.db, position) { res.extend(scope_based); has_completions = true; } - if let Some(scope_based) = resolve_based_completion(&self.db, file_id, offset)? { + if let Some(scope_based) = resolve_based_completion(&self.db, position)? { res.extend(scope_based); has_completions = true; } @@ -289,18 +282,19 @@ impl AnalysisImpl { } pub fn approximately_resolve_symbol( &self, - file_id: FileId, - offset: TextUnit, + position: FilePosition, ) -> Cancelable> { - let module_tree = self.module_tree(file_id)?; - let file = self.db.file_syntax(file_id); + let module_tree = self.module_tree(position.file_id)?; + let file = self.db.file_syntax(position.file_id); let syntax = file.syntax(); - if let Some(name_ref) = find_node_at_offset::(syntax, offset) { + if let Some(name_ref) = find_node_at_offset::(syntax, position.offset) { // First try to resolve the symbol locally - return if let Some((name, range)) = resolve_local_name(&self.db, file_id, name_ref) { + return if let Some((name, range)) = + resolve_local_name(&self.db, position.file_id, name_ref) + { let mut vec = vec![]; vec.push(( - file_id, + position.file_id, FileSymbol { name, node_range: range, @@ -313,10 +307,10 @@ impl AnalysisImpl { self.index_resolve(name_ref) }; } - if let Some(name) = find_node_at_offset::(syntax, offset) { + if let Some(name) = find_node_at_offset::(syntax, position.offset) { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { if module.has_semi() { - let file_ids = self.resolve_module(&*module_tree, file_id, module); + let file_ids = self.resolve_module(&*module_tree, position.file_id, module); let res = file_ids .into_iter() @@ -341,16 +335,17 @@ impl AnalysisImpl { Ok(vec![]) } - pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit) -> Vec<(FileId, TextRange)> { - let file = self.db.file_syntax(file_id); + pub fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> { + let file = self.db.file_syntax(position.file_id); let syntax = file.syntax(); // Find the binding associated with the offset - let maybe_binding = find_node_at_offset::(syntax, offset).or_else(|| { - let name_ref = find_node_at_offset::(syntax, offset)?; - let resolved = resolve_local_name(&self.db, file_id, name_ref)?; - find_node_at_offset::(syntax, resolved.1.end()) - }); + let maybe_binding = + find_node_at_offset::(syntax, position.offset).or_else(|| { + let name_ref = find_node_at_offset::(syntax, position.offset)?; + let resolved = resolve_local_name(&self.db, position.file_id, name_ref)?; + find_node_at_offset::(syntax, resolved.1.end()) + }); let binding = match maybe_binding { None => return Vec::new(), @@ -359,11 +354,11 @@ impl AnalysisImpl { let decl = DeclarationDescriptor::new(binding); - let mut ret = vec![(file_id, decl.range)]; + let mut ret = vec![(position.file_id, decl.range)]; ret.extend( decl.find_all_refs() .into_iter() - .map(|ref_desc| (file_id, ref_desc.range)), + .map(|ref_desc| (position.file_id, ref_desc.range)), ); ret @@ -457,14 +452,13 @@ impl AnalysisImpl { pub fn resolve_callable( &self, - file_id: FileId, - offset: TextUnit, + position: FilePosition, ) -> Cancelable)>> { - let file = self.db.file_syntax(file_id); + let file = self.db.file_syntax(position.file_id); let syntax = file.syntax(); // Find the calling expression and it's NameRef - let calling_node = match FnCallNode::with_node(syntax, offset) { + let calling_node = match FnCallNode::with_node(syntax, position.offset) { Some(node) => node, None => return Ok(None), }; @@ -499,7 +493,7 @@ impl AnalysisImpl { if let Some(ref arg_list) = calling_node.arg_list() { let start = arg_list.syntax().range().start(); - let range_search = TextRange::from_to(start, offset); + let range_search = TextRange::from_to(start, position.offset); let mut commas: usize = arg_list .syntax() .text() -- cgit v1.2.3