mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-27 10:17:15 +00:00
Merge Trait and TraitAlias handling
This commit is contained in:
parent
a9450ebba3
commit
82f174fbd9
74 changed files with 68 additions and 577 deletions
|
|
@ -26,8 +26,7 @@ pub use self::{
|
|||
generated::{nodes::*, tokens::*},
|
||||
node_ext::{
|
||||
AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind,
|
||||
SlicePatComponents, StructKind, TraitOrAlias, TypeBoundKind, TypeOrConstParam,
|
||||
VisibilityKind,
|
||||
SlicePatComponents, StructKind, TypeBoundKind, TypeOrConstParam, VisibilityKind,
|
||||
},
|
||||
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
|
||||
token_ext::{CommentKind, CommentPlacement, CommentShape, IsString, QuoteOffsets, Radix},
|
||||
|
|
|
|||
|
|
@ -99,38 +99,10 @@ impl GenericParamsOwnerEdit for ast::Trait {
|
|||
|
||||
fn get_or_create_where_clause(&self) -> ast::WhereClause {
|
||||
if self.where_clause().is_none() {
|
||||
let position = match self.assoc_item_list() {
|
||||
Some(items) => Position::before(items.syntax()),
|
||||
None => Position::last_child_of(self.syntax()),
|
||||
};
|
||||
create_where_clause(position);
|
||||
}
|
||||
self.where_clause().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl GenericParamsOwnerEdit for ast::TraitAlias {
|
||||
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
||||
match self.generic_param_list() {
|
||||
Some(it) => it,
|
||||
None => {
|
||||
let position = if let Some(name) = self.name() {
|
||||
Position::after(name.syntax)
|
||||
} else if let Some(trait_token) = self.trait_token() {
|
||||
Position::after(trait_token)
|
||||
} else {
|
||||
Position::last_child_of(self.syntax())
|
||||
};
|
||||
create_generic_param_list(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_or_create_where_clause(&self) -> ast::WhereClause {
|
||||
if self.where_clause().is_none() {
|
||||
let position = match self.semicolon_token() {
|
||||
Some(tok) => Position::before(tok),
|
||||
None => Position::last_child_of(self.syntax()),
|
||||
let position = match (self.assoc_item_list(), self.semicolon_token()) {
|
||||
(Some(items), _) => Position::before(items.syntax()),
|
||||
(_, Some(tok)) => Position::before(tok),
|
||||
(None, None) => Position::last_child_of(self.syntax()),
|
||||
};
|
||||
create_where_clause(position);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1614,30 +1614,16 @@ impl Trait {
|
|||
#[inline]
|
||||
pub fn assoc_item_list(&self) -> Option<AssocItemList> { support::child(&self.syntax) }
|
||||
#[inline]
|
||||
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
|
||||
#[inline]
|
||||
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
|
||||
#[inline]
|
||||
pub fn auto_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![auto]) }
|
||||
#[inline]
|
||||
pub fn trait_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![trait]) }
|
||||
#[inline]
|
||||
pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
|
||||
}
|
||||
pub struct TraitAlias {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
impl ast::HasAttrs for TraitAlias {}
|
||||
impl ast::HasDocComments for TraitAlias {}
|
||||
impl ast::HasGenericParams for TraitAlias {}
|
||||
impl ast::HasName for TraitAlias {}
|
||||
impl ast::HasVisibility for TraitAlias {}
|
||||
impl TraitAlias {
|
||||
#[inline]
|
||||
pub fn type_bound_list(&self) -> Option<TypeBoundList> { support::child(&self.syntax) }
|
||||
#[inline]
|
||||
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
|
||||
#[inline]
|
||||
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
|
||||
#[inline]
|
||||
pub fn trait_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![trait]) }
|
||||
}
|
||||
pub struct TryExpr {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
|
|
@ -2107,7 +2093,6 @@ pub enum Item {
|
|||
Static(Static),
|
||||
Struct(Struct),
|
||||
Trait(Trait),
|
||||
TraitAlias(TraitAlias),
|
||||
TypeAlias(TypeAlias),
|
||||
Union(Union),
|
||||
Use(Use),
|
||||
|
|
@ -6801,42 +6786,6 @@ impl fmt::Debug for Trait {
|
|||
f.debug_struct("Trait").field("syntax", &self.syntax).finish()
|
||||
}
|
||||
}
|
||||
impl AstNode for TraitAlias {
|
||||
#[inline]
|
||||
fn kind() -> SyntaxKind
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
TRAIT_ALIAS
|
||||
}
|
||||
#[inline]
|
||||
fn can_cast(kind: SyntaxKind) -> bool { kind == TRAIT_ALIAS }
|
||||
#[inline]
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
if Self::can_cast(syntax.kind()) {
|
||||
Some(Self { syntax })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
impl hash::Hash for TraitAlias {
|
||||
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
|
||||
}
|
||||
impl Eq for TraitAlias {}
|
||||
impl PartialEq for TraitAlias {
|
||||
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
|
||||
}
|
||||
impl Clone for TraitAlias {
|
||||
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
|
||||
}
|
||||
impl fmt::Debug for TraitAlias {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TraitAlias").field("syntax", &self.syntax).finish()
|
||||
}
|
||||
}
|
||||
impl AstNode for TryExpr {
|
||||
#[inline]
|
||||
fn kind() -> SyntaxKind
|
||||
|
|
@ -8471,10 +8420,6 @@ impl From<Trait> for Item {
|
|||
#[inline]
|
||||
fn from(node: Trait) -> Item { Item::Trait(node) }
|
||||
}
|
||||
impl From<TraitAlias> for Item {
|
||||
#[inline]
|
||||
fn from(node: TraitAlias) -> Item { Item::TraitAlias(node) }
|
||||
}
|
||||
impl From<TypeAlias> for Item {
|
||||
#[inline]
|
||||
fn from(node: TypeAlias) -> Item { Item::TypeAlias(node) }
|
||||
|
|
@ -8506,7 +8451,6 @@ impl AstNode for Item {
|
|||
| STATIC
|
||||
| STRUCT
|
||||
| TRAIT
|
||||
| TRAIT_ALIAS
|
||||
| TYPE_ALIAS
|
||||
| UNION
|
||||
| USE
|
||||
|
|
@ -8529,7 +8473,6 @@ impl AstNode for Item {
|
|||
STATIC => Item::Static(Static { syntax }),
|
||||
STRUCT => Item::Struct(Struct { syntax }),
|
||||
TRAIT => Item::Trait(Trait { syntax }),
|
||||
TRAIT_ALIAS => Item::TraitAlias(TraitAlias { syntax }),
|
||||
TYPE_ALIAS => Item::TypeAlias(TypeAlias { syntax }),
|
||||
UNION => Item::Union(Union { syntax }),
|
||||
USE => Item::Use(Use { syntax }),
|
||||
|
|
@ -8554,7 +8497,6 @@ impl AstNode for Item {
|
|||
Item::Static(it) => &it.syntax,
|
||||
Item::Struct(it) => &it.syntax,
|
||||
Item::Trait(it) => &it.syntax,
|
||||
Item::TraitAlias(it) => &it.syntax,
|
||||
Item::TypeAlias(it) => &it.syntax,
|
||||
Item::Union(it) => &it.syntax,
|
||||
Item::Use(it) => &it.syntax,
|
||||
|
|
@ -8984,7 +8926,6 @@ impl AstNode for AnyHasAttrs {
|
|||
| STMT_LIST
|
||||
| STRUCT
|
||||
| TRAIT
|
||||
| TRAIT_ALIAS
|
||||
| TRY_EXPR
|
||||
| TUPLE_EXPR
|
||||
| TUPLE_FIELD
|
||||
|
|
@ -9257,10 +9198,6 @@ impl From<Trait> for AnyHasAttrs {
|
|||
#[inline]
|
||||
fn from(node: Trait) -> AnyHasAttrs { AnyHasAttrs { syntax: node.syntax } }
|
||||
}
|
||||
impl From<TraitAlias> for AnyHasAttrs {
|
||||
#[inline]
|
||||
fn from(node: TraitAlias) -> AnyHasAttrs { AnyHasAttrs { syntax: node.syntax } }
|
||||
}
|
||||
impl From<TryExpr> for AnyHasAttrs {
|
||||
#[inline]
|
||||
fn from(node: TryExpr) -> AnyHasAttrs { AnyHasAttrs { syntax: node.syntax } }
|
||||
|
|
@ -9330,7 +9267,6 @@ impl AstNode for AnyHasDocComments {
|
|||
| STATIC
|
||||
| STRUCT
|
||||
| TRAIT
|
||||
| TRAIT_ALIAS
|
||||
| TUPLE_FIELD
|
||||
| TYPE_ALIAS
|
||||
| UNION
|
||||
|
|
@ -9420,10 +9356,6 @@ impl From<Trait> for AnyHasDocComments {
|
|||
#[inline]
|
||||
fn from(node: Trait) -> AnyHasDocComments { AnyHasDocComments { syntax: node.syntax } }
|
||||
}
|
||||
impl From<TraitAlias> for AnyHasDocComments {
|
||||
#[inline]
|
||||
fn from(node: TraitAlias) -> AnyHasDocComments { AnyHasDocComments { syntax: node.syntax } }
|
||||
}
|
||||
impl From<TupleField> for AnyHasDocComments {
|
||||
#[inline]
|
||||
fn from(node: TupleField) -> AnyHasDocComments { AnyHasDocComments { syntax: node.syntax } }
|
||||
|
|
@ -9488,7 +9420,7 @@ impl ast::HasGenericParams for AnyHasGenericParams {}
|
|||
impl AstNode for AnyHasGenericParams {
|
||||
#[inline]
|
||||
fn can_cast(kind: SyntaxKind) -> bool {
|
||||
matches!(kind, CONST | ENUM | FN | IMPL | STRUCT | TRAIT | TRAIT_ALIAS | TYPE_ALIAS | UNION)
|
||||
matches!(kind, CONST | ENUM | FN | IMPL | STRUCT | TRAIT | TYPE_ALIAS | UNION)
|
||||
}
|
||||
#[inline]
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
|
|
@ -9536,10 +9468,6 @@ impl From<Trait> for AnyHasGenericParams {
|
|||
#[inline]
|
||||
fn from(node: Trait) -> AnyHasGenericParams { AnyHasGenericParams { syntax: node.syntax } }
|
||||
}
|
||||
impl From<TraitAlias> for AnyHasGenericParams {
|
||||
#[inline]
|
||||
fn from(node: TraitAlias) -> AnyHasGenericParams { AnyHasGenericParams { syntax: node.syntax } }
|
||||
}
|
||||
impl From<TypeAlias> for AnyHasGenericParams {
|
||||
#[inline]
|
||||
fn from(node: TypeAlias) -> AnyHasGenericParams { AnyHasGenericParams { syntax: node.syntax } }
|
||||
|
|
@ -9646,7 +9574,6 @@ impl AstNode for AnyHasName {
|
|||
| STATIC
|
||||
| STRUCT
|
||||
| TRAIT
|
||||
| TRAIT_ALIAS
|
||||
| TYPE_ALIAS
|
||||
| TYPE_PARAM
|
||||
| UNION
|
||||
|
|
@ -9739,10 +9666,6 @@ impl From<Trait> for AnyHasName {
|
|||
#[inline]
|
||||
fn from(node: Trait) -> AnyHasName { AnyHasName { syntax: node.syntax } }
|
||||
}
|
||||
impl From<TraitAlias> for AnyHasName {
|
||||
#[inline]
|
||||
fn from(node: TraitAlias) -> AnyHasName { AnyHasName { syntax: node.syntax } }
|
||||
}
|
||||
impl From<TypeAlias> for AnyHasName {
|
||||
#[inline]
|
||||
fn from(node: TypeAlias) -> AnyHasName { AnyHasName { syntax: node.syntax } }
|
||||
|
|
@ -9832,7 +9755,6 @@ impl AstNode for AnyHasVisibility {
|
|||
| STATIC
|
||||
| STRUCT
|
||||
| TRAIT
|
||||
| TRAIT_ALIAS
|
||||
| TUPLE_FIELD
|
||||
| TYPE_ALIAS
|
||||
| UNION
|
||||
|
|
@ -9910,10 +9832,6 @@ impl From<Trait> for AnyHasVisibility {
|
|||
#[inline]
|
||||
fn from(node: Trait) -> AnyHasVisibility { AnyHasVisibility { syntax: node.syntax } }
|
||||
}
|
||||
impl From<TraitAlias> for AnyHasVisibility {
|
||||
#[inline]
|
||||
fn from(node: TraitAlias) -> AnyHasVisibility { AnyHasVisibility { syntax: node.syntax } }
|
||||
}
|
||||
impl From<TupleField> for AnyHasVisibility {
|
||||
#[inline]
|
||||
fn from(node: TupleField) -> AnyHasVisibility { AnyHasVisibility { syntax: node.syntax } }
|
||||
|
|
@ -10639,11 +10557,6 @@ impl std::fmt::Display for Trait {
|
|||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
}
|
||||
}
|
||||
impl std::fmt::Display for TraitAlias {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
}
|
||||
}
|
||||
impl std::fmt::Display for TryExpr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
|
|
|
|||
|
|
@ -880,51 +880,6 @@ impl AstNode for TypeOrConstParam {
|
|||
|
||||
impl HasAttrs for TypeOrConstParam {}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum TraitOrAlias {
|
||||
Trait(ast::Trait),
|
||||
TraitAlias(ast::TraitAlias),
|
||||
}
|
||||
|
||||
impl TraitOrAlias {
|
||||
pub fn name(&self) -> Option<ast::Name> {
|
||||
match self {
|
||||
TraitOrAlias::Trait(x) => x.name(),
|
||||
TraitOrAlias::TraitAlias(x) => x.name(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AstNode for TraitOrAlias {
|
||||
fn can_cast(kind: SyntaxKind) -> bool
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
matches!(kind, SyntaxKind::TRAIT | SyntaxKind::TRAIT_ALIAS)
|
||||
}
|
||||
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let res = match syntax.kind() {
|
||||
SyntaxKind::TRAIT => TraitOrAlias::Trait(ast::Trait { syntax }),
|
||||
SyntaxKind::TRAIT_ALIAS => TraitOrAlias::TraitAlias(ast::TraitAlias { syntax }),
|
||||
_ => return None,
|
||||
};
|
||||
Some(res)
|
||||
}
|
||||
|
||||
fn syntax(&self) -> &SyntaxNode {
|
||||
match self {
|
||||
TraitOrAlias::Trait(it) => it.syntax(),
|
||||
TraitOrAlias::TraitAlias(it) => it.syntax(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HasAttrs for TraitOrAlias {}
|
||||
|
||||
pub enum VisibilityKind {
|
||||
In(ast::Path),
|
||||
PubCrate,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue