diff options
-rw-r--r-- | .travis.yml | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/docs.rs | 7 | ||||
-rw-r--r-- | crates/ra_ide_api/src/call_info.rs | 3 | ||||
-rw-r--r-- | crates/ra_ide_api/src/hover.rs | 7 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 75 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 44 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar/expressions.rs | 81 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar/items.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/tests/data/parser/ok/0044_let_attrs.rs | 5 | ||||
-rw-r--r-- | crates/ra_syntax/tests/data/parser/ok/0044_let_attrs.txt | 73 | ||||
-rw-r--r-- | crates/tools/src/bin/pre-commit.rs | 7 | ||||
-rw-r--r-- | crates/tools/src/lib.rs | 106 | ||||
-rw-r--r-- | crates/tools/src/main.rs | 127 | ||||
-rw-r--r-- | crates/tools/tests/cli.rs | 16 |
14 files changed, 313 insertions, 242 deletions
diff --git a/.travis.yml b/.travis.yml index bddf1bebb..8d10a43f0 100644 --- a/.travis.yml +++ b/.travis.yml | |||
@@ -10,8 +10,6 @@ build: &rust_build | |||
10 | script: | 10 | script: |
11 | - rustup component add rustfmt | 11 | - rustup component add rustfmt |
12 | - rustup component add rust-src | 12 | - rustup component add rust-src |
13 | - cargo gen-tests --verify | ||
14 | - cargo gen-syntax --verify | ||
15 | - cargo test --no-run # let's measure compile time separately | 13 | - cargo test --no-run # let's measure compile time separately |
16 | - cargo test | 14 | - cargo test |
17 | env: | 15 | env: |
diff --git a/crates/ra_hir/src/docs.rs b/crates/ra_hir/src/docs.rs index b1b47af9e..5db72c08a 100644 --- a/crates/ra_hir/src/docs.rs +++ b/crates/ra_hir/src/docs.rs | |||
@@ -27,10 +27,5 @@ pub trait Docs { | |||
27 | } | 27 | } |
28 | 28 | ||
29 | pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> { | 29 | pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> { |
30 | let comments = node.doc_comment_text(); | 30 | node.doc_comment_text().map(|it| Documentation::new(&it)) |
31 | if comments.is_empty() { | ||
32 | None | ||
33 | } else { | ||
34 | Some(Documentation::new(&comments)) | ||
35 | } | ||
36 | } | 31 | } |
diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index f203a6bf1..ee1e13799 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs | |||
@@ -126,8 +126,7 @@ impl CallInfo { | |||
126 | }; | 126 | }; |
127 | 127 | ||
128 | let mut doc = None; | 128 | let mut doc = None; |
129 | let docs = node.doc_comment_text(); | 129 | if let Some(docs) = node.doc_comment_text() { |
130 | if !docs.is_empty() { | ||
131 | // Massage markdown | 130 | // Massage markdown |
132 | let mut processed_lines = Vec::new(); | 131 | let mut processed_lines = Vec::new(); |
133 | let mut in_code_block = false; | 132 | let mut in_code_block = false; |
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index ff9ae2d9c..f993a461c 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs | |||
@@ -100,12 +100,7 @@ impl NavigationTarget { | |||
100 | fn docs(&self, db: &RootDatabase) -> Option<String> { | 100 | fn docs(&self, db: &RootDatabase) -> Option<String> { |
101 | let node = self.node(db)?; | 101 | let node = self.node(db)?; |
102 | fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> { | 102 | fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> { |
103 | let comments = node.doc_comment_text(); | 103 | node.doc_comment_text() |
104 | if comments.is_empty() { | ||
105 | None | ||
106 | } else { | ||
107 | Some(comments) | ||
108 | } | ||
109 | } | 104 | } |
110 | 105 | ||
111 | visitor() | 106 | visitor() |
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index ab3dd1b84..3d22a88f3 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -115,21 +115,38 @@ pub trait DocCommentsOwner: AstNode { | |||
115 | } | 115 | } |
116 | 116 | ||
117 | /// Returns the textual content of a doc comment block as a single string. | 117 | /// Returns the textual content of a doc comment block as a single string. |
118 | /// That is, strips leading `///` and joins lines | 118 | /// That is, strips leading `///` (+ optional 1 character of whitespace) |
119 | fn doc_comment_text(&self) -> std::string::String { | 119 | /// and joins lines. |
120 | self.doc_comments() | 120 | fn doc_comment_text(&self) -> Option<std::string::String> { |
121 | let docs = self | ||
122 | .doc_comments() | ||
121 | .filter(|comment| comment.is_doc_comment()) | 123 | .filter(|comment| comment.is_doc_comment()) |
122 | .map(|comment| { | 124 | .map(|comment| { |
123 | let prefix = comment.prefix(); | 125 | let prefix_len = comment.prefix().len(); |
124 | let trimmed = comment | 126 | |
125 | .text() | 127 | let line = comment.text().as_str(); |
126 | .as_str() | 128 | |
127 | .trim() | 129 | // Determine if the prefix or prefix + 1 char is stripped |
128 | .trim_start_matches(prefix) | 130 | let pos = if line |
129 | .trim_start(); | 131 | .chars() |
130 | trimmed.to_owned() | 132 | .nth(prefix_len) |
133 | .map(|c| c.is_whitespace()) | ||
134 | .unwrap_or(false) | ||
135 | { | ||
136 | prefix_len + 1 | ||
137 | } else { | ||
138 | prefix_len | ||
139 | }; | ||
140 | |||
141 | line[pos..].to_owned() | ||
131 | }) | 142 | }) |
132 | .join("\n") | 143 | .join("\n"); |
144 | |||
145 | if docs.is_empty() { | ||
146 | None | ||
147 | } else { | ||
148 | Some(docs) | ||
149 | } | ||
133 | } | 150 | } |
134 | } | 151 | } |
135 | 152 | ||
@@ -704,6 +721,18 @@ impl BindPat { | |||
704 | } | 721 | } |
705 | 722 | ||
706 | #[test] | 723 | #[test] |
724 | fn test_doc_comment_none() { | ||
725 | let file = SourceFile::parse( | ||
726 | r#" | ||
727 | // non-doc | ||
728 | mod foo {} | ||
729 | "#, | ||
730 | ); | ||
731 | let module = file.syntax().descendants().find_map(Module::cast).unwrap(); | ||
732 | assert!(module.doc_comment_text().is_none()); | ||
733 | } | ||
734 | |||
735 | #[test] | ||
707 | fn test_doc_comment_of_items() { | 736 | fn test_doc_comment_of_items() { |
708 | let file = SourceFile::parse( | 737 | let file = SourceFile::parse( |
709 | r#" | 738 | r#" |
@@ -713,5 +742,25 @@ fn test_doc_comment_of_items() { | |||
713 | "#, | 742 | "#, |
714 | ); | 743 | ); |
715 | let module = file.syntax().descendants().find_map(Module::cast).unwrap(); | 744 | let module = file.syntax().descendants().find_map(Module::cast).unwrap(); |
716 | assert_eq!("doc", module.doc_comment_text()); | 745 | assert_eq!("doc", module.doc_comment_text().unwrap()); |
746 | } | ||
747 | |||
748 | #[test] | ||
749 | fn test_doc_comment_preserves_indents() { | ||
750 | let file = SourceFile::parse( | ||
751 | r#" | ||
752 | /// doc1 | ||
753 | /// ``` | ||
754 | /// fn foo() { | ||
755 | /// // ... | ||
756 | /// } | ||
757 | /// ``` | ||
758 | mod foo {} | ||
759 | "#, | ||
760 | ); | ||
761 | let module = file.syntax().descendants().find_map(Module::cast).unwrap(); | ||
762 | assert_eq!( | ||
763 | "doc1\n```\nfn foo() {\n // ...\n}\n```", | ||
764 | module.doc_comment_text().unwrap() | ||
765 | ); | ||
717 | } | 766 | } |
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 4f8723ae7..3ace6533c 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -660,50 +660,6 @@ impl ToOwned for DynTraitType { | |||
660 | 660 | ||
661 | impl DynTraitType {} | 661 | impl DynTraitType {} |
662 | 662 | ||
663 | // ElseBranch | ||
664 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
665 | #[repr(transparent)] | ||
666 | pub struct ElseBranch { | ||
667 | pub(crate) syntax: SyntaxNode, | ||
668 | } | ||
669 | unsafe impl TransparentNewType for ElseBranch { | ||
670 | type Repr = rowan::SyntaxNode<RaTypes>; | ||
671 | } | ||
672 | |||
673 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
674 | pub enum ElseBranchKind<'a> { | ||
675 | Block(&'a Block), | ||
676 | IfExpr(&'a IfExpr), | ||
677 | } | ||
678 | |||
679 | impl AstNode for ElseBranch { | ||
680 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | ||
681 | match syntax.kind() { | ||
682 | | BLOCK | ||
683 | | IF_EXPR => Some(ElseBranch::from_repr(syntax.into_repr())), | ||
684 | _ => None, | ||
685 | } | ||
686 | } | ||
687 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
688 | } | ||
689 | |||
690 | impl ToOwned for ElseBranch { | ||
691 | type Owned = TreeArc<ElseBranch>; | ||
692 | fn to_owned(&self) -> TreeArc<ElseBranch> { TreeArc::cast(self.syntax.to_owned()) } | ||
693 | } | ||
694 | |||
695 | impl ElseBranch { | ||
696 | pub fn kind(&self) -> ElseBranchKind { | ||
697 | match self.syntax.kind() { | ||
698 | BLOCK => ElseBranchKind::Block(Block::cast(&self.syntax).unwrap()), | ||
699 | IF_EXPR => ElseBranchKind::IfExpr(IfExpr::cast(&self.syntax).unwrap()), | ||
700 | _ => unreachable!(), | ||
701 | } | ||
702 | } | ||
703 | } | ||
704 | |||
705 | impl ElseBranch {} | ||
706 | |||
707 | // EnumDef | 663 | // EnumDef |
708 | #[derive(Debug, PartialEq, Eq, Hash)] | 664 | #[derive(Debug, PartialEq, Eq, Hash)] |
709 | #[repr(transparent)] | 665 | #[repr(transparent)] |
diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 1604d9b5a..d27eb8b7e 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs | |||
@@ -45,7 +45,6 @@ pub(crate) fn block(p: &mut Parser) { | |||
45 | 45 | ||
46 | while !p.at(EOF) && !p.at(R_CURLY) { | 46 | while !p.at(EOF) && !p.at(R_CURLY) { |
47 | match p.current() { | 47 | match p.current() { |
48 | LET_KW => let_stmt(p), | ||
49 | // test nocontentexpr | 48 | // test nocontentexpr |
50 | // fn foo(){ | 49 | // fn foo(){ |
51 | // ;;;some_expr();;;;{;;;};;;;Ok(()) | 50 | // ;;;some_expr();;;;{;;;};;;;Ok(()) |
@@ -55,41 +54,54 @@ pub(crate) fn block(p: &mut Parser) { | |||
55 | // test block_items | 54 | // test block_items |
56 | // fn a() { fn b() {} } | 55 | // fn a() { fn b() {} } |
57 | let m = p.start(); | 56 | let m = p.start(); |
58 | match items::maybe_item(p, items::ItemFlavor::Mod) { | 57 | let has_attrs = p.at(POUND); |
59 | items::MaybeItem::Item(kind) => { | 58 | attributes::outer_attributes(p); |
60 | m.complete(p, kind); | 59 | if p.at(LET_KW) { |
61 | } | 60 | let_stmt(p, m); |
62 | items::MaybeItem::Modifiers => { | 61 | } else { |
63 | m.abandon(p); | 62 | match items::maybe_item(p, items::ItemFlavor::Mod) { |
64 | p.error("expected an item"); | 63 | items::MaybeItem::Item(kind) => { |
65 | } | 64 | m.complete(p, kind); |
66 | // test pub_expr | 65 | } |
67 | // fn foo() { pub 92; } //FIXME | 66 | items::MaybeItem::Modifiers => { |
68 | items::MaybeItem::None => { | ||
69 | let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; | ||
70 | if p.at(R_CURLY) { | ||
71 | m.abandon(p); | 67 | m.abandon(p); |
72 | } else { | 68 | p.error("expected an item"); |
73 | // test no_semi_after_block | 69 | } |
74 | // fn foo() { | 70 | // test pub_expr |
75 | // if true {} | 71 | // fn foo() { pub 92; } //FIXME |
76 | // loop {} | 72 | items::MaybeItem::None => { |
77 | // match () {} | 73 | if has_attrs { |
78 | // while true {} | 74 | m.abandon(p); |
79 | // for _ in () {} | 75 | p.error( |
80 | // {} | 76 | "expected a let statement or an item after attributes in block", |
81 | // {} | 77 | ); |
82 | // macro_rules! test { | ||
83 | // () => {} | ||
84 | // } | ||
85 | // test!{} | ||
86 | // } | ||
87 | if is_blocklike { | ||
88 | p.eat(SEMI); | ||
89 | } else { | 78 | } else { |
90 | p.expect(SEMI); | 79 | let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; |
80 | if p.at(R_CURLY) { | ||
81 | m.abandon(p); | ||
82 | } else { | ||
83 | // test no_semi_after_block | ||
84 | // fn foo() { | ||
85 | // if true {} | ||
86 | // loop {} | ||
87 | // match () {} | ||
88 | // while true {} | ||
89 | // for _ in () {} | ||
90 | // {} | ||
91 | // {} | ||
92 | // macro_rules! test { | ||
93 | // () => {} | ||
94 | // } | ||
95 | // test!{} | ||
96 | // } | ||
97 | if is_blocklike { | ||
98 | p.eat(SEMI); | ||
99 | } else { | ||
100 | p.expect(SEMI); | ||
101 | } | ||
102 | m.complete(p, EXPR_STMT); | ||
103 | } | ||
91 | } | 104 | } |
92 | m.complete(p, EXPR_STMT); | ||
93 | } | 105 | } |
94 | } | 106 | } |
95 | } | 107 | } |
@@ -106,9 +118,8 @@ pub(crate) fn block(p: &mut Parser) { | |||
106 | // let c = 92; | 118 | // let c = 92; |
107 | // let d: i32 = 92; | 119 | // let d: i32 = 92; |
108 | // } | 120 | // } |
109 | fn let_stmt(p: &mut Parser) { | 121 | fn let_stmt(p: &mut Parser, m: Marker) { |
110 | assert!(p.at(LET_KW)); | 122 | assert!(p.at(LET_KW)); |
111 | let m = p.start(); | ||
112 | p.bump(); | 123 | p.bump(); |
113 | patterns::pattern(p); | 124 | patterns::pattern(p); |
114 | if p.at(COLON) { | 125 | if p.at(COLON) { |
diff --git a/crates/ra_syntax/src/grammar/items.rs b/crates/ra_syntax/src/grammar/items.rs index 265e84570..18039cd3f 100644 --- a/crates/ra_syntax/src/grammar/items.rs +++ b/crates/ra_syntax/src/grammar/items.rs | |||
@@ -36,6 +36,7 @@ pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![ | |||
36 | 36 | ||
37 | pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { | 37 | pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { |
38 | let m = p.start(); | 38 | let m = p.start(); |
39 | attributes::outer_attributes(p); | ||
39 | match maybe_item(p, flavor) { | 40 | match maybe_item(p, flavor) { |
40 | MaybeItem::Item(kind) => { | 41 | MaybeItem::Item(kind) => { |
41 | m.complete(p, kind); | 42 | m.complete(p, kind); |
@@ -79,7 +80,6 @@ pub(super) enum MaybeItem { | |||
79 | } | 80 | } |
80 | 81 | ||
81 | pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { | 82 | pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { |
82 | attributes::outer_attributes(p); | ||
83 | opt_visibility(p); | 83 | opt_visibility(p); |
84 | if let Some(kind) = items_without_modifiers(p) { | 84 | if let Some(kind) = items_without_modifiers(p) { |
85 | return MaybeItem::Item(kind); | 85 | return MaybeItem::Item(kind); |
diff --git a/crates/ra_syntax/tests/data/parser/ok/0044_let_attrs.rs b/crates/ra_syntax/tests/data/parser/ok/0044_let_attrs.rs new file mode 100644 index 000000000..325a97aeb --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/ok/0044_let_attrs.rs | |||
@@ -0,0 +1,5 @@ | |||
1 | // https://github.com/rust-analyzer/rust-analyzer/issues/677 | ||
2 | fn main() { | ||
3 | #[cfg(feature = "backtrace")] | ||
4 | let exit_code = panic::catch_unwind(move || main()); | ||
5 | } | ||
diff --git a/crates/ra_syntax/tests/data/parser/ok/0044_let_attrs.txt b/crates/ra_syntax/tests/data/parser/ok/0044_let_attrs.txt new file mode 100644 index 000000000..1f52f699b --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/ok/0044_let_attrs.txt | |||
@@ -0,0 +1,73 @@ | |||
1 | SOURCE_FILE@[0; 166) | ||
2 | FN_DEF@[0; 165) | ||
3 | COMMENT@[0; 60) | ||
4 | WHITESPACE@[60; 61) | ||
5 | FN_KW@[61; 63) | ||
6 | WHITESPACE@[63; 64) | ||
7 | NAME@[64; 68) | ||
8 | IDENT@[64; 68) "main" | ||
9 | PARAM_LIST@[68; 70) | ||
10 | L_PAREN@[68; 69) | ||
11 | R_PAREN@[69; 70) | ||
12 | WHITESPACE@[70; 71) | ||
13 | BLOCK@[71; 165) | ||
14 | L_CURLY@[71; 72) | ||
15 | WHITESPACE@[72; 77) | ||
16 | LET_STMT@[77; 163) | ||
17 | ATTR@[77; 106) | ||
18 | POUND@[77; 78) | ||
19 | TOKEN_TREE@[78; 106) | ||
20 | L_BRACK@[78; 79) | ||
21 | IDENT@[79; 82) "cfg" | ||
22 | TOKEN_TREE@[82; 105) | ||
23 | L_PAREN@[82; 83) | ||
24 | IDENT@[83; 90) "feature" | ||
25 | WHITESPACE@[90; 91) | ||
26 | EQ@[91; 92) | ||
27 | WHITESPACE@[92; 93) | ||
28 | STRING@[93; 104) | ||
29 | R_PAREN@[104; 105) | ||
30 | R_BRACK@[105; 106) | ||
31 | WHITESPACE@[106; 111) | ||
32 | LET_KW@[111; 114) | ||
33 | WHITESPACE@[114; 115) | ||
34 | BIND_PAT@[115; 124) | ||
35 | NAME@[115; 124) | ||
36 | IDENT@[115; 124) "exit_code" | ||
37 | WHITESPACE@[124; 125) | ||
38 | EQ@[125; 126) | ||
39 | WHITESPACE@[126; 127) | ||
40 | CALL_EXPR@[127; 162) | ||
41 | PATH_EXPR@[127; 146) | ||
42 | PATH@[127; 146) | ||
43 | PATH@[127; 132) | ||
44 | PATH_SEGMENT@[127; 132) | ||
45 | NAME_REF@[127; 132) | ||
46 | IDENT@[127; 132) "panic" | ||
47 | COLONCOLON@[132; 134) | ||
48 | PATH_SEGMENT@[134; 146) | ||
49 | NAME_REF@[134; 146) | ||
50 | IDENT@[134; 146) "catch_unwind" | ||
51 | ARG_LIST@[146; 162) | ||
52 | L_PAREN@[146; 147) | ||
53 | LAMBDA_EXPR@[147; 161) | ||
54 | MOVE_KW@[147; 151) | ||
55 | WHITESPACE@[151; 152) | ||
56 | PARAM_LIST@[152; 154) | ||
57 | PIPE@[152; 153) | ||
58 | PIPE@[153; 154) | ||
59 | WHITESPACE@[154; 155) | ||
60 | CALL_EXPR@[155; 161) | ||
61 | PATH_EXPR@[155; 159) | ||
62 | PATH@[155; 159) | ||
63 | PATH_SEGMENT@[155; 159) | ||
64 | NAME_REF@[155; 159) | ||
65 | IDENT@[155; 159) "main" | ||
66 | ARG_LIST@[159; 161) | ||
67 | L_PAREN@[159; 160) | ||
68 | R_PAREN@[160; 161) | ||
69 | R_PAREN@[161; 162) | ||
70 | SEMI@[162; 163) | ||
71 | WHITESPACE@[163; 164) | ||
72 | R_CURLY@[164; 165) | ||
73 | WHITESPACE@[165; 166) | ||
diff --git a/crates/tools/src/bin/pre-commit.rs b/crates/tools/src/bin/pre-commit.rs index bae3b26d3..e00bd0d3d 100644 --- a/crates/tools/src/bin/pre-commit.rs +++ b/crates/tools/src/bin/pre-commit.rs | |||
@@ -1,10 +1,9 @@ | |||
1 | use std::{ | 1 | use std::process::Command; |
2 | process::{Command}, | ||
3 | }; | ||
4 | 2 | ||
5 | use tools::{Result, run_rustfmt, run, project_root}; | ||
6 | use failure::bail; | 3 | use failure::bail; |
7 | 4 | ||
5 | use tools::{Result, run_rustfmt, run, project_root}; | ||
6 | |||
8 | fn main() -> tools::Result<()> { | 7 | fn main() -> tools::Result<()> { |
9 | run_rustfmt(tools::Overwrite)?; | 8 | run_rustfmt(tools::Overwrite)?; |
10 | update_staged() | 9 | update_staged() |
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index d404db214..311bcb4d8 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs | |||
@@ -1,7 +1,8 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fs, | ||
3 | collections::HashMap, | ||
2 | path::{Path, PathBuf}, | 4 | path::{Path, PathBuf}, |
3 | process::{Command, Stdio}, | 5 | process::{Command, Stdio}, |
4 | fs::copy, | ||
5 | io::{Error, ErrorKind} | 6 | io::{Error, ErrorKind} |
6 | }; | 7 | }; |
7 | 8 | ||
@@ -13,6 +14,10 @@ pub use teraron::{Mode, Overwrite, Verify}; | |||
13 | pub type Result<T> = std::result::Result<T, failure::Error>; | 14 | pub type Result<T> = std::result::Result<T, failure::Error>; |
14 | 15 | ||
15 | pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; | 16 | pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; |
17 | const GRAMMAR_DIR: &str = "crates/ra_syntax/src/grammar"; | ||
18 | const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/ok"; | ||
19 | const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/err"; | ||
20 | |||
16 | pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera"; | 21 | pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera"; |
17 | pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera"; | 22 | pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera"; |
18 | const TOOLCHAIN: &str = "stable"; | 23 | const TOOLCHAIN: &str = "stable"; |
@@ -130,9 +135,9 @@ pub fn install_format_hook() -> Result<()> { | |||
130 | if !result_path.exists() { | 135 | if !result_path.exists() { |
131 | run("cargo build --package tools --bin pre-commit", ".")?; | 136 | run("cargo build --package tools --bin pre-commit", ".")?; |
132 | if cfg!(windows) { | 137 | if cfg!(windows) { |
133 | copy("./target/debug/pre-commit.exe", result_path)?; | 138 | fs::copy("./target/debug/pre-commit.exe", result_path)?; |
134 | } else { | 139 | } else { |
135 | copy("./target/debug/pre-commit", result_path)?; | 140 | fs::copy("./target/debug/pre-commit", result_path)?; |
136 | } | 141 | } |
137 | } else { | 142 | } else { |
138 | return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into()); | 143 | return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into()); |
@@ -156,3 +161,98 @@ pub fn run_fuzzer() -> Result<()> { | |||
156 | "./crates/ra_syntax", | 161 | "./crates/ra_syntax", |
157 | ) | 162 | ) |
158 | } | 163 | } |
164 | |||
165 | pub fn gen_tests(mode: Mode) -> Result<()> { | ||
166 | let tests = tests_from_dir(&project_root().join(Path::new(GRAMMAR_DIR)))?; | ||
167 | fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> { | ||
168 | let tests_dir = project_root().join(into); | ||
169 | if !tests_dir.is_dir() { | ||
170 | fs::create_dir_all(&tests_dir)?; | ||
171 | } | ||
172 | // ok is never actually read, but it needs to be specified to create a Test in existing_tests | ||
173 | let existing = existing_tests(&tests_dir, true)?; | ||
174 | for t in existing.keys().filter(|&t| !tests.contains_key(t)) { | ||
175 | panic!("Test is deleted: {}", t); | ||
176 | } | ||
177 | |||
178 | let mut new_idx = existing.len() + 1; | ||
179 | for (name, test) in tests { | ||
180 | let path = match existing.get(name) { | ||
181 | Some((path, _test)) => path.clone(), | ||
182 | None => { | ||
183 | let file_name = format!("{:04}_{}.rs", new_idx, name); | ||
184 | new_idx += 1; | ||
185 | tests_dir.join(file_name) | ||
186 | } | ||
187 | }; | ||
188 | teraron::update(&path, &test.text, mode)?; | ||
189 | } | ||
190 | Ok(()) | ||
191 | } | ||
192 | install_tests(&tests.ok, OK_INLINE_TESTS_DIR, mode)?; | ||
193 | install_tests(&tests.err, ERR_INLINE_TESTS_DIR, mode) | ||
194 | } | ||
195 | |||
196 | #[derive(Default, Debug)] | ||
197 | struct Tests { | ||
198 | pub ok: HashMap<String, Test>, | ||
199 | pub err: HashMap<String, Test>, | ||
200 | } | ||
201 | |||
202 | fn tests_from_dir(dir: &Path) -> Result<Tests> { | ||
203 | let mut res = Tests::default(); | ||
204 | for entry in ::walkdir::WalkDir::new(dir) { | ||
205 | let entry = entry.unwrap(); | ||
206 | if !entry.file_type().is_file() { | ||
207 | continue; | ||
208 | } | ||
209 | if entry.path().extension().unwrap_or_default() != "rs" { | ||
210 | continue; | ||
211 | } | ||
212 | process_file(&mut res, entry.path())?; | ||
213 | } | ||
214 | let grammar_rs = dir.parent().unwrap().join("grammar.rs"); | ||
215 | process_file(&mut res, &grammar_rs)?; | ||
216 | return Ok(res); | ||
217 | fn process_file(res: &mut Tests, path: &Path) -> Result<()> { | ||
218 | let text = fs::read_to_string(path)?; | ||
219 | |||
220 | for (_, test) in collect_tests(&text) { | ||
221 | if test.ok { | ||
222 | if let Some(old_test) = res.ok.insert(test.name.clone(), test) { | ||
223 | bail!("Duplicate test: {}", old_test.name) | ||
224 | } | ||
225 | } else { | ||
226 | if let Some(old_test) = res.err.insert(test.name.clone(), test) { | ||
227 | bail!("Duplicate test: {}", old_test.name) | ||
228 | } | ||
229 | } | ||
230 | } | ||
231 | Ok(()) | ||
232 | } | ||
233 | } | ||
234 | |||
235 | fn existing_tests(dir: &Path, ok: bool) -> Result<HashMap<String, (PathBuf, Test)>> { | ||
236 | let mut res = HashMap::new(); | ||
237 | for file in fs::read_dir(dir)? { | ||
238 | let file = file?; | ||
239 | let path = file.path(); | ||
240 | if path.extension().unwrap_or_default() != "rs" { | ||
241 | continue; | ||
242 | } | ||
243 | let name = { | ||
244 | let file_name = path.file_name().unwrap().to_str().unwrap(); | ||
245 | file_name[5..file_name.len() - 3].to_string() | ||
246 | }; | ||
247 | let text = fs::read_to_string(&path)?; | ||
248 | let test = Test { | ||
249 | name: name.clone(), | ||
250 | text, | ||
251 | ok, | ||
252 | }; | ||
253 | if let Some(old) = res.insert(name, (path, test)) { | ||
254 | println!("Duplicate test: {:?}", old); | ||
255 | } | ||
256 | } | ||
257 | Ok(res) | ||
258 | } | ||
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index d6eabce6c..c3e293911 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs | |||
@@ -1,30 +1,13 @@ | |||
1 | use std::{ | 1 | use clap::{App, SubCommand}; |
2 | collections::HashMap, | ||
3 | fs, | ||
4 | path::{Path, PathBuf}, | ||
5 | }; | ||
6 | |||
7 | use clap::{App, Arg, SubCommand}; | ||
8 | use failure::bail; | ||
9 | 2 | ||
10 | use tools::{ | 3 | use tools::{ |
11 | collect_tests, generate,install_format_hook, run, run_rustfmt, | 4 | generate, gen_tests, install_format_hook, run, run_rustfmt, |
12 | Mode, Overwrite, Result, Test, Verify, project_root, run_fuzzer | 5 | Overwrite, Result, run_fuzzer, |
13 | }; | 6 | }; |
14 | 7 | ||
15 | const GRAMMAR_DIR: &str = "crates/ra_syntax/src/grammar"; | ||
16 | const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/ok"; | ||
17 | const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/err"; | ||
18 | |||
19 | fn main() -> Result<()> { | 8 | fn main() -> Result<()> { |
20 | let matches = App::new("tasks") | 9 | let matches = App::new("tasks") |
21 | .setting(clap::AppSettings::SubcommandRequiredElseHelp) | 10 | .setting(clap::AppSettings::SubcommandRequiredElseHelp) |
22 | .arg( | ||
23 | Arg::with_name("verify") | ||
24 | .long("--verify") | ||
25 | .help("Verify that generated code is up-to-date") | ||
26 | .global(true), | ||
27 | ) | ||
28 | .subcommand(SubCommand::with_name("gen-syntax")) | 11 | .subcommand(SubCommand::with_name("gen-syntax")) |
29 | .subcommand(SubCommand::with_name("gen-tests")) | 12 | .subcommand(SubCommand::with_name("gen-tests")) |
30 | .subcommand(SubCommand::with_name("install-code")) | 13 | .subcommand(SubCommand::with_name("install-code")) |
@@ -32,19 +15,14 @@ fn main() -> Result<()> { | |||
32 | .subcommand(SubCommand::with_name("format-hook")) | 15 | .subcommand(SubCommand::with_name("format-hook")) |
33 | .subcommand(SubCommand::with_name("fuzz-tests")) | 16 | .subcommand(SubCommand::with_name("fuzz-tests")) |
34 | .get_matches(); | 17 | .get_matches(); |
35 | let mode = if matches.is_present("verify") { | ||
36 | Verify | ||
37 | } else { | ||
38 | Overwrite | ||
39 | }; | ||
40 | match matches | 18 | match matches |
41 | .subcommand_name() | 19 | .subcommand_name() |
42 | .expect("Subcommand must be specified") | 20 | .expect("Subcommand must be specified") |
43 | { | 21 | { |
44 | "install-code" => install_code_extension()?, | 22 | "install-code" => install_code_extension()?, |
45 | "gen-tests" => gen_tests(mode)?, | 23 | "gen-tests" => gen_tests(Overwrite)?, |
46 | "gen-syntax" => generate(Overwrite)?, | 24 | "gen-syntax" => generate(Overwrite)?, |
47 | "format" => run_rustfmt(mode)?, | 25 | "format" => run_rustfmt(Overwrite)?, |
48 | "format-hook" => install_format_hook()?, | 26 | "format-hook" => install_format_hook()?, |
49 | "fuzz-tests" => run_fuzzer()?, | 27 | "fuzz-tests" => run_fuzzer()?, |
50 | _ => unreachable!(), | 28 | _ => unreachable!(), |
@@ -52,101 +30,6 @@ fn main() -> Result<()> { | |||
52 | Ok(()) | 30 | Ok(()) |
53 | } | 31 | } |
54 | 32 | ||
55 | fn gen_tests(mode: Mode) -> Result<()> { | ||
56 | let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?; | ||
57 | fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> { | ||
58 | let tests_dir = project_root().join(into); | ||
59 | if !tests_dir.is_dir() { | ||
60 | fs::create_dir_all(&tests_dir)?; | ||
61 | } | ||
62 | // ok is never actually read, but it needs to be specified to create a Test in existing_tests | ||
63 | let existing = existing_tests(&tests_dir, true)?; | ||
64 | for t in existing.keys().filter(|&t| !tests.contains_key(t)) { | ||
65 | panic!("Test is deleted: {}", t); | ||
66 | } | ||
67 | |||
68 | let mut new_idx = existing.len() + 1; | ||
69 | for (name, test) in tests { | ||
70 | let path = match existing.get(name) { | ||
71 | Some((path, _test)) => path.clone(), | ||
72 | None => { | ||
73 | let file_name = format!("{:04}_{}.rs", new_idx, name); | ||
74 | new_idx += 1; | ||
75 | tests_dir.join(file_name) | ||
76 | } | ||
77 | }; | ||
78 | teraron::update(&path, &test.text, mode)?; | ||
79 | } | ||
80 | Ok(()) | ||
81 | } | ||
82 | install_tests(&tests.ok, OK_INLINE_TESTS_DIR, mode)?; | ||
83 | install_tests(&tests.err, ERR_INLINE_TESTS_DIR, mode) | ||
84 | } | ||
85 | |||
86 | #[derive(Default, Debug)] | ||
87 | struct Tests { | ||
88 | pub ok: HashMap<String, Test>, | ||
89 | pub err: HashMap<String, Test>, | ||
90 | } | ||
91 | |||
92 | fn tests_from_dir(dir: &Path) -> Result<Tests> { | ||
93 | let mut res = Tests::default(); | ||
94 | for entry in ::walkdir::WalkDir::new(dir) { | ||
95 | let entry = entry.unwrap(); | ||
96 | if !entry.file_type().is_file() { | ||
97 | continue; | ||
98 | } | ||
99 | if entry.path().extension().unwrap_or_default() != "rs" { | ||
100 | continue; | ||
101 | } | ||
102 | process_file(&mut res, entry.path())?; | ||
103 | } | ||
104 | let grammar_rs = dir.parent().unwrap().join("grammar.rs"); | ||
105 | process_file(&mut res, &grammar_rs)?; | ||
106 | return Ok(res); | ||
107 | fn process_file(res: &mut Tests, path: &Path) -> Result<()> { | ||
108 | let text = fs::read_to_string(path)?; | ||
109 | |||
110 | for (_, test) in collect_tests(&text) { | ||
111 | if test.ok { | ||
112 | if let Some(old_test) = res.ok.insert(test.name.clone(), test) { | ||
113 | bail!("Duplicate test: {}", old_test.name) | ||
114 | } | ||
115 | } else { | ||
116 | if let Some(old_test) = res.err.insert(test.name.clone(), test) { | ||
117 | bail!("Duplicate test: {}", old_test.name) | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | Ok(()) | ||
122 | } | ||
123 | } | ||
124 | |||
125 | fn existing_tests(dir: &Path, ok: bool) -> Result<HashMap<String, (PathBuf, Test)>> { | ||
126 | let mut res = HashMap::new(); | ||
127 | for file in fs::read_dir(dir)? { | ||
128 | let file = file?; | ||
129 | let path = file.path(); | ||
130 | if path.extension().unwrap_or_default() != "rs" { | ||
131 | continue; | ||
132 | } | ||
133 | let name = { | ||
134 | let file_name = path.file_name().unwrap().to_str().unwrap(); | ||
135 | file_name[5..file_name.len() - 3].to_string() | ||
136 | }; | ||
137 | let text = fs::read_to_string(&path)?; | ||
138 | let test = Test { | ||
139 | name: name.clone(), | ||
140 | text, | ||
141 | ok, | ||
142 | }; | ||
143 | if let Some(old) = res.insert(name, (path, test)) { | ||
144 | println!("Duplicate test: {:?}", old); | ||
145 | } | ||
146 | } | ||
147 | Ok(res) | ||
148 | } | ||
149 | |||
150 | fn install_code_extension() -> Result<()> { | 33 | fn install_code_extension() -> Result<()> { |
151 | run("cargo install --path crates/ra_lsp_server --force", ".")?; | 34 | run("cargo install --path crates/ra_lsp_server --force", ".")?; |
152 | if cfg!(windows) { | 35 | if cfg!(windows) { |
diff --git a/crates/tools/tests/cli.rs b/crates/tools/tests/cli.rs index 2d238d9ea..2ee4b5223 100644 --- a/crates/tools/tests/cli.rs +++ b/crates/tools/tests/cli.rs | |||
@@ -1,15 +1,23 @@ | |||
1 | extern crate tools; | 1 | use tools::{generate, gen_tests, run_rustfmt, Verify}; |
2 | |||
3 | use tools::{generate, run_rustfmt, Verify}; | ||
4 | 2 | ||
5 | #[test] | 3 | #[test] |
6 | fn verify_template_generation() { | 4 | fn generated_grammar_is_fresh() { |
7 | if let Err(error) = generate(Verify) { | 5 | if let Err(error) = generate(Verify) { |
8 | panic!("{}. Please update it by running `cargo gen-syntax`", error); | 6 | panic!("{}. Please update it by running `cargo gen-syntax`", error); |
9 | } | 7 | } |
10 | } | 8 | } |
11 | 9 | ||
12 | #[test] | 10 | #[test] |
11 | fn generated_tests_are_fresh() { | ||
12 | if let Err(error) = gen_tests(Verify) { | ||
13 | panic!( | ||
14 | "{}. Please update tests by running `cargo gen-tests`", | ||
15 | error | ||
16 | ); | ||
17 | } | ||
18 | } | ||
19 | |||
20 | #[test] | ||
13 | fn check_code_formatting() { | 21 | fn check_code_formatting() { |
14 | if let Err(error) = run_rustfmt(Verify) { | 22 | if let Err(error) = run_rustfmt(Verify) { |
15 | panic!( | 23 | panic!( |