aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-03-23 17:25:40 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-03-23 17:25:40 +0000
commit18a8f48039fbfcbbf58e1dadcc95465fe9503691 (patch)
tree503037f1f48c53eb460fb730fed576070527203d /crates/ra_ide_api
parent2dfb47cc3dd68b7ca575e7eb4238221fdc8e7cdb (diff)
parenta3711e08dc4e393957dff136218c47d8b77da14f (diff)
Merge #1031
1031: Move most things out of ra_ide_api_light r=matklad a=detrumi This moves everything except `structure` out of `ra_ide_api_light`. So this PR and #1019 finish up #1009, whichever is merged last should probably remove the `ra_ide_api_light` crate. Also, `LocalEdit` was removed since it wasn't used any more. Co-authored-by: Wilco Kusee <[email protected]>
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs7
-rw-r--r--crates/ra_ide_api/src/lib.rs39
-rw-r--r--crates/ra_ide_api/src/matching_brace.rs45
-rw-r--r--crates/ra_ide_api/src/snapshots/tests__highlighting.snap34
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs81
-rw-r--r--crates/ra_ide_api/src/typing.rs419
6 files changed, 594 insertions, 31 deletions
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs
index e1c5a1b39..b9dc424c6 100644
--- a/crates/ra_ide_api/src/diagnostics.rs
+++ b/crates/ra_ide_api/src/diagnostics.rs
@@ -1,6 +1,5 @@
1use itertools::Itertools; 1use itertools::Itertools;
2use hir::{Problem, source_binder}; 2use hir::{Problem, source_binder};
3use ra_ide_api_light::Severity;
4use ra_db::SourceDatabase; 3use ra_db::SourceDatabase;
5use ra_syntax::{ 4use ra_syntax::{
6 Location, SourceFile, SyntaxKind, TextRange, SyntaxNode, 5 Location, SourceFile, SyntaxKind, TextRange, SyntaxNode,
@@ -11,6 +10,12 @@ use ra_text_edit::{TextEdit, TextEditBuilder};
11 10
12use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit, db::RootDatabase}; 11use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit, db::RootDatabase};
13 12
13#[derive(Debug, Copy, Clone)]
14pub enum Severity {
15 Error,
16 WeakWarning,
17}
18
14pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> { 19pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> {
15 let source_file = db.parse(file_id); 20 let source_file = db.parse(file_id);
16 let mut res = Vec::new(); 21 let mut res = Vec::new();
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index a838c30da..99f18b6b8 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -37,6 +37,8 @@ mod line_index;
37mod folding_ranges; 37mod folding_ranges;
38mod line_index_utils; 38mod line_index_utils;
39mod join_lines; 39mod join_lines;
40mod typing;
41mod matching_brace;
40 42
41#[cfg(test)] 43#[cfg(test)]
42mod marks; 44mod marks;
@@ -69,10 +71,10 @@ pub use crate::{
69 line_index::{LineIndex, LineCol}, 71 line_index::{LineIndex, LineCol},
70 line_index_utils::translate_offset_with_edit, 72 line_index_utils::translate_offset_with_edit,
71 folding_ranges::{Fold, FoldKind}, 73 folding_ranges::{Fold, FoldKind},
74 syntax_highlighting::HighlightedRange,
75 diagnostics::Severity,
72}; 76};
73pub use ra_ide_api_light::{ 77pub use ra_ide_api_light::StructureNode;
74 HighlightedRange, Severity, StructureNode, LocalEdit,
75};
76pub use ra_db::{ 78pub use ra_db::{
77 Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId, 79 Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId,
78 Edition 80 Edition
@@ -266,7 +268,7 @@ impl Analysis {
266 /// supported). 268 /// supported).
267 pub fn matching_brace(&self, position: FilePosition) -> Option<TextUnit> { 269 pub fn matching_brace(&self, position: FilePosition) -> Option<TextUnit> {
268 let file = self.db.parse(position.file_id); 270 let file = self.db.parse(position.file_id);
269 ra_ide_api_light::matching_brace(&file, position.offset) 271 matching_brace::matching_brace(&file, position.offset)
270 } 272 }
271 273
272 /// Returns a syntax tree represented as `String`, for debug purposes. 274 /// Returns a syntax tree represented as `String`, for debug purposes.
@@ -294,9 +296,7 @@ impl Analysis {
294 /// Returns an edit which should be applied when opening a new line, fixing 296 /// Returns an edit which should be applied when opening a new line, fixing
295 /// up minor stuff like continuing the comment. 297 /// up minor stuff like continuing the comment.
296 pub fn on_enter(&self, position: FilePosition) -> Option<SourceChange> { 298 pub fn on_enter(&self, position: FilePosition) -> Option<SourceChange> {
297 let file = self.db.parse(position.file_id); 299 typing::on_enter(&self.db, position)
298 let edit = ra_ide_api_light::on_enter(&file, position.offset)?;
299 Some(SourceChange::from_local_edit(position.file_id, edit))
300 } 300 }
301 301
302 /// Returns an edit which should be applied after `=` was typed. Primarily, 302 /// Returns an edit which should be applied after `=` was typed. Primarily,
@@ -304,15 +304,18 @@ impl Analysis {
304 // FIXME: use a snippet completion instead of this hack here. 304 // FIXME: use a snippet completion instead of this hack here.
305 pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> { 305 pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> {
306 let file = self.db.parse(position.file_id); 306 let file = self.db.parse(position.file_id);
307 let edit = ra_ide_api_light::on_eq_typed(&file, position.offset)?; 307 let edit = typing::on_eq_typed(&file, position.offset)?;
308 Some(SourceChange::from_local_edit(position.file_id, edit)) 308 Some(SourceChange {
309 label: "add semicolon".to_string(),
310 source_file_edits: vec![SourceFileEdit { edit, file_id: position.file_id }],
311 file_system_edits: vec![],
312 cursor_position: None,
313 })
309 } 314 }
310 315
311 /// Returns an edit which should be applied when a dot ('.') is typed on a blank line, indenting the line appropriately. 316 /// Returns an edit which should be applied when a dot ('.') is typed on a blank line, indenting the line appropriately.
312 pub fn on_dot_typed(&self, position: FilePosition) -> Option<SourceChange> { 317 pub fn on_dot_typed(&self, position: FilePosition) -> Option<SourceChange> {
313 let file = self.db.parse(position.file_id); 318 typing::on_dot_typed(&self.db, position)
314 let edit = ra_ide_api_light::on_dot_typed(&file, position.offset)?;
315 Some(SourceChange::from_local_edit(position.file_id, edit))
316 } 319 }
317 320
318 /// Returns a tree representation of symbols in the file. Useful to draw a 321 /// Returns a tree representation of symbols in the file. Useful to draw a
@@ -434,18 +437,6 @@ impl Analysis {
434 } 437 }
435} 438}
436 439
437impl SourceChange {
438 pub(crate) fn from_local_edit(file_id: FileId, edit: LocalEdit) -> SourceChange {
439 let file_edit = SourceFileEdit { file_id, edit: edit.edit };
440 SourceChange {
441 label: edit.label,
442 source_file_edits: vec![file_edit],
443 file_system_edits: vec![],
444 cursor_position: edit.cursor_position.map(|offset| FilePosition { offset, file_id }),
445 }
446 }
447}
448
449#[test] 440#[test]
450fn analysis_is_send() { 441fn analysis_is_send() {
451 fn is_send<T: Send>() {} 442 fn is_send<T: Send>() {}
diff --git a/crates/ra_ide_api/src/matching_brace.rs b/crates/ra_ide_api/src/matching_brace.rs
new file mode 100644
index 000000000..d1405f14f
--- /dev/null
+++ b/crates/ra_ide_api/src/matching_brace.rs
@@ -0,0 +1,45 @@
1use ra_syntax::{
2 SourceFile, TextUnit,
3 algo::find_leaf_at_offset,
4 SyntaxKind::{self, *},
5 ast::AstNode,
6};
7
8pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> {
9 const BRACES: &[SyntaxKind] =
10 &[L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE];
11 let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax(), offset)
12 .filter_map(|node| {
13 let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
14 Some((node, idx))
15 })
16 .next()?;
17 let parent = brace_node.parent()?;
18 let matching_kind = BRACES[brace_idx ^ 1];
19 let matching_node = parent.children().find(|node| node.kind() == matching_kind)?;
20 Some(matching_node.range().start())
21}
22
23#[cfg(test)]
24mod tests {
25 use test_utils::{add_cursor, assert_eq_text, extract_offset};
26
27 use super::*;
28
29 #[test]
30 fn test_matching_brace() {
31 fn do_check(before: &str, after: &str) {
32 let (pos, before) = extract_offset(before);
33 let file = SourceFile::parse(&before);
34 let new_pos = match matching_brace(&file, pos) {
35 None => pos,
36 Some(pos) => pos,
37 };
38 let actual = add_cursor(&before, new_pos);
39 assert_eq_text!(after, &actual);
40 }
41
42 do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }");
43 }
44
45}
diff --git a/crates/ra_ide_api/src/snapshots/tests__highlighting.snap b/crates/ra_ide_api/src/snapshots/tests__highlighting.snap
new file mode 100644
index 000000000..72029e0ed
--- /dev/null
+++ b/crates/ra_ide_api/src/snapshots/tests__highlighting.snap
@@ -0,0 +1,34 @@
1---
2created: "2019-03-23T16:20:31.394314144Z"
3creator: [email protected]
4source: crates/ra_ide_api/src/syntax_highlighting.rs
5expression: result
6---
7Ok(
8 [
9 HighlightedRange {
10 range: [1; 11),
11 tag: "comment"
12 },
13 HighlightedRange {
14 range: [12; 14),
15 tag: "keyword"
16 },
17 HighlightedRange {
18 range: [15; 19),
19 tag: "function"
20 },
21 HighlightedRange {
22 range: [29; 37),
23 tag: "macro"
24 },
25 HighlightedRange {
26 range: [38; 50),
27 tag: "string"
28 },
29 HighlightedRange {
30 range: [52; 54),
31 tag: "literal"
32 }
33 ]
34)
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs
index fdd87bcff..a0c5e78ad 100644
--- a/crates/ra_ide_api/src/syntax_highlighting.rs
+++ b/crates/ra_ide_api/src/syntax_highlighting.rs
@@ -1,12 +1,81 @@
1use ra_syntax::AstNode; 1use rustc_hash::FxHashSet;
2
3use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind::*};
2use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
3 5
4use crate::{ 6use crate::{FileId, db::RootDatabase};
5 FileId, HighlightedRange, 7
6 db::RootDatabase, 8#[derive(Debug)]
7}; 9pub struct HighlightedRange {
10 pub range: TextRange,
11 pub tag: &'static str,
12}
8 13
9pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> { 14pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> {
10 let source_file = db.parse(file_id); 15 let source_file = db.parse(file_id);
11 ra_ide_api_light::highlight(source_file.syntax()) 16
17 // Visited nodes to handle highlighting priorities
18 let mut highlighted = FxHashSet::default();
19 let mut res = Vec::new();
20 for node in source_file.syntax().descendants() {
21 if highlighted.contains(&node) {
22 continue;
23 }
24 let tag = match node.kind() {
25 COMMENT => "comment",
26 STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string",
27 ATTR => "attribute",
28 NAME_REF => "text",
29 NAME => "function",
30 INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
31 LIFETIME => "parameter",
32 k if k.is_keyword() => "keyword",
33 _ => {
34 if let Some(macro_call) = ast::MacroCall::cast(node) {
35 if let Some(path) = macro_call.path() {
36 if let Some(segment) = path.segment() {
37 if let Some(name_ref) = segment.name_ref() {
38 highlighted.insert(name_ref.syntax());
39 let range_start = name_ref.syntax().range().start();
40 let mut range_end = name_ref.syntax().range().end();
41 for sibling in path.syntax().siblings(Direction::Next) {
42 match sibling.kind() {
43 EXCL | IDENT => range_end = sibling.range().end(),
44 _ => (),
45 }
46 }
47 res.push(HighlightedRange {
48 range: TextRange::from_to(range_start, range_end),
49 tag: "macro",
50 })
51 }
52 }
53 }
54 }
55 continue;
56 }
57 };
58 res.push(HighlightedRange { range: node.range(), tag })
59 }
60 res
61}
62
63#[cfg(test)]
64mod tests {
65 use insta::assert_debug_snapshot_matches;
66
67 use crate::mock_analysis::single_file;
68
69 #[test]
70 fn test_highlighting() {
71 let (analysis, file_id) = single_file(
72 r#"
73// comment
74fn main() {}
75 println!("Hello, {}!", 92);
76"#,
77 );
78 let result = analysis.highlight(file_id);
79 assert_debug_snapshot_matches!("highlighting", result);
80 }
12} 81}
diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs
new file mode 100644
index 000000000..94b228466
--- /dev/null
+++ b/crates/ra_ide_api/src/typing.rs
@@ -0,0 +1,419 @@
1use ra_syntax::{
2 AstNode, SourceFile, SyntaxKind::*,
3 SyntaxNode, TextUnit, TextRange,
4 algo::{find_node_at_offset, find_leaf_at_offset, LeafAtOffset},
5 ast::{self, AstToken},
6};
7use ra_fmt::leading_indent;
8use ra_text_edit::{TextEdit, TextEditBuilder};
9use ra_db::{FilePosition, SourceDatabase};
10use crate::{db::RootDatabase, SourceChange, SourceFileEdit};
11
12pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> {
13 let file = db.parse(position.file_id);
14 let comment = find_leaf_at_offset(file.syntax(), position.offset)
15 .left_biased()
16 .and_then(ast::Comment::cast)?;
17
18 if let ast::CommentFlavor::Multiline = comment.flavor() {
19 return None;
20 }
21
22 let prefix = comment.prefix();
23 if position.offset
24 < comment.syntax().range().start() + TextUnit::of_str(prefix) + TextUnit::from(1)
25 {
26 return None;
27 }
28
29 let indent = node_indent(&file, comment.syntax())?;
30 let inserted = format!("\n{}{} ", indent, prefix);
31 let cursor_position = position.offset + TextUnit::of_str(&inserted);
32 let mut edit = TextEditBuilder::default();
33 edit.insert(position.offset, inserted);
34 Some(SourceChange {
35 label: "on enter".to_string(),
36 source_file_edits: vec![SourceFileEdit { edit: edit.finish(), file_id: position.file_id }],
37 file_system_edits: vec![],
38 cursor_position: Some(FilePosition { offset: cursor_position, file_id: position.file_id }),
39 })
40}
41
42fn node_indent<'a>(file: &'a SourceFile, node: &SyntaxNode) -> Option<&'a str> {
43 let ws = match find_leaf_at_offset(file.syntax(), node.range().start()) {
44 LeafAtOffset::Between(l, r) => {
45 assert!(r == node);
46 l
47 }
48 LeafAtOffset::Single(n) => {
49 assert!(n == node);
50 return Some("");
51 }
52 LeafAtOffset::None => unreachable!(),
53 };
54 if ws.kind() != WHITESPACE {
55 return None;
56 }
57 let text = ws.leaf_text().unwrap();
58 let pos = text.as_str().rfind('\n').map(|it| it + 1).unwrap_or(0);
59 Some(&text[pos..])
60}
61
62pub fn on_eq_typed(file: &SourceFile, eq_offset: TextUnit) -> Option<TextEdit> {
63 assert_eq!(file.syntax().text().char_at(eq_offset), Some('='));
64 let let_stmt: &ast::LetStmt = find_node_at_offset(file.syntax(), eq_offset)?;
65 if let_stmt.has_semi() {
66 return None;
67 }
68 if let Some(expr) = let_stmt.initializer() {
69 let expr_range = expr.syntax().range();
70 if expr_range.contains(eq_offset) && eq_offset != expr_range.start() {
71 return None;
72 }
73 if file.syntax().text().slice(eq_offset..expr_range.start()).contains('\n') {
74 return None;
75 }
76 } else {
77 return None;
78 }
79 let offset = let_stmt.syntax().range().end();
80 let mut edit = TextEditBuilder::default();
81 edit.insert(offset, ";".to_string());
82 Some(edit.finish())
83}
84
85pub(crate) fn on_dot_typed(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> {
86 let file = db.parse(position.file_id);
87 assert_eq!(file.syntax().text().char_at(position.offset), Some('.'));
88
89 let whitespace = find_leaf_at_offset(file.syntax(), position.offset)
90 .left_biased()
91 .and_then(ast::Whitespace::cast)?;
92
93 let current_indent = {
94 let text = whitespace.text();
95 let newline = text.rfind('\n')?;
96 &text[newline + 1..]
97 };
98 let current_indent_len = TextUnit::of_str(current_indent);
99
100 // Make sure dot is a part of call chain
101 let field_expr = whitespace.syntax().parent().and_then(ast::FieldExpr::cast)?;
102 let prev_indent = leading_indent(field_expr.syntax())?;
103 let target_indent = format!(" {}", prev_indent);
104 let target_indent_len = TextUnit::of_str(&target_indent);
105 if current_indent_len == target_indent_len {
106 return None;
107 }
108 let mut edit = TextEditBuilder::default();
109 edit.replace(
110 TextRange::from_to(position.offset - current_indent_len, position.offset),
111 target_indent.into(),
112 );
113 let res = SourceChange {
114 label: "reindent dot".to_string(),
115 source_file_edits: vec![SourceFileEdit { edit: edit.finish(), file_id: position.file_id }],
116 file_system_edits: vec![],
117 cursor_position: Some(FilePosition {
118 offset: position.offset + target_indent_len - current_indent_len
119 + TextUnit::of_char('.'),
120 file_id: position.file_id,
121 }),
122 };
123 Some(res)
124}
125
126#[cfg(test)]
127mod tests {
128 use test_utils::{add_cursor, assert_eq_text, extract_offset};
129
130 use crate::mock_analysis::single_file;
131
132 use super::*;
133
134 #[test]
135 fn test_on_eq_typed() {
136 fn type_eq(before: &str, after: &str) {
137 let (offset, before) = extract_offset(before);
138 let mut edit = TextEditBuilder::default();
139 edit.insert(offset, "=".to_string());
140 let before = edit.finish().apply(&before);
141 let file = SourceFile::parse(&before);
142 if let Some(result) = on_eq_typed(&file, offset) {
143 let actual = result.apply(&before);
144 assert_eq_text!(after, &actual);
145 } else {
146 assert_eq_text!(&before, after)
147 };
148 }
149
150 // do_check(r"
151 // fn foo() {
152 // let foo =<|>
153 // }
154 // ", r"
155 // fn foo() {
156 // let foo =;
157 // }
158 // ");
159 type_eq(
160 r"
161fn foo() {
162 let foo <|> 1 + 1
163}
164",
165 r"
166fn foo() {
167 let foo = 1 + 1;
168}
169",
170 );
171 // do_check(r"
172 // fn foo() {
173 // let foo =<|>
174 // let bar = 1;
175 // }
176 // ", r"
177 // fn foo() {
178 // let foo =;
179 // let bar = 1;
180 // }
181 // ");
182 }
183
184 fn type_dot(before: &str, after: &str) {
185 let (offset, before) = extract_offset(before);
186 let mut edit = TextEditBuilder::default();
187 edit.insert(offset, ".".to_string());
188 let before = edit.finish().apply(&before);
189 let (analysis, file_id) = single_file(&before);
190 if let Some(result) = analysis.on_dot_typed(FilePosition { offset, file_id }) {
191 assert_eq!(result.source_file_edits.len(), 1);
192 let actual = result.source_file_edits[0].edit.apply(&before);
193 assert_eq_text!(after, &actual);
194 } else {
195 assert_eq_text!(&before, after)
196 };
197 }
198
199 #[test]
200 fn indents_new_chain_call() {
201 type_dot(
202 r"
203 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
204 self.child_impl(db, name)
205 <|>
206 }
207 ",
208 r"
209 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
210 self.child_impl(db, name)
211 .
212 }
213 ",
214 );
215 type_dot(
216 r"
217 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
218 self.child_impl(db, name)
219 <|>
220 }
221 ",
222 r"
223 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
224 self.child_impl(db, name)
225 .
226 }
227 ",
228 )
229 }
230
231 #[test]
232 fn indents_new_chain_call_with_semi() {
233 type_dot(
234 r"
235 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
236 self.child_impl(db, name)
237 <|>;
238 }
239 ",
240 r"
241 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
242 self.child_impl(db, name)
243 .;
244 }
245 ",
246 );
247 type_dot(
248 r"
249 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
250 self.child_impl(db, name)
251 <|>;
252 }
253 ",
254 r"
255 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
256 self.child_impl(db, name)
257 .;
258 }
259 ",
260 )
261 }
262
263 #[test]
264 fn indents_continued_chain_call() {
265 type_dot(
266 r"
267 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
268 self.child_impl(db, name)
269 .first()
270 <|>
271 }
272 ",
273 r"
274 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
275 self.child_impl(db, name)
276 .first()
277 .
278 }
279 ",
280 );
281 type_dot(
282 r"
283 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
284 self.child_impl(db, name)
285 .first()
286 <|>
287 }
288 ",
289 r"
290 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
291 self.child_impl(db, name)
292 .first()
293 .
294 }
295 ",
296 );
297 }
298
299 #[test]
300 fn indents_middle_of_chain_call() {
301 type_dot(
302 r"
303 fn source_impl() {
304 let var = enum_defvariant_list().unwrap()
305 <|>
306 .nth(92)
307 .unwrap();
308 }
309 ",
310 r"
311 fn source_impl() {
312 let var = enum_defvariant_list().unwrap()
313 .
314 .nth(92)
315 .unwrap();
316 }
317 ",
318 );
319 type_dot(
320 r"
321 fn source_impl() {
322 let var = enum_defvariant_list().unwrap()
323 <|>
324 .nth(92)
325 .unwrap();
326 }
327 ",
328 r"
329 fn source_impl() {
330 let var = enum_defvariant_list().unwrap()
331 .
332 .nth(92)
333 .unwrap();
334 }
335 ",
336 );
337 }
338
339 #[test]
340 fn dont_indent_freestanding_dot() {
341 type_dot(
342 r"
343 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
344 <|>
345 }
346 ",
347 r"
348 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
349 .
350 }
351 ",
352 );
353 type_dot(
354 r"
355 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
356 <|>
357 }
358 ",
359 r"
360 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
361 .
362 }
363 ",
364 );
365 }
366
367 #[test]
368 fn test_on_enter() {
369 fn apply_on_enter(before: &str) -> Option<String> {
370 let (offset, before) = extract_offset(before);
371 let (analysis, file_id) = single_file(&before);
372 let result = analysis.on_enter(FilePosition { offset, file_id })?;
373
374 assert_eq!(result.source_file_edits.len(), 1);
375 let actual = result.source_file_edits[0].edit.apply(&before);
376 let actual = add_cursor(&actual, result.cursor_position.unwrap().offset);
377 Some(actual)
378 }
379
380 fn do_check(before: &str, after: &str) {
381 let actual = apply_on_enter(before).unwrap();
382 assert_eq_text!(after, &actual);
383 }
384
385 fn do_check_noop(text: &str) {
386 assert!(apply_on_enter(text).is_none())
387 }
388
389 do_check(
390 r"
391/// Some docs<|>
392fn foo() {
393}
394",
395 r"
396/// Some docs
397/// <|>
398fn foo() {
399}
400",
401 );
402 do_check(
403 r"
404impl S {
405 /// Some<|> docs.
406 fn foo() {}
407}
408",
409 r"
410impl S {
411 /// Some
412 /// <|> docs.
413 fn foo() {}
414}
415",
416 );
417 do_check_noop(r"<|>//! docz");
418 }
419}