diff options
Diffstat (limited to 'crates/ra_syntax/src')
-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 |
4 files changed, 109 insertions, 93 deletions
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); |