@@ -8,42 +8,43 @@ use crate::{
88 CompletionItemKind ,
99} ;
1010
11+ fn visible_fields (
12+ ctx : & RenderContext < ' _ > ,
13+ fields : & [ hir:: Field ] ,
14+ item : impl HasAttrs ,
15+ ) -> Option < ( Vec < hir:: Field > , bool ) > {
16+ let module = ctx. completion . scope . module ( ) ?;
17+ let n_fields = fields. len ( ) ;
18+ let fields = fields
19+ . into_iter ( )
20+ . filter ( |field| field. is_visible_from ( ctx. db ( ) , module) )
21+ . copied ( )
22+ . collect :: < Vec < _ > > ( ) ;
23+
24+ let fields_omitted =
25+ n_fields - fields. len ( ) > 0 || item. attrs ( ctx. db ( ) ) . by_key ( "non_exhaustive" ) . exists ( ) ;
26+ Some ( ( fields, fields_omitted) )
27+ }
28+
1129pub ( crate ) fn render_struct_pat (
1230 ctx : RenderContext < ' _ > ,
1331 strukt : hir:: Struct ,
1432 local_name : Option < Name > ,
1533) -> Option < CompletionItem > {
1634 let _p = profile:: span ( "render_struct_pat" ) ;
1735
18- let module = ctx. completion . scope . module ( ) ?;
1936 let fields = strukt. fields ( ctx. db ( ) ) ;
20- let n_fields = fields. len ( ) ;
21- let fields = fields
22- . into_iter ( )
23- . filter ( |field| field. is_visible_from ( ctx. db ( ) , module) )
24- . collect :: < Vec < _ > > ( ) ;
37+ let ( visible_fields, fields_omitted) = visible_fields ( & ctx, & fields, strukt) ?;
2538
26- if fields . is_empty ( ) {
39+ if visible_fields . is_empty ( ) {
2740 // Matching a struct without matching its fields is pointless, unlike matching a Variant without its fields
2841 return None ;
2942 }
30- let fields_omitted =
31- n_fields - fields. len ( ) > 0 || strukt. attrs ( ctx. db ( ) ) . by_key ( "non_exhaustive" ) . exists ( ) ;
3243
3344 let name = local_name. unwrap_or_else ( || strukt. name ( ctx. db ( ) ) ) . to_string ( ) ;
34- let pat = render_pat ( & ctx, & name, strukt. kind ( ctx. db ( ) ) , & fields , fields_omitted) ?;
45+ let pat = render_pat ( & ctx, & name, strukt. kind ( ctx. db ( ) ) , & visible_fields , fields_omitted) ?;
3546
36- let mut completion = CompletionItem :: new ( CompletionKind :: Snippet , ctx. source_range ( ) , name)
37- . kind ( CompletionItemKind :: Binding )
38- . set_documentation ( ctx. docs ( strukt) )
39- . set_deprecated ( ctx. is_deprecated ( strukt) )
40- . detail ( & pat) ;
41- if let Some ( snippet_cap) = ctx. snippet_cap ( ) {
42- completion = completion. insert_snippet ( snippet_cap, pat) ;
43- } else {
44- completion = completion. insert_text ( pat) ;
45- }
46- Some ( completion. build ( ) )
47+ Some ( build_completion ( ctx, name, pat, strukt) )
4748}
4849
4950pub ( crate ) fn render_variant_pat (
@@ -53,31 +54,32 @@ pub(crate) fn render_variant_pat(
5354) -> Option < CompletionItem > {
5455 let _p = profile:: span ( "render_variant_pat" ) ;
5556
56- let module = ctx. completion . scope . module ( ) ?;
5757 let fields = variant. fields ( ctx. db ( ) ) ;
58- let n_fields = fields. len ( ) ;
59- let fields = fields
60- . into_iter ( )
61- . filter ( |field| field. is_visible_from ( ctx. db ( ) , module) )
62- . collect :: < Vec < _ > > ( ) ;
63-
64- let fields_omitted =
65- n_fields - fields. len ( ) > 0 || variant. attrs ( ctx. db ( ) ) . by_key ( "non_exhaustive" ) . exists ( ) ;
58+ let ( visible_fields, fields_omitted) = visible_fields ( & ctx, & fields, variant) ?;
6659
6760 let name = local_name. unwrap_or_else ( || variant. name ( ctx. db ( ) ) ) . to_string ( ) ;
68- let pat = render_pat ( & ctx, & name, variant. kind ( ctx. db ( ) ) , & fields , fields_omitted) ?;
61+ let pat = render_pat ( & ctx, & name, variant. kind ( ctx. db ( ) ) , & visible_fields , fields_omitted) ?;
6962
70- let mut completion = CompletionItem :: new ( CompletionKind :: Snippet , ctx. source_range ( ) , name)
63+ Some ( build_completion ( ctx, name, pat, variant) )
64+ }
65+
66+ fn build_completion (
67+ ctx : RenderContext < ' _ > ,
68+ name : String ,
69+ pat : String ,
70+ item : impl HasAttrs + Copy ,
71+ ) -> CompletionItem {
72+ let completion = CompletionItem :: new ( CompletionKind :: Snippet , ctx. source_range ( ) , name)
7173 . kind ( CompletionItemKind :: Binding )
72- . set_documentation ( ctx. docs ( variant ) )
73- . set_deprecated ( ctx. is_deprecated ( variant ) )
74+ . set_documentation ( ctx. docs ( item ) )
75+ . set_deprecated ( ctx. is_deprecated ( item ) )
7476 . detail ( & pat) ;
75- if let Some ( snippet_cap) = ctx. snippet_cap ( ) {
76- completion = completion . insert_snippet ( snippet_cap, pat) ;
77+ let completion = if let Some ( snippet_cap) = ctx. snippet_cap ( ) {
78+ completion. insert_snippet ( snippet_cap, pat)
7779 } else {
78- completion = completion . insert_text ( pat) ;
79- }
80- Some ( completion. build ( ) )
80+ completion. insert_text ( pat)
81+ } ;
82+ completion. build ( )
8183}
8284
8385fn render_pat (
0 commit comments