diff options
author | ice1000 <[email protected]> | 2019-12-03 20:24:02 +0000 |
---|---|---|
committer | ice1000 <[email protected]> | 2019-12-04 23:30:42 +0000 |
commit | 38853459e3d964cc7f635829cdc66f5faee33d85 (patch) | |
tree | c4e60a8f5fa9c18915e8aed7857424d454cd130e /crates/ra_hir/src/code_model.rs | |
parent | 7cbedc50bcf048c87f141a85418581076d67fc7a (diff) |
Add `ModuleSource::Block`
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 65 |
1 files changed, 4 insertions, 61 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::{ | |||
21 | use hir_expand::{ | 22 | use hir_expand::{ |
22 | diagnostics::DiagnosticSink, | 23 | diagnostics::DiagnosticSink, |
23 | name::{self, AsName}, | 24 | name::{self, AsName}, |
24 | AstId, MacroDefId, | 25 | MacroDefId, |
25 | }; | 26 | }; |
26 | use hir_ty::expr::ExprValidator; | 27 | use hir_ty::expr::ExprValidator; |
27 | use ra_db::{CrateId, Edition, FileId, FilePosition}; | 28 | use ra_db::{CrateId, Edition}; |
28 | use ra_syntax::{ast, AstNode, SyntaxNode}; | 29 | use ra_syntax::ast; |
29 | 30 | ||
30 | use crate::{ | 31 | use crate::{ |
31 | db::{DefDatabase, HirDatabase}, | 32 | db::{DefDatabase, HirDatabase}, |
@@ -79,64 +80,6 @@ impl Crate { | |||
79 | } | 80 | } |
80 | } | 81 | } |
81 | 82 | ||
82 | pub enum ModuleSource { | ||
83 | SourceFile(ast::SourceFile), | ||
84 | Module(ast::Module), | ||
85 | } | ||
86 | |||
87 | impl 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)] |
141 | pub struct Module { | 84 | pub struct Module { |
142 | pub(crate) id: ModuleId, | 85 | pub(crate) id: ModuleId, |