Skip to content

Commit 5e28ecb

Browse files
committed
change signature from underlineType to appendTypeModifiers
1 parent 6cb8435 commit 5e28ecb

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

gopls/internal/golang/semtok.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (tv *tokenVisitor) comment(c *ast.Comment, importByName map[string]*types.P
217217
case *types.Func:
218218
return semtok.TokFunction, nil
219219
case *types.TypeName:
220-
return underlineType(obj)
220+
return semtok.TokType, appendTypeModifiers(nil, obj)
221221
case *types.Const, *types.Var:
222222
return semtok.TokVariable, nil
223223
default:
@@ -484,29 +484,45 @@ func (tv *tokenVisitor) inspect(n ast.Node) (descend bool) {
484484
return true
485485
}
486486

487-
func underlineType(obj types.Object) (semtok.TokenType, []string) {
488-
switch obj.Type().Underlying().(type) {
487+
// appendTypeModifiers appends optional modifiers that describe the top-level
488+
// type constructor of obj.Type(): "pointer", "map", etc.
489+
func appendTypeModifiers(mods []string, obj types.Object) []string {
490+
switch t := obj.Type().Underlying().(type) {
489491
case *types.Interface:
490-
return semtok.TokType, []string{"interface"}
492+
mods = append(mods, "interface")
491493
case *types.Struct:
492-
return semtok.TokType, []string{"struct"}
494+
mods = append(mods, "struct")
493495
case *types.Signature:
494-
return semtok.TokType, []string{"signature"}
496+
mods = append(mods, "signature")
495497
case *types.Pointer:
496-
return semtok.TokType, []string{"pointer"}
498+
mods = append(mods, "pointer")
497499
case *types.Array:
498-
return semtok.TokType, []string{"array"}
500+
mods = append(mods, "array")
499501
case *types.Map:
500-
return semtok.TokType, []string{"map"}
502+
mods = append(mods, "map")
501503
case *types.Slice:
502-
return semtok.TokType, []string{"slice"}
504+
mods = append(mods, "slice")
503505
case *types.Chan:
504-
return semtok.TokType, []string{"chan"}
506+
mods = append(mods, "chan")
505507
case *types.Basic:
506-
return semtok.TokType, []string{"defaultLibrary"}
507-
default:
508-
return semtok.TokType, nil
508+
mods = append(mods, "defaultLibrary")
509+
switch t.Kind() {
510+
case types.Invalid:
511+
mods = append(mods, "invalid")
512+
case types.String:
513+
mods = append(mods, "string")
514+
case types.Bool:
515+
mods = append(mods, "bool")
516+
case types.UnsafePointer:
517+
mods = append(mods, "pointer")
518+
default:
519+
info := t.Info()
520+
if info == types.IsFloat || info == types.IsComplex || info == types.IsInteger {
521+
mods = append(mods, "number")
522+
}
523+
}
509524
}
525+
return mods
510526
}
511527

512528
func (tv *tokenVisitor) ident(id *ast.Ident) {
@@ -562,8 +578,7 @@ func (tv *tokenVisitor) ident(id *ast.Ident) {
562578
if is[*types.TypeParam](aliases.Unalias(obj.Type())) {
563579
emit(semtok.TokTypeParam)
564580
} else {
565-
tok, mods := underlineType(obj)
566-
emit(tok, mods...)
581+
emit(semtok.TokType, appendTypeModifiers(nil, obj)...)
567582
}
568583
case *types.Var:
569584
if is[*types.Signature](aliases.Unalias(obj.Type())) {
@@ -820,15 +835,11 @@ func (tv *tokenVisitor) definitionFor(id *ast.Ident, obj types.Object) (semtok.T
820835
if fld, ok := fldm.(*ast.Field); ok {
821836
// if len(fld.names) == 0 this is a semtok.TokType, being used
822837
if len(fld.Names) == 0 {
823-
tok, mods := underlineType(obj)
824-
modifiers = append(modifiers, mods...)
825-
return tok, modifiers
838+
return semtok.TokType, appendTypeModifiers(nil, obj)
826839
}
827840
return semtok.TokVariable, modifiers
828841
}
829-
tok, mods := underlineType(obj)
830-
modifiers = append(modifiers, mods...)
831-
return tok, modifiers
842+
return semtok.TokType, appendTypeModifiers(modifiers, obj)
832843
}
833844
}
834845
// can't happen

gopls/internal/protocol/semantic.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var (
5252
semanticModifiers = [...]string{
5353
"declaration", "definition", "readonly", "static",
5454
"deprecated", "abstract", "async", "modification", "documentation", "defaultLibrary",
55-
"interface", "struct", "signature", "pointer", "array", "map", "slice", "chan",
55+
// Additional modifiers
56+
"interface", "struct", "signature", "pointer", "array", "map", "slice", "chan", "string", "number", "bool", "invalid",
5657
}
5758
)

gopls/internal/test/integration/fake/editor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func clientCapabilities(cfg EditorConfig) (protocol.ClientCapabilities, error) {
354354
"declaration", "definition", "readonly", "static",
355355
"deprecated", "abstract", "async", "modification", "documentation", "defaultLibrary",
356356
// Additional modifiers supported by this client:
357-
"interface", "struct", "signature", "pointer", "array", "map", "slice", "chan",
357+
"interface", "struct", "signature", "pointer", "array", "map", "slice", "chan", "string", "number", "bool", "invalid",
358358
}
359359
// The LSP tests have historically enabled this flag,
360360
// but really we should test both ways for older editors.

0 commit comments

Comments
 (0)