From 38853459e3d964cc7f635829cdc66f5faee33d85 Mon Sep 17 00:00:00 2001 From: ice1000 Date: Tue, 3 Dec 2019 15:24:02 -0500 Subject: Add `ModuleSource::Block` --- crates/ra_hir/src/code_model.rs | 65 ++---------------------- crates/ra_hir/src/code_model/src.rs | 3 +- crates/ra_hir/src/from_source.rs | 8 +-- crates/ra_hir/src/lib.rs | 5 +- crates/ra_hir/src/source_binder.rs | 3 +- crates/ra_hir_def/src/nameres.rs | 68 +++++++++++++++++++++++++- crates/ra_ide/src/display/navigation_target.rs | 43 ++++++---------- crates/ra_ide/src/references/rename.rs | 2 +- crates/ra_ide/src/references/search_scope.rs | 5 ++ 9 files changed, 102 insertions(+), 100 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 5877afefa..7706399ae 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -11,6 +11,7 @@ use hir_def::{ builtin_type::BuiltinType, docs::Documentation, expr::{BindingAnnotation, Pat, PatId}, + nameres::ModuleSource, per_ns::PerNs, resolver::HasResolver, type_ref::{Mutability, TypeRef}, @@ -21,11 +22,11 @@ use hir_def::{ use hir_expand::{ diagnostics::DiagnosticSink, name::{self, AsName}, - AstId, MacroDefId, + MacroDefId, }; use hir_ty::expr::ExprValidator; -use ra_db::{CrateId, Edition, FileId, FilePosition}; -use ra_syntax::{ast, AstNode, SyntaxNode}; +use ra_db::{CrateId, Edition}; +use ra_syntax::ast; use crate::{ db::{DefDatabase, HirDatabase}, @@ -79,64 +80,6 @@ impl Crate { } } -pub enum ModuleSource { - SourceFile(ast::SourceFile), - Module(ast::Module), -} - -impl ModuleSource { - pub fn new( - db: &impl DefDatabase, - file_id: Option, - decl_id: Option>, - ) -> ModuleSource { - match (file_id, decl_id) { - (Some(file_id), _) => { - let source_file = db.parse(file_id).tree(); - ModuleSource::SourceFile(source_file) - } - (None, Some(item_id)) => { - let module = item_id.to_node(db); - assert!(module.item_list().is_some(), "expected inline module"); - ModuleSource::Module(module) - } - (None, None) => panic!(), - } - } - - // FIXME: this methods do not belong here - pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource { - let parse = db.parse(position.file_id); - match &ra_syntax::algo::find_node_at_offset::( - parse.tree().syntax(), - position.offset, - ) { - Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), - _ => { - let source_file = parse.tree(); - ModuleSource::SourceFile(source_file) - } - } - } - - pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource { - if let Some(m) = - child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) - { - ModuleSource::Module(m) - } else { - let file_id = child.file_id.original_file(db); - let source_file = db.parse(file_id).tree(); - ModuleSource::SourceFile(source_file) - } - } - - pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource { - let source_file = db.parse(file_id).tree(); - ModuleSource::SourceFile(source_file) - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Module { pub(crate) id: ModuleId, diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 36cfbc8f1..083946729 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -2,6 +2,7 @@ use either::Either; use hir_def::{ + nameres::ModuleSource, src::{HasChildSource, HasSource as _}, AstItemDef, Lookup, VariantId, }; @@ -9,7 +10,7 @@ use ra_syntax::ast; use crate::{ db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, - Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, + Module, Static, Struct, StructField, Trait, TypeAlias, Union, }; pub use hir_expand::InFile; diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 82bf641dc..94a5e0b0d 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir_def::{AstItemDef, LocationCtx, ModuleId}; +use hir_def::{nameres::ModuleSource, AstItemDef, LocationCtx, ModuleId}; use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; use ra_syntax::{ ast::{self, AstNode, NameOwner}, @@ -10,8 +10,8 @@ use ra_syntax::{ use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, - InFile, Local, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, - TypeAlias, Union, VariantDef, + InFile, Local, MacroDef, Module, ModuleDef, Static, Struct, StructField, Trait, TypeAlias, + Union, VariantDef, }; pub trait FromSource: Sized { @@ -257,7 +257,7 @@ impl Module { InFile { file_id: src.file_id, value: module.clone() }, ); } - ModuleSource::SourceFile(_) => (), + ModuleSource::SourceFile(_) | ModuleSource::Block(_) => (), }; let original_file = src.file_id.original_file(db); diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 853760cb1..f12e4ca3f 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -43,8 +43,8 @@ pub use crate::{ code_model::{ src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, - HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, - Static, Struct, StructField, Trait, Type, TypeAlias, Union, VariantDef, + HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, + StructField, Trait, Type, TypeAlias, Union, VariantDef, }, from_source::FromSource, source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, @@ -59,6 +59,7 @@ pub use hir_def::{ body::scope::ExprScopes, builtin_type::BuiltinType, docs::Documentation, + nameres::ModuleSource, path::{Path, PathKind}, type_ref::Mutability, }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 28d41b647..db0451059 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -14,6 +14,7 @@ use hir_def::{ BodySourceMap, }, expr::{ExprId, PatId}, + nameres::ModuleSource, path::known, resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, AssocItemId, DefWithBodyId, @@ -46,7 +47,7 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) - Some(crate::Module::from_declaration(db, src)?.id.resolver(db)) }, ast::SourceFile(it) => { - let src = node.with_value(crate::ModuleSource::SourceFile(it)); + let src = node.with_value(ModuleSource::SourceFile(it)); Some(crate::Module::from_definition(db, src)?.id.resolver(db)) }, ast::StructDef(it) => { diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index ca8cbcd42..feb2a4d32 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -63,9 +63,12 @@ use hir_expand::{ }; use once_cell::sync::Lazy; use ra_arena::Arena; -use ra_db::{CrateId, Edition, FileId}; +use ra_db::{CrateId, Edition, FileId, FilePosition}; use ra_prof::profile; -use ra_syntax::ast; +use ra_syntax::{ + ast::{self, AstNode}, + SyntaxNode, +}; use rustc_hash::FxHashMap; use crate::{ @@ -361,6 +364,67 @@ impl ModuleData { } } +pub enum ModuleSource { + SourceFile(ast::SourceFile), + Module(ast::Module), + Block(ast::Block), +} + +impl ModuleSource { + pub fn new( + db: &impl DefDatabase, + file_id: Option, + decl_id: Option>, + ) -> ModuleSource { + match (file_id, decl_id) { + (Some(file_id), _) => { + let source_file = db.parse(file_id).tree(); + ModuleSource::SourceFile(source_file) + } + (None, Some(item_id)) => { + let module = item_id.to_node(db); + assert!(module.item_list().is_some(), "expected inline module"); + ModuleSource::Module(module) + } + (None, None) => panic!(), + } + } + + // FIXME: this methods do not belong here + pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource { + let parse = db.parse(position.file_id); + match &ra_syntax::algo::find_node_at_offset::( + parse.tree().syntax(), + position.offset, + ) { + Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), + _ => { + let source_file = parse.tree(); + ModuleSource::SourceFile(source_file) + } + } + } + + pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource { + if let Some(m) = + child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) + { + ModuleSource::Module(m) + } else if let Some(b) = child.value.ancestors().filter_map(ast::Block::cast).next() { + ModuleSource::Block(b) + } else { + let file_id = child.file_id.original_file(db); + let source_file = db.parse(file_id).tree(); + ModuleSource::SourceFile(source_file) + } + } + + pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource { + let source_file = db.parse(file_id).tree(); + ModuleSource::SourceFile(source_file) + } +} + mod diagnostics { use hir_expand::diagnostics::DiagnosticSink; use ra_db::RelativePathBuf; diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index f920d3db6..b376fcdae 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -231,34 +231,21 @@ impl ToNav for hir::Module { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { let src = self.definition_source(db); let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default(); - match &src.value { - ModuleSource::SourceFile(node) => { - let frange = original_range(db, src.with_value(node.syntax())); - - NavigationTarget::from_syntax( - frange.file_id, - name, - None, - frange.range, - node.syntax().kind(), - None, - None, - ) - } - ModuleSource::Module(node) => { - let frange = original_range(db, src.with_value(node.syntax())); - - NavigationTarget::from_syntax( - frange.file_id, - name, - None, - frange.range, - node.syntax().kind(), - node.doc_comment_text(), - node.short_label(), - ) - } - } + let syntax = match &src.value { + ModuleSource::SourceFile(node) => node.syntax(), + ModuleSource::Module(node) => node.syntax(), + ModuleSource::Block(node) => node.syntax(), + }; + let frange = original_range(db, src.with_value(syntax)); + NavigationTarget::from_syntax( + frange.file_id, + name, + None, + frange.range, + syntax.kind(), + None, + None, + ) } } diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index b804d5f6d..1abf20215 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -88,7 +88,7 @@ fn rename_mod( file_system_edits.push(move_file); } } - ModuleSource::Module(..) => {} + ModuleSource::Module(..) | ModuleSource::Block(..) => {} } } diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs index f5c9589f4..92eb2015b 100644 --- a/crates/ra_ide/src/references/search_scope.rs +++ b/crates/ra_ide/src/references/search_scope.rs @@ -96,6 +96,10 @@ impl NameDefinition { let range = Some(m.syntax().text_range()); res.insert(file_id, range); } + ModuleSource::Block(b) => { + let range = Some(b.syntax().text_range()); + res.insert(file_id, range); + } ModuleSource::SourceFile(_) => { res.insert(file_id, None); res.extend(parent_module.children(db).map(|m| { @@ -137,6 +141,7 @@ impl NameDefinition { let mut res = FxHashMap::default(); let range = match module_src.value { ModuleSource::Module(m) => Some(m.syntax().text_range()), + ModuleSource::Block(b) => Some(b.syntax().text_range()), ModuleSource::SourceFile(_) => None, }; res.insert(file_id, range); -- cgit v1.2.3