mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 06:03:58 +00:00
Auto merge of #17555 - Veykril:grammar-inline, r=Veykril
internal: Inline generated syntax methods
This commit is contained in:
commit
a5b21ea0aa
26 changed files with 1115 additions and 41 deletions
|
@ -15,8 +15,8 @@ use span::AstIdMap;
|
|||
use stdx::never;
|
||||
use syntax::{
|
||||
ast::{
|
||||
self, ArrayExprKind, AstChildren, BlockExpr, HasArgList, HasAttrs, HasLoopBody, HasName,
|
||||
RangeItem, SlicePatComponents,
|
||||
self, ArrayExprKind, AstChildren, BlockExpr, HasArgList, HasAttrs, HasGenericArgs,
|
||||
HasLoopBody, HasName, RangeItem, SlicePatComponents,
|
||||
},
|
||||
AstNode, AstPtr, AstToken as _, SyntaxNodePtr,
|
||||
};
|
||||
|
|
|
@ -10,7 +10,7 @@ use hir_expand::{
|
|||
AstId,
|
||||
};
|
||||
use intern::Interned;
|
||||
use syntax::ast::{self, HasName, IsString};
|
||||
use syntax::ast::{self, HasGenericArgs, HasName, IsString};
|
||||
|
||||
use crate::{
|
||||
builtin_type::{BuiltinInt, BuiltinType, BuiltinUint},
|
||||
|
|
|
@ -9,7 +9,7 @@ use hir_expand::{
|
|||
name::{name, AsName},
|
||||
};
|
||||
use intern::Interned;
|
||||
use syntax::ast::{self, AstNode, HasTypeBounds};
|
||||
use syntax::ast::{self, AstNode, HasGenericArgs, HasTypeBounds};
|
||||
|
||||
use crate::{
|
||||
path::{AssociatedTypeBinding, GenericArg, GenericArgs, ModPath, Path, PathKind},
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use either::Either;
|
||||
use ide_db::defs::{Definition, NameRefClass};
|
||||
use syntax::{
|
||||
ast::{self, make, HasArgList},
|
||||
ast::{self, make, HasArgList, HasGenericArgs},
|
||||
ted, AstNode,
|
||||
};
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use ide_db::{famous_defs::FamousDefs, traits::resolve_target_trait};
|
||||
use itertools::Itertools;
|
||||
use syntax::{
|
||||
ast::{self, make, AstNode, HasName},
|
||||
ast::{self, make, AstNode, HasGenericArgs, HasName},
|
||||
ted,
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use hir::ImportPathConfig;
|
||||
use ide_db::{famous_defs::FamousDefs, helpers::mod_path_to_ast, traits::resolve_target_trait};
|
||||
use syntax::ast::{self, AstNode, HasName};
|
||||
use syntax::ast::{self, AstNode, HasGenericArgs, HasName};
|
||||
|
||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use either::Either;
|
||||
use ide_db::syntax_helpers::node_ext::walk_ty;
|
||||
use syntax::{
|
||||
ast::{self, edit::IndentLevel, make, AstNode, HasGenericParams, HasName},
|
||||
ast::{self, edit::IndentLevel, make, AstNode, HasGenericArgs, HasGenericParams, HasName},
|
||||
ted,
|
||||
};
|
||||
|
||||
|
|
|
@ -17,8 +17,9 @@ use syntax::{
|
|||
self,
|
||||
edit::{self, AstNodeEdit},
|
||||
edit_in_place::AttrsOwnerEdit,
|
||||
make, AssocItem, GenericArgList, GenericParamList, HasAttrs, HasGenericParams, HasName,
|
||||
HasTypeBounds, HasVisibility as astHasVisibility, Path, WherePred,
|
||||
make, AssocItem, GenericArgList, GenericParamList, HasAttrs, HasGenericArgs,
|
||||
HasGenericParams, HasName, HasTypeBounds, HasVisibility as astHasVisibility, Path,
|
||||
WherePred,
|
||||
},
|
||||
ted::{self, Position},
|
||||
AstNode, NodeOrToken, SmolStr, SyntaxKind,
|
||||
|
|
|
@ -4,7 +4,7 @@ use itertools::Itertools;
|
|||
use stdx::{format_to, to_lower_snake_case};
|
||||
use syntax::{
|
||||
algo::skip_whitespace_token,
|
||||
ast::{self, edit::IndentLevel, HasDocComments, HasName},
|
||||
ast::{self, edit::IndentLevel, HasDocComments, HasGenericArgs, HasName},
|
||||
match_ast, AstNode, AstToken,
|
||||
};
|
||||
|
||||
|
|
|
@ -15,7 +15,9 @@ use ide_db::{
|
|||
};
|
||||
use itertools::{izip, Itertools};
|
||||
use syntax::{
|
||||
ast::{self, edit::IndentLevel, edit_in_place::Indent, HasArgList, Pat, PathExpr},
|
||||
ast::{
|
||||
self, edit::IndentLevel, edit_in_place::Indent, HasArgList, HasGenericArgs, Pat, PathExpr,
|
||||
},
|
||||
ted, AstNode, NodeOrToken, SyntaxKind,
|
||||
};
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ use ide_db::{
|
|||
helpers::mod_path_to_ast,
|
||||
imports::import_assets::{ImportCandidate, LocatedImport},
|
||||
};
|
||||
use syntax::ast::HasGenericArgs;
|
||||
use syntax::{
|
||||
ast,
|
||||
ast::{make, HasArgList},
|
||||
|
|
|
@ -4,7 +4,7 @@ use ide_db::{
|
|||
imports::insert_use::{insert_use, ImportScope},
|
||||
};
|
||||
use syntax::{
|
||||
ast::{self, make},
|
||||
ast::{self, make, HasGenericArgs},
|
||||
match_ast, ted, AstNode, SyntaxNode,
|
||||
};
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use hir::HirDisplay;
|
||||
use syntax::{
|
||||
ast::{Expr, GenericArg, GenericArgList},
|
||||
ast::{LetStmt, Type::InferType},
|
||||
ast::{Expr, GenericArg, GenericArgList, HasGenericArgs, LetStmt, Type::InferType},
|
||||
AstNode, TextRange,
|
||||
};
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ use ide_db::{
|
|||
famous_defs::FamousDefs,
|
||||
};
|
||||
use syntax::{
|
||||
ast::{self, HasVisibility},
|
||||
ast::{self, HasGenericArgs, HasVisibility},
|
||||
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, SyntaxToken, TextRange,
|
||||
};
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ use ide_db::{
|
|||
};
|
||||
use itertools::Itertools;
|
||||
use syntax::{
|
||||
ast::{self, Expr},
|
||||
ast::{self, Expr, HasGenericArgs},
|
||||
match_ast, AstNode, NodeOrToken, SyntaxKind, TextRange,
|
||||
};
|
||||
|
||||
|
|
|
@ -6,7 +6,10 @@ use ide_db::{active_parameter::ActiveParameter, RootDatabase};
|
|||
use itertools::Either;
|
||||
use syntax::{
|
||||
algo::{ancestors_at_offset, find_node_at_offset, non_trivia_sibling},
|
||||
ast::{self, AttrKind, HasArgList, HasGenericParams, HasLoopBody, HasName, NameOrNameRef},
|
||||
ast::{
|
||||
self, AttrKind, HasArgList, HasGenericArgs, HasGenericParams, HasLoopBody, HasName,
|
||||
NameOrNameRef,
|
||||
},
|
||||
match_ast, AstNode, AstToken, Direction, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode,
|
||||
SyntaxToken, TextRange, TextSize, T,
|
||||
};
|
||||
|
|
|
@ -6,7 +6,7 @@ use hir::{AsAssocItem, HirDisplay, ImportPathConfig, ModuleDef, SemanticsScope};
|
|||
use itertools::Itertools;
|
||||
use rustc_hash::FxHashMap;
|
||||
use syntax::{
|
||||
ast::{self, make, AstNode},
|
||||
ast::{self, make, AstNode, HasGenericArgs},
|
||||
ted, SyntaxNode,
|
||||
};
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ use hir::{ImportPathConfig, Semantics};
|
|||
use ide_db::{base_db::FileRange, FxHashMap};
|
||||
use std::{cell::Cell, iter::Peekable};
|
||||
use syntax::{
|
||||
ast::{self, AstNode, AstToken},
|
||||
ast::{self, AstNode, AstToken, HasGenericArgs},
|
||||
SmolStr, SyntaxElement, SyntaxElementChildren, SyntaxKind, SyntaxNode, SyntaxToken,
|
||||
};
|
||||
|
||||
|
|
|
@ -3,7 +3,10 @@
|
|||
use hir::AsAssocItem;
|
||||
use ide_db::{base_db::FilePosition, FxHashMap};
|
||||
use parsing::Placeholder;
|
||||
use syntax::{ast, SmolStr, SyntaxKind, SyntaxNode, SyntaxToken};
|
||||
use syntax::{
|
||||
ast::{self, HasGenericArgs},
|
||||
SmolStr, SyntaxKind, SyntaxNode, SyntaxToken,
|
||||
};
|
||||
|
||||
use crate::{errors::error, parsing, SsrError};
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ use ide_db::{base_db::FileId, famous_defs::FamousDefs, RootDatabase};
|
|||
|
||||
use itertools::Itertools;
|
||||
use syntax::{
|
||||
ast::{self, AstNode, HasName},
|
||||
ast::{self, AstNode, HasGenericArgs, HasName},
|
||||
match_ast,
|
||||
};
|
||||
|
||||
|
|
|
@ -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},
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -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> {
|
||||
|
|
|
@ -122,18 +122,21 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
|
|||
|
||||
if field.is_many() {
|
||||
quote! {
|
||||
#[inline]
|
||||
pub fn #method_name(&self) -> AstChildren<#ty> {
|
||||
support::children(&self.syntax)
|
||||
}
|
||||
}
|
||||
} else if let Some(token_kind) = field.token_kind() {
|
||||
quote! {
|
||||
#[inline]
|
||||
pub fn #method_name(&self) -> Option<#ty> {
|
||||
support::token(&self.syntax, #token_kind)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#[inline]
|
||||
pub fn #method_name(&self) -> Option<#ty> {
|
||||
support::child(&self.syntax)
|
||||
}
|
||||
|
@ -156,12 +159,15 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
|
|||
},
|
||||
quote! {
|
||||
impl AstNode for #name {
|
||||
#[inline]
|
||||
fn can_cast(kind: SyntaxKind) -> bool {
|
||||
kind == #kind
|
||||
}
|
||||
#[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 }
|
||||
}
|
||||
},
|
||||
|
@ -190,9 +196,11 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
|
|||
} else {
|
||||
quote! {
|
||||
impl AstNode for #name {
|
||||
#[inline]
|
||||
fn can_cast(kind: SyntaxKind) -> bool {
|
||||
matches!(kind, #(#kinds)|*)
|
||||
}
|
||||
#[inline]
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
let res = match syntax.kind() {
|
||||
#(
|
||||
|
@ -202,6 +210,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
|
|||
};
|
||||
Some(res)
|
||||
}
|
||||
#[inline]
|
||||
fn syntax(&self) -> &SyntaxNode {
|
||||
match self {
|
||||
#(
|
||||
|
@ -226,6 +235,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
|
|||
quote! {
|
||||
#(
|
||||
impl From<#variants> for #name {
|
||||
#[inline]
|
||||
fn from(node: #variants) -> #name {
|
||||
#name::#variants(node)
|
||||
}
|
||||
|
@ -270,12 +280,15 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
|
|||
}
|
||||
}
|
||||
impl AstNode for #name {
|
||||
#[inline]
|
||||
fn can_cast(kind: SyntaxKind) -> bool {
|
||||
matches!(kind, #(#kinds)|*)
|
||||
}
|
||||
#[inline]
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
Self::can_cast(syntax.kind()).then_some(#name { syntax })
|
||||
}
|
||||
#[inline]
|
||||
fn syntax(&self) -> &SyntaxNode {
|
||||
&self.syntax
|
||||
}
|
||||
|
@ -797,20 +810,21 @@ fn extract_enums(ast: &mut AstSrc) {
|
|||
}
|
||||
}
|
||||
|
||||
fn extract_struct_traits(ast: &mut AstSrc) {
|
||||
let traits: &[(&str, &[&str])] = &[
|
||||
const TRAITS: &[(&str, &[&str])] = &[
|
||||
("HasAttrs", &["attrs"]),
|
||||
("HasName", &["name"]),
|
||||
("HasVisibility", &["visibility"]),
|
||||
("HasGenericParams", &["generic_param_list", "where_clause"]),
|
||||
("HasGenericArgs", &["generic_arg_list"]),
|
||||
("HasTypeBounds", &["type_bound_list", "colon_token"]),
|
||||
("HasModuleItem", &["items"]),
|
||||
("HasLoopBody", &["label", "loop_body"]),
|
||||
("HasArgList", &["arg_list"]),
|
||||
];
|
||||
|
||||
fn extract_struct_traits(ast: &mut AstSrc) {
|
||||
for node in &mut ast.nodes {
|
||||
for (name, methods) in traits {
|
||||
for (name, methods) in TRAITS {
|
||||
extract_struct_trait(node, name, methods);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue