From c3a4c4429de83450654795534e64e878a774a088 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Feb 2020 18:35:10 +0100 Subject: Refactor primary IDE API This introduces the new type -- Semantics. Semantics maps SyntaxNodes to various semantic info, such as type, name resolution or macro expansions. To do so, Semantics maintains a HashMap which maps every node it saw to the file from which the node originated. This is enough to get all the necessary hir bits just from syntax. --- crates/ra_ide/src/references/classify.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'crates/ra_ide/src/references/classify.rs') diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index 478e18871..91b21429a 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs @@ -1,34 +1,32 @@ //! Functions that are used to classify an element from its definition or reference. -use hir::{InFile, PathResolution, SourceBinder}; +use hir::{PathResolution, Semantics}; +use ra_ide_db::defs::NameDefinition; +use ra_ide_db::RootDatabase; use ra_prof::profile; use ra_syntax::{ast, AstNode}; use test_utils::tested_by; -use super::NameDefinition; -use ra_ide_db::RootDatabase; - -pub use ra_ide_db::defs::{classify_name, from_module_def, from_struct_field}; +pub use ra_ide_db::defs::{from_module_def, from_struct_field}; pub(crate) fn classify_name_ref( - sb: &mut SourceBinder, - name_ref: InFile<&ast::NameRef>, + sema: &Semantics, + name_ref: &ast::NameRef, ) -> Option { let _p = profile("classify_name_ref"); - let parent = name_ref.value.syntax().parent()?; - let analyzer = sb.analyze(name_ref.map(|it| it.syntax()), None); + let parent = name_ref.syntax().parent()?; if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { tested_by!(goto_def_for_methods); - if let Some(func) = analyzer.resolve_method_call(&method_call) { + if let Some(func) = sema.resolve_method_call(&method_call) { return Some(from_module_def(func.into())); } } if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { tested_by!(goto_def_for_fields); - if let Some(field) = analyzer.resolve_field(&field_expr) { + if let Some(field) = sema.resolve_field(&field_expr) { return Some(from_struct_field(field)); } } @@ -36,22 +34,20 @@ pub(crate) fn classify_name_ref( if let Some(record_field) = ast::RecordField::cast(parent.clone()) { tested_by!(goto_def_for_record_fields); tested_by!(goto_def_for_field_init_shorthand); - if let Some(field_def) = analyzer.resolve_record_field(&record_field) { + if let Some(field_def) = sema.resolve_record_field(&record_field) { return Some(from_struct_field(field_def)); } } if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { tested_by!(goto_def_for_macros); - if let Some(macro_def) = - analyzer.resolve_macro_call(sb.db, name_ref.with_value(¯o_call)) - { + if let Some(macro_def) = sema.resolve_macro_call(¯o_call) { return Some(NameDefinition::Macro(macro_def)); } } - let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?; - let resolved = analyzer.resolve_path(sb.db, &path)?; + let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; + let resolved = sema.resolve_path(&path)?; let res = match resolved { PathResolution::Def(def) => from_module_def(def), PathResolution::AssocItem(item) => { -- cgit v1.2.3