mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
HasGenericArgs syntax trait
This commit is contained in:
parent
6c8c49b01b
commit
c08d419fba
26 changed files with 83 additions and 41 deletions
|
@ -31,8 +31,8 @@ pub use self::{
|
|||
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
|
||||
token_ext::{CommentKind, CommentPlacement, CommentShape, IsString, QuoteOffsets, Radix},
|
||||
traits::{
|
||||
AttrDocCommentIter, DocCommentIter, HasArgList, HasAttrs, HasDocComments, HasGenericParams,
|
||||
HasLoopBody, HasModuleItem, HasName, HasTypeBounds, HasVisibility,
|
||||
AttrDocCommentIter, DocCommentIter, HasArgList, HasAttrs, HasDocComments, HasGenericArgs,
|
||||
HasGenericParams, HasLoopBody, HasModuleItem, HasName, HasTypeBounds, HasVisibility,
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -149,14 +149,17 @@ pub trait RangeItem {
|
|||
mod support {
|
||||
use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken};
|
||||
|
||||
#[inline]
|
||||
pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
|
||||
parent.children().find_map(N::cast)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn children<N: AstNode>(parent: &SyntaxNode) -> AstChildren<N> {
|
||||
AstChildren::new(parent)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn token(parent: &SyntaxNode, kind: SyntaxKind) -> Option<SyntaxToken> {
|
||||
parent.children_with_tokens().filter_map(|it| it.into_token()).find(|it| it.kind() == kind)
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use parser::{SyntaxKind, T};
|
|||
|
||||
use crate::{
|
||||
algo::{self, neighbor},
|
||||
ast::{self, edit::IndentLevel, make, HasGenericParams},
|
||||
ast::{self, edit::IndentLevel, make, HasGenericArgs, HasGenericParams},
|
||||
ted::{self, Position},
|
||||
AstNode, AstToken, Direction, SyntaxElement,
|
||||
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
|
||||
|
|
|
@ -78,10 +78,10 @@ impl AssocItemList {
|
|||
pub struct AssocTypeArg {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
impl ast::HasGenericArgs for AssocTypeArg {}
|
||||
impl ast::HasTypeBounds for AssocTypeArg {}
|
||||
impl AssocTypeArg {
|
||||
pub fn const_arg(&self) -> Option<ConstArg> { support::child(&self.syntax) }
|
||||
pub fn generic_arg_list(&self) -> Option<GenericArgList> { support::child(&self.syntax) }
|
||||
pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
|
||||
pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) }
|
||||
pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) }
|
||||
|
@ -783,8 +783,8 @@ pub struct MethodCallExpr {
|
|||
}
|
||||
impl ast::HasArgList for MethodCallExpr {}
|
||||
impl ast::HasAttrs for MethodCallExpr {}
|
||||
impl ast::HasGenericArgs for MethodCallExpr {}
|
||||
impl MethodCallExpr {
|
||||
pub fn generic_arg_list(&self) -> Option<GenericArgList> { support::child(&self.syntax) }
|
||||
pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
|
||||
pub fn receiver(&self) -> Option<Expr> { support::child(&self.syntax) }
|
||||
pub fn dot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![.]) }
|
||||
|
@ -946,8 +946,8 @@ impl PathPat {
|
|||
pub struct PathSegment {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
impl ast::HasGenericArgs for PathSegment {}
|
||||
impl PathSegment {
|
||||
pub fn generic_arg_list(&self) -> Option<GenericArgList> { support::child(&self.syntax) }
|
||||
pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
|
||||
pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) }
|
||||
pub fn path_type(&self) -> Option<PathType> { support::child(&self.syntax) }
|
||||
|
@ -1763,6 +1763,12 @@ pub struct AnyHasDocComments {
|
|||
}
|
||||
impl ast::HasDocComments for AnyHasDocComments {}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct AnyHasGenericArgs {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
impl ast::HasGenericArgs for AnyHasGenericArgs {}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct AnyHasGenericParams {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
|
@ -4235,6 +4241,21 @@ impl AstNode for AnyHasDocComments {
|
|||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
impl AnyHasGenericArgs {
|
||||
#[inline]
|
||||
pub fn new<T: ast::HasGenericArgs>(node: T) -> AnyHasGenericArgs {
|
||||
AnyHasGenericArgs { syntax: node.syntax().clone() }
|
||||
}
|
||||
}
|
||||
impl AstNode for AnyHasGenericArgs {
|
||||
fn can_cast(kind: SyntaxKind) -> bool {
|
||||
matches!(kind, ASSOC_TYPE_ARG | METHOD_CALL_EXPR | PATH_SEGMENT)
|
||||
}
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
Self::can_cast(syntax.kind()).then_some(AnyHasGenericArgs { syntax })
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
impl AnyHasGenericParams {
|
||||
#[inline]
|
||||
pub fn new<T: ast::HasGenericParams>(node: T) -> AnyHasGenericParams {
|
||||
|
|
|
@ -10,7 +10,10 @@ use parser::SyntaxKind;
|
|||
use rowan::{GreenNodeData, GreenTokenData};
|
||||
|
||||
use crate::{
|
||||
ast::{self, support, AstNode, AstToken, HasAttrs, HasGenericParams, HasName, SyntaxNode},
|
||||
ast::{
|
||||
self, support, AstNode, AstToken, HasAttrs, HasGenericArgs, HasGenericParams, HasName,
|
||||
SyntaxNode,
|
||||
},
|
||||
ted, NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
|
||||
};
|
||||
|
||||
|
|
|
@ -52,6 +52,11 @@ pub trait HasGenericParams: AstNode {
|
|||
support::child(self.syntax())
|
||||
}
|
||||
}
|
||||
pub trait HasGenericArgs: AstNode {
|
||||
fn generic_arg_list(&self) -> Option<ast::GenericArgList> {
|
||||
support::child(self.syntax())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HasTypeBounds: AstNode {
|
||||
fn type_bound_list(&self) -> Option<ast::TypeBoundList> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue