diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-11 11:58:55 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-11 11:58:55 +0000 |
commit | bddf6b5266997dd7e017fcb963e54a86b68afbaa (patch) | |
tree | e47b310e3abe307772be9a47ebb4801866ff94ba /crates | |
parent | c48dcf74118b6e0df747f036a9b66701037f3fc7 (diff) | |
parent | 85c30b1915314487515f69f32dfe5e1a7301ab95 (diff) |
Merge #3557
3557: Continue multiline non-doc comment blocks r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide/src/typing.rs | 160 | ||||
-rw-r--r-- | crates/ra_ide/src/typing/on_enter.rs | 216 |
2 files changed, 222 insertions, 154 deletions
diff --git a/crates/ra_ide/src/typing.rs b/crates/ra_ide/src/typing.rs index 7f1b9150f..53c65f8bc 100644 --- a/crates/ra_ide/src/typing.rs +++ b/crates/ra_ide/src/typing.rs | |||
@@ -13,77 +13,21 @@ | |||
13 | //! Language server executes such typing assists synchronously. That is, they | 13 | //! Language server executes such typing assists synchronously. That is, they |
14 | //! block user's typing and should be pretty fast for this reason! | 14 | //! block user's typing and should be pretty fast for this reason! |
15 | 15 | ||
16 | mod on_enter; | ||
17 | |||
16 | use ra_db::{FilePosition, SourceDatabase}; | 18 | use ra_db::{FilePosition, SourceDatabase}; |
17 | use ra_fmt::leading_indent; | 19 | use ra_fmt::leading_indent; |
18 | use ra_ide_db::RootDatabase; | 20 | use ra_ide_db::RootDatabase; |
19 | use ra_syntax::{ | 21 | use ra_syntax::{ |
20 | algo::find_node_at_offset, | 22 | algo::find_node_at_offset, |
21 | ast::{self, AstToken}, | 23 | ast::{self, AstToken}, |
22 | AstNode, SmolStr, SourceFile, | 24 | AstNode, SourceFile, TextRange, TextUnit, |
23 | SyntaxKind::*, | ||
24 | SyntaxToken, TextRange, TextUnit, TokenAtOffset, | ||
25 | }; | 25 | }; |
26 | use ra_text_edit::TextEdit; | 26 | use ra_text_edit::TextEdit; |
27 | 27 | ||
28 | use crate::{source_change::SingleFileChange, SourceChange, SourceFileEdit}; | 28 | use crate::{source_change::SingleFileChange, SourceChange}; |
29 | |||
30 | pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> { | ||
31 | let parse = db.parse(position.file_id); | ||
32 | let file = parse.tree(); | ||
33 | let comment = file | ||
34 | .syntax() | ||
35 | .token_at_offset(position.offset) | ||
36 | .left_biased() | ||
37 | .and_then(ast::Comment::cast)?; | ||
38 | |||
39 | if comment.kind().shape.is_block() { | ||
40 | return None; | ||
41 | } | ||
42 | |||
43 | let prefix = comment.prefix(); | ||
44 | let comment_range = comment.syntax().text_range(); | ||
45 | if position.offset < comment_range.start() + TextUnit::of_str(prefix) { | ||
46 | return None; | ||
47 | } | ||
48 | |||
49 | // Continuing non-doc line comments (like this one :) ) is annoying | ||
50 | if prefix == "//" && comment_range.end() == position.offset { | ||
51 | return None; | ||
52 | } | ||
53 | |||
54 | let indent = node_indent(&file, comment.syntax())?; | ||
55 | let inserted = format!("\n{}{} ", indent, prefix); | ||
56 | let cursor_position = position.offset + TextUnit::of_str(&inserted); | ||
57 | let edit = TextEdit::insert(position.offset, inserted); | ||
58 | 29 | ||
59 | Some( | 30 | pub(crate) use on_enter::on_enter; |
60 | SourceChange::source_file_edit( | ||
61 | "on enter", | ||
62 | SourceFileEdit { edit, file_id: position.file_id }, | ||
63 | ) | ||
64 | .with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }), | ||
65 | ) | ||
66 | } | ||
67 | |||
68 | fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> { | ||
69 | let ws = match file.syntax().token_at_offset(token.text_range().start()) { | ||
70 | TokenAtOffset::Between(l, r) => { | ||
71 | assert!(r == *token); | ||
72 | l | ||
73 | } | ||
74 | TokenAtOffset::Single(n) => { | ||
75 | assert!(n == *token); | ||
76 | return Some("".into()); | ||
77 | } | ||
78 | TokenAtOffset::None => unreachable!(), | ||
79 | }; | ||
80 | if ws.kind() != WHITESPACE { | ||
81 | return None; | ||
82 | } | ||
83 | let text = ws.text(); | ||
84 | let pos = text.rfind('\n').map(|it| it + 1).unwrap_or(0); | ||
85 | Some(text[pos..].into()) | ||
86 | } | ||
87 | 31 | ||
88 | pub(crate) const TRIGGER_CHARS: &str = ".=>"; | 32 | pub(crate) const TRIGGER_CHARS: &str = ".=>"; |
89 | 33 | ||
@@ -196,102 +140,10 @@ fn on_arrow_typed(file: &SourceFile, offset: TextUnit) -> Option<SingleFileChang | |||
196 | 140 | ||
197 | #[cfg(test)] | 141 | #[cfg(test)] |
198 | mod tests { | 142 | mod tests { |
199 | use test_utils::{add_cursor, assert_eq_text, extract_offset}; | 143 | use test_utils::{assert_eq_text, extract_offset}; |
200 | |||
201 | use crate::mock_analysis::single_file; | ||
202 | 144 | ||
203 | use super::*; | 145 | use super::*; |
204 | 146 | ||
205 | #[test] | ||
206 | fn test_on_enter() { | ||
207 | fn apply_on_enter(before: &str) -> Option<String> { | ||
208 | let (offset, before) = extract_offset(before); | ||
209 | let (analysis, file_id) = single_file(&before); | ||
210 | let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?; | ||
211 | |||
212 | assert_eq!(result.source_file_edits.len(), 1); | ||
213 | let actual = result.source_file_edits[0].edit.apply(&before); | ||
214 | let actual = add_cursor(&actual, result.cursor_position.unwrap().offset); | ||
215 | Some(actual) | ||
216 | } | ||
217 | |||
218 | fn do_check(before: &str, after: &str) { | ||
219 | let actual = apply_on_enter(before).unwrap(); | ||
220 | assert_eq_text!(after, &actual); | ||
221 | } | ||
222 | |||
223 | fn do_check_noop(text: &str) { | ||
224 | assert!(apply_on_enter(text).is_none()) | ||
225 | } | ||
226 | |||
227 | do_check( | ||
228 | r" | ||
229 | /// Some docs<|> | ||
230 | fn foo() { | ||
231 | } | ||
232 | ", | ||
233 | r" | ||
234 | /// Some docs | ||
235 | /// <|> | ||
236 | fn foo() { | ||
237 | } | ||
238 | ", | ||
239 | ); | ||
240 | do_check( | ||
241 | r" | ||
242 | impl S { | ||
243 | /// Some<|> docs. | ||
244 | fn foo() {} | ||
245 | } | ||
246 | ", | ||
247 | r" | ||
248 | impl S { | ||
249 | /// Some | ||
250 | /// <|> docs. | ||
251 | fn foo() {} | ||
252 | } | ||
253 | ", | ||
254 | ); | ||
255 | do_check( | ||
256 | r" | ||
257 | fn main() { | ||
258 | // Fix<|> me | ||
259 | let x = 1 + 1; | ||
260 | } | ||
261 | ", | ||
262 | r" | ||
263 | fn main() { | ||
264 | // Fix | ||
265 | // <|> me | ||
266 | let x = 1 + 1; | ||
267 | } | ||
268 | ", | ||
269 | ); | ||
270 | do_check( | ||
271 | r" | ||
272 | ///<|> Some docs | ||
273 | fn foo() { | ||
274 | } | ||
275 | ", | ||
276 | r" | ||
277 | /// | ||
278 | /// <|> Some docs | ||
279 | fn foo() { | ||
280 | } | ||
281 | ", | ||
282 | ); | ||
283 | do_check_noop( | ||
284 | r" | ||
285 | fn main() { | ||
286 | // Fix me<|> | ||
287 | let x = 1 + 1; | ||
288 | } | ||
289 | ", | ||
290 | ); | ||
291 | |||
292 | do_check_noop(r"<|>//! docz"); | ||
293 | } | ||
294 | |||
295 | fn do_type_char(char_typed: char, before: &str) -> Option<(String, SingleFileChange)> { | 147 | fn do_type_char(char_typed: char, before: &str) -> Option<(String, SingleFileChange)> { |
296 | let (offset, before) = extract_offset(before); | 148 | let (offset, before) = extract_offset(before); |
297 | let edit = TextEdit::insert(offset, char_typed.to_string()); | 149 | let edit = TextEdit::insert(offset, char_typed.to_string()); |
diff --git a/crates/ra_ide/src/typing/on_enter.rs b/crates/ra_ide/src/typing/on_enter.rs new file mode 100644 index 000000000..6bcf2d72b --- /dev/null +++ b/crates/ra_ide/src/typing/on_enter.rs | |||
@@ -0,0 +1,216 @@ | |||
1 | //! Handles the `Enter` key press. At the momently, this only continues | ||
2 | //! comments, but should handle indent some time in the future as well. | ||
3 | |||
4 | use ra_db::{FilePosition, SourceDatabase}; | ||
5 | use ra_ide_db::RootDatabase; | ||
6 | use ra_syntax::{ | ||
7 | ast::{self, AstToken}, | ||
8 | AstNode, SmolStr, SourceFile, | ||
9 | SyntaxKind::*, | ||
10 | SyntaxToken, TextUnit, TokenAtOffset, | ||
11 | }; | ||
12 | use ra_text_edit::TextEdit; | ||
13 | |||
14 | use crate::{SourceChange, SourceFileEdit}; | ||
15 | |||
16 | pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> { | ||
17 | let parse = db.parse(position.file_id); | ||
18 | let file = parse.tree(); | ||
19 | let comment = file | ||
20 | .syntax() | ||
21 | .token_at_offset(position.offset) | ||
22 | .left_biased() | ||
23 | .and_then(ast::Comment::cast)?; | ||
24 | |||
25 | if comment.kind().shape.is_block() { | ||
26 | return None; | ||
27 | } | ||
28 | |||
29 | let prefix = comment.prefix(); | ||
30 | let comment_range = comment.syntax().text_range(); | ||
31 | if position.offset < comment_range.start() + TextUnit::of_str(prefix) { | ||
32 | return None; | ||
33 | } | ||
34 | |||
35 | // Continuing single-line non-doc comments (like this one :) ) is annoying | ||
36 | if prefix == "//" && comment_range.end() == position.offset && !followed_by_comment(&comment) { | ||
37 | return None; | ||
38 | } | ||
39 | |||
40 | let indent = node_indent(&file, comment.syntax())?; | ||
41 | let inserted = format!("\n{}{} ", indent, prefix); | ||
42 | let cursor_position = position.offset + TextUnit::of_str(&inserted); | ||
43 | let edit = TextEdit::insert(position.offset, inserted); | ||
44 | |||
45 | Some( | ||
46 | SourceChange::source_file_edit( | ||
47 | "on enter", | ||
48 | SourceFileEdit { edit, file_id: position.file_id }, | ||
49 | ) | ||
50 | .with_cursor(FilePosition { offset: cursor_position, file_id: position.file_id }), | ||
51 | ) | ||
52 | } | ||
53 | |||
54 | fn followed_by_comment(comment: &ast::Comment) -> bool { | ||
55 | let ws = match comment.syntax().next_token().and_then(ast::Whitespace::cast) { | ||
56 | Some(it) => it, | ||
57 | None => return false, | ||
58 | }; | ||
59 | if ws.spans_multiple_lines() { | ||
60 | return false; | ||
61 | } | ||
62 | ws.syntax().next_token().and_then(ast::Comment::cast).is_some() | ||
63 | } | ||
64 | |||
65 | fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> { | ||
66 | let ws = match file.syntax().token_at_offset(token.text_range().start()) { | ||
67 | TokenAtOffset::Between(l, r) => { | ||
68 | assert!(r == *token); | ||
69 | l | ||
70 | } | ||
71 | TokenAtOffset::Single(n) => { | ||
72 | assert!(n == *token); | ||
73 | return Some("".into()); | ||
74 | } | ||
75 | TokenAtOffset::None => unreachable!(), | ||
76 | }; | ||
77 | if ws.kind() != WHITESPACE { | ||
78 | return None; | ||
79 | } | ||
80 | let text = ws.text(); | ||
81 | let pos = text.rfind('\n').map(|it| it + 1).unwrap_or(0); | ||
82 | Some(text[pos..].into()) | ||
83 | } | ||
84 | |||
85 | #[cfg(test)] | ||
86 | mod tests { | ||
87 | use test_utils::{add_cursor, assert_eq_text, extract_offset}; | ||
88 | |||
89 | use crate::mock_analysis::single_file; | ||
90 | |||
91 | use super::*; | ||
92 | |||
93 | fn apply_on_enter(before: &str) -> Option<String> { | ||
94 | let (offset, before) = extract_offset(before); | ||
95 | let (analysis, file_id) = single_file(&before); | ||
96 | let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?; | ||
97 | |||
98 | assert_eq!(result.source_file_edits.len(), 1); | ||
99 | let actual = result.source_file_edits[0].edit.apply(&before); | ||
100 | let actual = add_cursor(&actual, result.cursor_position.unwrap().offset); | ||
101 | Some(actual) | ||
102 | } | ||
103 | |||
104 | fn do_check(ra_fixture_before: &str, ra_fixture_after: &str) { | ||
105 | let actual = apply_on_enter(ra_fixture_before).unwrap(); | ||
106 | assert_eq_text!(ra_fixture_after, &actual); | ||
107 | } | ||
108 | |||
109 | fn do_check_noop(ra_fixture_text: &str) { | ||
110 | assert!(apply_on_enter(ra_fixture_text).is_none()) | ||
111 | } | ||
112 | |||
113 | #[test] | ||
114 | fn continues_doc_comment() { | ||
115 | do_check( | ||
116 | r" | ||
117 | /// Some docs<|> | ||
118 | fn foo() { | ||
119 | } | ||
120 | ", | ||
121 | r" | ||
122 | /// Some docs | ||
123 | /// <|> | ||
124 | fn foo() { | ||
125 | } | ||
126 | ", | ||
127 | ); | ||
128 | |||
129 | do_check( | ||
130 | r" | ||
131 | impl S { | ||
132 | /// Some<|> docs. | ||
133 | fn foo() {} | ||
134 | } | ||
135 | ", | ||
136 | r" | ||
137 | impl S { | ||
138 | /// Some | ||
139 | /// <|> docs. | ||
140 | fn foo() {} | ||
141 | } | ||
142 | ", | ||
143 | ); | ||
144 | |||
145 | do_check( | ||
146 | r" | ||
147 | ///<|> Some docs | ||
148 | fn foo() { | ||
149 | } | ||
150 | ", | ||
151 | r" | ||
152 | /// | ||
153 | /// <|> Some docs | ||
154 | fn foo() { | ||
155 | } | ||
156 | ", | ||
157 | ); | ||
158 | } | ||
159 | |||
160 | #[test] | ||
161 | fn does_not_continue_before_doc_comment() { | ||
162 | do_check_noop(r"<|>//! docz"); | ||
163 | } | ||
164 | |||
165 | #[test] | ||
166 | fn continues_code_comment_in_the_middle_of_line() { | ||
167 | do_check( | ||
168 | r" | ||
169 | fn main() { | ||
170 | // Fix<|> me | ||
171 | let x = 1 + 1; | ||
172 | } | ||
173 | ", | ||
174 | r" | ||
175 | fn main() { | ||
176 | // Fix | ||
177 | // <|> me | ||
178 | let x = 1 + 1; | ||
179 | } | ||
180 | ", | ||
181 | ); | ||
182 | } | ||
183 | |||
184 | #[test] | ||
185 | fn continues_code_comment_in_the_middle_several_lines() { | ||
186 | do_check( | ||
187 | r" | ||
188 | fn main() { | ||
189 | // Fix<|> | ||
190 | // me | ||
191 | let x = 1 + 1; | ||
192 | } | ||
193 | ", | ||
194 | r" | ||
195 | fn main() { | ||
196 | // Fix | ||
197 | // <|> | ||
198 | // me | ||
199 | let x = 1 + 1; | ||
200 | } | ||
201 | ", | ||
202 | ); | ||
203 | } | ||
204 | |||
205 | #[test] | ||
206 | fn does_not_continue_end_of_code_comment() { | ||
207 | do_check_noop( | ||
208 | r" | ||
209 | fn main() { | ||
210 | // Fix me<|> | ||
211 | let x = 1 + 1; | ||
212 | } | ||
213 | ", | ||
214 | ); | ||
215 | } | ||
216 | } | ||