aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/completion/complete_record.rs101
-rw-r--r--crates/ra_ide/src/completion/completion_context.rs4
-rw-r--r--crates/ra_ide/src/completion/presentation.rs43
-rw-r--r--crates/ra_ide/src/marks.rs1
4 files changed, 93 insertions, 56 deletions
diff --git a/crates/ra_ide/src/completion/complete_record.rs b/crates/ra_ide/src/completion/complete_record.rs
index 01dd8c6db..f46bcee5c 100644
--- a/crates/ra_ide/src/completion/complete_record.rs
+++ b/crates/ra_ide/src/completion/complete_record.rs
@@ -1,67 +1,28 @@
1//! Complete fields in record literals and patterns. 1//! Complete fields in record literals and patterns.
2use crate::completion::{CompletionContext, Completions}; 2use crate::completion::{CompletionContext, Completions};
3use ra_syntax::{ast, ast::NameOwner, SmolStr};
4 3
5pub(super) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { 4pub(super) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
6 let (ty, variant, already_present_fields) = 5 let missing_fields = match (ctx.record_lit_pat.as_ref(), ctx.record_lit_syntax.as_ref()) {
7 match (ctx.record_lit_pat.as_ref(), ctx.record_lit_syntax.as_ref()) { 6 (None, None) => return None,
8 (None, None) => return None, 7 (Some(_), Some(_)) => unreachable!("A record cannot be both a literal and a pattern"),
9 (Some(_), Some(_)) => panic!("A record cannot be both a literal and a pattern"), 8 (Some(record_pat), _) => ctx.sema.record_pattern_missing_fields(record_pat),
10 (Some(record_pat), _) => ( 9 (_, Some(record_lit)) => ctx.sema.record_literal_missing_fields(record_lit),
11 ctx.sema.type_of_pat(&record_pat.clone().into())?, 10 };
12 ctx.sema.resolve_record_pattern(record_pat)?,
13 pattern_ascribed_fields(record_pat),
14 ),
15 (_, Some(record_lit)) => (
16 ctx.sema.type_of_expr(&record_lit.clone().into())?,
17 ctx.sema.resolve_record_literal(record_lit)?,
18 literal_ascribed_fields(record_lit),
19 ),
20 };
21 11
22 for (field, field_ty) in ty.variant_fields(ctx.db, variant).into_iter().filter(|(field, _)| { 12 for (field, ty) in missing_fields {
23 // FIXME: already_present_names better be `Vec<hir::Name>` 13 acc.add_field(ctx, field, &ty)
24 !already_present_fields.contains(&SmolStr::from(field.name(ctx.db).to_string()))
25 }) {
26 acc.add_field(ctx, field, &field_ty);
27 } 14 }
28 Some(())
29}
30
31fn literal_ascribed_fields(record_lit: &ast::RecordLit) -> Vec<SmolStr> {
32 record_lit
33 .record_field_list()
34 .map(|field_list| field_list.fields())
35 .map(|fields| {
36 fields
37 .into_iter()
38 .filter_map(|field| field.name_ref())
39 .map(|name_ref| name_ref.text().clone())
40 .collect()
41 })
42 .unwrap_or_default()
43}
44 15
45fn pattern_ascribed_fields(record_pat: &ast::RecordPat) -> Vec<SmolStr> { 16 Some(())
46 record_pat
47 .record_field_pat_list()
48 .map(|pat_list| {
49 pat_list
50 .record_field_pats()
51 .filter_map(|fild_pat| fild_pat.name())
52 .chain(pat_list.bind_pats().filter_map(|bind_pat| bind_pat.name()))
53 .map(|name| name.text().clone())
54 .collect()
55 })
56 .unwrap_or_default()
57} 17}
58 18
59#[cfg(test)] 19#[cfg(test)]
60mod tests { 20mod tests {
61 mod record_lit_tests { 21 mod record_pat_tests {
62 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
63 use insta::assert_debug_snapshot; 22 use insta::assert_debug_snapshot;
64 23
24 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
25
65 fn complete(code: &str) -> Vec<CompletionItem> { 26 fn complete(code: &str) -> Vec<CompletionItem> {
66 do_completion(code, CompletionKind::Reference) 27 do_completion(code, CompletionKind::Reference)
67 } 28 }
@@ -203,10 +164,11 @@ mod tests {
203 } 164 }
204 } 165 }
205 166
206 mod record_pat_tests { 167 mod record_lit_tests {
207 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
208 use insta::assert_debug_snapshot; 168 use insta::assert_debug_snapshot;
209 169
170 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
171
210 fn complete(code: &str) -> Vec<CompletionItem> { 172 fn complete(code: &str) -> Vec<CompletionItem> {
211 do_completion(code, CompletionKind::Reference) 173 do_completion(code, CompletionKind::Reference)
212 } 174 }
@@ -407,5 +369,38 @@ mod tests {
407 ] 369 ]
408 "###); 370 "###);
409 } 371 }
372
373 #[test]
374 fn completes_functional_update() {
375 let completions = complete(
376 r"
377 struct S {
378 foo1: u32,
379 foo2: u32,
380 }
381
382 fn main() {
383 let foo1 = 1;
384 let s = S {
385 foo1,
386 <|>
387 .. loop {}
388 }
389 }
390 ",
391 );
392 assert_debug_snapshot!(completions, @r###"
393 [
394 CompletionItem {
395 label: "foo2",
396 source_range: [221; 221),
397 delete: [221; 221),
398 insert: "foo2",
399 kind: Field,
400 detail: "u32",
401 },
402 ]
403 "###);
404 }
410 } 405 }
411} 406}
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs
index b8213d62f..f833d2a9a 100644
--- a/crates/ra_ide/src/completion/completion_context.rs
+++ b/crates/ra_ide/src/completion/completion_context.rs
@@ -50,6 +50,8 @@ pub(crate) struct CompletionContext<'a> {
50 pub(super) dot_receiver_is_ambiguous_float_literal: bool, 50 pub(super) dot_receiver_is_ambiguous_float_literal: bool,
51 /// If this is a call (method or function) in particular, i.e. the () are already there. 51 /// If this is a call (method or function) in particular, i.e. the () are already there.
52 pub(super) is_call: bool, 52 pub(super) is_call: bool,
53 /// If this is a macro call, i.e. the () are already there.
54 pub(super) is_macro_call: bool,
53 pub(super) is_path_type: bool, 55 pub(super) is_path_type: bool,
54 pub(super) has_type_args: bool, 56 pub(super) has_type_args: bool,
55} 57}
@@ -102,6 +104,7 @@ impl<'a> CompletionContext<'a> {
102 is_new_item: false, 104 is_new_item: false,
103 dot_receiver: None, 105 dot_receiver: None,
104 is_call: false, 106 is_call: false,
107 is_macro_call: false,
105 is_path_type: false, 108 is_path_type: false,
106 has_type_args: false, 109 has_type_args: false,
107 dot_receiver_is_ambiguous_float_literal: false, 110 dot_receiver_is_ambiguous_float_literal: false,
@@ -269,6 +272,7 @@ impl<'a> CompletionContext<'a> {
269 .and_then(ast::PathExpr::cast) 272 .and_then(ast::PathExpr::cast)
270 .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast)) 273 .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast))
271 .is_some(); 274 .is_some();
275 self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some();
272 276
273 self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); 277 self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
274 self.has_type_args = segment.type_arg_list().is_some(); 278 self.has_type_args = segment.type_arg_list().is_some();
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs
index cdfd7bc32..55f75b15a 100644
--- a/crates/ra_ide/src/completion/presentation.rs
+++ b/crates/ra_ide/src/completion/presentation.rs
@@ -174,7 +174,8 @@ impl Completions {
174 .set_deprecated(is_deprecated(macro_, ctx.db)) 174 .set_deprecated(is_deprecated(macro_, ctx.db))
175 .detail(detail); 175 .detail(detail);
176 176
177 builder = if ctx.use_item_syntax.is_some() { 177 builder = if ctx.use_item_syntax.is_some() || ctx.is_macro_call {
178 tested_by!(dont_insert_macro_call_parens_unncessary);
178 builder.insert_text(name) 179 builder.insert_text(name)
179 } else { 180 } else {
180 let macro_braces_to_insert = 181 let macro_braces_to_insert =
@@ -960,7 +961,8 @@ mod tests {
960 } 961 }
961 962
962 #[test] 963 #[test]
963 fn dont_insert_macro_call_braces_in_use() { 964 fn dont_insert_macro_call_parens_unncessary() {
965 covers!(dont_insert_macro_call_parens_unncessary);
964 assert_debug_snapshot!( 966 assert_debug_snapshot!(
965 do_reference_completion( 967 do_reference_completion(
966 r" 968 r"
@@ -986,6 +988,41 @@ mod tests {
986 }, 988 },
987 ] 989 ]
988 "### 990 "###
989 ) 991 );
992
993 assert_debug_snapshot!(
994 do_reference_completion(
995 r"
996 //- /main.rs
997 macro_rules frobnicate {
998 () => ()
999 }
1000 fn main() {
1001 frob<|>!();
1002 }
1003 "
1004 ),
1005 @r###"
1006 [
1007 CompletionItem {
1008 label: "frobnicate!",
1009 source_range: [56; 60),
1010 delete: [56; 60),
1011 insert: "frobnicate",
1012 kind: Macro,
1013 detail: "macro_rules! frobnicate",
1014 },
1015 CompletionItem {
1016 label: "main()",
1017 source_range: [56; 60),
1018 delete: [56; 60),
1019 insert: "main()$0",
1020 kind: Function,
1021 lookup: "main",
1022 detail: "fn main()",
1023 },
1024 ]
1025 "###
1026 );
990 } 1027 }
991} 1028}
diff --git a/crates/ra_ide/src/marks.rs b/crates/ra_ide/src/marks.rs
index 1236cb773..5e1f135c5 100644
--- a/crates/ra_ide/src/marks.rs
+++ b/crates/ra_ide/src/marks.rs
@@ -7,4 +7,5 @@ test_utils::marks!(
7 dont_complete_current_use 7 dont_complete_current_use
8 test_resolve_parent_module_on_module_decl 8 test_resolve_parent_module_on_module_decl
9 search_filters_by_range 9 search_filters_by_range
10 dont_insert_macro_call_parens_unncessary
10); 11);