Skip to content

Commit ba960e8

Browse files
committed
better bitset completion
1 parent e53da4d commit ba960e8

File tree

1 file changed

+159
-80
lines changed

1 file changed

+159
-80
lines changed

src/server/completion.odin

+159-80
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,40 @@ get_selector_completion :: proc(
517517
append(&items, item)
518518
}
519519

520+
case SymbolBitSetValue:
521+
list.isIncomplete = false
522+
523+
enumv, ok := unwrap_bitset(ast_context, selector)
524+
if !ok { break }
525+
526+
range, rok := get_range_from_selection_start_to_dot(position_context)
527+
if !rok { break }
528+
529+
range.end.character -= 1
530+
531+
variable, vok := position_context.selector.derived_expr.(^ast.Ident)
532+
if !vok { break }
533+
534+
remove_edit := TextEdit {
535+
range = {
536+
start = range.start,
537+
end = range.end,
538+
},
539+
newText = "",
540+
}
541+
542+
additionalTextEdits := make([]TextEdit, 1, context.temp_allocator)
543+
additionalTextEdits[0] = remove_edit
544+
545+
for name in enumv.names {
546+
append(&items, CompletionItem {
547+
label = fmt.tprintf(".%s", name),
548+
kind = .EnumMember,
549+
detail = fmt.tprintf("%s.%s", selector.name, name),
550+
additionalTextEdits = additionalTextEdits,
551+
})
552+
}
553+
520554
case SymbolStructValue:
521555
list.isIncomplete = false
522556

@@ -669,11 +703,31 @@ get_implicit_completion :: proc(
669703
//value decl infer a : My_Enum = .*
670704
if position_context.value_decl != nil &&
671705
position_context.value_decl.type != nil {
672-
if enum_value, ok := unwrap_enum(
706+
enum_value: Maybe(SymbolEnumValue)
707+
708+
if _enum_value, ok := unwrap_enum(
673709
ast_context,
674710
position_context.value_decl.type,
675711
); ok {
676-
for name in enum_value.names {
712+
enum_value = _enum_value
713+
}
714+
715+
if position_context.comp_lit != nil {
716+
if bitset_symbol, ok := resolve_type_expression(
717+
ast_context,
718+
position_context.value_decl.type,
719+
); ok {
720+
if _enum_value, ok := unwrap_bitset(
721+
ast_context,
722+
bitset_symbol,
723+
); ok {
724+
enum_value = _enum_value
725+
}
726+
}
727+
}
728+
729+
if ev, ok := enum_value.?; ok {
730+
for name in ev.names {
677731
item := CompletionItem {
678732
label = name,
679733
kind = .EnumMember,
@@ -790,114 +844,112 @@ get_implicit_completion :: proc(
790844

791845
//infer bitset and enums based on the identifier comp_lit, i.e. a := My_Struct { my_ident = . }
792846
if position_context.comp_lit != nil {
793-
if position_context.parent_comp_lit.type == nil {
794-
return
795-
}
796-
797-
field_name: string
847+
if position_context.parent_comp_lit.type != nil {
848+
field_name: string
798849

799-
if position_context.field_value != nil {
800-
if field, ok := position_context.field_value.field.derived.(^ast.Ident);
801-
ok {
802-
field_name = field.name
803-
} else {
804-
return
850+
if position_context.field_value != nil {
851+
if field, ok := position_context.field_value.field.derived.(^ast.Ident);
852+
ok {
853+
field_name = field.name
854+
} else {
855+
return
856+
}
805857
}
806-
}
807858

808-
if symbol, ok := resolve_type_expression(
809-
ast_context,
810-
position_context.parent_comp_lit.type,
811-
); ok {
812-
if comp_symbol, comp_lit, ok := resolve_type_comp_literal(
859+
if symbol, ok := resolve_type_expression(
813860
ast_context,
814-
position_context,
815-
symbol,
816-
position_context.parent_comp_lit,
861+
position_context.parent_comp_lit.type,
817862
); ok {
818-
if s, ok := comp_symbol.value.(SymbolStructValue); ok {
819-
ast_context.current_package = comp_symbol.pkg
820-
821-
//We can either have the final
822-
elem_index := -1
823-
824-
for elem, i in comp_lit.elems {
825-
if position_in_node(elem, position_context.position) {
826-
elem_index = i
827-
}
828-
}
863+
if comp_symbol, comp_lit, ok := resolve_type_comp_literal(
864+
ast_context,
865+
position_context,
866+
symbol,
867+
position_context.parent_comp_lit,
868+
); ok {
869+
if s, ok := comp_symbol.value.(SymbolStructValue); ok {
870+
ast_context.current_package = comp_symbol.pkg
829871

830-
type: ^ast.Expr
872+
//We can either have the final
873+
elem_index := -1
831874

832-
for name, i in s.names {
833-
if name != field_name {
834-
continue
875+
for elem, i in comp_lit.elems {
876+
if position_in_node(elem, position_context.position) {
877+
elem_index = i
878+
}
835879
}
836880

837-
type = s.types[i]
838-
break
839-
}
840-
841-
if type == nil && len(s.types) > elem_index {
842-
type = s.types[elem_index]
843-
}
881+
type: ^ast.Expr
844882

845-
if enum_value, ok := unwrap_enum(ast_context, type); ok {
846-
for enum_name in enum_value.names {
847-
item := CompletionItem {
848-
label = enum_name,
849-
kind = .EnumMember,
850-
detail = enum_name,
883+
for name, i in s.names {
884+
if name != field_name {
885+
continue
851886
}
852887

853-
append(&items, item)
888+
type = s.types[i]
889+
break
854890
}
855891

856-
list.items = items[:]
857-
return
858-
} else if bitset_symbol, ok := resolve_type_expression(
859-
ast_context,
860-
type,
861-
); ok {
862-
ast_context.current_package = bitset_symbol.pkg
863-
864-
if value, ok := unwrap_bitset(
865-
ast_context,
866-
bitset_symbol,
867-
); ok {
868-
for name in value.names {
892+
if type == nil && len(s.types) > elem_index {
893+
type = s.types[elem_index]
894+
}
869895

896+
if enum_value, ok := unwrap_enum(ast_context, type); ok {
897+
for enum_name in enum_value.names {
870898
item := CompletionItem {
871-
label = name,
899+
label = enum_name,
872900
kind = .EnumMember,
873-
detail = name,
901+
detail = enum_name,
874902
}
875903

876904
append(&items, item)
877905
}
906+
878907
list.items = items[:]
879908
return
909+
} else if bitset_symbol, ok := resolve_type_expression(
910+
ast_context,
911+
type,
912+
); ok {
913+
ast_context.current_package = bitset_symbol.pkg
914+
915+
if value, ok := unwrap_bitset(
916+
ast_context,
917+
bitset_symbol,
918+
); ok {
919+
for name in value.names {
920+
921+
item := CompletionItem {
922+
label = name,
923+
kind = .EnumMember,
924+
detail = name,
925+
}
926+
927+
append(&items, item)
928+
}
929+
list.items = items[:]
930+
return
931+
}
880932
}
881-
}
882-
} else if s, ok := unwrap_bitset(ast_context, comp_symbol);
883-
ok {
884-
for enum_name in s.names {
885-
item := CompletionItem {
886-
label = enum_name,
887-
kind = .EnumMember,
888-
detail = enum_name,
933+
} else if s, ok := unwrap_bitset(ast_context, comp_symbol);
934+
ok {
935+
for enum_name in s.names {
936+
item := CompletionItem {
937+
label = enum_name,
938+
kind = .EnumMember,
939+
detail = enum_name,
940+
}
941+
942+
append(&items, item)
889943
}
890944

891-
append(&items, item)
945+
list.items = items[:]
946+
return
892947
}
893-
894-
list.items = items[:]
895-
return
896948
}
897949
}
898-
}
899950

900-
reset_ast_context(ast_context)
951+
reset_ast_context(ast_context)
952+
}
901953
}
902954

903955
if position_context.binary != nil &&
@@ -1066,6 +1118,33 @@ get_implicit_completion :: proc(
10661118
list.items = items[:]
10671119
return
10681120
}
1121+
1122+
// Bitset comp literal in parameter, eg: `hello({ . })`.
1123+
if position_context.comp_lit != nil {
1124+
if bitset_symbol, ok := resolve_type_expression(
1125+
ast_context,
1126+
proc_value.arg_types[parameter_index].type,
1127+
); ok {
1128+
if enum_value, ok := unwrap_bitset(
1129+
ast_context,
1130+
bitset_symbol,
1131+
); ok {
1132+
for name in enum_value.names {
1133+
item := CompletionItem {
1134+
label = name,
1135+
kind = .EnumMember,
1136+
detail = name,
1137+
}
1138+
1139+
append(&items, item)
1140+
}
1141+
1142+
list.items = items[:]
1143+
return
1144+
}
1145+
}
1146+
}
1147+
10691148
} else if enum_value, ok := symbol.value.(SymbolEnumValue);
10701149
ok {
10711150
for name in enum_value.names {

0 commit comments

Comments
 (0)