aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-01-15 12:13:30 +0000
committerAleksey Kladov <[email protected]>2021-01-15 12:14:31 +0000
commit41ea2602017027e22f5a68df3d6ad98e2ae880f8 (patch)
treef894fdce88b1299309cd3f933adf93d9c9f0dd6d /crates
parent9b1bc19d67ab6e4549f9bc0098949b842ce19fd0 (diff)
Simplify
Diffstat (limited to 'crates')
-rw-r--r--crates/completion/src/completions/keyword.rs46
1 files changed, 19 insertions, 27 deletions
diff --git a/crates/completion/src/completions/keyword.rs b/crates/completion/src/completions/keyword.rs
index 665214895..effc3e4bf 100644
--- a/crates/completion/src/completions/keyword.rs
+++ b/crates/completion/src/completions/keyword.rs
@@ -1,6 +1,6 @@
1//! Completes keywords. 1//! Completes keywords.
2 2
3use syntax::{ast, SyntaxKind}; 3use syntax::SyntaxKind;
4use test_utils::mark; 4use test_utils::mark;
5 5
6use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions}; 6use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
@@ -143,47 +143,39 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
143 Some(it) => it, 143 Some(it) => it,
144 None => return, 144 None => return,
145 }; 145 };
146 acc.add_all(complete_return(ctx, &fn_def, ctx.can_be_stmt));
147}
148
149fn keyword(ctx: &CompletionContext, kw: &str, snippet: &str) -> CompletionItem {
150 let res = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw)
151 .kind(CompletionItemKind::Keyword);
152 146
153 match ctx.config.snippet_cap { 147 add_keyword(
154 Some(cap) => res.insert_snippet(cap, snippet), 148 ctx,
155 _ => res.insert_text(if snippet.contains('$') { kw } else { snippet }), 149 acc,
156 } 150 "return",
157 .build() 151 match (ctx.can_be_stmt, fn_def.ret_type().is_some()) {
152 (true, true) => "return $0;",
153 (true, false) => "return;",
154 (false, true) => "return $0",
155 (false, false) => "return",
156 },
157 )
158} 158}
159 159
160fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) { 160fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) {
161 acc.add(keyword(ctx, kw, snippet)); 161 let builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw)
162} 162 .kind(CompletionItemKind::Keyword);
163 163 let builder = match ctx.config.snippet_cap {
164fn complete_return( 164 Some(cap) => builder.insert_snippet(cap, snippet),
165 ctx: &CompletionContext, 165 None => builder.insert_text(if snippet.contains('$') { kw } else { snippet }),
166 fn_def: &ast::Fn,
167 can_be_stmt: bool,
168) -> Option<CompletionItem> {
169 let snip = match (can_be_stmt, fn_def.ret_type().is_some()) {
170 (true, true) => "return $0;",
171 (true, false) => "return;",
172 (false, true) => "return $0",
173 (false, false) => "return",
174 }; 166 };
175 Some(keyword(ctx, "return", snip)) 167 acc.add(builder.build());
176} 168}
177 169
178#[cfg(test)] 170#[cfg(test)]
179mod tests { 171mod tests {
180 use expect_test::{expect, Expect}; 172 use expect_test::{expect, Expect};
173 use test_utils::mark;
181 174
182 use crate::{ 175 use crate::{
183 test_utils::{check_edit, completion_list}, 176 test_utils::{check_edit, completion_list},
184 CompletionKind, 177 CompletionKind,
185 }; 178 };
186 use test_utils::mark;
187 179
188 fn check(ra_fixture: &str, expect: Expect) { 180 fn check(ra_fixture: &str, expect: Expect) {
189 let actual = completion_list(ra_fixture, CompletionKind::Keyword); 181 let actual = completion_list(ra_fixture, CompletionKind::Keyword);