diff options
Diffstat (limited to 'crates/ra_hir/src/semantics.rs')
-rw-r--r-- | crates/ra_hir/src/semantics.rs | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 5d6edc45c..86bfb416c 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs | |||
@@ -14,7 +14,7 @@ use ra_db::{FileId, FileRange}; | |||
14 | use ra_prof::profile; | 14 | use ra_prof::profile; |
15 | use ra_syntax::{ | 15 | use ra_syntax::{ |
16 | algo::{find_node_at_offset, skip_trivia_token}, | 16 | algo::{find_node_at_offset, skip_trivia_token}, |
17 | ast, AstNode, Direction, SyntaxNode, SyntaxToken, TextRange, TextUnit, | 17 | ast, AstNode, Direction, SyntaxNode, SyntaxToken, TextRange, TextSize, |
18 | }; | 18 | }; |
19 | use rustc_hash::{FxHashMap, FxHashSet}; | 19 | use rustc_hash::{FxHashMap, FxHashSet}; |
20 | 20 | ||
@@ -23,8 +23,8 @@ use crate::{ | |||
23 | diagnostics::Diagnostic, | 23 | diagnostics::Diagnostic, |
24 | semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, | 24 | semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, |
25 | source_analyzer::{resolve_hir_path, SourceAnalyzer}, | 25 | source_analyzer::{resolve_hir_path, SourceAnalyzer}, |
26 | AssocItem, Function, HirFileId, ImplDef, InFile, Local, MacroDef, Module, ModuleDef, Name, | 26 | AssocItem, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, Module, ModuleDef, |
27 | Origin, Path, ScopeDef, StructField, Trait, Type, TypeParam, | 27 | Name, Origin, Path, ScopeDef, Trait, Type, TypeParam, |
28 | }; | 28 | }; |
29 | 29 | ||
30 | #[derive(Debug, Clone, PartialEq, Eq)] | 30 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -95,7 +95,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
95 | let token = successors(Some(parent.with_value(token)), |token| { | 95 | let token = successors(Some(parent.with_value(token)), |token| { |
96 | let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?; | 96 | let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?; |
97 | let tt = macro_call.token_tree()?; | 97 | let tt = macro_call.token_tree()?; |
98 | if !token.value.text_range().is_subrange(&tt.syntax().text_range()) { | 98 | if !tt.syntax().text_range().contains_range(token.value.text_range()) { |
99 | return None; | 99 | return None; |
100 | } | 100 | } |
101 | let file_id = sa.expand(self.db, token.with_value(¯o_call))?; | 101 | let file_id = sa.expand(self.db, token.with_value(¯o_call))?; |
@@ -114,7 +114,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
114 | pub fn descend_node_at_offset<N: ast::AstNode>( | 114 | pub fn descend_node_at_offset<N: ast::AstNode>( |
115 | &self, | 115 | &self, |
116 | node: &SyntaxNode, | 116 | node: &SyntaxNode, |
117 | offset: TextUnit, | 117 | offset: TextSize, |
118 | ) -> Option<N> { | 118 | ) -> Option<N> { |
119 | // Handle macro token cases | 119 | // Handle macro token cases |
120 | node.token_at_offset(offset) | 120 | node.token_at_offset(offset) |
@@ -142,7 +142,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
142 | pub fn ancestors_at_offset_with_macros( | 142 | pub fn ancestors_at_offset_with_macros( |
143 | &self, | 143 | &self, |
144 | node: &SyntaxNode, | 144 | node: &SyntaxNode, |
145 | offset: TextUnit, | 145 | offset: TextSize, |
146 | ) -> impl Iterator<Item = SyntaxNode> + '_ { | 146 | ) -> impl Iterator<Item = SyntaxNode> + '_ { |
147 | node.token_at_offset(offset) | 147 | node.token_at_offset(offset) |
148 | .map(|token| self.ancestors_with_macros(token.parent())) | 148 | .map(|token| self.ancestors_with_macros(token.parent())) |
@@ -154,7 +154,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
154 | pub fn find_node_at_offset_with_macros<N: AstNode>( | 154 | pub fn find_node_at_offset_with_macros<N: AstNode>( |
155 | &self, | 155 | &self, |
156 | node: &SyntaxNode, | 156 | node: &SyntaxNode, |
157 | offset: TextUnit, | 157 | offset: TextSize, |
158 | ) -> Option<N> { | 158 | ) -> Option<N> { |
159 | self.ancestors_at_offset_with_macros(node, offset).find_map(N::cast) | 159 | self.ancestors_at_offset_with_macros(node, offset).find_map(N::cast) |
160 | } | 160 | } |
@@ -164,7 +164,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
164 | pub fn find_node_at_offset_with_descend<N: AstNode>( | 164 | pub fn find_node_at_offset_with_descend<N: AstNode>( |
165 | &self, | 165 | &self, |
166 | node: &SyntaxNode, | 166 | node: &SyntaxNode, |
167 | offset: TextUnit, | 167 | offset: TextSize, |
168 | ) -> Option<N> { | 168 | ) -> Option<N> { |
169 | if let Some(it) = find_node_at_offset(&node, offset) { | 169 | if let Some(it) = find_node_at_offset(&node, offset) { |
170 | return Some(it); | 170 | return Some(it); |
@@ -184,18 +184,15 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
184 | self.analyze(call.syntax()).resolve_method_call(self.db, call) | 184 | self.analyze(call.syntax()).resolve_method_call(self.db, call) |
185 | } | 185 | } |
186 | 186 | ||
187 | pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<StructField> { | 187 | pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Field> { |
188 | self.analyze(field.syntax()).resolve_field(self.db, field) | 188 | self.analyze(field.syntax()).resolve_field(self.db, field) |
189 | } | 189 | } |
190 | 190 | ||
191 | pub fn resolve_record_field( | 191 | pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<(Field, Option<Local>)> { |
192 | &self, | ||
193 | field: &ast::RecordField, | ||
194 | ) -> Option<(StructField, Option<Local>)> { | ||
195 | self.analyze(field.syntax()).resolve_record_field(self.db, field) | 192 | self.analyze(field.syntax()).resolve_record_field(self.db, field) |
196 | } | 193 | } |
197 | 194 | ||
198 | pub fn resolve_record_field_pat(&self, field: &ast::RecordFieldPat) -> Option<StructField> { | 195 | pub fn resolve_record_field_pat(&self, field: &ast::RecordFieldPat) -> Option<Field> { |
199 | self.analyze(field.syntax()).resolve_record_field_pat(self.db, field) | 196 | self.analyze(field.syntax()).resolve_record_field_pat(self.db, field) |
200 | } | 197 | } |
201 | 198 | ||
@@ -216,19 +213,13 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
216 | // FIXME: use this instead? | 213 | // FIXME: use this instead? |
217 | // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>; | 214 | // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>; |
218 | 215 | ||
219 | pub fn record_literal_missing_fields( | 216 | pub fn record_literal_missing_fields(&self, literal: &ast::RecordLit) -> Vec<(Field, Type)> { |
220 | &self, | ||
221 | literal: &ast::RecordLit, | ||
222 | ) -> Vec<(StructField, Type)> { | ||
223 | self.analyze(literal.syntax()) | 217 | self.analyze(literal.syntax()) |
224 | .record_literal_missing_fields(self.db, literal) | 218 | .record_literal_missing_fields(self.db, literal) |
225 | .unwrap_or_default() | 219 | .unwrap_or_default() |
226 | } | 220 | } |
227 | 221 | ||
228 | pub fn record_pattern_missing_fields( | 222 | pub fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> { |
229 | &self, | ||
230 | pattern: &ast::RecordPat, | ||
231 | ) -> Vec<(StructField, Type)> { | ||
232 | self.analyze(pattern.syntax()) | 223 | self.analyze(pattern.syntax()) |
233 | .record_pattern_missing_fields(self.db, pattern) | 224 | .record_pattern_missing_fields(self.db, pattern) |
234 | .unwrap_or_default() | 225 | .unwrap_or_default() |
@@ -255,7 +246,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
255 | SemanticsScope { db: self.db, resolver } | 246 | SemanticsScope { db: self.db, resolver } |
256 | } | 247 | } |
257 | 248 | ||
258 | pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextUnit) -> SemanticsScope<'db, DB> { | 249 | pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db, DB> { |
259 | let node = self.find_file(node.clone()); | 250 | let node = self.find_file(node.clone()); |
260 | let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver; | 251 | let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver; |
261 | SemanticsScope { db: self.db, resolver } | 252 | SemanticsScope { db: self.db, resolver } |
@@ -271,7 +262,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
271 | self.analyze2(src.as_ref(), None) | 262 | self.analyze2(src.as_ref(), None) |
272 | } | 263 | } |
273 | 264 | ||
274 | fn analyze2(&self, src: InFile<&SyntaxNode>, offset: Option<TextUnit>) -> SourceAnalyzer { | 265 | fn analyze2(&self, src: InFile<&SyntaxNode>, offset: Option<TextSize>) -> SourceAnalyzer { |
275 | let _p = profile("Semantics::analyze2"); | 266 | let _p = profile("Semantics::analyze2"); |
276 | 267 | ||
277 | let container = match self.with_ctx(|ctx| ctx.find_container(src)) { | 268 | let container = match self.with_ctx(|ctx| ctx.find_container(src)) { |
@@ -359,8 +350,8 @@ to_def_impls![ | |||
359 | (crate::Const, ast::ConstDef, const_to_def), | 350 | (crate::Const, ast::ConstDef, const_to_def), |
360 | (crate::Static, ast::StaticDef, static_to_def), | 351 | (crate::Static, ast::StaticDef, static_to_def), |
361 | (crate::Function, ast::FnDef, fn_to_def), | 352 | (crate::Function, ast::FnDef, fn_to_def), |
362 | (crate::StructField, ast::RecordFieldDef, record_field_to_def), | 353 | (crate::Field, ast::RecordFieldDef, record_field_to_def), |
363 | (crate::StructField, ast::TupleFieldDef, tuple_field_to_def), | 354 | (crate::Field, ast::TupleFieldDef, tuple_field_to_def), |
364 | (crate::EnumVariant, ast::EnumVariant, enum_variant_to_def), | 355 | (crate::EnumVariant, ast::EnumVariant, enum_variant_to_def), |
365 | (crate::TypeParam, ast::TypeParam, type_param_to_def), | 356 | (crate::TypeParam, ast::TypeParam, type_param_to_def), |
366 | (crate::MacroDef, ast::MacroCall, macro_call_to_def), // this one is dubious, not all calls are macros | 357 | (crate::MacroDef, ast::MacroCall, macro_call_to_def), // this one is dubious, not all calls are macros |
@@ -463,7 +454,7 @@ fn original_range_opt( | |||
463 | return None; | 454 | return None; |
464 | } | 455 | } |
465 | 456 | ||
466 | Some(first.with_value(first.value.text_range().extend_to(&last.value.text_range()))) | 457 | Some(first.with_value(first.value.text_range().cover(last.value.text_range()))) |
467 | })?) | 458 | })?) |
468 | } | 459 | } |
469 | 460 | ||