Remove old find refs infra

This commit is contained in:
Aleksey Kladov 2020-03-04 14:25:22 +01:00
parent 94189d0a1c
commit f57682c0b3
4 changed files with 23 additions and 54 deletions

View file

@ -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<Assist> {
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<Assist> {
} 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<Assist> {
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"

View file

@ -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
];

View file

@ -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<ReferenceDescriptor> {
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)

View file

@ -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<Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>> {
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<ReferenceDescriptor> {
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,