From 10d66d63d716a10ba7a5a8d1b69c9066249caf69 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 10 Apr 2019 11:15:55 +0300 Subject: introduce SourceAnalyzer --- crates/ra_assists/src/add_explicit_type.rs | 8 ++------ crates/ra_assists/src/add_missing_impl_members.rs | 15 +++++++-------- crates/ra_assists/src/fill_match_arms.rs | 10 +++------- crates/ra_assists/src/fill_struct_fields.rs | 11 ++++------- 4 files changed, 16 insertions(+), 28 deletions(-) (limited to 'crates/ra_assists') 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 @@ use hir::{ HirDisplay, Ty, db::HirDatabase, - source_binder::function_from_child_node, }; use ra_syntax::{ SyntaxKind, @@ -30,11 +29,8 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx) -> Option< } // Infer type let db = ctx.db; - let func = function_from_child_node(db, ctx.frange.file_id, pat.syntax())?; - let inference_res = func.infer(db); - let source_map = func.body_source_map(db); - let expr_id = source_map.node_expr(expr.into())?; - let ty = inference_res[expr_id].clone(); + let analyzer = hir::SourceAnalyser::new(db, ctx.frange.file_id, stmt.syntax()); + let ty = analyzer.type_of(db, expr)?; // Assist not applicable if the type is unknown if is_unknown(&ty) { 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; use crate::{Assist, AssistId, AssistCtx}; -use hir::Resolver; use hir::db::HirDatabase; use ra_syntax::{SmolStr, SyntaxKind, TextRange, TextUnit, TreeArc}; use ra_syntax::ast::{self, AstNode, AstToken, FnDef, ImplItem, ImplItemKind, NameOwner}; @@ -46,9 +45,9 @@ fn add_missing_impl_members_inner( let trait_def = { let file_id = ctx.frange.file_id; let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; - let resolver = hir::source_binder::resolver_for_position(ctx.db, position); + let analyser = hir::SourceAnalyser::new(ctx.db, position.file_id, impl_node.syntax()); - resolve_target_trait_def(ctx.db, &resolver, impl_node)? + resolve_target_trait_def(ctx.db, &analyser, impl_node)? }; let missing_fns: Vec<_> = { @@ -122,14 +121,14 @@ fn add_missing_impl_members_inner( /// implemented) to a `ast::TraitDef`. fn resolve_target_trait_def( db: &impl HirDatabase, - resolver: &Resolver, + binder: &hir::SourceAnalyser, impl_block: &ast::ImplBlock, ) -> Option> { - let ast_path = impl_block.target_trait().map(AstNode::syntax).and_then(ast::PathType::cast)?; - let hir_path = ast_path.path().and_then(hir::Path::from_ast)?; + let ast_path = + impl_block.target_trait().map(AstNode::syntax).and_then(ast::PathType::cast)?.path()?; - match resolver.resolve_path(db, &hir_path).take_types() { - Some(hir::Resolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).1), + match binder.resolve_path(db, &ast_path) { + Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).1), _ => None, } } 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 @@ use std::fmt::Write; use hir::{ - AdtDef, FieldSource, source_binder, + AdtDef, FieldSource, db::HirDatabase, }; use ra_syntax::ast::{self, AstNode}; @@ -20,12 +20,8 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx) -> Option Some(e), _ => 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 @@ use std::fmt::Write; -use hir::{AdtDef, db::HirDatabase, source_binder::function_from_child_node}; +use hir::{AdtDef, db::HirDatabase}; use ra_syntax::ast::{self, AstNode}; @@ -51,15 +51,12 @@ where } fn evaluate_struct_def_fields(&mut self) -> Option<()> { - let function = function_from_child_node( + let analyzer = hir::SourceAnalyser::new( self.ctx.db, self.ctx.frange.file_id, self.struct_lit.syntax(), - )?; - let infer_result = function.infer(self.ctx.db); - let source_map = function.body_source_map(self.ctx.db); - let node_expr = source_map.node_expr(self.struct_lit.into())?; - let struct_lit_ty = infer_result[node_expr].clone(); + ); + let struct_lit_ty = analyzer.type_of(self.ctx.db, self.struct_lit.into())?; let struct_def = match struct_lit_ty.as_adt() { Some((AdtDef::Struct(s), _)) => s, _ => return None, -- cgit v1.2.3 From b6809b6695f9c4cec82ff98d73f7ff24f96cbecf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 11 Apr 2019 15:51:02 +0300 Subject: rename --- crates/ra_assists/src/add_explicit_type.rs | 2 +- crates/ra_assists/src/add_missing_impl_members.rs | 6 +++--- crates/ra_assists/src/fill_match_arms.rs | 2 +- crates/ra_assists/src/fill_struct_fields.rs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/add_explicit_type.rs b/crates/ra_assists/src/add_explicit_type.rs index c50db4e21..c3674ffdc 100644 --- a/crates/ra_assists/src/add_explicit_type.rs +++ b/crates/ra_assists/src/add_explicit_type.rs @@ -29,7 +29,7 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx) -> Option< } // Infer type let db = ctx.db; - let analyzer = hir::SourceAnalyser::new(db, ctx.frange.file_id, stmt.syntax()); + let analyzer = hir::SourceAnalyzer::new(db, ctx.frange.file_id, stmt.syntax()); let ty = analyzer.type_of(db, expr)?; // Assist not applicable if the type is unknown if is_unknown(&ty) { diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index 0b2127e11..04b3f3c76 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs @@ -45,7 +45,7 @@ fn add_missing_impl_members_inner( let trait_def = { let file_id = ctx.frange.file_id; let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; - let analyser = hir::SourceAnalyser::new(ctx.db, position.file_id, impl_node.syntax()); + let analyser = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax()); resolve_target_trait_def(ctx.db, &analyser, impl_node)? }; @@ -121,13 +121,13 @@ fn add_missing_impl_members_inner( /// implemented) to a `ast::TraitDef`. fn resolve_target_trait_def( db: &impl HirDatabase, - binder: &hir::SourceAnalyser, + analyzer: &hir::SourceAnalyzer, impl_block: &ast::ImplBlock, ) -> Option> { let ast_path = impl_block.target_trait().map(AstNode::syntax).and_then(ast::PathType::cast)?.path()?; - match binder.resolve_path(db, &ast_path) { + match analyzer.resolve_path(db, &ast_path) { Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).1), _ => None, } diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs index 050b1c73f..8110b2676 100644 --- a/crates/ra_assists/src/fill_match_arms.rs +++ b/crates/ra_assists/src/fill_match_arms.rs @@ -20,7 +20,7 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx) -> Option Some(e), diff --git a/crates/ra_assists/src/fill_struct_fields.rs b/crates/ra_assists/src/fill_struct_fields.rs index e23cccbb8..81f762e8d 100644 --- a/crates/ra_assists/src/fill_struct_fields.rs +++ b/crates/ra_assists/src/fill_struct_fields.rs @@ -51,7 +51,7 @@ where } fn evaluate_struct_def_fields(&mut self) -> Option<()> { - let analyzer = hir::SourceAnalyser::new( + let analyzer = hir::SourceAnalyzer::new( self.ctx.db, self.ctx.frange.file_id, self.struct_lit.syntax(), -- cgit v1.2.3 From 20013de2abb95bc024f55163b1a5044cfb52a873 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 11 Apr 2019 17:15:20 +0300 Subject: fix typo --- crates/ra_assists/src/add_missing_impl_members.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index 04b3f3c76..104a4d78a 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs @@ -45,9 +45,9 @@ fn add_missing_impl_members_inner( let trait_def = { let file_id = ctx.frange.file_id; let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; - let analyser = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax()); + let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax()); - resolve_target_trait_def(ctx.db, &analyser, impl_node)? + resolve_target_trait_def(ctx.db, &analyzer, impl_node)? }; let missing_fns: Vec<_> = { -- cgit v1.2.3 From 0fd93bc14a2d0ce2edd682d26c18979c13f181c5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 13 Apr 2019 00:44:47 +0300 Subject: use really correct resolver for expressions --- crates/ra_assists/src/add_explicit_type.rs | 2 +- crates/ra_assists/src/add_missing_impl_members.rs | 2 +- crates/ra_assists/src/fill_match_arms.rs | 2 +- crates/ra_assists/src/fill_struct_fields.rs | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/add_explicit_type.rs b/crates/ra_assists/src/add_explicit_type.rs index c3674ffdc..cb0ac9885 100644 --- a/crates/ra_assists/src/add_explicit_type.rs +++ b/crates/ra_assists/src/add_explicit_type.rs @@ -29,7 +29,7 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx) -> Option< } // Infer type let db = ctx.db; - let analyzer = hir::SourceAnalyzer::new(db, ctx.frange.file_id, stmt.syntax()); + let analyzer = hir::SourceAnalyzer::new(db, ctx.frange.file_id, stmt.syntax(), None); let ty = analyzer.type_of(db, expr)?; // Assist not applicable if the type is unknown if is_unknown(&ty) { diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index 104a4d78a..100ebb7b6 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs @@ -45,7 +45,7 @@ fn add_missing_impl_members_inner( let trait_def = { let file_id = ctx.frange.file_id; let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; - let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax()); + let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax(), None); resolve_target_trait_def(ctx.db, &analyzer, impl_node)? }; diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs index 8110b2676..45e327cd4 100644 --- a/crates/ra_assists/src/fill_match_arms.rs +++ b/crates/ra_assists/src/fill_match_arms.rs @@ -20,7 +20,7 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx) -> Option Some(e), diff --git a/crates/ra_assists/src/fill_struct_fields.rs b/crates/ra_assists/src/fill_struct_fields.rs index 81f762e8d..663b4f669 100644 --- a/crates/ra_assists/src/fill_struct_fields.rs +++ b/crates/ra_assists/src/fill_struct_fields.rs @@ -55,6 +55,7 @@ where self.ctx.db, self.ctx.frange.file_id, self.struct_lit.syntax(), + None, ); let struct_lit_ty = analyzer.type_of(self.ctx.db, self.struct_lit.into())?; let struct_def = match struct_lit_ty.as_adt() { -- cgit v1.2.3 From 3aae223d938e5a36d997c45a0f86cfcabf83b570 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 13 Apr 2019 09:31:03 +0300 Subject: hide some scopes --- crates/ra_assists/src/inline_local_variable.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs index 950c2910b..9493acec9 100644 --- a/crates/ra_assists/src/inline_local_variable.rs +++ b/crates/ra_assists/src/inline_local_variable.rs @@ -1,7 +1,4 @@ -use hir::{ - db::HirDatabase, - source_binder::function_from_child_node, -}; +use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode, AstToken, PatKind, ExprKind}, TextRange, @@ -29,10 +26,8 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx) -> Opt } else { let_stmt.syntax().range() }; - - let function = function_from_child_node(ctx.db, ctx.frange.file_id, bind_pat.syntax())?; - let scope = function.scopes(ctx.db); - let refs = scope.find_all_refs(bind_pat); + let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, bind_pat.syntax(), None); + let refs = analyzer.find_all_refs(bind_pat)?; let mut wrap_in_parens = vec![true; refs.len()]; -- cgit v1.2.3 From f4a94e74bcd6c8f9275a57a775e64314af1878da Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 13 Apr 2019 10:49:01 +0300 Subject: fold ScopeWithSyntax into SourceAnalyzer --- crates/ra_assists/src/inline_local_variable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs index 9493acec9..e0479ef13 100644 --- a/crates/ra_assists/src/inline_local_variable.rs +++ b/crates/ra_assists/src/inline_local_variable.rs @@ -27,7 +27,7 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx) -> Opt let_stmt.syntax().range() }; let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, bind_pat.syntax(), None); - let refs = analyzer.find_all_refs(bind_pat)?; + let refs = analyzer.find_all_refs(bind_pat); let mut wrap_in_parens = vec![true; refs.len()]; -- cgit v1.2.3