diff options
Diffstat (limited to 'crates/ra_ide_api')
3 files changed, 91 insertions, 28 deletions
diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index 1b279615c..c5e8d5843 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use test_utils::tested_by; | 1 | use test_utils::tested_by; |
2 | use ra_db::SourceDatabase; | 2 | use ra_db::SourceDatabase; |
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | AstNode, SyntaxNode, TextUnit, TextRange, | 4 | AstNode, SyntaxNode, TextUnit, |
5 | SyntaxKind::FN_DEF, | 5 | SyntaxKind::FN_DEF, |
6 | ast::{self, ArgListOwner}, | 6 | ast::{self, ArgListOwner}, |
7 | algo::find_node_at_offset, | 7 | algo::find_node_at_offset, |
@@ -38,28 +38,20 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal | |||
38 | } | 38 | } |
39 | } else if num_params > 1 { | 39 | } else if num_params > 1 { |
40 | // Count how many parameters into the call we are. | 40 | // Count how many parameters into the call we are. |
41 | // TODO: This is best effort for now and should be fixed at some point. | ||
42 | // It may be better to see where we are in the arg_list and then check | ||
43 | // where offset is in that list (or beyond). | ||
44 | // Revisit this after we get documentation comments in. | ||
45 | if let Some(ref arg_list) = calling_node.arg_list() { | 41 | if let Some(ref arg_list) = calling_node.arg_list() { |
46 | let arg_list_range = arg_list.syntax().range(); | 42 | let arg_list_range = arg_list.syntax().range(); |
47 | if !arg_list_range.contains_inclusive(position.offset) { | 43 | if !arg_list_range.contains_inclusive(position.offset) { |
48 | tested_by!(call_info_bad_offset); | 44 | tested_by!(call_info_bad_offset); |
49 | return None; | 45 | return None; |
50 | } | 46 | } |
51 | let start = arg_list_range.start(); | ||
52 | 47 | ||
53 | let range_search = TextRange::from_to(start, position.offset); | 48 | let param = arg_list |
54 | let mut commas: usize = | 49 | .args() |
55 | arg_list.syntax().text().slice(range_search).to_string().matches(',').count(); | 50 | .position(|arg| arg.syntax().range().contains(position.offset)) |
51 | .or(Some(num_params - 1)) | ||
52 | .unwrap(); | ||
56 | 53 | ||
57 | // If we have a method call eat the first param since it's just self. | 54 | call_info.active_parameter = Some(param); |
58 | if has_self { | ||
59 | commas += 1; | ||
60 | } | ||
61 | |||
62 | call_info.active_parameter = Some(commas); | ||
63 | } | 55 | } |
64 | } | 56 | } |
65 | 57 | ||
diff --git a/crates/ra_ide_api/src/completion/complete_postfix.rs b/crates/ra_ide_api/src/completion/complete_postfix.rs index 10a3c8db7..cf8c63269 100644 --- a/crates/ra_ide_api/src/completion/complete_postfix.rs +++ b/crates/ra_ide_api/src/completion/complete_postfix.rs | |||
@@ -15,7 +15,7 @@ use ra_syntax::{ | |||
15 | }; | 15 | }; |
16 | use ra_text_edit::TextEditBuilder; | 16 | use ra_text_edit::TextEditBuilder; |
17 | 17 | ||
18 | fn postfix_snippet(ctx: &CompletionContext, label: &str, snippet: &str) -> Builder { | 18 | fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder { |
19 | let replace_range = ctx.source_range(); | 19 | let replace_range = ctx.source_range(); |
20 | let receiver_range = ctx.dot_receiver.expect("no receiver available").syntax().range(); | 20 | let receiver_range = ctx.dot_receiver.expect("no receiver available").syntax().range(); |
21 | let delete_range = TextRange::from_to(receiver_range.start(), replace_range.start()); | 21 | let delete_range = TextRange::from_to(receiver_range.start(), replace_range.start()); |
@@ -23,22 +23,33 @@ fn postfix_snippet(ctx: &CompletionContext, label: &str, snippet: &str) -> Build | |||
23 | builder.delete(delete_range); | 23 | builder.delete(delete_range); |
24 | CompletionItem::new(CompletionKind::Postfix, replace_range, label) | 24 | CompletionItem::new(CompletionKind::Postfix, replace_range, label) |
25 | .snippet(snippet) | 25 | .snippet(snippet) |
26 | .detail(detail) | ||
26 | .text_edit(builder.finish()) | 27 | .text_edit(builder.finish()) |
27 | } | 28 | } |
28 | 29 | ||
29 | pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { | 30 | pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { |
30 | if let Some(dot_receiver) = ctx.dot_receiver { | 31 | if let Some(dot_receiver) = ctx.dot_receiver { |
31 | let receiver_text = dot_receiver.syntax().text().to_string(); | 32 | let receiver_text = dot_receiver.syntax().text().to_string(); |
32 | postfix_snippet(ctx, "not", &format!("!{}", receiver_text)).add_to(acc); | 33 | postfix_snippet(ctx, "not", "!expr", &format!("!{}", receiver_text)).add_to(acc); |
33 | postfix_snippet(ctx, "if", &format!("if {} {{$0}}", receiver_text)).add_to(acc); | 34 | postfix_snippet(ctx, "ref", "&expr", &format!("&{}", receiver_text)).add_to(acc); |
35 | postfix_snippet(ctx, "refm", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc); | ||
36 | postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text)) | ||
37 | .add_to(acc); | ||
34 | postfix_snippet( | 38 | postfix_snippet( |
35 | ctx, | 39 | ctx, |
36 | "match", | 40 | "match", |
41 | "match expr {}", | ||
37 | &format!("match {} {{\n${{1:_}} => {{$0\\}},\n}}", receiver_text), | 42 | &format!("match {} {{\n${{1:_}} => {{$0\\}},\n}}", receiver_text), |
38 | ) | 43 | ) |
39 | .add_to(acc); | 44 | .add_to(acc); |
40 | postfix_snippet(ctx, "while", &format!("while {} {{\n$0\n}}", receiver_text)).add_to(acc); | 45 | postfix_snippet( |
41 | postfix_snippet(ctx, "dbg", &format!("dbg!({})", receiver_text)).add_to(acc); | 46 | ctx, |
47 | "while", | ||
48 | "while expr {}", | ||
49 | &format!("while {} {{\n$0\n}}", receiver_text), | ||
50 | ) | ||
51 | .add_to(acc); | ||
52 | postfix_snippet(ctx, "dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc); | ||
42 | } | 53 | } |
43 | } | 54 | } |
44 | 55 | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__postfix_completion_works_for_trivial_path_expression.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__postfix_completion_works_for_trivial_path_expression.snap index bc886ef0b..d3e53e4e8 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__postfix_completion_works_for_trivial_path_expression.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__postfix_completion_works_for_trivial_path_expression.snap | |||
@@ -1,15 +1,17 @@ | |||
1 | --- | 1 | --- |
2 | created: "2019-02-03T11:38:42.897384636+00:00" | 2 | created: "2019-02-14T18:33:26.102469493Z" |
3 | creator: [email protected] | 3 | creator: [email protected] |
4 | expression: kind_completions | ||
5 | source: crates/ra_ide_api/src/completion/completion_item.rs | 4 | source: crates/ra_ide_api/src/completion/completion_item.rs |
5 | expression: kind_completions | ||
6 | --- | 6 | --- |
7 | [ | 7 | [ |
8 | CompletionItem { | 8 | CompletionItem { |
9 | completion_kind: Postfix, | 9 | completion_kind: Postfix, |
10 | label: "dbg", | 10 | label: "dbg", |
11 | kind: None, | 11 | kind: None, |
12 | detail: None, | 12 | detail: Some( |
13 | "dbg!(expr)" | ||
14 | ), | ||
13 | documentation: None, | 15 | documentation: None, |
14 | lookup: None, | 16 | lookup: None, |
15 | insert_text: Some( | 17 | insert_text: Some( |
@@ -32,7 +34,9 @@ source: crates/ra_ide_api/src/completion/completion_item.rs | |||
32 | completion_kind: Postfix, | 34 | completion_kind: Postfix, |
33 | label: "if", | 35 | label: "if", |
34 | kind: None, | 36 | kind: None, |
35 | detail: None, | 37 | detail: Some( |
38 | "if expr {}" | ||
39 | ), | ||
36 | documentation: None, | 40 | documentation: None, |
37 | lookup: None, | 41 | lookup: None, |
38 | insert_text: Some( | 42 | insert_text: Some( |
@@ -55,7 +59,9 @@ source: crates/ra_ide_api/src/completion/completion_item.rs | |||
55 | completion_kind: Postfix, | 59 | completion_kind: Postfix, |
56 | label: "match", | 60 | label: "match", |
57 | kind: None, | 61 | kind: None, |
58 | detail: None, | 62 | detail: Some( |
63 | "match expr {}" | ||
64 | ), | ||
59 | documentation: None, | 65 | documentation: None, |
60 | lookup: None, | 66 | lookup: None, |
61 | insert_text: Some( | 67 | insert_text: Some( |
@@ -78,7 +84,9 @@ source: crates/ra_ide_api/src/completion/completion_item.rs | |||
78 | completion_kind: Postfix, | 84 | completion_kind: Postfix, |
79 | label: "not", | 85 | label: "not", |
80 | kind: None, | 86 | kind: None, |
81 | detail: None, | 87 | detail: Some( |
88 | "!expr" | ||
89 | ), | ||
82 | documentation: None, | 90 | documentation: None, |
83 | lookup: None, | 91 | lookup: None, |
84 | insert_text: Some( | 92 | insert_text: Some( |
@@ -99,9 +107,61 @@ source: crates/ra_ide_api/src/completion/completion_item.rs | |||
99 | }, | 107 | }, |
100 | CompletionItem { | 108 | CompletionItem { |
101 | completion_kind: Postfix, | 109 | completion_kind: Postfix, |
110 | label: "ref", | ||
111 | kind: None, | ||
112 | detail: Some( | ||
113 | "&expr" | ||
114 | ), | ||
115 | documentation: None, | ||
116 | lookup: None, | ||
117 | insert_text: Some( | ||
118 | "&bar" | ||
119 | ), | ||
120 | insert_text_format: Snippet, | ||
121 | source_range: [76; 76), | ||
122 | text_edit: Some( | ||
123 | TextEdit { | ||
124 | atoms: [ | ||
125 | AtomTextEdit { | ||
126 | delete: [72; 76), | ||
127 | insert: "" | ||
128 | } | ||
129 | ] | ||
130 | } | ||
131 | ) | ||
132 | }, | ||
133 | CompletionItem { | ||
134 | completion_kind: Postfix, | ||
135 | label: "refm", | ||
136 | kind: None, | ||
137 | detail: Some( | ||
138 | "&mut expr" | ||
139 | ), | ||
140 | documentation: None, | ||
141 | lookup: None, | ||
142 | insert_text: Some( | ||
143 | "&mut bar" | ||
144 | ), | ||
145 | insert_text_format: Snippet, | ||
146 | source_range: [76; 76), | ||
147 | text_edit: Some( | ||
148 | TextEdit { | ||
149 | atoms: [ | ||
150 | AtomTextEdit { | ||
151 | delete: [72; 76), | ||
152 | insert: "" | ||
153 | } | ||
154 | ] | ||
155 | } | ||
156 | ) | ||
157 | }, | ||
158 | CompletionItem { | ||
159 | completion_kind: Postfix, | ||
102 | label: "while", | 160 | label: "while", |
103 | kind: None, | 161 | kind: None, |
104 | detail: None, | 162 | detail: Some( |
163 | "while expr {}" | ||
164 | ), | ||
105 | documentation: None, | 165 | documentation: None, |
106 | lookup: None, | 166 | lookup: None, |
107 | insert_text: Some( | 167 | insert_text: Some( |