diff options
author | Aleksey Kladov <[email protected]> | 2020-07-30 17:52:02 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-30 17:52:02 +0100 |
commit | 3dce34aaf83c67836c94a526cb90eba8f36e3985 (patch) | |
tree | 43f1d76098133aa12a1f228c8dc3ec9503bebc8e /crates/ra_syntax | |
parent | 917c89c103597d09e95bdee273633f79123dd19e (diff) |
Introduce GenericParam
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/ast/generated/nodes.rs | 213 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/node_ext.rs | 21 |
2 files changed, 149 insertions, 85 deletions
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index 9345a5e8f..e3b3f44cc 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs | |||
@@ -312,9 +312,7 @@ pub struct GenericParamList { | |||
312 | } | 312 | } |
313 | impl GenericParamList { | 313 | impl GenericParamList { |
314 | pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) } | 314 | pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) } |
315 | pub fn type_params(&self) -> AstChildren<TypeParam> { support::children(&self.syntax) } | 315 | pub fn generic_params(&self) -> AstChildren<GenericParam> { support::children(&self.syntax) } |
316 | pub fn lifetime_params(&self) -> AstChildren<LifetimeParam> { support::children(&self.syntax) } | ||
317 | pub fn const_params(&self) -> AstChildren<ConstParam> { support::children(&self.syntax) } | ||
318 | pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) } | 316 | pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) } |
319 | } | 317 | } |
320 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 318 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -470,6 +468,40 @@ impl ExternItemList { | |||
470 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | 468 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } |
471 | } | 469 | } |
472 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 470 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
471 | pub struct LifetimeParam { | ||
472 | pub(crate) syntax: SyntaxNode, | ||
473 | } | ||
474 | impl ast::AttrsOwner for LifetimeParam {} | ||
475 | impl LifetimeParam { | ||
476 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
477 | support::token(&self.syntax, T![lifetime]) | ||
478 | } | ||
479 | } | ||
480 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
481 | pub struct TypeParam { | ||
482 | pub(crate) syntax: SyntaxNode, | ||
483 | } | ||
484 | impl ast::AttrsOwner for TypeParam {} | ||
485 | impl ast::NameOwner for TypeParam {} | ||
486 | impl ast::TypeBoundsOwner for TypeParam {} | ||
487 | impl TypeParam { | ||
488 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
489 | pub fn default_type(&self) -> Option<TypeRef> { support::child(&self.syntax) } | ||
490 | } | ||
491 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
492 | pub struct ConstParam { | ||
493 | pub(crate) syntax: SyntaxNode, | ||
494 | } | ||
495 | impl ast::AttrsOwner for ConstParam {} | ||
496 | impl ast::NameOwner for ConstParam {} | ||
497 | impl ast::TypeAscriptionOwner for ConstParam {} | ||
498 | impl ConstParam { | ||
499 | pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } | ||
500 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
501 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
502 | pub fn default_val(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
503 | } | ||
504 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
473 | pub struct ParenType { | 505 | pub struct ParenType { |
474 | pub(crate) syntax: SyntaxNode, | 506 | pub(crate) syntax: SyntaxNode, |
475 | } | 507 | } |
@@ -1133,40 +1165,6 @@ impl MacroStmts { | |||
1133 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | 1165 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } |
1134 | } | 1166 | } |
1135 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 1167 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
1136 | pub struct TypeParam { | ||
1137 | pub(crate) syntax: SyntaxNode, | ||
1138 | } | ||
1139 | impl ast::AttrsOwner for TypeParam {} | ||
1140 | impl ast::NameOwner for TypeParam {} | ||
1141 | impl ast::TypeBoundsOwner for TypeParam {} | ||
1142 | impl TypeParam { | ||
1143 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
1144 | pub fn default_type(&self) -> Option<TypeRef> { support::child(&self.syntax) } | ||
1145 | } | ||
1146 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1147 | pub struct LifetimeParam { | ||
1148 | pub(crate) syntax: SyntaxNode, | ||
1149 | } | ||
1150 | impl ast::AttrsOwner for LifetimeParam {} | ||
1151 | impl LifetimeParam { | ||
1152 | pub fn lifetime_token(&self) -> Option<SyntaxToken> { | ||
1153 | support::token(&self.syntax, T![lifetime]) | ||
1154 | } | ||
1155 | } | ||
1156 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1157 | pub struct ConstParam { | ||
1158 | pub(crate) syntax: SyntaxNode, | ||
1159 | } | ||
1160 | impl ast::AttrsOwner for ConstParam {} | ||
1161 | impl ast::NameOwner for ConstParam {} | ||
1162 | impl ast::TypeAscriptionOwner for ConstParam {} | ||
1163 | impl ConstParam { | ||
1164 | pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } | ||
1165 | pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } | ||
1166 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | ||
1167 | pub fn default_val(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
1168 | } | ||
1169 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1170 | pub struct TypeBound { | 1168 | pub struct TypeBound { |
1171 | pub(crate) syntax: SyntaxNode, | 1169 | pub(crate) syntax: SyntaxNode, |
1172 | } | 1170 | } |
@@ -1383,6 +1381,13 @@ pub enum ExternItem { | |||
1383 | impl ast::AttrsOwner for ExternItem {} | 1381 | impl ast::AttrsOwner for ExternItem {} |
1384 | impl ast::NameOwner for ExternItem {} | 1382 | impl ast::NameOwner for ExternItem {} |
1385 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 1383 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
1384 | pub enum GenericParam { | ||
1385 | LifetimeParam(LifetimeParam), | ||
1386 | TypeParam(TypeParam), | ||
1387 | ConstParam(ConstParam), | ||
1388 | } | ||
1389 | impl ast::AttrsOwner for GenericParam {} | ||
1390 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1386 | pub enum Stmt { | 1391 | pub enum Stmt { |
1387 | LetStmt(LetStmt), | 1392 | LetStmt(LetStmt), |
1388 | ExprStmt(ExprStmt), | 1393 | ExprStmt(ExprStmt), |
@@ -1854,6 +1859,39 @@ impl AstNode for ExternItemList { | |||
1854 | } | 1859 | } |
1855 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | 1860 | fn syntax(&self) -> &SyntaxNode { &self.syntax } |
1856 | } | 1861 | } |
1862 | impl AstNode for LifetimeParam { | ||
1863 | fn can_cast(kind: SyntaxKind) -> bool { kind == LIFETIME_PARAM } | ||
1864 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1865 | if Self::can_cast(syntax.kind()) { | ||
1866 | Some(Self { syntax }) | ||
1867 | } else { | ||
1868 | None | ||
1869 | } | ||
1870 | } | ||
1871 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1872 | } | ||
1873 | impl AstNode for TypeParam { | ||
1874 | fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_PARAM } | ||
1875 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1876 | if Self::can_cast(syntax.kind()) { | ||
1877 | Some(Self { syntax }) | ||
1878 | } else { | ||
1879 | None | ||
1880 | } | ||
1881 | } | ||
1882 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1883 | } | ||
1884 | impl AstNode for ConstParam { | ||
1885 | fn can_cast(kind: SyntaxKind) -> bool { kind == CONST_PARAM } | ||
1886 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1887 | if Self::can_cast(syntax.kind()) { | ||
1888 | Some(Self { syntax }) | ||
1889 | } else { | ||
1890 | None | ||
1891 | } | ||
1892 | } | ||
1893 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1894 | } | ||
1857 | impl AstNode for ParenType { | 1895 | impl AstNode for ParenType { |
1858 | fn can_cast(kind: SyntaxKind) -> bool { kind == PAREN_TYPE } | 1896 | fn can_cast(kind: SyntaxKind) -> bool { kind == PAREN_TYPE } |
1859 | fn cast(syntax: SyntaxNode) -> Option<Self> { | 1897 | fn cast(syntax: SyntaxNode) -> Option<Self> { |
@@ -2635,39 +2673,6 @@ impl AstNode for MacroStmts { | |||
2635 | } | 2673 | } |
2636 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | 2674 | fn syntax(&self) -> &SyntaxNode { &self.syntax } |
2637 | } | 2675 | } |
2638 | impl AstNode for TypeParam { | ||
2639 | fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_PARAM } | ||
2640 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2641 | if Self::can_cast(syntax.kind()) { | ||
2642 | Some(Self { syntax }) | ||
2643 | } else { | ||
2644 | None | ||
2645 | } | ||
2646 | } | ||
2647 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2648 | } | ||
2649 | impl AstNode for LifetimeParam { | ||
2650 | fn can_cast(kind: SyntaxKind) -> bool { kind == LIFETIME_PARAM } | ||
2651 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2652 | if Self::can_cast(syntax.kind()) { | ||
2653 | Some(Self { syntax }) | ||
2654 | } else { | ||
2655 | None | ||
2656 | } | ||
2657 | } | ||
2658 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2659 | } | ||
2660 | impl AstNode for ConstParam { | ||
2661 | fn can_cast(kind: SyntaxKind) -> bool { kind == CONST_PARAM } | ||
2662 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2663 | if Self::can_cast(syntax.kind()) { | ||
2664 | Some(Self { syntax }) | ||
2665 | } else { | ||
2666 | None | ||
2667 | } | ||
2668 | } | ||
2669 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2670 | } | ||
2671 | impl AstNode for TypeBound { | 2676 | impl AstNode for TypeBound { |
2672 | fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_BOUND } | 2677 | fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_BOUND } |
2673 | fn cast(syntax: SyntaxNode) -> Option<Self> { | 2678 | fn cast(syntax: SyntaxNode) -> Option<Self> { |
@@ -3325,6 +3330,39 @@ impl AstNode for ExternItem { | |||
3325 | } | 3330 | } |
3326 | } | 3331 | } |
3327 | } | 3332 | } |
3333 | impl From<LifetimeParam> for GenericParam { | ||
3334 | fn from(node: LifetimeParam) -> GenericParam { GenericParam::LifetimeParam(node) } | ||
3335 | } | ||
3336 | impl From<TypeParam> for GenericParam { | ||
3337 | fn from(node: TypeParam) -> GenericParam { GenericParam::TypeParam(node) } | ||
3338 | } | ||
3339 | impl From<ConstParam> for GenericParam { | ||
3340 | fn from(node: ConstParam) -> GenericParam { GenericParam::ConstParam(node) } | ||
3341 | } | ||
3342 | impl AstNode for GenericParam { | ||
3343 | fn can_cast(kind: SyntaxKind) -> bool { | ||
3344 | match kind { | ||
3345 | LIFETIME_PARAM | TYPE_PARAM | CONST_PARAM => true, | ||
3346 | _ => false, | ||
3347 | } | ||
3348 | } | ||
3349 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
3350 | let res = match syntax.kind() { | ||
3351 | LIFETIME_PARAM => GenericParam::LifetimeParam(LifetimeParam { syntax }), | ||
3352 | TYPE_PARAM => GenericParam::TypeParam(TypeParam { syntax }), | ||
3353 | CONST_PARAM => GenericParam::ConstParam(ConstParam { syntax }), | ||
3354 | _ => return None, | ||
3355 | }; | ||
3356 | Some(res) | ||
3357 | } | ||
3358 | fn syntax(&self) -> &SyntaxNode { | ||
3359 | match self { | ||
3360 | GenericParam::LifetimeParam(it) => &it.syntax, | ||
3361 | GenericParam::TypeParam(it) => &it.syntax, | ||
3362 | GenericParam::ConstParam(it) => &it.syntax, | ||
3363 | } | ||
3364 | } | ||
3365 | } | ||
3328 | impl From<LetStmt> for Stmt { | 3366 | impl From<LetStmt> for Stmt { |
3329 | fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) } | 3367 | fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) } |
3330 | } | 3368 | } |
@@ -3449,6 +3487,11 @@ impl std::fmt::Display for ExternItem { | |||
3449 | std::fmt::Display::fmt(self.syntax(), f) | 3487 | std::fmt::Display::fmt(self.syntax(), f) |
3450 | } | 3488 | } |
3451 | } | 3489 | } |
3490 | impl std::fmt::Display for GenericParam { | ||
3491 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3492 | std::fmt::Display::fmt(self.syntax(), f) | ||
3493 | } | ||
3494 | } | ||
3452 | impl std::fmt::Display for Stmt { | 3495 | impl std::fmt::Display for Stmt { |
3453 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 3496 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
3454 | std::fmt::Display::fmt(self.syntax(), f) | 3497 | std::fmt::Display::fmt(self.syntax(), f) |
@@ -3669,6 +3712,21 @@ impl std::fmt::Display for ExternItemList { | |||
3669 | std::fmt::Display::fmt(self.syntax(), f) | 3712 | std::fmt::Display::fmt(self.syntax(), f) |
3670 | } | 3713 | } |
3671 | } | 3714 | } |
3715 | impl std::fmt::Display for LifetimeParam { | ||
3716 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3717 | std::fmt::Display::fmt(self.syntax(), f) | ||
3718 | } | ||
3719 | } | ||
3720 | impl std::fmt::Display for TypeParam { | ||
3721 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3722 | std::fmt::Display::fmt(self.syntax(), f) | ||
3723 | } | ||
3724 | } | ||
3725 | impl std::fmt::Display for ConstParam { | ||
3726 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3727 | std::fmt::Display::fmt(self.syntax(), f) | ||
3728 | } | ||
3729 | } | ||
3672 | impl std::fmt::Display for ParenType { | 3730 | impl std::fmt::Display for ParenType { |
3673 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 3731 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
3674 | std::fmt::Display::fmt(self.syntax(), f) | 3732 | std::fmt::Display::fmt(self.syntax(), f) |
@@ -4024,21 +4082,6 @@ impl std::fmt::Display for MacroStmts { | |||
4024 | std::fmt::Display::fmt(self.syntax(), f) | 4082 | std::fmt::Display::fmt(self.syntax(), f) |
4025 | } | 4083 | } |
4026 | } | 4084 | } |
4027 | impl std::fmt::Display for TypeParam { | ||
4028 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4029 | std::fmt::Display::fmt(self.syntax(), f) | ||
4030 | } | ||
4031 | } | ||
4032 | impl std::fmt::Display for LifetimeParam { | ||
4033 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4034 | std::fmt::Display::fmt(self.syntax(), f) | ||
4035 | } | ||
4036 | } | ||
4037 | impl std::fmt::Display for ConstParam { | ||
4038 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
4039 | std::fmt::Display::fmt(self.syntax(), f) | ||
4040 | } | ||
4041 | } | ||
4042 | impl std::fmt::Display for TypeBound { | 4085 | impl std::fmt::Display for TypeBound { |
4043 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 4086 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
4044 | std::fmt::Display::fmt(self.syntax(), f) | 4087 | std::fmt::Display::fmt(self.syntax(), f) |
diff --git a/crates/ra_syntax/src/ast/node_ext.rs b/crates/ra_syntax/src/ast/node_ext.rs index 313f52226..d2ee9586d 100644 --- a/crates/ra_syntax/src/ast/node_ext.rs +++ b/crates/ra_syntax/src/ast/node_ext.rs | |||
@@ -473,6 +473,27 @@ impl ast::TokenTree { | |||
473 | } | 473 | } |
474 | } | 474 | } |
475 | 475 | ||
476 | impl ast::GenericParamList { | ||
477 | pub fn lifetime_params(&self) -> impl Iterator<Item = ast::LifetimeParam> { | ||
478 | self.generic_params().filter_map(|param| match param { | ||
479 | ast::GenericParam::LifetimeParam(it) => Some(it), | ||
480 | ast::GenericParam::TypeParam(_) | ast::GenericParam::ConstParam(_) => None, | ||
481 | }) | ||
482 | } | ||
483 | pub fn type_params(&self) -> impl Iterator<Item = ast::TypeParam> { | ||
484 | self.generic_params().filter_map(|param| match param { | ||
485 | ast::GenericParam::TypeParam(it) => Some(it), | ||
486 | ast::GenericParam::LifetimeParam(_) | ast::GenericParam::ConstParam(_) => None, | ||
487 | }) | ||
488 | } | ||
489 | pub fn const_params(&self) -> impl Iterator<Item = ast::ConstParam> { | ||
490 | self.generic_params().filter_map(|param| match param { | ||
491 | ast::GenericParam::ConstParam(it) => Some(it), | ||
492 | ast::GenericParam::TypeParam(_) | ast::GenericParam::LifetimeParam(_) => None, | ||
493 | }) | ||
494 | } | ||
495 | } | ||
496 | |||
476 | impl ast::DocCommentsOwner for ast::SourceFile {} | 497 | impl ast::DocCommentsOwner for ast::SourceFile {} |
477 | impl ast::DocCommentsOwner for ast::Fn {} | 498 | impl ast::DocCommentsOwner for ast::Fn {} |
478 | impl ast::DocCommentsOwner for ast::Struct {} | 499 | impl ast::DocCommentsOwner for ast::Struct {} |