diff options
author | Aleksey Kladov <[email protected]> | 2019-04-10 09:15:55 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-04-11 14:29:33 +0100 |
commit | 10d66d63d716a10ba7a5a8d1b69c9066249caf69 (patch) | |
tree | 3897deea123f2bec49c28cdeb453ae0cfa0f49f1 /crates/ra_assists | |
parent | e6e2571bdf780d304c792d4317bbaf1d6f5d7a0a (diff) |
introduce SourceAnalyzer
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/add_explicit_type.rs | 8 | ||||
-rw-r--r-- | crates/ra_assists/src/add_missing_impl_members.rs | 15 | ||||
-rw-r--r-- | crates/ra_assists/src/fill_match_arms.rs | 10 | ||||
-rw-r--r-- | crates/ra_assists/src/fill_struct_fields.rs | 11 |
4 files changed, 16 insertions, 28 deletions
diff --git a/crates/ra_assists/src/add_explicit_type.rs b/crates/ra_assists/src/add_explicit_type.rs index 1dc59bb87..c50db4e21 100644 --- a/crates/ra_assists/src/add_explicit_type.rs +++ b/crates/ra_assists/src/add_explicit_type.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | use hir::{ | 1 | use hir::{ |
2 | HirDisplay, Ty, | 2 | HirDisplay, Ty, |
3 | db::HirDatabase, | 3 | db::HirDatabase, |
4 | source_binder::function_from_child_node, | ||
5 | }; | 4 | }; |
6 | use ra_syntax::{ | 5 | use ra_syntax::{ |
7 | SyntaxKind, | 6 | SyntaxKind, |
@@ -30,11 +29,8 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option< | |||
30 | } | 29 | } |
31 | // Infer type | 30 | // Infer type |
32 | let db = ctx.db; | 31 | let db = ctx.db; |
33 | let func = function_from_child_node(db, ctx.frange.file_id, pat.syntax())?; | 32 | let analyzer = hir::SourceAnalyser::new(db, ctx.frange.file_id, stmt.syntax()); |
34 | let inference_res = func.infer(db); | 33 | let ty = analyzer.type_of(db, expr)?; |
35 | let source_map = func.body_source_map(db); | ||
36 | let expr_id = source_map.node_expr(expr.into())?; | ||
37 | let ty = inference_res[expr_id].clone(); | ||
38 | // Assist not applicable if the type is unknown | 34 | // Assist not applicable if the type is unknown |
39 | if is_unknown(&ty) { | 35 | if is_unknown(&ty) { |
40 | return None; | 36 | return None; |
diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index 19a2d05bc..0b2127e11 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs | |||
@@ -2,7 +2,6 @@ use std::fmt::Write; | |||
2 | 2 | ||
3 | use crate::{Assist, AssistId, AssistCtx}; | 3 | use crate::{Assist, AssistId, AssistCtx}; |
4 | 4 | ||
5 | use hir::Resolver; | ||
6 | use hir::db::HirDatabase; | 5 | use hir::db::HirDatabase; |
7 | use ra_syntax::{SmolStr, SyntaxKind, TextRange, TextUnit, TreeArc}; | 6 | use ra_syntax::{SmolStr, SyntaxKind, TextRange, TextUnit, TreeArc}; |
8 | use ra_syntax::ast::{self, AstNode, AstToken, FnDef, ImplItem, ImplItemKind, NameOwner}; | 7 | use ra_syntax::ast::{self, AstNode, AstToken, FnDef, ImplItem, ImplItemKind, NameOwner}; |
@@ -46,9 +45,9 @@ fn add_missing_impl_members_inner( | |||
46 | let trait_def = { | 45 | let trait_def = { |
47 | let file_id = ctx.frange.file_id; | 46 | let file_id = ctx.frange.file_id; |
48 | let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; | 47 | let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; |
49 | let resolver = hir::source_binder::resolver_for_position(ctx.db, position); | 48 | let analyser = hir::SourceAnalyser::new(ctx.db, position.file_id, impl_node.syntax()); |
50 | 49 | ||
51 | resolve_target_trait_def(ctx.db, &resolver, impl_node)? | 50 | resolve_target_trait_def(ctx.db, &analyser, impl_node)? |
52 | }; | 51 | }; |
53 | 52 | ||
54 | let missing_fns: Vec<_> = { | 53 | let missing_fns: Vec<_> = { |
@@ -122,14 +121,14 @@ fn add_missing_impl_members_inner( | |||
122 | /// implemented) to a `ast::TraitDef`. | 121 | /// implemented) to a `ast::TraitDef`. |
123 | fn resolve_target_trait_def( | 122 | fn resolve_target_trait_def( |
124 | db: &impl HirDatabase, | 123 | db: &impl HirDatabase, |
125 | resolver: &Resolver, | 124 | binder: &hir::SourceAnalyser, |
126 | impl_block: &ast::ImplBlock, | 125 | impl_block: &ast::ImplBlock, |
127 | ) -> Option<TreeArc<ast::TraitDef>> { | 126 | ) -> Option<TreeArc<ast::TraitDef>> { |
128 | let ast_path = impl_block.target_trait().map(AstNode::syntax).and_then(ast::PathType::cast)?; | 127 | let ast_path = |
129 | let hir_path = ast_path.path().and_then(hir::Path::from_ast)?; | 128 | impl_block.target_trait().map(AstNode::syntax).and_then(ast::PathType::cast)?.path()?; |
130 | 129 | ||
131 | match resolver.resolve_path(db, &hir_path).take_types() { | 130 | match binder.resolve_path(db, &ast_path) { |
132 | Some(hir::Resolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).1), | 131 | Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).1), |
133 | _ => None, | 132 | _ => None, |
134 | } | 133 | } |
135 | } | 134 | } |
diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs index da67ab667..050b1c73f 100644 --- a/crates/ra_assists/src/fill_match_arms.rs +++ b/crates/ra_assists/src/fill_match_arms.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use std::fmt::Write; | 1 | use std::fmt::Write; |
2 | 2 | ||
3 | use hir::{ | 3 | use hir::{ |
4 | AdtDef, FieldSource, source_binder, | 4 | AdtDef, FieldSource, |
5 | db::HirDatabase, | 5 | db::HirDatabase, |
6 | }; | 6 | }; |
7 | use ra_syntax::ast::{self, AstNode}; | 7 | use ra_syntax::ast::{self, AstNode}; |
@@ -20,12 +20,8 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
20 | } | 20 | } |
21 | 21 | ||
22 | let expr = match_expr.expr()?; | 22 | let expr = match_expr.expr()?; |
23 | let function = | 23 | let analyzer = hir::SourceAnalyser::new(ctx.db, ctx.frange.file_id, expr.syntax()); |
24 | source_binder::function_from_child_node(ctx.db, ctx.frange.file_id, expr.syntax())?; | 24 | let match_expr_ty = analyzer.type_of(ctx.db, expr)?; |
25 | let infer_result = function.infer(ctx.db); | ||
26 | let source_map = function.body_source_map(ctx.db); | ||
27 | let node_expr = source_map.node_expr(expr)?; | ||
28 | let match_expr_ty = infer_result[node_expr].clone(); | ||
29 | let enum_def = match_expr_ty.autoderef(ctx.db).find_map(|ty| match ty.as_adt() { | 25 | let enum_def = match_expr_ty.autoderef(ctx.db).find_map(|ty| match ty.as_adt() { |
30 | Some((AdtDef::Enum(e), _)) => Some(e), | 26 | Some((AdtDef::Enum(e), _)) => Some(e), |
31 | _ => None, | 27 | _ => None, |
diff --git a/crates/ra_assists/src/fill_struct_fields.rs b/crates/ra_assists/src/fill_struct_fields.rs index c7790dc72..e23cccbb8 100644 --- a/crates/ra_assists/src/fill_struct_fields.rs +++ b/crates/ra_assists/src/fill_struct_fields.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::fmt::Write; | 1 | use std::fmt::Write; |
2 | 2 | ||
3 | use hir::{AdtDef, db::HirDatabase, source_binder::function_from_child_node}; | 3 | use hir::{AdtDef, db::HirDatabase}; |
4 | 4 | ||
5 | use ra_syntax::ast::{self, AstNode}; | 5 | use ra_syntax::ast::{self, AstNode}; |
6 | 6 | ||
@@ -51,15 +51,12 @@ where | |||
51 | } | 51 | } |
52 | 52 | ||
53 | fn evaluate_struct_def_fields(&mut self) -> Option<()> { | 53 | fn evaluate_struct_def_fields(&mut self) -> Option<()> { |
54 | let function = function_from_child_node( | 54 | let analyzer = hir::SourceAnalyser::new( |
55 | self.ctx.db, | 55 | self.ctx.db, |
56 | self.ctx.frange.file_id, | 56 | self.ctx.frange.file_id, |
57 | self.struct_lit.syntax(), | 57 | self.struct_lit.syntax(), |
58 | )?; | 58 | ); |
59 | let infer_result = function.infer(self.ctx.db); | 59 | let struct_lit_ty = analyzer.type_of(self.ctx.db, self.struct_lit.into())?; |
60 | let source_map = function.body_source_map(self.ctx.db); | ||
61 | let node_expr = source_map.node_expr(self.struct_lit.into())?; | ||
62 | let struct_lit_ty = infer_result[node_expr].clone(); | ||
63 | let struct_def = match struct_lit_ty.as_adt() { | 60 | let struct_def = match struct_lit_ty.as_adt() { |
64 | Some((AdtDef::Struct(s), _)) => s, | 61 | Some((AdtDef::Struct(s), _)) => s, |
65 | _ => return None, | 62 | _ => return None, |