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::{ use crate::{
ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, 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, Assist, AssistCtx, AssistId,
}; };
@ -112,25 +112,25 @@ fn add_missing_impl_members_inner(
let trait_ = resolve_target_trait(&ctx.sema, &impl_node)?; 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 { match item {
ast::ImplItem::FnDef(def) => def.name(), ast::AssocItem::FnDef(def) => def.name(),
ast::ImplItem::TypeAliasDef(def) => def.name(), ast::AssocItem::TypeAliasDef(def) => def.name(),
ast::ImplItem::ConstDef(def) => def.name(), ast::AssocItem::ConstDef(def) => def.name(),
} }
.map(|it| it.text().clone()) .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() .iter()
.map(|i| match i { .map(|i| match i {
hir::AssocItem::Function(i) => ast::ImplItem::FnDef(i.source(ctx.db).value), hir::AssocItem::Function(i) => ast::AssocItem::FnDef(i.source(ctx.db).value),
hir::AssocItem::TypeAlias(i) => ast::ImplItem::TypeAliasDef(i.source(ctx.db).value), hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAliasDef(i.source(ctx.db).value),
hir::AssocItem::Const(i) => ast::ImplItem::ConstDef(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| def_name(&t).is_some())
.filter(|t| match t { .filter(|t| match t {
ast::ImplItem::FnDef(def) => match mode { ast::AssocItem::FnDef(def) => match mode {
AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(), AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(),
AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(), AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(),
}, },
@ -145,7 +145,7 @@ fn add_missing_impl_members_inner(
let sema = ctx.sema; let sema = ctx.sema;
ctx.add_assist(AssistId(assist_id), label, |edit| { 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 source_scope = sema.scope_for_def(trait_);
let target_scope = sema.scope(impl_item_list.syntax()); let target_scope = sema.scope(impl_item_list.syntax());
let ast_transform = QualifyPaths::new(&target_scope, &source_scope) let ast_transform = QualifyPaths::new(&target_scope, &source_scope)
@ -154,13 +154,13 @@ fn add_missing_impl_members_inner(
.into_iter() .into_iter()
.map(|it| ast_transform::apply(&*ast_transform, it)) .map(|it| ast_transform::apply(&*ast_transform, it))
.map(|it| match 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, _ => it,
}) })
.map(|it| edit::remove_attrs_and_docs(&it)); .map(|it| edit::remove_attrs_and_docs(&it));
let new_impl_item_list = impl_item_list.append_items(items); let new_impl_item_list = impl_item_list.append_items(items);
let cursor_position = { 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() 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 { fn has_new_fn(imp: &ast::ImplDef) -> bool {
if let Some(il) = imp.item_list() { if let Some(il) = imp.item_list() {
for item in il.impl_items() { for item in il.assoc_items() {
if let ast::ImplItem::FnDef(f) = item { if let ast::AssocItem::FnDef(f) = item {
if let Some(name) = f.name() { if let Some(name) = f.name() {
if name.text().eq_ignore_ascii_case("new") { if name.text().eq_ignore_ascii_case("new") {
return true; return true;

View file

@ -13,7 +13,7 @@ use rustc_hash::FxHashSet;
pub(crate) use insert_use::insert_use_statement; pub(crate) use insert_use::insert_use_statement;
pub fn get_missing_impl_items( pub fn get_missing_assoc_items(
sema: &Semantics<RootDatabase>, sema: &Semantics<RootDatabase>,
impl_def: &ast::ImplDef, impl_def: &ast::ImplDef,
) -> Vec<hir::AssocItem> { ) -> Vec<hir::AssocItem> {
@ -23,21 +23,21 @@ pub fn get_missing_impl_items(
let mut impl_type = FxHashSet::default(); let mut impl_type = FxHashSet::default();
if let Some(item_list) = impl_def.item_list() { 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 { match item {
ast::ImplItem::FnDef(f) => { ast::AssocItem::FnDef(f) => {
if let Some(n) = f.name() { if let Some(n) = f.name() {
impl_fns_consts.insert(n.syntax().to_string()); impl_fns_consts.insert(n.syntax().to_string());
} }
} }
ast::ImplItem::TypeAliasDef(t) => { ast::AssocItem::TypeAliasDef(t) => {
if let Some(n) = t.name() { if let Some(n) = t.name() {
impl_type.insert(n.syntax().to_string()); impl_type.insert(n.syntax().to_string());
} }
} }
ast::ImplItem::ConstDef(c) => { ast::AssocItem::ConstDef(c) => {
if let Some(n) = c.name() { if let Some(n) = c.name() {
impl_fns_consts.insert(n.syntax().to_string()); 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 // This would be nicer if it just returned an iterator, but that runs into
// lifetime problems, because we need to borrow temp `CrateImplDefs`. // lifetime problems, because we need to borrow temp `CrateImplDefs`.
pub fn iterate_impl_items<T>( pub fn iterate_assoc_items<T>(
self, self,
db: &dyn HirDatabase, db: &dyn HirDatabase,
krate: Crate, krate: Crate,

View file

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

View file

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

View file

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

View file

@ -32,7 +32,7 @@
//! ``` //! ```
use hir::{self, Docs, HasSource}; use hir::{self, Docs, HasSource};
use ra_assists::utils::get_missing_impl_items; use ra_assists::utils::get_missing_assoc_items;
use ra_syntax::{ use ra_syntax::{
ast::{self, edit, ImplDef}, ast::{self, edit, ImplDef},
AstNode, SyntaxKind, SyntaxNode, TextRange, T, 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) { if let Some((trigger, impl_def)) = completion_match(ctx) {
match trigger.kind() { match trigger.kind() {
SyntaxKind::NAME_REF => { 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) => { hir::AssocItem::Function(fn_item) => {
add_function_impl(&trigger, acc, ctx, &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 => { SyntaxKind::FN_DEF => {
for missing_fn in get_missing_impl_items(&ctx.sema, &impl_def).iter().filter_map( for missing_fn in
|item| match item { get_missing_assoc_items(&ctx.sema, &impl_def).iter().filter_map(|item| {
hir::AssocItem::Function(fn_item) => Some(fn_item), match item {
_ => None, hir::AssocItem::Function(fn_item) => Some(fn_item),
}, _ => None,
) { }
})
{
add_function_impl(&trigger, acc, ctx, &missing_fn); add_function_impl(&trigger, acc, ctx, &missing_fn);
} }
} }
SyntaxKind::TYPE_ALIAS_DEF => { SyntaxKind::TYPE_ALIAS_DEF => {
for missing_fn in get_missing_impl_items(&ctx.sema, &impl_def).iter().filter_map( for missing_fn in
|item| match item { get_missing_assoc_items(&ctx.sema, &impl_def).iter().filter_map(|item| {
hir::AssocItem::TypeAlias(type_item) => Some(type_item), match item {
_ => None, hir::AssocItem::TypeAlias(type_item) => Some(type_item),
}, _ => None,
) { }
})
{
add_type_alias_impl(&trigger, acc, ctx, &missing_fn); add_type_alias_impl(&trigger, acc, ctx, &missing_fn);
} }
} }
SyntaxKind::CONST_DEF => { SyntaxKind::CONST_DEF => {
for missing_fn in get_missing_impl_items(&ctx.sema, &impl_def).iter().filter_map( for missing_fn in
|item| match item { get_missing_assoc_items(&ctx.sema, &impl_def).iter().filter_map(|item| {
hir::AssocItem::Const(const_item) => Some(const_item), match item {
_ => None, hir::AssocItem::Const(const_item) => Some(const_item),
}, _ => None,
) { }
})
{
add_const_impl(&trigger, acc, ctx, &missing_fn); 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 // * Items(SmallVec<[P<ast::Item>; 1]>) -> token_tree_to_items
// //
// * TraitItems(SmallVec<[ast::TraitItem; 1]>) // * TraitItems(SmallVec<[ast::TraitItem; 1]>)
// * ImplItems(SmallVec<[ast::ImplItem; 1]>) // * AssocItems(SmallVec<[ast::AssocItem; 1]>)
// * ForeignItems(SmallVec<[ast::ForeignItem; 1]> // * ForeignItems(SmallVec<[ast::ForeignItem; 1]>
pub fn token_tree_to_syntax_node( pub fn token_tree_to_syntax_node(

View file

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

View file

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

View file

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