Rename ImplItem to AssocItem

This commit is contained in:
Edwin Cheng 2020-05-05 23:56:10 +08:00
parent 756e91732b
commit 92665358cd
12 changed files with 83 additions and 77 deletions

View file

@ -10,7 +10,7 @@ use ra_syntax::{
use crate::{
ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams},
utils::{get_missing_impl_items, resolve_target_trait},
utils::{get_missing_assoc_items, resolve_target_trait},
Assist, AssistCtx, AssistId,
};
@ -112,25 +112,25 @@ fn add_missing_impl_members_inner(
let trait_ = resolve_target_trait(&ctx.sema, &impl_node)?;
let def_name = |item: &ast::ImplItem| -> Option<SmolStr> {
let def_name = |item: &ast::AssocItem| -> Option<SmolStr> {
match item {
ast::ImplItem::FnDef(def) => def.name(),
ast::ImplItem::TypeAliasDef(def) => def.name(),
ast::ImplItem::ConstDef(def) => def.name(),
ast::AssocItem::FnDef(def) => def.name(),
ast::AssocItem::TypeAliasDef(def) => def.name(),
ast::AssocItem::ConstDef(def) => def.name(),
}
.map(|it| it.text().clone())
};
let missing_items = get_missing_impl_items(&ctx.sema, &impl_node)
let missing_items = get_missing_assoc_items(&ctx.sema, &impl_node)
.iter()
.map(|i| match i {
hir::AssocItem::Function(i) => ast::ImplItem::FnDef(i.source(ctx.db).value),
hir::AssocItem::TypeAlias(i) => ast::ImplItem::TypeAliasDef(i.source(ctx.db).value),
hir::AssocItem::Const(i) => ast::ImplItem::ConstDef(i.source(ctx.db).value),
hir::AssocItem::Function(i) => ast::AssocItem::FnDef(i.source(ctx.db).value),
hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAliasDef(i.source(ctx.db).value),
hir::AssocItem::Const(i) => ast::AssocItem::ConstDef(i.source(ctx.db).value),
})
.filter(|t| def_name(&t).is_some())
.filter(|t| match t {
ast::ImplItem::FnDef(def) => match mode {
ast::AssocItem::FnDef(def) => match mode {
AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(),
AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(),
},
@ -145,7 +145,7 @@ fn add_missing_impl_members_inner(
let sema = ctx.sema;
ctx.add_assist(AssistId(assist_id), label, |edit| {
let n_existing_items = impl_item_list.impl_items().count();
let n_existing_items = impl_item_list.assoc_items().count();
let source_scope = sema.scope_for_def(trait_);
let target_scope = sema.scope(impl_item_list.syntax());
let ast_transform = QualifyPaths::new(&target_scope, &source_scope)
@ -154,13 +154,13 @@ fn add_missing_impl_members_inner(
.into_iter()
.map(|it| ast_transform::apply(&*ast_transform, it))
.map(|it| match it {
ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)),
ast::AssocItem::FnDef(def) => ast::AssocItem::FnDef(add_body(def)),
_ => it,
})
.map(|it| edit::remove_attrs_and_docs(&it));
let new_impl_item_list = impl_item_list.append_items(items);
let cursor_position = {
let first_new_item = new_impl_item_list.impl_items().nth(n_existing_items).unwrap();
let first_new_item = new_impl_item_list.assoc_items().nth(n_existing_items).unwrap();
first_new_item.syntax().text_range().start()
};

View file

@ -162,8 +162,8 @@ fn find_struct_impl(ctx: &AssistCtx, strukt: &ast::StructDef) -> Option<Option<a
fn has_new_fn(imp: &ast::ImplDef) -> bool {
if let Some(il) = imp.item_list() {
for item in il.impl_items() {
if let ast::ImplItem::FnDef(f) = item {
for item in il.assoc_items() {
if let ast::AssocItem::FnDef(f) = item {
if let Some(name) = f.name() {
if name.text().eq_ignore_ascii_case("new") {
return true;

View file

@ -13,7 +13,7 @@ use rustc_hash::FxHashSet;
pub(crate) use insert_use::insert_use_statement;
pub fn get_missing_impl_items(
pub fn get_missing_assoc_items(
sema: &Semantics<RootDatabase>,
impl_def: &ast::ImplDef,
) -> Vec<hir::AssocItem> {
@ -23,21 +23,21 @@ pub fn get_missing_impl_items(
let mut impl_type = FxHashSet::default();
if let Some(item_list) = impl_def.item_list() {
for item in item_list.impl_items() {
for item in item_list.assoc_items() {
match item {
ast::ImplItem::FnDef(f) => {
ast::AssocItem::FnDef(f) => {
if let Some(n) = f.name() {
impl_fns_consts.insert(n.syntax().to_string());
}
}
ast::ImplItem::TypeAliasDef(t) => {
ast::AssocItem::TypeAliasDef(t) => {
if let Some(n) = t.name() {
impl_type.insert(n.syntax().to_string());
}
}
ast::ImplItem::ConstDef(c) => {
ast::AssocItem::ConstDef(c) => {
if let Some(n) = c.name() {
impl_fns_consts.insert(n.syntax().to_string());
}

View file

@ -1211,7 +1211,7 @@ impl Type {
// This would be nicer if it just returned an iterator, but that runs into
// lifetime problems, because we need to borrow temp `CrateImplDefs`.
pub fn iterate_impl_items<T>(
pub fn iterate_assoc_items<T>(
self,
db: &dyn HirDatabase,
krate: Crate,

View file

@ -9,7 +9,7 @@ use hir_expand::{
};
use ra_prof::profile;
use ra_syntax::ast::{
self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, TypeBoundsOwner,
self, AssocItem, AstNode, ModuleItemOwner, NameOwner, TypeAscriptionOwner, TypeBoundsOwner,
VisibilityOwner,
};
@ -164,7 +164,7 @@ impl TraitData {
items.extend(collect_items(
db,
&mut expander,
item_list.impl_items(),
item_list.assoc_items(),
src.file_id,
container,
));
@ -219,7 +219,7 @@ impl ImplData {
if let Some(item_list) = src.value.item_list() {
let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id);
items.extend(
collect_items(db, &mut expander, item_list.impl_items(), src.file_id, container)
collect_items(db, &mut expander, item_list.assoc_items(), src.file_id, container)
.into_iter()
.map(|(_, item)| item),
);
@ -304,7 +304,7 @@ fn collect_items_in_macro(
let mut res = collect_items(
db,
expander,
items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())),
items.value.items().filter_map(|it| AssocItem::cast(it.syntax().clone())),
items.file_id,
container,
);
@ -325,15 +325,15 @@ fn collect_items_in_macro(
fn collect_items(
db: &dyn DefDatabase,
expander: &mut Expander,
impl_items: impl Iterator<Item = ImplItem>,
assoc_items: impl Iterator<Item = AssocItem>,
file_id: crate::HirFileId,
container: AssocContainerId,
) -> Vec<(Name, AssocItemId)> {
let items = db.ast_id_map(file_id);
impl_items
assoc_items
.filter_map(|item_node| match item_node {
ast::ImplItem::FnDef(it) => {
ast::AssocItem::FnDef(it) => {
let name = it.name().map_or_else(Name::missing, |it| it.as_name());
if !expander.is_cfg_enabled(&it) {
return None;
@ -342,13 +342,13 @@ fn collect_items(
.intern(db);
Some((name, def.into()))
}
ast::ImplItem::ConstDef(it) => {
ast::AssocItem::ConstDef(it) => {
let name = it.name().map_or_else(Name::missing, |it| it.as_name());
let def = ConstLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) }
.intern(db);
Some((name, def.into()))
}
ast::ImplItem::TypeAliasDef(it) => {
ast::AssocItem::TypeAliasDef(it) => {
let name = it.name().map_or_else(Name::missing, |it| it.as_name());
let def =
TypeAliasLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) }

View file

@ -269,7 +269,7 @@ fn test() { S.foo()<|>; }
}
#[test]
fn infer_impl_items_generated_by_macros() {
fn infer_assoc_items_generated_by_macros() {
let t = type_at(
r#"
//- /main.rs
@ -288,7 +288,7 @@ fn test() { S.foo()<|>; }
}
#[test]
fn infer_impl_items_generated_by_macros_chain() {
fn infer_assoc_items_generated_by_macros_chain() {
let t = type_at(
r#"
//- /main.rs

View file

@ -84,7 +84,7 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
});
// Iterate assoc types separately
ty.iterate_impl_items(ctx.db, krate, |item| {
ty.iterate_assoc_items(ctx.db, krate, |item| {
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
return None;
}

View file

@ -32,7 +32,7 @@
//! ```
use hir::{self, Docs, HasSource};
use ra_assists::utils::get_missing_impl_items;
use ra_assists::utils::get_missing_assoc_items;
use ra_syntax::{
ast::{self, edit, ImplDef},
AstNode, SyntaxKind, SyntaxNode, TextRange, T,
@ -50,7 +50,7 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
if let Some((trigger, impl_def)) = completion_match(ctx) {
match trigger.kind() {
SyntaxKind::NAME_REF => {
get_missing_impl_items(&ctx.sema, &impl_def).iter().for_each(|item| match item {
get_missing_assoc_items(&ctx.sema, &impl_def).iter().for_each(|item| match item {
hir::AssocItem::Function(fn_item) => {
add_function_impl(&trigger, acc, ctx, &fn_item)
}
@ -64,34 +64,40 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
}
SyntaxKind::FN_DEF => {
for missing_fn in get_missing_impl_items(&ctx.sema, &impl_def).iter().filter_map(
|item| match item {
for missing_fn in
get_missing_assoc_items(&ctx.sema, &impl_def).iter().filter_map(|item| {
match item {
hir::AssocItem::Function(fn_item) => Some(fn_item),
_ => None,
},
) {
}
})
{
add_function_impl(&trigger, acc, ctx, &missing_fn);
}
}
SyntaxKind::TYPE_ALIAS_DEF => {
for missing_fn in get_missing_impl_items(&ctx.sema, &impl_def).iter().filter_map(
|item| match item {
for missing_fn in
get_missing_assoc_items(&ctx.sema, &impl_def).iter().filter_map(|item| {
match item {
hir::AssocItem::TypeAlias(type_item) => Some(type_item),
_ => None,
},
) {
}
})
{
add_type_alias_impl(&trigger, acc, ctx, &missing_fn);
}
}
SyntaxKind::CONST_DEF => {
for missing_fn in get_missing_impl_items(&ctx.sema, &impl_def).iter().filter_map(
|item| match item {
for missing_fn in
get_missing_assoc_items(&ctx.sema, &impl_def).iter().filter_map(|item| {
match item {
hir::AssocItem::Const(const_item) => Some(const_item),
_ => None,
},
) {
}
})
{
add_const_impl(&trigger, acc, ctx, &missing_fn);
}
}

View file

@ -63,7 +63,7 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
// * Items(SmallVec<[P<ast::Item>; 1]>) -> token_tree_to_items
//
// * TraitItems(SmallVec<[ast::TraitItem; 1]>)
// * ImplItems(SmallVec<[ast::ImplItem; 1]>)
// * AssocItems(SmallVec<[ast::AssocItem; 1]>)
// * ForeignItems(SmallVec<[ast::ForeignItem; 1]>
pub fn token_tree_to_syntax_node(

View file

@ -79,7 +79,7 @@ where
impl ast::ItemList {
#[must_use]
pub fn append_items(&self, items: impl IntoIterator<Item = ast::ImplItem>) -> ast::ItemList {
pub fn append_items(&self, items: impl IntoIterator<Item = ast::AssocItem>) -> ast::ItemList {
let mut res = self.clone();
if !self.syntax().text().contains_char('\n') {
res = make_multiline(res);
@ -89,8 +89,8 @@ impl ast::ItemList {
}
#[must_use]
pub fn append_item(&self, item: ast::ImplItem) -> ast::ItemList {
let (indent, position) = match self.impl_items().last() {
pub fn append_item(&self, item: ast::AssocItem) -> ast::ItemList {
let (indent, position) = match self.assoc_items().last() {
Some(it) => (
leading_indent(it.syntax()).unwrap_or_default().to_string(),
InsertPosition::After(it.syntax().clone().into()),

View file

@ -196,7 +196,7 @@ pub struct ItemList {
impl ast::ModuleItemOwner for ItemList {}
impl ItemList {
pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) }
pub fn impl_items(&self) -> AstChildren<ImplItem> { support::children(&self.syntax) }
pub fn assoc_items(&self) -> AstChildren<AssocItem> { support::children(&self.syntax) }
pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
}
@ -1429,13 +1429,13 @@ impl ast::AttrsOwner for ModuleItem {}
impl ast::VisibilityOwner for ModuleItem {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ImplItem {
pub enum AssocItem {
FnDef(FnDef),
TypeAliasDef(TypeAliasDef),
ConstDef(ConstDef),
}
impl ast::NameOwner for ImplItem {}
impl ast::AttrsOwner for ImplItem {}
impl ast::NameOwner for AssocItem {}
impl ast::AttrsOwner for AssocItem {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ExternItem {
@ -3167,16 +3167,16 @@ impl AstNode for ModuleItem {
}
}
}
impl From<FnDef> for ImplItem {
fn from(node: FnDef) -> ImplItem { ImplItem::FnDef(node) }
impl From<FnDef> for AssocItem {
fn from(node: FnDef) -> AssocItem { AssocItem::FnDef(node) }
}
impl From<TypeAliasDef> for ImplItem {
fn from(node: TypeAliasDef) -> ImplItem { ImplItem::TypeAliasDef(node) }
impl From<TypeAliasDef> for AssocItem {
fn from(node: TypeAliasDef) -> AssocItem { AssocItem::TypeAliasDef(node) }
}
impl From<ConstDef> for ImplItem {
fn from(node: ConstDef) -> ImplItem { ImplItem::ConstDef(node) }
impl From<ConstDef> for AssocItem {
fn from(node: ConstDef) -> AssocItem { AssocItem::ConstDef(node) }
}
impl AstNode for ImplItem {
impl AstNode for AssocItem {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
FN_DEF | TYPE_ALIAS_DEF | CONST_DEF => true,
@ -3185,18 +3185,18 @@ impl AstNode for ImplItem {
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() {
FN_DEF => ImplItem::FnDef(FnDef { syntax }),
TYPE_ALIAS_DEF => ImplItem::TypeAliasDef(TypeAliasDef { syntax }),
CONST_DEF => ImplItem::ConstDef(ConstDef { syntax }),
FN_DEF => AssocItem::FnDef(FnDef { syntax }),
TYPE_ALIAS_DEF => AssocItem::TypeAliasDef(TypeAliasDef { syntax }),
CONST_DEF => AssocItem::ConstDef(ConstDef { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxNode {
match self {
ImplItem::FnDef(it) => &it.syntax,
ImplItem::TypeAliasDef(it) => &it.syntax,
ImplItem::ConstDef(it) => &it.syntax,
AssocItem::FnDef(it) => &it.syntax,
AssocItem::TypeAliasDef(it) => &it.syntax,
AssocItem::ConstDef(it) => &it.syntax,
}
}
}
@ -3641,7 +3641,7 @@ impl std::fmt::Display for ModuleItem {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for ImplItem {
impl std::fmt::Display for AssocItem {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}

View file

@ -373,7 +373,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
struct ItemList: ModuleItemOwner {
T!['{'],
impl_items: [ImplItem],
assoc_items: [AssocItem],
T!['}']
}
@ -685,7 +685,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
}
/* impl blocks can also contain MacroCall */
enum ImplItem: NameOwner, AttrsOwner {
enum AssocItem: NameOwner, AttrsOwner {
FnDef, TypeAliasDef, ConstDef
}