aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/completion/complete_keyword.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-12-30 14:09:27 +0000
committerAleksey Kladov <[email protected]>2018-12-30 14:22:12 +0000
commit0f75ac1ae073e8735a84484ef9d1453e6c919b24 (patch)
treefa52db75cb2b16c86050852f7dc6c02d0803f53b /crates/ra_analysis/src/completion/complete_keyword.rs
parentc182aab5460fb5dc14b8c0e47fae45a09ccf0ac8 (diff)
add `;` to last return in block
Diffstat (limited to 'crates/ra_analysis/src/completion/complete_keyword.rs')
-rw-r--r--crates/ra_analysis/src/completion/complete_keyword.rs43
1 files changed, 32 insertions, 11 deletions
diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs
index d1e0a20a8..2869e67e0 100644
--- a/crates/ra_analysis/src/completion/complete_keyword.rs
+++ b/crates/ra_analysis/src/completion/complete_keyword.rs
@@ -35,7 +35,7 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
35 acc.add(keyword("continue", "continue")); 35 acc.add(keyword("continue", "continue"));
36 acc.add(keyword("break", "break")); 36 acc.add(keyword("break", "break"));
37 } 37 }
38 acc.add_all(complete_return(fn_def, ctx.is_stmt)); 38 acc.add_all(complete_return(fn_def, ctx.can_be_stmt));
39} 39}
40 40
41fn is_in_loop_body(leaf: SyntaxNodeRef) -> bool { 41fn is_in_loop_body(leaf: SyntaxNodeRef) -> bool {
@@ -57,8 +57,8 @@ fn is_in_loop_body(leaf: SyntaxNodeRef) -> bool {
57 false 57 false
58} 58}
59 59
60fn complete_return(fn_def: ast::FnDef, is_stmt: bool) -> Option<CompletionItem> { 60fn complete_return(fn_def: ast::FnDef, can_be_stmt: bool) -> Option<CompletionItem> {
61 let snip = match (is_stmt, fn_def.ret_type().is_some()) { 61 let snip = match (can_be_stmt, fn_def.ret_type().is_some()) {
62 (true, true) => "return $0;", 62 (true, true) => "return $0;",
63 (true, false) => "return;", 63 (true, false) => "return;",
64 (false, true) => "return $0", 64 (false, true) => "return $0",
@@ -75,7 +75,7 @@ mod tests {
75 } 75 }
76 76
77 #[test] 77 #[test]
78 fn test_completion_kewords() { 78 fn completes_various_keywords_in_function() {
79 check_keyword_completion( 79 check_keyword_completion(
80 r" 80 r"
81 fn quux() { 81 fn quux() {
@@ -87,13 +87,13 @@ mod tests {
87 match "match $0 {}" 87 match "match $0 {}"
88 while "while $0 {}" 88 while "while $0 {}"
89 loop "loop {$0}" 89 loop "loop {$0}"
90 return "return" 90 return "return;"
91 "#, 91 "#,
92 ); 92 );
93 } 93 }
94 94
95 #[test] 95 #[test]
96 fn test_completion_else() { 96 fn completes_else_after_if() {
97 check_keyword_completion( 97 check_keyword_completion(
98 r" 98 r"
99 fn quux() { 99 fn quux() {
@@ -109,7 +109,7 @@ mod tests {
109 loop "loop {$0}" 109 loop "loop {$0}"
110 else "else {$0}" 110 else "else {$0}"
111 else if "else if $0 {}" 111 else if "else if $0 {}"
112 return "return" 112 return "return;"
113 "#, 113 "#,
114 ); 114 );
115 } 115 }
@@ -149,7 +149,7 @@ mod tests {
149 } 149 }
150 150
151 #[test] 151 #[test]
152 fn test_completion_return_no_stmt() { 152 fn dont_add_semi_after_return_if_not_a_statement() {
153 check_keyword_completion( 153 check_keyword_completion(
154 r" 154 r"
155 fn quux() -> i32 { 155 fn quux() -> i32 {
@@ -169,7 +169,27 @@ mod tests {
169 } 169 }
170 170
171 #[test] 171 #[test]
172 fn test_continue_break_completion() { 172 fn last_return_in_block_has_semi() {
173 check_keyword_completion(
174 r"
175 fn quux() -> i32 {
176 if condition {
177 <|>
178 }
179 }
180 ",
181 r#"
182 if "if $0 {}"
183 match "match $0 {}"
184 while "while $0 {}"
185 loop "loop {$0}"
186 return "return $0;"
187 "#,
188 );
189 }
190
191 #[test]
192 fn completes_break_and_continue_in_loops() {
173 check_keyword_completion( 193 check_keyword_completion(
174 r" 194 r"
175 fn quux() -> i32 { 195 fn quux() -> i32 {
@@ -183,9 +203,10 @@ mod tests {
183 loop "loop {$0}" 203 loop "loop {$0}"
184 continue "continue" 204 continue "continue"
185 break "break" 205 break "break"
186 return "return $0" 206 return "return $0;"
187 "#, 207 "#,
188 ); 208 );
209 // No completion: lambda isolates control flow
189 check_keyword_completion( 210 check_keyword_completion(
190 r" 211 r"
191 fn quux() -> i32 { 212 fn quux() -> i32 {
@@ -197,7 +218,7 @@ mod tests {
197 match "match $0 {}" 218 match "match $0 {}"
198 while "while $0 {}" 219 while "while $0 {}"
199 loop "loop {$0}" 220 loop "loop {$0}"
200 return "return $0" 221 return "return $0;"
201 "#, 222 "#,
202 ); 223 );
203 } 224 }