@@ -106,6 +106,7 @@ struct NodeFinder<'a> {
106
106
nesting : usize ,
107
107
/// The line where an auto_import must be inserted
108
108
auto_import_line : usize ,
109
+ self_type : Option < Type > ,
109
110
}
110
111
111
112
impl < ' a > NodeFinder < ' a > {
@@ -147,6 +148,7 @@ impl<'a> NodeFinder<'a> {
147
148
suggested_module_def_ids : HashSet :: new ( ) ,
148
149
nesting : 0 ,
149
150
auto_import_line : 0 ,
151
+ self_type : None ,
150
152
}
151
153
}
152
154
@@ -191,8 +193,9 @@ impl<'a> NodeFinder<'a> {
191
193
fields. remove ( & field. 0 . contents ) ;
192
194
}
193
195
196
+ let self_prefix = false ;
194
197
for ( field, typ) in fields {
195
- self . completion_items . push ( struct_field_completion_item ( field, typ) ) ;
198
+ self . completion_items . push ( struct_field_completion_item ( field, typ, self_prefix ) ) ;
196
199
}
197
200
}
198
201
@@ -293,6 +296,7 @@ impl<'a> NodeFinder<'a> {
293
296
& prefix,
294
297
FunctionKind :: Any ,
295
298
function_completion_kind,
299
+ false , // self_prefix
296
300
) ;
297
301
return ;
298
302
}
@@ -308,6 +312,7 @@ impl<'a> NodeFinder<'a> {
308
312
& prefix,
309
313
FunctionKind :: Any ,
310
314
function_completion_kind,
315
+ false , // self_prefix
311
316
) ;
312
317
return ;
313
318
}
@@ -340,6 +345,15 @@ impl<'a> NodeFinder<'a> {
340
345
self . local_variables_completion ( & prefix) ;
341
346
self . builtin_functions_completion ( & prefix, function_completion_kind) ;
342
347
self . builtin_values_completion ( & prefix) ;
348
+ if let Some ( self_type) = & self . self_type {
349
+ let self_prefix = true ;
350
+ self . complete_type_fields_and_methods (
351
+ & self_type. clone ( ) ,
352
+ & prefix,
353
+ function_completion_kind,
354
+ self_prefix,
355
+ ) ;
356
+ }
343
357
}
344
358
RequestedItems :: OnlyTypes => {
345
359
self . builtin_types_completion ( & prefix) ;
@@ -518,16 +532,18 @@ impl<'a> NodeFinder<'a> {
518
532
typ : & Type ,
519
533
prefix : & str ,
520
534
function_completion_kind : FunctionCompletionKind ,
535
+ self_prefix : bool ,
521
536
) {
522
537
match typ {
523
538
Type :: Struct ( struct_type, generics) => {
524
- self . complete_struct_fields ( & struct_type. borrow ( ) , generics, prefix) ;
539
+ self . complete_struct_fields ( & struct_type. borrow ( ) , generics, prefix, self_prefix ) ;
525
540
}
526
541
Type :: MutableReference ( typ) => {
527
542
return self . complete_type_fields_and_methods (
528
543
typ,
529
544
prefix,
530
545
function_completion_kind,
546
+ self_prefix,
531
547
) ;
532
548
}
533
549
Type :: Alias ( type_alias, _) => {
@@ -536,10 +552,11 @@ impl<'a> NodeFinder<'a> {
536
552
& type_alias. typ ,
537
553
prefix,
538
554
function_completion_kind,
555
+ self_prefix,
539
556
) ;
540
557
}
541
558
Type :: Tuple ( types) => {
542
- self . complete_tuple_fields ( types) ;
559
+ self . complete_tuple_fields ( types, self_prefix ) ;
543
560
}
544
561
Type :: FieldElement
545
562
| Type :: Array ( _, _)
@@ -565,6 +582,7 @@ impl<'a> NodeFinder<'a> {
565
582
prefix,
566
583
FunctionKind :: SelfType ( typ) ,
567
584
function_completion_kind,
585
+ self_prefix,
568
586
) ;
569
587
}
570
588
@@ -574,6 +592,7 @@ impl<'a> NodeFinder<'a> {
574
592
prefix : & str ,
575
593
function_kind : FunctionKind ,
576
594
function_completion_kind : FunctionCompletionKind ,
595
+ self_prefix : bool ,
577
596
) {
578
597
let Some ( methods_by_name) = self . interner . get_type_methods ( typ) else {
579
598
return ;
@@ -587,6 +606,7 @@ impl<'a> NodeFinder<'a> {
587
606
func_id,
588
607
function_completion_kind,
589
608
function_kind,
609
+ self_prefix,
590
610
) {
591
611
self . completion_items . push ( completion_item) ;
592
612
self . suggested_module_def_ids . insert ( ModuleDefId :: FunctionId ( func_id) ) ;
@@ -603,13 +623,16 @@ impl<'a> NodeFinder<'a> {
603
623
function_kind : FunctionKind ,
604
624
function_completion_kind : FunctionCompletionKind ,
605
625
) {
626
+ let self_prefix = false ;
627
+
606
628
for ( name, func_id) in & trait_. method_ids {
607
629
if name_matches ( name, prefix) {
608
630
if let Some ( completion_item) = self . function_completion_item (
609
631
name,
610
632
* func_id,
611
633
function_completion_kind,
612
634
function_kind,
635
+ self_prefix,
613
636
) {
614
637
self . completion_items . push ( completion_item) ;
615
638
self . suggested_module_def_ids . insert ( ModuleDefId :: FunctionId ( * func_id) ) ;
@@ -623,17 +646,19 @@ impl<'a> NodeFinder<'a> {
623
646
struct_type : & StructType ,
624
647
generics : & [ Type ] ,
625
648
prefix : & str ,
649
+ self_prefix : bool ,
626
650
) {
627
651
for ( name, typ) in & struct_type. get_fields ( generics) {
628
652
if name_matches ( name, prefix) {
629
- self . completion_items . push ( struct_field_completion_item ( name, typ) ) ;
653
+ self . completion_items . push ( struct_field_completion_item ( name, typ, self_prefix ) ) ;
630
654
}
631
655
}
632
656
}
633
657
634
- fn complete_tuple_fields ( & mut self , types : & [ Type ] ) {
658
+ fn complete_tuple_fields ( & mut self , types : & [ Type ] , self_prefix : bool ) {
635
659
for ( index, typ) in types. iter ( ) . enumerate ( ) {
636
- self . completion_items . push ( field_completion_item ( & index. to_string ( ) , typ. to_string ( ) ) ) ;
660
+ let name = index. to_string ( ) ;
661
+ self . completion_items . push ( field_completion_item ( & name, typ. to_string ( ) , self_prefix) ) ;
637
662
}
638
663
}
639
664
@@ -761,6 +786,23 @@ impl<'a> NodeFinder<'a> {
761
786
None
762
787
}
763
788
789
+ fn try_set_self_type ( & mut self , pattern : & Pattern ) {
790
+ match pattern {
791
+ Pattern :: Identifier ( ident) => {
792
+ if ident. 0 . contents == "self" {
793
+ let location = Location :: new ( ident. span ( ) , self . file ) ;
794
+ if let Some ( ReferenceId :: Local ( definition_id) ) =
795
+ self . interner . find_referenced ( location)
796
+ {
797
+ self . self_type = Some ( self . interner . definition_type ( definition_id) ) ;
798
+ }
799
+ }
800
+ }
801
+ Pattern :: Mutable ( pattern, ..) => self . try_set_self_type ( pattern) ,
802
+ Pattern :: Tuple ( ..) | Pattern :: Struct ( ..) => ( ) ,
803
+ }
804
+ }
805
+
764
806
fn includes_span ( & self , span : Span ) -> bool {
765
807
span. start ( ) as usize <= self . byte_index && self . byte_index <= span. end ( ) as usize
766
808
}
@@ -817,6 +859,7 @@ impl<'a> Visitor for NodeFinder<'a> {
817
859
self . collect_type_parameters_in_generics ( & noir_function. def . generics ) ;
818
860
819
861
for param in & noir_function. def . parameters {
862
+ self . try_set_self_type ( & param. pattern ) ;
820
863
param. typ . accept ( self ) ;
821
864
}
822
865
@@ -830,6 +873,7 @@ impl<'a> Visitor for NodeFinder<'a> {
830
873
noir_function. def . body . accept ( Some ( span) , self ) ;
831
874
832
875
self . type_parameters = old_type_parameters;
876
+ self . self_type = None ;
833
877
834
878
false
835
879
}
@@ -945,7 +989,13 @@ impl<'a> Visitor for NodeFinder<'a> {
945
989
if let Some ( typ) = self . interner . type_at_location ( location) {
946
990
let typ = typ. follow_bindings ( ) ;
947
991
let prefix = "" ;
948
- self . complete_type_fields_and_methods ( & typ, prefix, FunctionCompletionKind :: Name ) ;
992
+ let self_prefix = false ;
993
+ self . complete_type_fields_and_methods (
994
+ & typ,
995
+ prefix,
996
+ FunctionCompletionKind :: Name ,
997
+ self_prefix,
998
+ ) ;
949
999
return false ;
950
1000
}
951
1001
}
@@ -973,7 +1023,13 @@ impl<'a> Visitor for NodeFinder<'a> {
973
1023
let offset =
974
1024
self . byte_index - method_call_expression. method_name . span ( ) . start ( ) as usize ;
975
1025
let prefix = prefix[ 0 ..offset] . to_string ( ) ;
976
- self . complete_type_fields_and_methods ( & typ, & prefix, FunctionCompletionKind :: Name ) ;
1026
+ let self_prefix = false ;
1027
+ self . complete_type_fields_and_methods (
1028
+ & typ,
1029
+ & prefix,
1030
+ FunctionCompletionKind :: Name ,
1031
+ self_prefix,
1032
+ ) ;
977
1033
return false ;
978
1034
}
979
1035
}
@@ -1042,10 +1098,12 @@ impl<'a> Visitor for NodeFinder<'a> {
1042
1098
{
1043
1099
let typ = self . interner . definition_type ( definition_id) ;
1044
1100
let prefix = "" ;
1101
+ let self_prefix = false ;
1045
1102
self . complete_type_fields_and_methods (
1046
1103
& typ,
1047
1104
prefix,
1048
1105
FunctionCompletionKind :: NameAndParameters ,
1106
+ self_prefix,
1049
1107
) ;
1050
1108
}
1051
1109
}
@@ -1072,10 +1130,12 @@ impl<'a> Visitor for NodeFinder<'a> {
1072
1130
if let Some ( typ) = self . interner . type_at_location ( location) {
1073
1131
let typ = typ. follow_bindings ( ) ;
1074
1132
let prefix = "" ;
1133
+ let self_prefix = false ;
1075
1134
self . complete_type_fields_and_methods (
1076
1135
& typ,
1077
1136
prefix,
1078
1137
FunctionCompletionKind :: NameAndParameters ,
1138
+ self_prefix,
1079
1139
) ;
1080
1140
}
1081
1141
}
@@ -1136,10 +1196,12 @@ impl<'a> Visitor for NodeFinder<'a> {
1136
1196
if let Some ( typ) = self . interner . type_at_location ( location) {
1137
1197
let typ = typ. follow_bindings ( ) ;
1138
1198
let prefix = ident. to_string ( ) . to_case ( Case :: Snake ) ;
1199
+ let self_prefix = false ;
1139
1200
self . complete_type_fields_and_methods (
1140
1201
& typ,
1141
1202
& prefix,
1142
1203
FunctionCompletionKind :: NameAndParameters ,
1204
+ self_prefix,
1143
1205
) ;
1144
1206
return false ;
1145
1207
}
0 commit comments