Skip to content

Commit

Permalink
Merge #4321
Browse files Browse the repository at this point in the history
4321: Rename ImplItem to AssocItem r=matklad a=edwin0cheng

Related discussion: #4283 (comment)

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
  • Loading branch information
bors[bot] and edwin0cheng authored May 5, 2020
2 parents 756e917 + 9266535 commit 58f6431
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 77 deletions.
26 changes: 13 additions & 13 deletions crates/ra_assists/src/handlers/add_missing_impl_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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(),
},
Expand All @@ -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)
Expand All @@ -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()
};

Expand Down
4 changes: 2 additions & 2 deletions crates/ra_assists/src/handlers/add_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions crates/ra_assists/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_hir/src/code_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions crates/ra_hir_def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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,
));
Expand Down Expand Up @@ -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),
);
Expand Down Expand Up @@ -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,
);
Expand All @@ -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;
Expand All @@ -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)) }
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_hir_ty/src/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_ide/src/completion/complete_qualified_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
46 changes: 26 additions & 20 deletions crates/ra_ide/src/completion/complete_trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_mbe/src/syntax_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions crates/ra_syntax/src/ast/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()),
Expand Down
Loading

0 comments on commit 58f6431

Please sign in to comment.