diff options
author | Kirill Bulatov <[email protected]> | 2020-03-31 22:50:45 +0100 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-04-01 11:03:56 +0100 |
commit | 1335608dae47cf8baa1dd2e8809264b030e85c3e (patch) | |
tree | 82146ddc9953c579aa779135d6d06b7a0366f484 /crates/ra_ide/src/completion.rs | |
parent | d35b94352090c201f7e0cceae43035ee0e72df98 (diff) |
Unite record completion logic into a single module
Diffstat (limited to 'crates/ra_ide/src/completion.rs')
-rw-r--r-- | crates/ra_ide/src/completion.rs | 66 |
1 files changed, 2 insertions, 64 deletions
diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs index 67dfd6f2e..93157bbba 100644 --- a/crates/ra_ide/src/completion.rs +++ b/crates/ra_ide/src/completion.rs | |||
@@ -5,8 +5,7 @@ mod completion_context; | |||
5 | mod presentation; | 5 | mod presentation; |
6 | 6 | ||
7 | mod complete_dot; | 7 | mod complete_dot; |
8 | mod complete_record_literal; | 8 | mod complete_record; |
9 | mod complete_record_pattern; | ||
10 | mod complete_pattern; | 9 | mod complete_pattern; |
11 | mod complete_fn_param; | 10 | mod complete_fn_param; |
12 | mod complete_keyword; | 11 | mod complete_keyword; |
@@ -32,12 +31,6 @@ use crate::{ | |||
32 | pub use crate::completion::completion_item::{ | 31 | pub use crate::completion::completion_item::{ |
33 | CompletionItem, CompletionItemKind, InsertTextFormat, | 32 | CompletionItem, CompletionItemKind, InsertTextFormat, |
34 | }; | 33 | }; |
35 | use either::Either; | ||
36 | use hir::{StructField, Type}; | ||
37 | use ra_syntax::{ | ||
38 | ast::{self, NameOwner}, | ||
39 | SmolStr, | ||
40 | }; | ||
41 | 34 | ||
42 | #[derive(Clone, Debug, PartialEq, Eq)] | 35 | #[derive(Clone, Debug, PartialEq, Eq)] |
43 | pub struct CompletionConfig { | 36 | pub struct CompletionConfig { |
@@ -95,8 +88,7 @@ pub(crate) fn completions( | |||
95 | complete_path::complete_path(&mut acc, &ctx); | 88 | complete_path::complete_path(&mut acc, &ctx); |
96 | complete_scope::complete_scope(&mut acc, &ctx); | 89 | complete_scope::complete_scope(&mut acc, &ctx); |
97 | complete_dot::complete_dot(&mut acc, &ctx); | 90 | complete_dot::complete_dot(&mut acc, &ctx); |
98 | complete_record_literal::complete_record_literal(&mut acc, &ctx); | 91 | complete_record::complete_record(&mut acc, &ctx); |
99 | complete_record_pattern::complete_record_pattern(&mut acc, &ctx); | ||
100 | complete_pattern::complete_pattern(&mut acc, &ctx); | 92 | complete_pattern::complete_pattern(&mut acc, &ctx); |
101 | complete_postfix::complete_postfix(&mut acc, &ctx); | 93 | complete_postfix::complete_postfix(&mut acc, &ctx); |
102 | complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); | 94 | complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); |
@@ -104,57 +96,3 @@ pub(crate) fn completions( | |||
104 | 96 | ||
105 | Some(acc) | 97 | Some(acc) |
106 | } | 98 | } |
107 | |||
108 | pub(crate) fn get_missing_fields( | ||
109 | ctx: &CompletionContext, | ||
110 | record: Either<&ast::RecordLit, &ast::RecordPat>, | ||
111 | ) -> Option<Vec<(StructField, Type)>> { | ||
112 | let (ty, variant) = match record { | ||
113 | Either::Left(record_lit) => ( | ||
114 | ctx.sema.type_of_expr(&record_lit.clone().into())?, | ||
115 | ctx.sema.resolve_record_literal(record_lit)?, | ||
116 | ), | ||
117 | Either::Right(record_pat) => ( | ||
118 | ctx.sema.type_of_pat(&record_pat.clone().into())?, | ||
119 | ctx.sema.resolve_record_pattern(record_pat)?, | ||
120 | ), | ||
121 | }; | ||
122 | |||
123 | let already_present_names = get_already_present_names(record); | ||
124 | Some( | ||
125 | ty.variant_fields(ctx.db, variant) | ||
126 | .into_iter() | ||
127 | .filter(|(field, _)| { | ||
128 | !already_present_names.contains(&SmolStr::from(field.name(ctx.db).to_string())) | ||
129 | }) | ||
130 | .collect(), | ||
131 | ) | ||
132 | } | ||
133 | |||
134 | fn get_already_present_names(record: Either<&ast::RecordLit, &ast::RecordPat>) -> Vec<SmolStr> { | ||
135 | // TODO kb have a single match | ||
136 | match record { | ||
137 | Either::Left(record_lit) => record_lit | ||
138 | .record_field_list() | ||
139 | .map(|field_list| field_list.fields()) | ||
140 | .map(|fields| { | ||
141 | fields | ||
142 | .into_iter() | ||
143 | .filter_map(|field| field.name_ref()) | ||
144 | .map(|name_ref| name_ref.text().clone()) | ||
145 | .collect() | ||
146 | }) | ||
147 | .unwrap_or_default(), | ||
148 | Either::Right(record_pat) => record_pat | ||
149 | .record_field_pat_list() | ||
150 | .map(|pat_list| pat_list.bind_pats()) | ||
151 | .map(|bind_pats| { | ||
152 | bind_pats | ||
153 | .into_iter() | ||
154 | .filter_map(|pat| pat.name()) | ||
155 | .map(|name| name.text().clone()) | ||
156 | .collect() | ||
157 | }) | ||
158 | .unwrap_or_default(), | ||
159 | } | ||
160 | } | ||