aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_ide_api/src/completion.rs2
-rw-r--r--crates/ra_ide_api/src/completion/complete_struct_literal.rs73
-rw-r--r--crates/ra_ide_api/src/completion/completion_context.rs6
-rw-r--r--crates/ra_syntax/src/ast.rs6
-rw-r--r--crates/ra_syntax/src/ast/generated.rs398
-rw-r--r--crates/ra_syntax/src/ast/generated.rs.tera9
6 files changed, 494 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/completion.rs b/crates/ra_ide_api/src/completion.rs
index 4a38d62bb..d92e01bfb 100644
--- a/crates/ra_ide_api/src/completion.rs
+++ b/crates/ra_ide_api/src/completion.rs
@@ -2,6 +2,7 @@ mod completion_item;
2mod completion_context; 2mod completion_context;
3 3
4mod complete_dot; 4mod complete_dot;
5mod complete_struct_literal;
5mod complete_fn_param; 6mod complete_fn_param;
6mod complete_keyword; 7mod complete_keyword;
7mod complete_snippet; 8mod complete_snippet;
@@ -59,6 +60,7 @@ pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Opti
59 complete_path::complete_path(&mut acc, &ctx); 60 complete_path::complete_path(&mut acc, &ctx);
60 complete_scope::complete_scope(&mut acc, &ctx); 61 complete_scope::complete_scope(&mut acc, &ctx);
61 complete_dot::complete_dot(&mut acc, &ctx); 62 complete_dot::complete_dot(&mut acc, &ctx);
63 complete_struct_literal::complete_struct_literal(&mut acc, &ctx);
62 complete_postfix::complete_postfix(&mut acc, &ctx); 64 complete_postfix::complete_postfix(&mut acc, &ctx);
63 Some(acc) 65 Some(acc)
64} 66}
diff --git a/crates/ra_ide_api/src/completion/complete_struct_literal.rs b/crates/ra_ide_api/src/completion/complete_struct_literal.rs
new file mode 100644
index 000000000..f8dd2baad
--- /dev/null
+++ b/crates/ra_ide_api/src/completion/complete_struct_literal.rs
@@ -0,0 +1,73 @@
1use hir::{Ty, AdtDef, Docs};
2
3use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind};
4use crate::completion::completion_item::CompletionKind;
5
6/// Complete fields in fields literals.
7pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) {
8 let (function, struct_lit) = match (&ctx.function, ctx.struct_lit_syntax) {
9 (Some(function), Some(struct_lit)) => (function, struct_lit),
10 _ => return,
11 };
12 let infer_result = function.infer(ctx.db);
13 let syntax_mapping = function.body_syntax_mapping(ctx.db);
14 let expr = match syntax_mapping.node_expr(struct_lit.into()) {
15 Some(expr) => expr,
16 None => return,
17 };
18 let ty = infer_result[expr].clone();
19 let (adt, substs) = match ty {
20 Ty::Adt { def_id, ref substs, .. } => (def_id, substs),
21 _ => return,
22 };
23 match adt {
24 AdtDef::Struct(s) => {
25 for field in s.fields(ctx.db) {
26 CompletionItem::new(
27 CompletionKind::Reference,
28 ctx.source_range(),
29 field.name(ctx.db).to_string(),
30 )
31 .kind(CompletionItemKind::Field)
32 .detail(field.ty(ctx.db).subst(substs).to_string())
33 .set_documentation(field.docs(ctx.db))
34 .add_to(acc);
35 }
36 }
37
38 // TODO unions
39 AdtDef::Enum(_) => (),
40 };
41}
42
43#[cfg(test)]
44mod tests {
45 use insta::assert_debug_snapshot_matches;
46 use crate::completion::{CompletionItem, CompletionKind};
47
48 fn complete(code: &str) -> Vec<CompletionItem> {
49 crate::completion::completion_item::do_completion(code, CompletionKind::Reference)
50 }
51
52 #[test]
53 fn test_struct_literal_field() {
54 let completions = complete(
55 r"
56 struct A { the_field: u32 }
57 fn foo() {
58 A { the<|> }
59 }
60 ",
61 );
62 assert_debug_snapshot_matches!(completions, @r###"[
63 CompletionItem {
64 label: "the_field",
65 source_range: [83; 86),
66 delete: [83; 86),
67 insert: "the_field",
68 kind: Field,
69 detail: "u32"
70 }
71]"###);
72 }
73}
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs
index b9f0abe19..d351be054 100644
--- a/crates/ra_ide_api/src/completion/completion_context.rs
+++ b/crates/ra_ide_api/src/completion/completion_context.rs
@@ -21,6 +21,7 @@ pub(crate) struct CompletionContext<'a> {
21 pub(super) function: Option<hir::Function>, 21 pub(super) function: Option<hir::Function>,
22 pub(super) function_syntax: Option<&'a ast::FnDef>, 22 pub(super) function_syntax: Option<&'a ast::FnDef>,
23 pub(super) use_item_syntax: Option<&'a ast::UseItem>, 23 pub(super) use_item_syntax: Option<&'a ast::UseItem>,
24 pub(super) struct_lit_syntax: Option<&'a ast::StructLit>,
24 pub(super) is_param: bool, 25 pub(super) is_param: bool,
25 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. 26 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
26 pub(super) is_trivial_path: bool, 27 pub(super) is_trivial_path: bool,
@@ -55,6 +56,7 @@ impl<'a> CompletionContext<'a> {
55 function: None, 56 function: None,
56 function_syntax: None, 57 function_syntax: None,
57 use_item_syntax: None, 58 use_item_syntax: None,
59 struct_lit_syntax: None,
58 is_param: false, 60 is_param: false,
59 is_trivial_path: false, 61 is_trivial_path: false,
60 path_prefix: None, 62 path_prefix: None,
@@ -108,6 +110,10 @@ impl<'a> CompletionContext<'a> {
108 } 110 }
109 fn classify_name_ref(&mut self, original_file: &'a SourceFile, name_ref: &ast::NameRef) { 111 fn classify_name_ref(&mut self, original_file: &'a SourceFile, name_ref: &ast::NameRef) {
110 let name_range = name_ref.syntax().range(); 112 let name_range = name_ref.syntax().range();
113 if name_ref.syntax().parent().and_then(ast::NamedField::cast).is_some() {
114 self.struct_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset);
115 }
116
111 let top_node = 117 let top_node =
112 name_ref.syntax().ancestors().take_while(|it| it.range() == name_range).last().unwrap(); 118 name_ref.syntax().ancestors().take_while(|it| it.range() == name_range).last().unwrap();
113 119
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 20e0a6856..56fb7c20c 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -724,6 +724,12 @@ impl LiteralExpr {
724 } 724 }
725} 725}
726 726
727impl NamedField {
728 pub fn parent_struct_lit(&self) -> &StructLit {
729 self.syntax().ancestors().find_map(StructLit::cast).unwrap()
730 }
731}
732
727impl BindPat { 733impl BindPat {
728 pub fn is_mutable(&self) -> bool { 734 pub fn is_mutable(&self) -> bool {
729 self.syntax().children().any(|n| n.kind() == MUT_KW) 735 self.syntax().children().any(|n| n.kind() == MUT_KW)
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 47107a58b..aa88b1e28 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -834,6 +834,142 @@ pub enum ExprKind<'a> {
834 BinExpr(&'a BinExpr), 834 BinExpr(&'a BinExpr),
835 Literal(&'a Literal), 835 Literal(&'a Literal),
836} 836}
837impl<'a> From<&'a TupleExpr> for &'a Expr {
838 fn from(n: &'a TupleExpr) -> &'a Expr {
839 Expr::cast(&n.syntax).unwrap()
840 }
841}
842impl<'a> From<&'a ArrayExpr> for &'a Expr {
843 fn from(n: &'a ArrayExpr) -> &'a Expr {
844 Expr::cast(&n.syntax).unwrap()
845 }
846}
847impl<'a> From<&'a ParenExpr> for &'a Expr {
848 fn from(n: &'a ParenExpr) -> &'a Expr {
849 Expr::cast(&n.syntax).unwrap()
850 }
851}
852impl<'a> From<&'a PathExpr> for &'a Expr {
853 fn from(n: &'a PathExpr) -> &'a Expr {
854 Expr::cast(&n.syntax).unwrap()
855 }
856}
857impl<'a> From<&'a LambdaExpr> for &'a Expr {
858 fn from(n: &'a LambdaExpr) -> &'a Expr {
859 Expr::cast(&n.syntax).unwrap()
860 }
861}
862impl<'a> From<&'a IfExpr> for &'a Expr {
863 fn from(n: &'a IfExpr) -> &'a Expr {
864 Expr::cast(&n.syntax).unwrap()
865 }
866}
867impl<'a> From<&'a LoopExpr> for &'a Expr {
868 fn from(n: &'a LoopExpr) -> &'a Expr {
869 Expr::cast(&n.syntax).unwrap()
870 }
871}
872impl<'a> From<&'a ForExpr> for &'a Expr {
873 fn from(n: &'a ForExpr) -> &'a Expr {
874 Expr::cast(&n.syntax).unwrap()
875 }
876}
877impl<'a> From<&'a WhileExpr> for &'a Expr {
878 fn from(n: &'a WhileExpr) -> &'a Expr {
879 Expr::cast(&n.syntax).unwrap()
880 }
881}
882impl<'a> From<&'a ContinueExpr> for &'a Expr {
883 fn from(n: &'a ContinueExpr) -> &'a Expr {
884 Expr::cast(&n.syntax).unwrap()
885 }
886}
887impl<'a> From<&'a BreakExpr> for &'a Expr {
888 fn from(n: &'a BreakExpr) -> &'a Expr {
889 Expr::cast(&n.syntax).unwrap()
890 }
891}
892impl<'a> From<&'a Label> for &'a Expr {
893 fn from(n: &'a Label) -> &'a Expr {
894 Expr::cast(&n.syntax).unwrap()
895 }
896}
897impl<'a> From<&'a BlockExpr> for &'a Expr {
898 fn from(n: &'a BlockExpr) -> &'a Expr {
899 Expr::cast(&n.syntax).unwrap()
900 }
901}
902impl<'a> From<&'a ReturnExpr> for &'a Expr {
903 fn from(n: &'a ReturnExpr) -> &'a Expr {
904 Expr::cast(&n.syntax).unwrap()
905 }
906}
907impl<'a> From<&'a MatchExpr> for &'a Expr {
908 fn from(n: &'a MatchExpr) -> &'a Expr {
909 Expr::cast(&n.syntax).unwrap()
910 }
911}
912impl<'a> From<&'a StructLit> for &'a Expr {
913 fn from(n: &'a StructLit) -> &'a Expr {
914 Expr::cast(&n.syntax).unwrap()
915 }
916}
917impl<'a> From<&'a CallExpr> for &'a Expr {
918 fn from(n: &'a CallExpr) -> &'a Expr {
919 Expr::cast(&n.syntax).unwrap()
920 }
921}
922impl<'a> From<&'a IndexExpr> for &'a Expr {
923 fn from(n: &'a IndexExpr) -> &'a Expr {
924 Expr::cast(&n.syntax).unwrap()
925 }
926}
927impl<'a> From<&'a MethodCallExpr> for &'a Expr {
928 fn from(n: &'a MethodCallExpr) -> &'a Expr {
929 Expr::cast(&n.syntax).unwrap()
930 }
931}
932impl<'a> From<&'a FieldExpr> for &'a Expr {
933 fn from(n: &'a FieldExpr) -> &'a Expr {
934 Expr::cast(&n.syntax).unwrap()
935 }
936}
937impl<'a> From<&'a TryExpr> for &'a Expr {
938 fn from(n: &'a TryExpr) -> &'a Expr {
939 Expr::cast(&n.syntax).unwrap()
940 }
941}
942impl<'a> From<&'a CastExpr> for &'a Expr {
943 fn from(n: &'a CastExpr) -> &'a Expr {
944 Expr::cast(&n.syntax).unwrap()
945 }
946}
947impl<'a> From<&'a RefExpr> for &'a Expr {
948 fn from(n: &'a RefExpr) -> &'a Expr {
949 Expr::cast(&n.syntax).unwrap()
950 }
951}
952impl<'a> From<&'a PrefixExpr> for &'a Expr {
953 fn from(n: &'a PrefixExpr) -> &'a Expr {
954 Expr::cast(&n.syntax).unwrap()
955 }
956}
957impl<'a> From<&'a RangeExpr> for &'a Expr {
958 fn from(n: &'a RangeExpr) -> &'a Expr {
959 Expr::cast(&n.syntax).unwrap()
960 }
961}
962impl<'a> From<&'a BinExpr> for &'a Expr {
963 fn from(n: &'a BinExpr) -> &'a Expr {
964 Expr::cast(&n.syntax).unwrap()
965 }
966}
967impl<'a> From<&'a Literal> for &'a Expr {
968 fn from(n: &'a Literal) -> &'a Expr {
969 Expr::cast(&n.syntax).unwrap()
970 }
971}
972
837 973
838impl AstNode for Expr { 974impl AstNode for Expr {
839 fn cast(syntax: &SyntaxNode) -> Option<&Self> { 975 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
@@ -1375,6 +1511,22 @@ pub enum ImplItemKind<'a> {
1375 TypeDef(&'a TypeDef), 1511 TypeDef(&'a TypeDef),
1376 ConstDef(&'a ConstDef), 1512 ConstDef(&'a ConstDef),
1377} 1513}
1514impl<'a> From<&'a FnDef> for &'a ImplItem {
1515 fn from(n: &'a FnDef) -> &'a ImplItem {
1516 ImplItem::cast(&n.syntax).unwrap()
1517 }
1518}
1519impl<'a> From<&'a TypeDef> for &'a ImplItem {
1520 fn from(n: &'a TypeDef) -> &'a ImplItem {
1521 ImplItem::cast(&n.syntax).unwrap()
1522 }
1523}
1524impl<'a> From<&'a ConstDef> for &'a ImplItem {
1525 fn from(n: &'a ConstDef) -> &'a ImplItem {
1526 ImplItem::cast(&n.syntax).unwrap()
1527 }
1528}
1529
1378 1530
1379impl AstNode for ImplItem { 1531impl AstNode for ImplItem {
1380 fn cast(syntax: &SyntaxNode) -> Option<&Self> { 1532 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
@@ -1778,6 +1930,57 @@ pub enum LiteralExprKind<'a> {
1778 TrueKw(&'a TrueKw), 1930 TrueKw(&'a TrueKw),
1779 FalseKw(&'a FalseKw), 1931 FalseKw(&'a FalseKw),
1780} 1932}
1933impl<'a> From<&'a String> for &'a LiteralExpr {
1934 fn from(n: &'a String) -> &'a LiteralExpr {
1935 LiteralExpr::cast(&n.syntax).unwrap()
1936 }
1937}
1938impl<'a> From<&'a ByteString> for &'a LiteralExpr {
1939 fn from(n: &'a ByteString) -> &'a LiteralExpr {
1940 LiteralExpr::cast(&n.syntax).unwrap()
1941 }
1942}
1943impl<'a> From<&'a RawString> for &'a LiteralExpr {
1944 fn from(n: &'a RawString) -> &'a LiteralExpr {
1945 LiteralExpr::cast(&n.syntax).unwrap()
1946 }
1947}
1948impl<'a> From<&'a RawByteString> for &'a LiteralExpr {
1949 fn from(n: &'a RawByteString) -> &'a LiteralExpr {
1950 LiteralExpr::cast(&n.syntax).unwrap()
1951 }
1952}
1953impl<'a> From<&'a Char> for &'a LiteralExpr {
1954 fn from(n: &'a Char) -> &'a LiteralExpr {
1955 LiteralExpr::cast(&n.syntax).unwrap()
1956 }
1957}
1958impl<'a> From<&'a Byte> for &'a LiteralExpr {
1959 fn from(n: &'a Byte) -> &'a LiteralExpr {
1960 LiteralExpr::cast(&n.syntax).unwrap()
1961 }
1962}
1963impl<'a> From<&'a IntNumber> for &'a LiteralExpr {
1964 fn from(n: &'a IntNumber) -> &'a LiteralExpr {
1965 LiteralExpr::cast(&n.syntax).unwrap()
1966 }
1967}
1968impl<'a> From<&'a FloatNumber> for &'a LiteralExpr {
1969 fn from(n: &'a FloatNumber) -> &'a LiteralExpr {
1970 LiteralExpr::cast(&n.syntax).unwrap()
1971 }
1972}
1973impl<'a> From<&'a TrueKw> for &'a LiteralExpr {
1974 fn from(n: &'a TrueKw) -> &'a LiteralExpr {
1975 LiteralExpr::cast(&n.syntax).unwrap()
1976 }
1977}
1978impl<'a> From<&'a FalseKw> for &'a LiteralExpr {
1979 fn from(n: &'a FalseKw) -> &'a LiteralExpr {
1980 LiteralExpr::cast(&n.syntax).unwrap()
1981 }
1982}
1983
1781 1984
1782impl AstNode for LiteralExpr { 1985impl AstNode for LiteralExpr {
1783 fn cast(syntax: &SyntaxNode) -> Option<&Self> { 1986 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
@@ -2164,6 +2367,62 @@ pub enum ModuleItemKind<'a> {
2164 StaticDef(&'a StaticDef), 2367 StaticDef(&'a StaticDef),
2165 Module(&'a Module), 2368 Module(&'a Module),
2166} 2369}
2370impl<'a> From<&'a StructDef> for &'a ModuleItem {
2371 fn from(n: &'a StructDef) -> &'a ModuleItem {
2372 ModuleItem::cast(&n.syntax).unwrap()
2373 }
2374}
2375impl<'a> From<&'a EnumDef> for &'a ModuleItem {
2376 fn from(n: &'a EnumDef) -> &'a ModuleItem {
2377 ModuleItem::cast(&n.syntax).unwrap()
2378 }
2379}
2380impl<'a> From<&'a FnDef> for &'a ModuleItem {
2381 fn from(n: &'a FnDef) -> &'a ModuleItem {
2382 ModuleItem::cast(&n.syntax).unwrap()
2383 }
2384}
2385impl<'a> From<&'a TraitDef> for &'a ModuleItem {
2386 fn from(n: &'a TraitDef) -> &'a ModuleItem {
2387 ModuleItem::cast(&n.syntax).unwrap()
2388 }
2389}
2390impl<'a> From<&'a TypeDef> for &'a ModuleItem {
2391 fn from(n: &'a TypeDef) -> &'a ModuleItem {
2392 ModuleItem::cast(&n.syntax).unwrap()
2393 }
2394}
2395impl<'a> From<&'a ImplBlock> for &'a ModuleItem {
2396 fn from(n: &'a ImplBlock) -> &'a ModuleItem {
2397 ModuleItem::cast(&n.syntax).unwrap()
2398 }
2399}
2400impl<'a> From<&'a UseItem> for &'a ModuleItem {
2401 fn from(n: &'a UseItem) -> &'a ModuleItem {
2402 ModuleItem::cast(&n.syntax).unwrap()
2403 }
2404}
2405impl<'a> From<&'a ExternCrateItem> for &'a ModuleItem {
2406 fn from(n: &'a ExternCrateItem) -> &'a ModuleItem {
2407 ModuleItem::cast(&n.syntax).unwrap()
2408 }
2409}
2410impl<'a> From<&'a ConstDef> for &'a ModuleItem {
2411 fn from(n: &'a ConstDef) -> &'a ModuleItem {
2412 ModuleItem::cast(&n.syntax).unwrap()
2413 }
2414}
2415impl<'a> From<&'a StaticDef> for &'a ModuleItem {
2416 fn from(n: &'a StaticDef) -> &'a ModuleItem {
2417 ModuleItem::cast(&n.syntax).unwrap()
2418 }
2419}
2420impl<'a> From<&'a Module> for &'a ModuleItem {
2421 fn from(n: &'a Module) -> &'a ModuleItem {
2422 ModuleItem::cast(&n.syntax).unwrap()
2423 }
2424}
2425
2167 2426
2168impl AstNode for ModuleItem { 2427impl AstNode for ModuleItem {
2169 fn cast(syntax: &SyntaxNode) -> Option<&Self> { 2428 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
@@ -2446,6 +2705,17 @@ pub enum NominalDefKind<'a> {
2446 StructDef(&'a StructDef), 2705 StructDef(&'a StructDef),
2447 EnumDef(&'a EnumDef), 2706 EnumDef(&'a EnumDef),
2448} 2707}
2708impl<'a> From<&'a StructDef> for &'a NominalDef {
2709 fn from(n: &'a StructDef) -> &'a NominalDef {
2710 NominalDef::cast(&n.syntax).unwrap()
2711 }
2712}
2713impl<'a> From<&'a EnumDef> for &'a NominalDef {
2714 fn from(n: &'a EnumDef) -> &'a NominalDef {
2715 NominalDef::cast(&n.syntax).unwrap()
2716 }
2717}
2718
2449 2719
2450impl AstNode for NominalDef { 2720impl AstNode for NominalDef {
2451 fn cast(syntax: &SyntaxNode) -> Option<&Self> { 2721 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
@@ -2637,6 +2907,57 @@ pub enum PatKind<'a> {
2637 RangePat(&'a RangePat), 2907 RangePat(&'a RangePat),
2638 LiteralPat(&'a LiteralPat), 2908 LiteralPat(&'a LiteralPat),
2639} 2909}
2910impl<'a> From<&'a RefPat> for &'a Pat {
2911 fn from(n: &'a RefPat) -> &'a Pat {
2912 Pat::cast(&n.syntax).unwrap()
2913 }
2914}
2915impl<'a> From<&'a BindPat> for &'a Pat {
2916 fn from(n: &'a BindPat) -> &'a Pat {
2917 Pat::cast(&n.syntax).unwrap()
2918 }
2919}
2920impl<'a> From<&'a PlaceholderPat> for &'a Pat {
2921 fn from(n: &'a PlaceholderPat) -> &'a Pat {
2922 Pat::cast(&n.syntax).unwrap()
2923 }
2924}
2925impl<'a> From<&'a PathPat> for &'a Pat {
2926 fn from(n: &'a PathPat) -> &'a Pat {
2927 Pat::cast(&n.syntax).unwrap()
2928 }
2929}
2930impl<'a> From<&'a StructPat> for &'a Pat {
2931 fn from(n: &'a StructPat) -> &'a Pat {
2932 Pat::cast(&n.syntax).unwrap()
2933 }
2934}
2935impl<'a> From<&'a TupleStructPat> for &'a Pat {
2936 fn from(n: &'a TupleStructPat) -> &'a Pat {
2937 Pat::cast(&n.syntax).unwrap()
2938 }
2939}
2940impl<'a> From<&'a TuplePat> for &'a Pat {
2941 fn from(n: &'a TuplePat) -> &'a Pat {
2942 Pat::cast(&n.syntax).unwrap()
2943 }
2944}
2945impl<'a> From<&'a SlicePat> for &'a Pat {
2946 fn from(n: &'a SlicePat) -> &'a Pat {
2947 Pat::cast(&n.syntax).unwrap()
2948 }
2949}
2950impl<'a> From<&'a RangePat> for &'a Pat {
2951 fn from(n: &'a RangePat) -> &'a Pat {
2952 Pat::cast(&n.syntax).unwrap()
2953 }
2954}
2955impl<'a> From<&'a LiteralPat> for &'a Pat {
2956 fn from(n: &'a LiteralPat) -> &'a Pat {
2957 Pat::cast(&n.syntax).unwrap()
2958 }
2959}
2960
2640 2961
2641impl AstNode for Pat { 2962impl AstNode for Pat {
2642 fn cast(syntax: &SyntaxNode) -> Option<&Self> { 2963 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
@@ -3520,6 +3841,17 @@ pub enum StmtKind<'a> {
3520 ExprStmt(&'a ExprStmt), 3841 ExprStmt(&'a ExprStmt),
3521 LetStmt(&'a LetStmt), 3842 LetStmt(&'a LetStmt),
3522} 3843}
3844impl<'a> From<&'a ExprStmt> for &'a Stmt {
3845 fn from(n: &'a ExprStmt) -> &'a Stmt {
3846 Stmt::cast(&n.syntax).unwrap()
3847 }
3848}
3849impl<'a> From<&'a LetStmt> for &'a Stmt {
3850 fn from(n: &'a LetStmt) -> &'a Stmt {
3851 Stmt::cast(&n.syntax).unwrap()
3852 }
3853}
3854
3523 3855
3524impl AstNode for Stmt { 3856impl AstNode for Stmt {
3525 fn cast(syntax: &SyntaxNode) -> Option<&Self> { 3857 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
@@ -4142,6 +4474,72 @@ pub enum TypeRefKind<'a> {
4142 ImplTraitType(&'a ImplTraitType), 4474 ImplTraitType(&'a ImplTraitType),
4143 DynTraitType(&'a DynTraitType), 4475 DynTraitType(&'a DynTraitType),
4144} 4476}
4477impl<'a> From<&'a ParenType> for &'a TypeRef {
4478 fn from(n: &'a ParenType) -> &'a TypeRef {
4479 TypeRef::cast(&n.syntax).unwrap()
4480 }
4481}
4482impl<'a> From<&'a TupleType> for &'a TypeRef {
4483 fn from(n: &'a TupleType) -> &'a TypeRef {
4484 TypeRef::cast(&n.syntax).unwrap()
4485 }
4486}
4487impl<'a> From<&'a NeverType> for &'a TypeRef {
4488 fn from(n: &'a NeverType) -> &'a TypeRef {
4489 TypeRef::cast(&n.syntax).unwrap()
4490 }
4491}
4492impl<'a> From<&'a PathType> for &'a TypeRef {
4493 fn from(n: &'a PathType) -> &'a TypeRef {
4494 TypeRef::cast(&n.syntax).unwrap()
4495 }
4496}
4497impl<'a> From<&'a PointerType> for &'a TypeRef {
4498 fn from(n: &'a PointerType) -> &'a TypeRef {
4499 TypeRef::cast(&n.syntax).unwrap()
4500 }
4501}
4502impl<'a> From<&'a ArrayType> for &'a TypeRef {
4503 fn from(n: &'a ArrayType) -> &'a TypeRef {
4504 TypeRef::cast(&n.syntax).unwrap()
4505 }
4506}
4507impl<'a> From<&'a SliceType> for &'a TypeRef {
4508 fn from(n: &'a SliceType) -> &'a TypeRef {
4509 TypeRef::cast(&n.syntax).unwrap()
4510 }
4511}
4512impl<'a> From<&'a ReferenceType> for &'a TypeRef {
4513 fn from(n: &'a ReferenceType) -> &'a TypeRef {
4514 TypeRef::cast(&n.syntax).unwrap()
4515 }
4516}
4517impl<'a> From<&'a PlaceholderType> for &'a TypeRef {
4518 fn from(n: &'a PlaceholderType) -> &'a TypeRef {
4519 TypeRef::cast(&n.syntax).unwrap()
4520 }
4521}
4522impl<'a> From<&'a FnPointerType> for &'a TypeRef {
4523 fn from(n: &'a FnPointerType) -> &'a TypeRef {
4524 TypeRef::cast(&n.syntax).unwrap()
4525 }
4526}
4527impl<'a> From<&'a ForType> for &'a TypeRef {
4528 fn from(n: &'a ForType) -> &'a TypeRef {
4529 TypeRef::cast(&n.syntax).unwrap()
4530 }
4531}
4532impl<'a> From<&'a ImplTraitType> for &'a TypeRef {
4533 fn from(n: &'a ImplTraitType) -> &'a TypeRef {
4534 TypeRef::cast(&n.syntax).unwrap()
4535 }
4536}
4537impl<'a> From<&'a DynTraitType> for &'a TypeRef {
4538 fn from(n: &'a DynTraitType) -> &'a TypeRef {
4539 TypeRef::cast(&n.syntax).unwrap()
4540 }
4541}
4542
4145 4543
4146impl AstNode for TypeRef { 4544impl AstNode for TypeRef {
4147 fn cast(syntax: &SyntaxNode) -> Option<&Self> { 4545 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
diff --git a/crates/ra_syntax/src/ast/generated.rs.tera b/crates/ra_syntax/src/ast/generated.rs.tera
index ca7a28581..e2d4856cf 100644
--- a/crates/ra_syntax/src/ast/generated.rs.tera
+++ b/crates/ra_syntax/src/ast/generated.rs.tera
@@ -38,6 +38,15 @@ pub enum {{ node }}Kind<'a> {
38{%- endfor %} 38{%- endfor %}
39} 39}
40 40
41{%- for kind in methods.enum %}
42impl<'a> From<&'a {{ kind }}> for &'a {{ node }} {
43 fn from(n: &'a {{ kind }}) -> &'a {{ node }} {
44 {{ node }}::cast(&n.syntax).unwrap()
45 }
46}
47{%- endfor %}
48
49
41impl AstNode for {{ node }} { 50impl AstNode for {{ node }} {
42 fn cast(syntax: &SyntaxNode) -> Option<&Self> { 51 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
43 match syntax.kind() { 52 match syntax.kind() {