From f57682c0b3b17a09d728c77134200b4151b2358d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 4 Mar 2020 14:25:22 +0100 Subject: Remove old find refs infra --- .../src/handlers/inline_local_variable.rs | 29 +++++++++++------- crates/ra_assists/src/marks.rs | 5 ++-- crates/ra_hir/src/semantics.rs | 8 +---- crates/ra_hir/src/source_analyzer.rs | 35 +--------------------- 4 files changed, 23 insertions(+), 54 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs index eb5112343..3bfcba8ff 100644 --- a/crates/ra_assists/src/handlers/inline_local_variable.rs +++ b/crates/ra_assists/src/handlers/inline_local_variable.rs @@ -1,3 +1,4 @@ +use ra_ide_db::defs::Definition; use ra_syntax::{ ast::{self, AstNode, AstToken}, TextRange, @@ -37,6 +38,15 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option { return None; } let initializer_expr = let_stmt.initializer()?; + + let def = ctx.sema.to_def(&bind_pat)?; + let def = Definition::Local(def); + let refs = def.find_usages(ctx.db, None); + if refs.is_empty() { + tested_by!(test_not_applicable_if_variable_unused); + return None; + }; + let delete_range = if let Some(whitespace) = let_stmt .syntax() .next_sibling_or_token() @@ -49,16 +59,14 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option { } else { let_stmt.syntax().text_range() }; - let refs = ctx.sema.find_all_refs(&bind_pat); - if refs.is_empty() { - return None; - }; let mut wrap_in_parens = vec![true; refs.len()]; for (i, desc) in refs.iter().enumerate() { - let usage_node = - ctx.covering_node_for_range(desc.range).ancestors().find_map(ast::PathExpr::cast)?; + let usage_node = ctx + .covering_node_for_range(desc.file_range.range) + .ancestors() + .find_map(ast::PathExpr::cast)?; let usage_parent_option = usage_node.syntax().parent().and_then(ast::Expr::cast); let usage_parent = match usage_parent_option { Some(u) => u, @@ -103,11 +111,9 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option { move |edit: &mut ActionBuilder| { edit.delete(delete_range); for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) { - if should_wrap { - edit.replace(desc.range, init_in_paren.clone()) - } else { - edit.replace(desc.range, init_str.clone()) - } + let replacement = + if should_wrap { init_in_paren.clone() } else { init_str.clone() }; + edit.replace(desc.file_range.range, replacement) } edit.set_cursor(delete_range.start()) }, @@ -657,6 +663,7 @@ fn foo() { #[test] fn test_not_applicable_if_variable_unused() { + covers!(test_not_applicable_if_variable_unused); check_assist_not_applicable( inline_local_variable, r" diff --git a/crates/ra_assists/src/marks.rs b/crates/ra_assists/src/marks.rs index cef3df4e5..22404ee80 100644 --- a/crates/ra_assists/src/marks.rs +++ b/crates/ra_assists/src/marks.rs @@ -1,9 +1,10 @@ //! See test_utils/src/marks.rs -test_utils::marks!( +test_utils::marks![ introduce_var_in_comment_is_not_applicable test_introduce_var_expr_stmt test_introduce_var_last_expr not_applicable_outside_of_bind_pat test_not_inline_mut_variable -); + test_not_applicable_if_variable_unused +]; diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index afc7f7ee7..7ce785791 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -19,7 +19,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ db::HirDatabase, semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, - source_analyzer::{resolve_hir_path, ReferenceDescriptor, SourceAnalyzer}, + source_analyzer::{resolve_hir_path, SourceAnalyzer}, Function, HirFileId, InFile, Local, MacroDef, Module, ModuleDef, Name, Origin, Path, PathResolution, ScopeDef, StructField, Trait, Type, TypeParam, VariantDef, }; @@ -171,12 +171,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { SemanticsScope { db: self.db, resolver } } - // FIXME: we only use this in `inline_local_variable` assist, ideally, we - // should switch to general reference search infra there. - pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec { - self.analyze(pat.syntax()).find_all_refs(pat) - } - fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer { let src = self.find_file(node.clone()); self.analyze2(src.as_ref(), None) diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index 015389fb0..73cff17c9 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -7,7 +7,6 @@ //! purely for "IDE needs". use std::{iter::once, sync::Arc}; -use either::Either; use hir_def::{ body::{ scope::{ExprScopes, ScopeId}, @@ -21,7 +20,7 @@ use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; use hir_ty::{InEnvironment, InferenceResult, TraitEnvironment}; use ra_syntax::{ ast::{self, AstNode}, - AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit, + SyntaxNode, SyntaxNodePtr, TextRange, TextUnit, }; use crate::{ @@ -251,38 +250,6 @@ impl SourceAnalyzer { resolve_hir_path(db, &self.resolver, &hir_path) } - fn resolve_local_name( - &self, - name_ref: &ast::NameRef, - ) -> Option, AstPtr>> { - let name = name_ref.as_name(); - let source_map = self.body_source_map.as_ref()?; - let scopes = self.scopes.as_ref()?; - let scope = scope_for(scopes, source_map, InFile::new(self.file_id, name_ref.syntax()))?; - let entry = scopes.resolve_name_in_scope(scope, &name)?; - Some(source_map.pat_syntax(entry.pat())?.value) - } - - // FIXME: we only use this in `inline_local_variable` assist, ideally, we - // should switch to general reference search infra there. - pub(crate) fn find_all_refs(&self, pat: &ast::BindPat) -> Vec { - let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); - let ptr = Either::Left(AstPtr::new(&ast::Pat::from(pat.clone()))); - fn_def - .syntax() - .descendants() - .filter_map(ast::NameRef::cast) - .filter(|name_ref| match self.resolve_local_name(&name_ref) { - None => false, - Some(d_ptr) => d_ptr == ptr, - }) - .map(|name_ref| ReferenceDescriptor { - name: name_ref.text().to_string(), - range: name_ref.syntax().text_range(), - }) - .collect() - } - pub(crate) fn expand( &self, db: &impl HirDatabase, -- cgit v1.2.3