From 92665358cd98913e3fef8294e1889cc0bb919e3f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 May 2020 23:56:10 +0800 Subject: Rename ImplItem to AssocItem --- .../src/handlers/add_missing_impl_members.rs | 26 ++++++------ crates/ra_assists/src/handlers/add_new.rs | 4 +- crates/ra_assists/src/utils.rs | 10 ++--- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir_def/src/data.rs | 18 ++++----- crates/ra_hir_ty/src/tests/macros.rs | 4 +- .../src/completion/complete_qualified_path.rs | 2 +- .../ra_ide/src/completion/complete_trait_impl.rs | 46 ++++++++++++---------- crates/ra_mbe/src/syntax_bridge.rs | 2 +- crates/ra_syntax/src/ast/edit.rs | 6 +-- crates/ra_syntax/src/ast/generated/nodes.rs | 36 ++++++++--------- xtask/src/ast_src.rs | 4 +- 12 files changed, 83 insertions(+), 77 deletions(-) diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs index e466c9a86..e47feda71 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -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 { + let def_name = |item: &ast::AssocItem| -> Option { 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() }; diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs index 0f9174a29..e8a36c7de 100644 --- a/crates/ra_assists/src/handlers/add_new.rs +++ b/crates/ra_assists/src/handlers/add_new.rs @@ -162,8 +162,8 @@ fn find_struct_impl(ctx: &AssistCtx, strukt: &ast::StructDef) -> Option 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; diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 6be704ce3..2f15a3f15 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -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, impl_def: &ast::ImplDef, ) -> Vec { @@ -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()); } diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index a004363ee..5f480c304 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -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( + pub fn iterate_assoc_items( self, db: &dyn HirDatabase, krate: Crate, diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 2dbae04d3..e7eb2bb11 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -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, + assoc_items: impl Iterator, 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)) } diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs index 29e38a06c..07398ddcc 100644 --- a/crates/ra_hir_ty/src/tests/macros.rs +++ b/crates/ra_hir_ty/src/tests/macros.rs @@ -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 diff --git a/crates/ra_ide/src/completion/complete_qualified_path.rs b/crates/ra_ide/src/completion/complete_qualified_path.rs index d9ea92ef8..7fcd22525 100644 --- a/crates/ra_ide/src/completion/complete_qualified_path.rs +++ b/crates/ra_ide/src/completion/complete_qualified_path.rs @@ -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; } diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs index ee32d1ff6..039df03e0 100644 --- a/crates/ra_ide/src/completion/complete_trait_impl.rs +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs @@ -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 { - hir::AssocItem::Function(fn_item) => Some(fn_item), - _ => None, - }, - ) { + 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 { - hir::AssocItem::TypeAlias(type_item) => Some(type_item), - _ => None, - }, - ) { + 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 { - hir::AssocItem::Const(const_item) => Some(const_item), - _ => None, - }, - ) { + 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); } } diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index bb28acfd9..fc4133a67 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -63,7 +63,7 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke // * Items(SmallVec<[P; 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( diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index c507dc683..3e6dd6061 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -79,7 +79,7 @@ where impl ast::ItemList { #[must_use] - pub fn append_items(&self, items: impl IntoIterator) -> ast::ItemList { + pub fn append_items(&self, items: impl IntoIterator) -> 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()), diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index c2cc25958..b00c15608 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs @@ -196,7 +196,7 @@ pub struct ItemList { impl ast::ModuleItemOwner for ItemList {} impl ItemList { pub fn l_curly_token(&self) -> Option { support::token(&self.syntax, T!['{']) } - pub fn impl_items(&self) -> AstChildren { support::children(&self.syntax) } + pub fn assoc_items(&self) -> AstChildren { support::children(&self.syntax) } pub fn r_curly_token(&self) -> Option { 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 for ImplItem { - fn from(node: FnDef) -> ImplItem { ImplItem::FnDef(node) } +impl From for AssocItem { + fn from(node: FnDef) -> AssocItem { AssocItem::FnDef(node) } } -impl From for ImplItem { - fn from(node: TypeAliasDef) -> ImplItem { ImplItem::TypeAliasDef(node) } +impl From for AssocItem { + fn from(node: TypeAliasDef) -> AssocItem { AssocItem::TypeAliasDef(node) } } -impl From for ImplItem { - fn from(node: ConstDef) -> ImplItem { ImplItem::ConstDef(node) } +impl From 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 { 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) } diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs index 2f8065b73..fe3eb85de 100644 --- a/xtask/src/ast_src.rs +++ b/xtask/src/ast_src.rs @@ -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 } -- cgit v1.2.3