Use NameLike in FileReference directly as its not exported from ide anymore

This commit is contained in:
Lukas Wirth 2021-02-09 16:03:39 +01:00
parent d644728d82
commit c5be0b2589
5 changed files with 29 additions and 59 deletions

View File

@ -124,11 +124,10 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O
move |builder| {
builder.delete(delete_range);
for (file_id, references) in usages.references {
let root = ctx.sema.parse(file_id);
for (&should_wrap, reference) in wrap_in_parens[&file_id].iter().zip(references) {
let replacement =
if should_wrap { init_in_paren.clone() } else { init_str.clone() };
match &reference.as_name_ref(root.syntax()) {
match reference.name.as_name_ref() {
Some(name_ref)
if ast::RecordExprField::for_field_name(name_ref).is_some() =>
{

View File

@ -94,7 +94,7 @@ macro_rules! eprintln {
call_info::CallInfo,
label::Label,
line_index::{LineCol, LineIndex},
search::{FileReference, ReferenceAccess, SearchScope},
search::{ReferenceAccess, SearchScope},
source_change::{FileSystemEdit, SourceChange},
symbol_index::Query,
RootDatabase,

View File

@ -66,12 +66,8 @@ pub(crate) fn find_all_refs(
let mut usages = def.usages(sema).set_scope(search_scope).all();
if let Some(ctor_filter) = ctor_filter {
// filter for constructor-literals
usages.references.iter_mut().for_each(|(&file_id, it)| {
let root = sema.parse(file_id);
let root = root.syntax();
it.retain(|reference| {
reference.as_name_ref(root).map_or(false, |name_ref| ctor_filter(&name_ref))
})
usages.references.values_mut().for_each(|it| {
it.retain(|reference| reference.name.as_name_ref().map_or(false, ctor_filter));
});
usages.references.retain(|_, it| !it.is_empty());
}

View File

@ -164,20 +164,18 @@ fn find_definition(
}
fn source_edit_from_references(
sema: &Semantics<RootDatabase>,
_sema: &Semantics<RootDatabase>,
file_id: FileId,
references: &[FileReference],
def: Definition,
new_name: &str,
) -> (FileId, TextEdit) {
let root = sema.parse(file_id);
let mut edit = TextEdit::builder();
for reference in references {
let (range, replacement) = match &reference.name_from_syntax(root.syntax()) {
Some(NameLike::Name(_)) => (None, format!("{}", new_name)),
Some(NameLike::NameRef(name_ref)) => source_edit_from_name_ref(name_ref, new_name, def),
Some(NameLike::Lifetime(_)) => (None, format!("{}", new_name)),
None => (None, new_name.to_owned()),
let (range, replacement) = match &reference.name {
NameLike::Name(_) => (None, format!("{}", new_name)),
NameLike::NameRef(name_ref) => source_edit_from_name_ref(name_ref, new_name, def),
NameLike::Lifetime(_) => (None, format!("{}", new_name)),
};
// FIXME: Some(range) will be incorrect when we are inside macros
edit.replace(range.unwrap_or(reference.range), replacement);

View File

@ -10,9 +10,7 @@
use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility};
use once_cell::unsync::Lazy;
use rustc_hash::FxHashMap;
use syntax::{
ast, match_ast, AstNode, NodeOrToken, SyntaxElement, SyntaxNode, TextRange, TextSize,
};
use syntax::{ast, match_ast, AstNode, TextRange, TextSize};
use crate::defs::NameClass;
use crate::{
@ -20,13 +18,6 @@
RootDatabase,
};
#[derive(Debug, Clone)]
pub enum NameKind {
Name,
NameRef,
Lifetime,
}
#[derive(Debug, Default, Clone)]
pub struct UsageSearchResult {
pub references: FxHashMap<FileId, Vec<FileReference>>,
@ -68,6 +59,15 @@ pub enum NameLike {
Lifetime(ast::Lifetime),
}
impl NameLike {
pub fn as_name_ref(&self) -> Option<&ast::NameRef> {
match self {
NameLike::NameRef(name_ref) => Some(name_ref),
_ => None,
}
}
}
mod __ {
use super::{
ast::{Lifetime, Name, NameRef},
@ -79,37 +79,10 @@ mod __ {
#[derive(Debug, Clone)]
pub struct FileReference {
pub range: TextRange,
pub name: NameKind,
pub name: NameLike,
pub access: Option<ReferenceAccess>,
}
impl FileReference {
pub fn name_from_syntax(&self, root: &SyntaxNode) -> Option<NameLike> {
let node = node_or_parent(root.covering_element(self.range));
match self.name {
NameKind::Name => ast::Name::cast(node).map(Into::into),
NameKind::NameRef => ast::NameRef::cast(node).map(Into::into),
NameKind::Lifetime => ast::Lifetime::cast(node).map(Into::into),
}
}
pub fn as_name_ref(&self, root: &SyntaxNode) -> Option<ast::NameRef> {
match self.name {
NameKind::NameRef => {
ast::NameRef::cast(node_or_parent(root.covering_element(self.range)))
}
_ => None,
}
}
}
fn node_or_parent(ele: SyntaxElement) -> SyntaxNode {
match ele {
NodeOrToken::Node(node) => node,
NodeOrToken::Token(token) => token.parent(),
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ReferenceAccess {
Read,
@ -408,7 +381,11 @@ fn found_lifetime(
match NameRefClass::classify_lifetime(self.sema, lifetime) {
Some(NameRefClass::Definition(def)) if &def == self.def => {
let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax());
let reference = FileReference { range, name: NameKind::Lifetime, access: None };
let reference = FileReference {
range,
name: NameLike::Lifetime(lifetime.clone()),
access: None,
};
sink(file_id, reference)
}
_ => false, // not a usage
@ -425,7 +402,7 @@ fn found_name_ref(
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
let reference = FileReference {
range,
name: NameKind::NameRef,
name: NameLike::NameRef(name_ref.clone()),
access: reference_access(&def, &name_ref),
};
sink(file_id, reference)
@ -435,12 +412,12 @@ fn found_name_ref(
let reference = match self.def {
Definition::Field(_) if &field == self.def => FileReference {
range,
name: NameKind::NameRef,
name: NameLike::NameRef(name_ref.clone()),
access: reference_access(&field, &name_ref),
},
Definition::Local(l) if &local == l => FileReference {
range,
name: NameKind::NameRef,
name: NameLike::NameRef(name_ref.clone()),
access: reference_access(&Definition::Local(local), &name_ref),
},
_ => return false, // not a usage
@ -464,7 +441,7 @@ fn found_name(
let FileRange { file_id, range } = self.sema.original_range(name.syntax());
let reference = FileReference {
range,
name: NameKind::Name,
name: NameLike::Name(name.clone()),
// FIXME: mutable patterns should have `Write` access
access: Some(ReferenceAccess::Read),
};