diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-15 17:48:17 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-15 17:48:17 +0100 |
commit | a230b438e025c5878b8d17e11d3928cbad95bb28 (patch) | |
tree | 231872915ede46874b6e7d2b3d1240d1defa5231 /crates/ra_syntax/src/ast | |
parent | e031b65f93f73164a5729cf81ff60299708bc931 (diff) | |
parent | 2bc9e9f32711047b06940c335eb5327281f8c555 (diff) |
Merge #127
127: Improve folding r=matklad a=aochagavia
I was messing around with adding support for multiline comments in folding and ended up changing a bunch of other things.
First of all, I am not convinced of folding groups of successive items. For instance, I don't see why it is worthwhile to be able to fold something like the following:
```rust
use foo;
use bar;
```
Furthermore, this causes problems if you want to fold a multiline import:
```rust
use foo::{
quux
};
use bar;
```
The problem is that now there are two possible folds at the same position: we could fold the first use or we could fold the import group. IMO, the only place where folding groups makes sense is when folding comments. Therefore I have **removed folding import groups in favor of folding multiline imports**.
Regarding folding comments, I made it a bit more robust by requiring that comments can only be folded if they have the same flavor. So if you have a bunch of `//` comments followed by `//!` comments, you will get two separate fold groups instead of a single one.
Finally, I rewrote the API in such a way that it should be trivial to add new folds. You only need to:
* Create a new FoldKind
* Add it to the `fold_kind` function that converts from `SyntaxKind` to `FoldKind`
Fixes #113
Co-authored-by: Adolfo OchagavĂa <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 18 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/mod.rs | 28 |
2 files changed, 43 insertions, 3 deletions
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 48c9038dc..4db1bcbf9 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -2197,3 +2197,21 @@ impl<'a> WhileExpr<'a> { | |||
2197 | } | 2197 | } |
2198 | } | 2198 | } |
2199 | 2199 | ||
2200 | // Whitespace | ||
2201 | #[derive(Debug, Clone, Copy)] | ||
2202 | pub struct Whitespace<'a> { | ||
2203 | syntax: SyntaxNodeRef<'a>, | ||
2204 | } | ||
2205 | |||
2206 | impl<'a> AstNode<'a> for Whitespace<'a> { | ||
2207 | fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { | ||
2208 | match syntax.kind() { | ||
2209 | WHITESPACE => Some(Whitespace { syntax }), | ||
2210 | _ => None, | ||
2211 | } | ||
2212 | } | ||
2213 | fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } | ||
2214 | } | ||
2215 | |||
2216 | impl<'a> Whitespace<'a> {} | ||
2217 | |||
diff --git a/crates/ra_syntax/src/ast/mod.rs b/crates/ra_syntax/src/ast/mod.rs index 10dac72e5..12ddc0210 100644 --- a/crates/ra_syntax/src/ast/mod.rs +++ b/crates/ra_syntax/src/ast/mod.rs | |||
@@ -100,8 +100,8 @@ impl<'a> Lifetime<'a> { | |||
100 | } | 100 | } |
101 | 101 | ||
102 | impl<'a> Comment<'a> { | 102 | impl<'a> Comment<'a> { |
103 | pub fn text(&self) -> SmolStr { | 103 | pub fn text(&self) -> &SmolStr { |
104 | self.syntax().leaf_text().unwrap().clone() | 104 | self.syntax().leaf_text().unwrap() |
105 | } | 105 | } |
106 | 106 | ||
107 | pub fn flavor(&self) -> CommentFlavor { | 107 | pub fn flavor(&self) -> CommentFlavor { |
@@ -120,9 +120,17 @@ impl<'a> Comment<'a> { | |||
120 | pub fn prefix(&self) -> &'static str { | 120 | pub fn prefix(&self) -> &'static str { |
121 | self.flavor().prefix() | 121 | self.flavor().prefix() |
122 | } | 122 | } |
123 | |||
124 | pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> { | ||
125 | self.text().chars().filter(|&c| c == '\n').map(|_| &()) | ||
126 | } | ||
127 | |||
128 | pub fn has_newlines(&self) -> bool { | ||
129 | self.count_newlines_lazy().count() > 0 | ||
130 | } | ||
123 | } | 131 | } |
124 | 132 | ||
125 | #[derive(Debug)] | 133 | #[derive(Debug, PartialEq, Eq)] |
126 | pub enum CommentFlavor { | 134 | pub enum CommentFlavor { |
127 | Line, | 135 | Line, |
128 | Doc, | 136 | Doc, |
@@ -142,6 +150,20 @@ impl CommentFlavor { | |||
142 | } | 150 | } |
143 | } | 151 | } |
144 | 152 | ||
153 | impl<'a> Whitespace<'a> { | ||
154 | pub fn text(&self) -> &SmolStr { | ||
155 | &self.syntax().leaf_text().unwrap() | ||
156 | } | ||
157 | |||
158 | pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> { | ||
159 | self.text().chars().filter(|&c| c == '\n').map(|_| &()) | ||
160 | } | ||
161 | |||
162 | pub fn has_newlines(&self) -> bool { | ||
163 | self.count_newlines_lazy().count() > 0 | ||
164 | } | ||
165 | } | ||
166 | |||
145 | impl<'a> Name<'a> { | 167 | impl<'a> Name<'a> { |
146 | pub fn text(&self) -> SmolStr { | 168 | pub fn text(&self) -> SmolStr { |
147 | let ident = self.syntax().first_child() | 169 | let ident = self.syntax().first_child() |