From 272af56a5c5a0e89fc3c2db41e125f0f16df0d0c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 16 Nov 2019 13:33:25 +0300 Subject: Sourcify classify_name_ref --- crates/ra_ide_api/src/goto_definition.rs | 11 ++++++----- crates/ra_ide_api/src/hover.rs | 5 +++-- crates/ra_ide_api/src/references.rs | 5 +++-- crates/ra_ide_api/src/references/classify.rs | 17 +++++++---------- crates/ra_ide_api/src/syntax_highlighting.rs | 5 +++-- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 6c8387f6c..5b7fb3c15 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -1,5 +1,6 @@ //! FIXME: write short doc here +use hir::Source; use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ algo::find_node_at_offset, @@ -21,7 +22,8 @@ pub(crate) fn goto_definition( let parse = db.parse(position.file_id); let syntax = parse.tree().syntax().clone(); if let Some(name_ref) = find_node_at_offset::(&syntax, position.offset) { - let navs = reference_definition(db, position.file_id, &name_ref).to_vec(); + let navs = + reference_definition(db, Source::new(position.file_id.into(), &name_ref)).to_vec(); return Some(RangeInfo::new(name_ref.syntax().text_range(), navs.to_vec())); } if let Some(name) = find_node_at_offset::(&syntax, position.offset) { @@ -49,12 +51,11 @@ impl ReferenceResult { pub(crate) fn reference_definition( db: &RootDatabase, - file_id: FileId, - name_ref: &ast::NameRef, + name_ref: Source<&ast::NameRef>, ) -> ReferenceResult { use self::ReferenceResult::*; - let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind); + let name_kind = classify_name_ref(db, name_ref).map(|d| d.kind); match name_kind { Some(Macro(mac)) => return Exact(mac.to_nav(db)), Some(Field(field)) => return Exact(field.to_nav(db)), @@ -76,7 +77,7 @@ pub(crate) fn reference_definition( }; // Fallback index based approach: - let navs = crate::symbol_index::index_resolve(db, name_ref) + let navs = crate::symbol_index::index_resolve(db, name_ref.ast) .into_iter() .map(|s| s.to_nav(db)) .collect(); diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 92b4b1f79..cc25f4c37 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir::{Adt, HasSource, HirDisplay}; +use hir::{Adt, HasSource, HirDisplay, Source}; use ra_db::SourceDatabase; use ra_syntax::{ algo::{ancestors_at_offset, find_covering_element, find_node_at_offset}, @@ -171,7 +171,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option(file.syntax(), position.offset) { let mut no_fallback = false; - if let Some(name_kind) = classify_name_ref(db, position.file_id, &name_ref).map(|d| d.kind) + if let Some(name_kind) = + classify_name_ref(db, Source::new(position.file_id.into(), &name_ref)).map(|d| d.kind) { res.extend(hover_text_from_name_kind(db, name_kind, &mut no_fallback)) } diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index faa88d988..1af7e8a9f 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -14,6 +14,7 @@ mod name_definition; mod rename; mod search_scope; +use hir::Source; use once_cell::unsync::Lazy; use ra_db::{SourceDatabase, SourceDatabaseExt}; use ra_prof::profile; @@ -114,7 +115,7 @@ fn find_name<'a>( return Some(RangeInfo::new(range, (name.text().to_string(), def))); } let name_ref = find_node_at_offset::(&syntax, position.offset)?; - let def = classify_name_ref(db, position.file_id, &name_ref)?; + let def = classify_name_ref(db, Source::new(position.file_id.into(), &name_ref))?; let range = name_ref.syntax().text_range(); Some(RangeInfo::new(range, (name_ref.text().to_string(), def))) } @@ -146,7 +147,7 @@ fn process_definition( continue; } } - if let Some(d) = classify_name_ref(db, file_id, &name_ref) { + if let Some(d) = classify_name_ref(db, Source::new(file_id.into(), &name_ref)) { if d == def { refs.push(FileRange { file_id, range }); } diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index f12b58cb9..5ca9da15e 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs @@ -123,14 +123,12 @@ pub(crate) fn classify_name( pub(crate) fn classify_name_ref( db: &RootDatabase, - file_id: FileId, - name_ref: &ast::NameRef, + name_ref: Source<&ast::NameRef>, ) -> Option { let _p = profile("classify_name_ref"); - let parent = name_ref.syntax().parent()?; - let analyzer = - SourceAnalyzer::new(db, hir::Source::new(file_id.into(), name_ref.syntax()), None); + let parent = name_ref.ast.syntax().parent()?; + let analyzer = SourceAnalyzer::new(db, name_ref.map(|it| it.syntax()), None); if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { tested_by!(goto_definition_works_for_methods); @@ -150,17 +148,16 @@ pub(crate) fn classify_name_ref( tested_by!(goto_definition_works_for_record_fields); if let Some(record_lit) = record_field.syntax().ancestors().find_map(ast::RecordLit::cast) { let variant_def = analyzer.resolve_record_literal(&record_lit)?; - let hir_path = Path::from_name_ref(name_ref); + let hir_path = Path::from_name_ref(name_ref.ast); let hir_name = hir_path.as_ident()?; let field = variant_def.field(db, hir_name)?; return Some(from_struct_field(db, field)); } } - let file_id = file_id.into(); - let ast = ModuleSource::from_child_node(db, Source::new(file_id, &parent)); + let ast = ModuleSource::from_child_node(db, name_ref.with_ast(&parent)); // FIXME: find correct container and visibility for each case - let container = Module::from_definition(db, Source { file_id, ast })?; + let container = Module::from_definition(db, name_ref.with_ast(ast))?; let visibility = None; if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { @@ -171,7 +168,7 @@ pub(crate) fn classify_name_ref( } } - let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; + let path = name_ref.ast.syntax().ancestors().find_map(ast::Path::cast)?; let resolved = analyzer.resolve_path(db, &path)?; match resolved { PathResolution::Def(def) => Some(from_module_def(db, def, Some(container))), diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index d53a759ee..584657ca2 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs @@ -2,7 +2,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; -use hir::{Mutability, Name}; +use hir::{Mutability, Name, Source}; use ra_db::SourceDatabase; use ra_prof::profile; use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T}; @@ -80,7 +80,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec