diff options
author | Kirill Bulatov <[email protected]> | 2020-03-31 21:39:46 +0100 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-04-01 10:49:31 +0100 |
commit | d35b94352090c201f7e0cceae43035ee0e72df98 (patch) | |
tree | 7ac4cca1aa23aa005077cc1e1e1c8911ad69eadd /crates/ra_ide/src/completion | |
parent | bc71631e75f42f7730084a307e4765c3df92791b (diff) |
Split draft
Diffstat (limited to 'crates/ra_ide/src/completion')
-rw-r--r-- | crates/ra_ide/src/completion/complete_record_literal.rs | 37 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/complete_record_pattern.rs | 47 |
2 files changed, 25 insertions, 59 deletions
diff --git a/crates/ra_ide/src/completion/complete_record_literal.rs b/crates/ra_ide/src/completion/complete_record_literal.rs index e4e764f58..8b67d3ba2 100644 --- a/crates/ra_ide/src/completion/complete_record_literal.rs +++ b/crates/ra_ide/src/completion/complete_record_literal.rs | |||
@@ -1,36 +1,19 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use super::get_missing_fields; | ||
3 | use crate::completion::{CompletionContext, Completions}; | 4 | use crate::completion::{CompletionContext, Completions}; |
4 | use ra_syntax::SmolStr; | 5 | use either::Either; |
5 | 6 | ||
6 | /// Complete fields in fields literals. | 7 | /// Complete fields in fields literals. |
7 | pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionContext) { | 8 | pub(super) fn complete_record_literal( |
8 | let (ty, variant) = match ctx.record_lit_syntax.as_ref().and_then(|it| { | 9 | acc: &mut Completions, |
9 | Some((ctx.sema.type_of_expr(&it.clone().into())?, ctx.sema.resolve_record_literal(it)?)) | 10 | ctx: &CompletionContext, |
10 | }) { | 11 | ) -> Option<()> { |
11 | Some(it) => it, | 12 | let record_lit = ctx.record_lit_syntax.as_ref()?; |
12 | _ => return, | 13 | for (field, field_ty) in get_missing_fields(ctx, Either::Left(record_lit))? { |
13 | }; | 14 | acc.add_field(ctx, field, &field_ty); |
14 | |||
15 | let already_present_names: Vec<SmolStr> = ctx | ||
16 | .record_lit_syntax | ||
17 | .as_ref() | ||
18 | .and_then(|record_literal| record_literal.record_field_list()) | ||
19 | .map(|field_list| field_list.fields()) | ||
20 | .map(|fields| { | ||
21 | fields | ||
22 | .into_iter() | ||
23 | .filter_map(|field| field.name_ref()) | ||
24 | .map(|name_ref| name_ref.text().clone()) | ||
25 | .collect() | ||
26 | }) | ||
27 | .unwrap_or_default(); | ||
28 | |||
29 | for (field, field_ty) in ty.variant_fields(ctx.db, variant) { | ||
30 | if !already_present_names.contains(&SmolStr::from(field.name(ctx.db).to_string())) { | ||
31 | acc.add_field(ctx, field, &field_ty); | ||
32 | } | ||
33 | } | 15 | } |
16 | Some(()) | ||
34 | } | 17 | } |
35 | 18 | ||
36 | #[cfg(test)] | 19 | #[cfg(test)] |
diff --git a/crates/ra_ide/src/completion/complete_record_pattern.rs b/crates/ra_ide/src/completion/complete_record_pattern.rs index 78315eeb8..f94dced04 100644 --- a/crates/ra_ide/src/completion/complete_record_pattern.rs +++ b/crates/ra_ide/src/completion/complete_record_pattern.rs | |||
@@ -1,35 +1,18 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use super::get_missing_fields; | ||
3 | use crate::completion::{CompletionContext, Completions}; | 4 | use crate::completion::{CompletionContext, Completions}; |
4 | use ra_syntax::{ast::NameOwner, SmolStr}; | 5 | use either::Either; |
5 | 6 | ||
6 | pub(super) fn complete_record_pattern(acc: &mut Completions, ctx: &CompletionContext) { | 7 | pub(super) fn complete_record_pattern( |
7 | let (ty, variant) = match ctx.record_lit_pat.as_ref().and_then(|it| { | 8 | acc: &mut Completions, |
8 | Some((ctx.sema.type_of_pat(&it.clone().into())?, ctx.sema.resolve_record_pattern(it)?)) | 9 | ctx: &CompletionContext, |
9 | }) { | 10 | ) -> Option<()> { |
10 | Some(it) => it, | 11 | let record_pat = ctx.record_lit_pat.as_ref()?; |
11 | _ => return, | 12 | for (field, field_ty) in get_missing_fields(ctx, Either::Right(record_pat))? { |
12 | }; | 13 | acc.add_field(ctx, field, &field_ty); |
13 | |||
14 | let already_present_names: Vec<SmolStr> = ctx | ||
15 | .record_lit_pat | ||
16 | .as_ref() | ||
17 | .and_then(|record_pat| record_pat.record_field_pat_list()) | ||
18 | .map(|pat_list| pat_list.bind_pats()) | ||
19 | .map(|bind_pats| { | ||
20 | bind_pats | ||
21 | .into_iter() | ||
22 | .filter_map(|pat| pat.name()) | ||
23 | .map(|name| name.text().clone()) | ||
24 | .collect() | ||
25 | }) | ||
26 | .unwrap_or_default(); | ||
27 | |||
28 | for (field, field_ty) in ty.variant_fields(ctx.db, variant) { | ||
29 | if !already_present_names.contains(&SmolStr::from(field.name(ctx.db).to_string())) { | ||
30 | acc.add_field(ctx, field, &field_ty); | ||
31 | } | ||
32 | } | 14 | } |
15 | Some(()) | ||
33 | } | 16 | } |
34 | 17 | ||
35 | #[cfg(test)] | 18 | #[cfg(test)] |
@@ -151,7 +134,7 @@ mod tests { | |||
151 | bar: 3, | 134 | bar: 3, |
152 | baz: 4, | 135 | baz: 4, |
153 | }; | 136 | }; |
154 | if let S { foo1, foo2, <|> } = s {} | 137 | if let S { foo1, foo2: a, <|> } = s {} |
155 | } | 138 | } |
156 | ", | 139 | ", |
157 | ); | 140 | ); |
@@ -159,16 +142,16 @@ mod tests { | |||
159 | [ | 142 | [ |
160 | CompletionItem { | 143 | CompletionItem { |
161 | label: "bar", | 144 | label: "bar", |
162 | source_range: [369; 369), | 145 | source_range: [372; 372), |
163 | delete: [369; 369), | 146 | delete: [372; 372), |
164 | insert: "bar", | 147 | insert: "bar", |
165 | kind: Field, | 148 | kind: Field, |
166 | detail: "u32", | 149 | detail: "u32", |
167 | }, | 150 | }, |
168 | CompletionItem { | 151 | CompletionItem { |
169 | label: "baz", | 152 | label: "baz", |
170 | source_range: [369; 369), | 153 | source_range: [372; 372), |
171 | delete: [369; 369), | 154 | delete: [372; 372), |
172 | insert: "baz", | 155 | insert: "baz", |
173 | kind: Field, | 156 | kind: Field, |
174 | detail: "u32", | 157 | detail: "u32", |