mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-08 05:08:40 +00:00
Merge #7587
7587: AdtDef -> Adt r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
336909b63a
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -1816,9 +1816,9 @@ checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ungrammar"
|
name = "ungrammar"
|
||||||
version = "1.10.0"
|
version = "1.11.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ce24866975a8858d3a35eba845efc6b42962c5067afd2bc1a07b9ce0108d335c"
|
checksum = "84c629795d377049f2a1dc5f42cf505dc5ba8b28a5df0a03f4183a24480e4a6a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicase"
|
name = "unicase"
|
||||||
|
@ -26,7 +26,7 @@ pub(crate) fn add_lifetime_to_type(acc: &mut Assists, ctx: &AssistContext) -> Op
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let node = ctx.find_node_at_offset::<ast::AdtDef>()?;
|
let node = ctx.find_node_at_offset::<ast::Adt>()?;
|
||||||
let has_lifetime = node
|
let has_lifetime = node
|
||||||
.generic_param_list()
|
.generic_param_list()
|
||||||
.map(|gen_list| gen_list.lifetime_params().count() > 0)
|
.map(|gen_list| gen_list.lifetime_params().count() > 0)
|
||||||
@ -66,9 +66,9 @@ pub(crate) fn add_lifetime_to_type(acc: &mut Assists, ctx: &AssistContext) -> Op
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetch_borrowed_types(node: &ast::AdtDef) -> Option<Vec<RefType>> {
|
fn fetch_borrowed_types(node: &ast::Adt) -> Option<Vec<RefType>> {
|
||||||
let ref_types: Vec<RefType> = match node {
|
let ref_types: Vec<RefType> = match node {
|
||||||
ast::AdtDef::Enum(enum_) => {
|
ast::Adt::Enum(enum_) => {
|
||||||
let variant_list = enum_.variant_list()?;
|
let variant_list = enum_.variant_list()?;
|
||||||
variant_list
|
variant_list
|
||||||
.variants()
|
.variants()
|
||||||
@ -80,11 +80,11 @@ fn fetch_borrowed_types(node: &ast::AdtDef) -> Option<Vec<RefType>> {
|
|||||||
.flatten()
|
.flatten()
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
ast::AdtDef::Struct(strukt) => {
|
ast::Adt::Struct(strukt) => {
|
||||||
let field_list = strukt.field_list()?;
|
let field_list = strukt.field_list()?;
|
||||||
find_ref_types_from_field_list(&field_list)?
|
find_ref_types_from_field_list(&field_list)?
|
||||||
}
|
}
|
||||||
ast::AdtDef::Union(un) => {
|
ast::Adt::Union(un) => {
|
||||||
let record_field_list = un.record_field_list()?;
|
let record_field_list = un.record_field_list()?;
|
||||||
record_field_list
|
record_field_list
|
||||||
.fields()
|
.fields()
|
||||||
|
@ -26,7 +26,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|||||||
// ```
|
// ```
|
||||||
pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||||
let cap = ctx.config.snippet_cap?;
|
let cap = ctx.config.snippet_cap?;
|
||||||
let nominal = ctx.find_node_at_offset::<ast::AdtDef>()?;
|
let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
|
||||||
let node_start = derive_insertion_offset(&nominal)?;
|
let node_start = derive_insertion_offset(&nominal)?;
|
||||||
let target = nominal.syntax().text_range();
|
let target = nominal.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
@ -58,7 +58,7 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Insert `derive` after doc comments.
|
// Insert `derive` after doc comments.
|
||||||
fn derive_insertion_offset(nominal: &ast::AdtDef) -> Option<TextSize> {
|
fn derive_insertion_offset(nominal: &ast::Adt) -> Option<TextSize> {
|
||||||
let non_ws_child = nominal
|
let non_ws_child = nominal
|
||||||
.syntax()
|
.syntax()
|
||||||
.children_with_tokens()
|
.children_with_tokens()
|
||||||
|
@ -49,7 +49,7 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext)
|
|||||||
// Return early if we've found an existing new fn
|
// Return early if we've found an existing new fn
|
||||||
let impl_def = find_struct_impl(
|
let impl_def = find_struct_impl(
|
||||||
&ctx,
|
&ctx,
|
||||||
&ast::AdtDef::Enum(parent_enum.clone()),
|
&ast::Adt::Enum(parent_enum.clone()),
|
||||||
format!("is_{}", fn_name).as_str(),
|
format!("is_{}", fn_name).as_str(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||||
let nominal = ctx.find_node_at_offset::<ast::AdtDef>()?;
|
let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
|
||||||
let name = nominal.name()?;
|
let name = nominal.name()?;
|
||||||
let target = nominal.syntax().text_range();
|
let target = nominal.syntax().text_range();
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Return early if we've found an existing new fn
|
// Return early if we've found an existing new fn
|
||||||
let impl_def = find_struct_impl(&ctx, &ast::AdtDef::Struct(strukt.clone()), "new")?;
|
let impl_def = find_struct_impl(&ctx, &ast::Adt::Struct(strukt.clone()), "new")?;
|
||||||
|
|
||||||
let target = strukt.syntax().text_range();
|
let target = strukt.syntax().text_range();
|
||||||
acc.add(AssistId("generate_new", AssistKind::Generate), "Generate `new`", target, |builder| {
|
acc.add(AssistId("generate_new", AssistKind::Generate), "Generate `new`", target, |builder| {
|
||||||
|
@ -281,7 +281,7 @@ pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
|
|||||||
// FIXME: this partially overlaps with `find_impl_block`
|
// FIXME: this partially overlaps with `find_impl_block`
|
||||||
pub(crate) fn find_struct_impl(
|
pub(crate) fn find_struct_impl(
|
||||||
ctx: &AssistContext,
|
ctx: &AssistContext,
|
||||||
strukt: &ast::AdtDef,
|
strukt: &ast::Adt,
|
||||||
name: &str,
|
name: &str,
|
||||||
) -> Option<Option<ast::Impl>> {
|
) -> Option<Option<ast::Impl>> {
|
||||||
let db = ctx.db();
|
let db = ctx.db();
|
||||||
@ -290,9 +290,9 @@ pub(crate) fn find_struct_impl(
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
let struct_def = match strukt {
|
let struct_def = match strukt {
|
||||||
ast::AdtDef::Enum(e) => Adt::Enum(ctx.sema.to_def(e)?),
|
ast::Adt::Enum(e) => Adt::Enum(ctx.sema.to_def(e)?),
|
||||||
ast::AdtDef::Struct(s) => Adt::Struct(ctx.sema.to_def(s)?),
|
ast::Adt::Struct(s) => Adt::Struct(ctx.sema.to_def(s)?),
|
||||||
ast::AdtDef::Union(u) => Adt::Union(ctx.sema.to_def(u)?),
|
ast::Adt::Union(u) => Adt::Union(ctx.sema.to_def(u)?),
|
||||||
};
|
};
|
||||||
|
|
||||||
let block = module.descendants().filter_map(ast::Impl::cast).find_map(|impl_blk| {
|
let block = module.descendants().filter_map(ast::Impl::cast).find_map(|impl_blk| {
|
||||||
|
@ -23,7 +23,7 @@ pub(crate) fn goto_implementation(
|
|||||||
|
|
||||||
let krate = sema.to_module_def(position.file_id)?.krate();
|
let krate = sema.to_module_def(position.file_id)?.krate();
|
||||||
|
|
||||||
if let Some(nominal_def) = find_node_at_offset::<ast::AdtDef>(&syntax, position.offset) {
|
if let Some(nominal_def) = find_node_at_offset::<ast::Adt>(&syntax, position.offset) {
|
||||||
return Some(RangeInfo::new(
|
return Some(RangeInfo::new(
|
||||||
nominal_def.syntax().text_range(),
|
nominal_def.syntax().text_range(),
|
||||||
impls_for_def(&sema, &nominal_def, krate)?,
|
impls_for_def(&sema, &nominal_def, krate)?,
|
||||||
@ -40,13 +40,13 @@ pub(crate) fn goto_implementation(
|
|||||||
|
|
||||||
fn impls_for_def(
|
fn impls_for_def(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
node: &ast::AdtDef,
|
node: &ast::Adt,
|
||||||
krate: Crate,
|
krate: Crate,
|
||||||
) -> Option<Vec<NavigationTarget>> {
|
) -> Option<Vec<NavigationTarget>> {
|
||||||
let ty = match node {
|
let ty = match node {
|
||||||
ast::AdtDef::Struct(def) => sema.to_def(def)?.ty(sema.db),
|
ast::Adt::Struct(def) => sema.to_def(def)?.ty(sema.db),
|
||||||
ast::AdtDef::Enum(def) => sema.to_def(def)?.ty(sema.db),
|
ast::Adt::Enum(def) => sema.to_def(def)?.ty(sema.db),
|
||||||
ast::AdtDef::Union(def) => sema.to_def(def)?.ty(sema.db),
|
ast::Adt::Union(def) => sema.to_def(def)?.ty(sema.db),
|
||||||
};
|
};
|
||||||
|
|
||||||
let impls = Impl::all_in_crate(sema.db, krate);
|
let impls = Impl::all_in_crate(sema.db, krate);
|
||||||
|
@ -1401,15 +1401,15 @@ pub enum FieldList {
|
|||||||
TupleFieldList(TupleFieldList),
|
TupleFieldList(TupleFieldList),
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum AdtDef {
|
pub enum Adt {
|
||||||
Enum(Enum),
|
Enum(Enum),
|
||||||
Struct(Struct),
|
Struct(Struct),
|
||||||
Union(Union),
|
Union(Union),
|
||||||
}
|
}
|
||||||
impl ast::AttrsOwner for AdtDef {}
|
impl ast::AttrsOwner for Adt {}
|
||||||
impl ast::GenericParamsOwner for AdtDef {}
|
impl ast::GenericParamsOwner for Adt {}
|
||||||
impl ast::NameOwner for AdtDef {}
|
impl ast::NameOwner for Adt {}
|
||||||
impl ast::VisibilityOwner for AdtDef {}
|
impl ast::VisibilityOwner for Adt {}
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum AssocItem {
|
pub enum AssocItem {
|
||||||
Const(Const),
|
Const(Const),
|
||||||
@ -3394,16 +3394,16 @@ impl AstNode for FieldList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<Enum> for AdtDef {
|
impl From<Enum> for Adt {
|
||||||
fn from(node: Enum) -> AdtDef { AdtDef::Enum(node) }
|
fn from(node: Enum) -> Adt { Adt::Enum(node) }
|
||||||
}
|
}
|
||||||
impl From<Struct> for AdtDef {
|
impl From<Struct> for Adt {
|
||||||
fn from(node: Struct) -> AdtDef { AdtDef::Struct(node) }
|
fn from(node: Struct) -> Adt { Adt::Struct(node) }
|
||||||
}
|
}
|
||||||
impl From<Union> for AdtDef {
|
impl From<Union> for Adt {
|
||||||
fn from(node: Union) -> AdtDef { AdtDef::Union(node) }
|
fn from(node: Union) -> Adt { Adt::Union(node) }
|
||||||
}
|
}
|
||||||
impl AstNode for AdtDef {
|
impl AstNode for Adt {
|
||||||
fn can_cast(kind: SyntaxKind) -> bool {
|
fn can_cast(kind: SyntaxKind) -> bool {
|
||||||
match kind {
|
match kind {
|
||||||
ENUM | STRUCT | UNION => true,
|
ENUM | STRUCT | UNION => true,
|
||||||
@ -3412,18 +3412,18 @@ impl AstNode for AdtDef {
|
|||||||
}
|
}
|
||||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||||
let res = match syntax.kind() {
|
let res = match syntax.kind() {
|
||||||
ENUM => AdtDef::Enum(Enum { syntax }),
|
ENUM => Adt::Enum(Enum { syntax }),
|
||||||
STRUCT => AdtDef::Struct(Struct { syntax }),
|
STRUCT => Adt::Struct(Struct { syntax }),
|
||||||
UNION => AdtDef::Union(Union { syntax }),
|
UNION => Adt::Union(Union { syntax }),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
Some(res)
|
Some(res)
|
||||||
}
|
}
|
||||||
fn syntax(&self) -> &SyntaxNode {
|
fn syntax(&self) -> &SyntaxNode {
|
||||||
match self {
|
match self {
|
||||||
AdtDef::Enum(it) => &it.syntax,
|
Adt::Enum(it) => &it.syntax,
|
||||||
AdtDef::Struct(it) => &it.syntax,
|
Adt::Struct(it) => &it.syntax,
|
||||||
AdtDef::Union(it) => &it.syntax,
|
Adt::Union(it) => &it.syntax,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3571,7 +3571,7 @@ impl std::fmt::Display for FieldList {
|
|||||||
std::fmt::Display::fmt(self.syntax(), f)
|
std::fmt::Display::fmt(self.syntax(), f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl std::fmt::Display for AdtDef {
|
impl std::fmt::Display for Adt {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user