aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-23 13:25:44 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-23 13:25:44 +0000
commit81fcfc55d247bfe6090741f2e4ae9aa89947bf32 (patch)
tree407c0ed2edf70d73c78224a9934c4a8c9e7eac2c /crates/ra_ide_api
parent86507c0626eb07485b21c61638673acbb3c2a9ad (diff)
parent71b9f06c89735b78439d0720e314b71969c17eb3 (diff)
Merge #608
608: Complete parens r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs26
-rw-r--r--crates/ra_ide_api/src/completion/complete_scope.rs17
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs76
-rw-r--r--crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls3.snap26
-rw-r--r--crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap8
-rw-r--r--crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap8
-rw-r--r--crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap8
-rw-r--r--crates/ra_ide_api/src/lib.rs3
-rw-r--r--crates/ra_ide_api/src/marks.rs3
9 files changed, 118 insertions, 57 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index 804954ee1..6bed299d2 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -121,30 +121,4 @@ mod tests {
121 ", 121 ",
122 ); 122 );
123 } 123 }
124
125 #[test]
126 fn dont_render_function_parens_in_use_item() {
127 check_reference_completion(
128 "dont_render_function_parens_in_use_item",
129 "
130 //- /lib.rs
131 mod m { pub fn foo() {} }
132 use crate::m::f<|>;
133 ",
134 )
135 }
136
137 #[test]
138 fn dont_render_function_parens_if_already_call() {
139 check_reference_completion(
140 "dont_render_function_parens_if_already_call",
141 "
142 //- /lib.rs
143 fn frobnicate() {}
144 fn main() {
145 frob<|>();
146 }
147 ",
148 )
149 }
150} 124}
diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs
index 20fc77f06..f837bb1db 100644
--- a/crates/ra_ide_api/src/completion/complete_scope.rs
+++ b/crates/ra_ide_api/src/completion/complete_scope.rs
@@ -175,21 +175,4 @@ mod tests {
175 check_reference_completion("self_in_methods", r"impl S { fn foo(&self) { <|> } }") 175 check_reference_completion("self_in_methods", r"impl S { fn foo(&self) { <|> } }")
176 } 176 }
177 177
178 #[test]
179 fn inserts_parens_for_function_calls() {
180 check_reference_completion(
181 "inserts_parens_for_function_calls1",
182 r"
183 fn no_args() {}
184 fn main() { no_<|> }
185 ",
186 );
187 check_reference_completion(
188 "inserts_parens_for_function_calls2",
189 r"
190 fn with_args(x: i32, y: String) {}
191 fn main() { with_<|> }
192 ",
193 );
194 }
195} 178}
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index 680fd8d1b..e3bf82304 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -3,9 +3,10 @@ use hir::PerNs;
3use crate::completion::completion_context::CompletionContext; 3use crate::completion::completion_context::CompletionContext;
4use ra_syntax::{ 4use ra_syntax::{
5 ast::{self, AstNode}, 5 ast::{self, AstNode},
6 TextRange 6 TextRange,
7}; 7};
8use ra_text_edit::TextEdit; 8use ra_text_edit::TextEdit;
9use test_utils::tested_by;
9 10
10/// `CompletionItem` describes a single completion variant in the editor pop-up. 11/// `CompletionItem` describes a single completion variant in the editor pop-up.
11/// It is basically a POD with various properties. To construct a 12/// It is basically a POD with various properties. To construct a
@@ -255,7 +256,9 @@ impl Builder {
255 ) -> Builder { 256 ) -> Builder {
256 // If not an import, add parenthesis automatically. 257 // If not an import, add parenthesis automatically.
257 if ctx.use_item_syntax.is_none() && !ctx.is_call { 258 if ctx.use_item_syntax.is_none() && !ctx.is_call {
258 if function.signature(ctx.db).params().is_empty() { 259 tested_by!(inserts_parens_for_function_calls);
260 let sig = function.signature(ctx.db);
261 if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 {
259 self.insert_text = Some(format!("{}()$0", self.label)); 262 self.insert_text = Some(format!("{}()$0", self.label));
260 } else { 263 } else {
261 self.insert_text = Some(format!("{}($0)", self.label)); 264 self.insert_text = Some(format!("{}($0)", self.label));
@@ -344,3 +347,72 @@ pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind
344 .collect(); 347 .collect();
345 assert_debug_snapshot_matches!(test_name, kind_completions); 348 assert_debug_snapshot_matches!(test_name, kind_completions);
346} 349}
350
351#[cfg(test)]
352mod tests {
353 use test_utils::covers;
354
355 use super::*;
356
357 fn check_reference_completion(code: &str, expected_completions: &str) {
358 check_completion(code, expected_completions, CompletionKind::Reference);
359 }
360
361 #[test]
362 fn inserts_parens_for_function_calls() {
363 covers!(inserts_parens_for_function_calls);
364 check_reference_completion(
365 "inserts_parens_for_function_calls1",
366 r"
367 fn no_args() {}
368 fn main() { no_<|> }
369 ",
370 );
371 check_reference_completion(
372 "inserts_parens_for_function_calls2",
373 r"
374 fn with_args(x: i32, y: String) {}
375 fn main() { with_<|> }
376 ",
377 );
378 check_reference_completion(
379 "inserts_parens_for_function_calls3",
380 r"
381 struct S {}
382 impl S {
383 fn foo(&self) {}
384 }
385 fn bar(s: &S) {
386 s.f<|>
387 }
388 ",
389 )
390 }
391
392 #[test]
393 fn dont_render_function_parens_in_use_item() {
394 check_reference_completion(
395 "dont_render_function_parens_in_use_item",
396 "
397 //- /lib.rs
398 mod m { pub fn foo() {} }
399 use crate::m::f<|>;
400 ",
401 )
402 }
403
404 #[test]
405 fn dont_render_function_parens_if_already_call() {
406 check_reference_completion(
407 "dont_render_function_parens_if_already_call",
408 "
409 //- /lib.rs
410 fn frobnicate() {}
411 fn main() {
412 frob<|>();
413 }
414 ",
415 )
416 }
417
418}
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls3.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls3.snap
new file mode 100644
index 000000000..92068e50a
--- /dev/null
+++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls3.snap
@@ -0,0 +1,26 @@
1---
2created: "2019-01-23T13:19:23.525922020+00:00"
3creator: [email protected]
4expression: kind_completions
5source: crates/ra_ide_api/src/completion/completion_item.rs
6---
7[
8 CompletionItem {
9 completion_kind: Reference,
10 label: "foo",
11 kind: Some(
12 Method
13 ),
14 detail: Some(
15 "fn foo(&self)"
16 ),
17 documentation: None,
18 lookup: None,
19 insert_text: Some(
20 "foo()$0"
21 ),
22 insert_text_format: Snippet,
23 source_range: [139; 140),
24 text_edit: None
25 }
26]
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap
index ffeac0bf4..10fe59248 100644
--- a/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap
+++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap
@@ -1,8 +1,8 @@
1--- 1---
2created: "2019-01-22T15:38:19.541947400+00:00" 2created: "2019-01-23T13:19:23.501258181+00:00"
3creator: insta@0.4.0 3creator: insta@0.5.2
4expression: kind_completions 4expression: kind_completions
5source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" 5source: crates/ra_ide_api/src/completion/completion_item.rs
6--- 6---
7[ 7[
8 CompletionItem { 8 CompletionItem {
@@ -17,7 +17,7 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs"
17 documentation: None, 17 documentation: None,
18 lookup: None, 18 lookup: None,
19 insert_text: Some( 19 insert_text: Some(
20 "the_method($0)" 20 "the_method()$0"
21 ), 21 ),
22 insert_text_format: Snippet, 22 insert_text_format: Snippet,
23 source_range: [144; 144), 23 source_range: [144; 144),
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap
index 5ef140b28..5f5df0033 100644
--- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap
+++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap
@@ -1,8 +1,8 @@
1--- 1---
2created: "2019-01-22T15:38:19.541947400+00:00" 2created: "2019-01-23T13:19:23.501353210+00:00"
3creator: insta@0.4.0 3creator: insta@0.5.2
4expression: kind_completions 4expression: kind_completions
5source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" 5source: crates/ra_ide_api/src/completion/completion_item.rs
6--- 6---
7[ 7[
8 CompletionItem { 8 CompletionItem {
@@ -33,7 +33,7 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs"
33 documentation: None, 33 documentation: None,
34 lookup: None, 34 lookup: None,
35 insert_text: Some( 35 insert_text: Some(
36 "foo($0)" 36 "foo()$0"
37 ), 37 ),
38 insert_text_format: Snippet, 38 insert_text_format: Snippet,
39 source_range: [126; 126), 39 source_range: [126; 126),
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap
index b7903c5bc..80e8f3df5 100644
--- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap
+++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap
@@ -1,8 +1,8 @@
1--- 1---
2created: "2019-01-22T15:38:19.541947400+00:00" 2created: "2019-01-23T13:19:23.501297515+00:00"
3creator: insta@0.4.0 3creator: insta@0.5.2
4expression: kind_completions 4expression: kind_completions
5source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" 5source: crates/ra_ide_api/src/completion/completion_item.rs
6--- 6---
7[ 7[
8 CompletionItem { 8 CompletionItem {
@@ -33,7 +33,7 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs"
33 documentation: None, 33 documentation: None,
34 lookup: None, 34 lookup: None,
35 insert_text: Some( 35 insert_text: Some(
36 "foo($0)" 36 "foo()$0"
37 ), 37 ),
38 insert_text_format: Snippet, 38 insert_text_format: Snippet,
39 source_range: [121; 121), 39 source_range: [121; 121),
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index 3c53e75ac..3502bfd2e 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -26,6 +26,9 @@ mod syntax_highlighting;
26mod parent_module; 26mod parent_module;
27mod rename; 27mod rename;
28 28
29#[cfg(test)]
30mod marks;
31
29use std::{fmt, sync::Arc}; 32use std::{fmt, sync::Arc};
30 33
31use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit}; 34use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit};
diff --git a/crates/ra_ide_api/src/marks.rs b/crates/ra_ide_api/src/marks.rs
new file mode 100644
index 000000000..b4a726ef0
--- /dev/null
+++ b/crates/ra_ide_api/src/marks.rs
@@ -0,0 +1,3 @@
1use test_utils::mark;
2
3mark!(inserts_parens_for_function_calls);