aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs65
-rw-r--r--crates/ra_hir/src/code_model/src.rs3
-rw-r--r--crates/ra_hir/src/from_source.rs8
-rw-r--r--crates/ra_hir/src/lib.rs5
-rw-r--r--crates/ra_hir/src/source_binder.rs3
-rw-r--r--crates/ra_hir_def/src/nameres.rs68
-rw-r--r--crates/ra_ide/src/display/navigation_target.rs43
-rw-r--r--crates/ra_ide/src/references/rename.rs2
-rw-r--r--crates/ra_ide/src/references/search_scope.rs5
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::{
11 builtin_type::BuiltinType, 11 builtin_type::BuiltinType,
12 docs::Documentation, 12 docs::Documentation,
13 expr::{BindingAnnotation, Pat, PatId}, 13 expr::{BindingAnnotation, Pat, PatId},
14 nameres::ModuleSource,
14 per_ns::PerNs, 15 per_ns::PerNs,
15 resolver::HasResolver, 16 resolver::HasResolver,
16 type_ref::{Mutability, TypeRef}, 17 type_ref::{Mutability, TypeRef},
@@ -21,11 +22,11 @@ use hir_def::{
21use hir_expand::{ 22use hir_expand::{
22 diagnostics::DiagnosticSink, 23 diagnostics::DiagnosticSink,
23 name::{self, AsName}, 24 name::{self, AsName},
24 AstId, MacroDefId, 25 MacroDefId,
25}; 26};
26use hir_ty::expr::ExprValidator; 27use hir_ty::expr::ExprValidator;
27use ra_db::{CrateId, Edition, FileId, FilePosition}; 28use ra_db::{CrateId, Edition};
28use ra_syntax::{ast, AstNode, SyntaxNode}; 29use ra_syntax::ast;
29 30
30use crate::{ 31use crate::{
31 db::{DefDatabase, HirDatabase}, 32 db::{DefDatabase, HirDatabase},
@@ -79,64 +80,6 @@ impl Crate {
79 } 80 }
80} 81}
81 82
82pub enum ModuleSource {
83 SourceFile(ast::SourceFile),
84 Module(ast::Module),
85}
86
87impl ModuleSource {
88 pub fn new(
89 db: &impl DefDatabase,
90 file_id: Option<FileId>,
91 decl_id: Option<AstId<ast::Module>>,
92 ) -> ModuleSource {
93 match (file_id, decl_id) {
94 (Some(file_id), _) => {
95 let source_file = db.parse(file_id).tree();
96 ModuleSource::SourceFile(source_file)
97 }
98 (None, Some(item_id)) => {
99 let module = item_id.to_node(db);
100 assert!(module.item_list().is_some(), "expected inline module");
101 ModuleSource::Module(module)
102 }
103 (None, None) => panic!(),
104 }
105 }
106
107 // FIXME: this methods do not belong here
108 pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource {
109 let parse = db.parse(position.file_id);
110 match &ra_syntax::algo::find_node_at_offset::<ast::Module>(
111 parse.tree().syntax(),
112 position.offset,
113 ) {
114 Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()),
115 _ => {
116 let source_file = parse.tree();
117 ModuleSource::SourceFile(source_file)
118 }
119 }
120 }
121
122 pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource {
123 if let Some(m) =
124 child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi())
125 {
126 ModuleSource::Module(m)
127 } else {
128 let file_id = child.file_id.original_file(db);
129 let source_file = db.parse(file_id).tree();
130 ModuleSource::SourceFile(source_file)
131 }
132 }
133
134 pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource {
135 let source_file = db.parse(file_id).tree();
136 ModuleSource::SourceFile(source_file)
137 }
138}
139
140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 83#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
141pub struct Module { 84pub struct Module {
142 pub(crate) id: ModuleId, 85 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 @@
2 2
3use either::Either; 3use either::Either;
4use hir_def::{ 4use hir_def::{
5 nameres::ModuleSource,
5 src::{HasChildSource, HasSource as _}, 6 src::{HasChildSource, HasSource as _},
6 AstItemDef, Lookup, VariantId, 7 AstItemDef, Lookup, VariantId,
7}; 8};
@@ -9,7 +10,7 @@ use ra_syntax::ast;
9 10
10use crate::{ 11use crate::{
11 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, 12 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef,
12 Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, 13 Module, Static, Struct, StructField, Trait, TypeAlias, Union,
13}; 14};
14 15
15pub use hir_expand::InFile; 16pub 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 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir_def::{AstItemDef, LocationCtx, ModuleId}; 3use hir_def::{nameres::ModuleSource, AstItemDef, LocationCtx, ModuleId};
4use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; 4use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
5use ra_syntax::{ 5use ra_syntax::{
6 ast::{self, AstNode, NameOwner}, 6 ast::{self, AstNode, NameOwner},
@@ -10,8 +10,8 @@ use ra_syntax::{
10use crate::{ 10use crate::{
11 db::{AstDatabase, DefDatabase, HirDatabase}, 11 db::{AstDatabase, DefDatabase, HirDatabase},
12 AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, 12 AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock,
13 InFile, Local, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, 13 InFile, Local, MacroDef, Module, ModuleDef, Static, Struct, StructField, Trait, TypeAlias,
14 TypeAlias, Union, VariantDef, 14 Union, VariantDef,
15}; 15};
16 16
17pub trait FromSource: Sized { 17pub trait FromSource: Sized {
@@ -257,7 +257,7 @@ impl Module {
257 InFile { file_id: src.file_id, value: module.clone() }, 257 InFile { file_id: src.file_id, value: module.clone() },
258 ); 258 );
259 } 259 }
260 ModuleSource::SourceFile(_) => (), 260 ModuleSource::SourceFile(_) | ModuleSource::Block(_) => (),
261 }; 261 };
262 262
263 let original_file = src.file_id.original_file(db); 263 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::{
43 code_model::{ 43 code_model::{
44 src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, 44 src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency,
45 DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, 45 DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam,
46 HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, 46 HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct,
47 Static, Struct, StructField, Trait, Type, TypeAlias, Union, VariantDef, 47 StructField, Trait, Type, TypeAlias, Union, VariantDef,
48 }, 48 },
49 from_source::FromSource, 49 from_source::FromSource,
50 source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, 50 source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
@@ -59,6 +59,7 @@ pub use hir_def::{
59 body::scope::ExprScopes, 59 body::scope::ExprScopes,
60 builtin_type::BuiltinType, 60 builtin_type::BuiltinType,
61 docs::Documentation, 61 docs::Documentation,
62 nameres::ModuleSource,
62 path::{Path, PathKind}, 63 path::{Path, PathKind},
63 type_ref::Mutability, 64 type_ref::Mutability,
64}; 65};
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::{
14 BodySourceMap, 14 BodySourceMap,
15 }, 15 },
16 expr::{ExprId, PatId}, 16 expr::{ExprId, PatId},
17 nameres::ModuleSource,
17 path::known, 18 path::known,
18 resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, 19 resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
19 AssocItemId, DefWithBodyId, 20 AssocItemId, DefWithBodyId,
@@ -46,7 +47,7 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -
46 Some(crate::Module::from_declaration(db, src)?.id.resolver(db)) 47 Some(crate::Module::from_declaration(db, src)?.id.resolver(db))
47 }, 48 },
48 ast::SourceFile(it) => { 49 ast::SourceFile(it) => {
49 let src = node.with_value(crate::ModuleSource::SourceFile(it)); 50 let src = node.with_value(ModuleSource::SourceFile(it));
50 Some(crate::Module::from_definition(db, src)?.id.resolver(db)) 51 Some(crate::Module::from_definition(db, src)?.id.resolver(db))
51 }, 52 },
52 ast::StructDef(it) => { 53 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::{
63}; 63};
64use once_cell::sync::Lazy; 64use once_cell::sync::Lazy;
65use ra_arena::Arena; 65use ra_arena::Arena;
66use ra_db::{CrateId, Edition, FileId}; 66use ra_db::{CrateId, Edition, FileId, FilePosition};
67use ra_prof::profile; 67use ra_prof::profile;
68use ra_syntax::ast; 68use ra_syntax::{
69 ast::{self, AstNode},
70 SyntaxNode,
71};
69use rustc_hash::FxHashMap; 72use rustc_hash::FxHashMap;
70 73
71use crate::{ 74use crate::{
@@ -361,6 +364,67 @@ impl ModuleData {
361 } 364 }
362} 365}
363 366
367pub enum ModuleSource {
368 SourceFile(ast::SourceFile),
369 Module(ast::Module),
370 Block(ast::Block),
371}
372
373impl ModuleSource {
374 pub fn new(
375 db: &impl DefDatabase,
376 file_id: Option<FileId>,
377 decl_id: Option<AstId<ast::Module>>,
378 ) -> ModuleSource {
379 match (file_id, decl_id) {
380 (Some(file_id), _) => {
381 let source_file = db.parse(file_id).tree();
382 ModuleSource::SourceFile(source_file)
383 }
384 (None, Some(item_id)) => {
385 let module = item_id.to_node(db);
386 assert!(module.item_list().is_some(), "expected inline module");
387 ModuleSource::Module(module)
388 }
389 (None, None) => panic!(),
390 }
391 }
392
393 // FIXME: this methods do not belong here
394 pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource {
395 let parse = db.parse(position.file_id);
396 match &ra_syntax::algo::find_node_at_offset::<ast::Module>(
397 parse.tree().syntax(),
398 position.offset,
399 ) {
400 Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()),
401 _ => {
402 let source_file = parse.tree();
403 ModuleSource::SourceFile(source_file)
404 }
405 }
406 }
407
408 pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource {
409 if let Some(m) =
410 child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi())
411 {
412 ModuleSource::Module(m)
413 } else if let Some(b) = child.value.ancestors().filter_map(ast::Block::cast).next() {
414 ModuleSource::Block(b)
415 } else {
416 let file_id = child.file_id.original_file(db);
417 let source_file = db.parse(file_id).tree();
418 ModuleSource::SourceFile(source_file)
419 }
420 }
421
422 pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource {
423 let source_file = db.parse(file_id).tree();
424 ModuleSource::SourceFile(source_file)
425 }
426}
427
364mod diagnostics { 428mod diagnostics {
365 use hir_expand::diagnostics::DiagnosticSink; 429 use hir_expand::diagnostics::DiagnosticSink;
366 use ra_db::RelativePathBuf; 430 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 {
231 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 231 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
232 let src = self.definition_source(db); 232 let src = self.definition_source(db);
233 let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default(); 233 let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
234 match &src.value { 234 let syntax = match &src.value {
235 ModuleSource::SourceFile(node) => { 235 ModuleSource::SourceFile(node) => node.syntax(),
236 let frange = original_range(db, src.with_value(node.syntax())); 236 ModuleSource::Module(node) => node.syntax(),
237 237 ModuleSource::Block(node) => node.syntax(),
238 NavigationTarget::from_syntax( 238 };
239 frange.file_id, 239 let frange = original_range(db, src.with_value(syntax));
240 name, 240 NavigationTarget::from_syntax(
241 None, 241 frange.file_id,
242 frange.range, 242 name,
243 node.syntax().kind(), 243 None,
244 None, 244 frange.range,
245 None, 245 syntax.kind(),
246 ) 246 None,
247 } 247 None,
248 ModuleSource::Module(node) => { 248 )
249 let frange = original_range(db, src.with_value(node.syntax()));
250
251 NavigationTarget::from_syntax(
252 frange.file_id,
253 name,
254 None,
255 frange.range,
256 node.syntax().kind(),
257 node.doc_comment_text(),
258 node.short_label(),
259 )
260 }
261 }
262 } 249 }
263} 250}
264 251
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(
88 file_system_edits.push(move_file); 88 file_system_edits.push(move_file);
89 } 89 }
90 } 90 }
91 ModuleSource::Module(..) => {} 91 ModuleSource::Module(..) | ModuleSource::Block(..) => {}
92 } 92 }
93 } 93 }
94 94
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 {
96 let range = Some(m.syntax().text_range()); 96 let range = Some(m.syntax().text_range());
97 res.insert(file_id, range); 97 res.insert(file_id, range);
98 } 98 }
99 ModuleSource::Block(b) => {
100 let range = Some(b.syntax().text_range());
101 res.insert(file_id, range);
102 }
99 ModuleSource::SourceFile(_) => { 103 ModuleSource::SourceFile(_) => {
100 res.insert(file_id, None); 104 res.insert(file_id, None);
101 res.extend(parent_module.children(db).map(|m| { 105 res.extend(parent_module.children(db).map(|m| {
@@ -137,6 +141,7 @@ impl NameDefinition {
137 let mut res = FxHashMap::default(); 141 let mut res = FxHashMap::default();
138 let range = match module_src.value { 142 let range = match module_src.value {
139 ModuleSource::Module(m) => Some(m.syntax().text_range()), 143 ModuleSource::Module(m) => Some(m.syntax().text_range()),
144 ModuleSource::Block(b) => Some(b.syntax().text_range()),
140 ModuleSource::SourceFile(_) => None, 145 ModuleSource::SourceFile(_) => None,
141 }; 146 };
142 res.insert(file_id, range); 147 res.insert(file_id, range);