Skip to content

Commit 174ba9f

Browse files
committed
gopls: semantic tokens should report modifiers for the top-level type constructor of each symbol
These new semantic type modifiers will report in type definition, type embedding, type alias, variable definition like `var foo Bar`'s `Bar', parameter type and return type
1 parent 0734f62 commit 174ba9f

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

gopls/internal/golang/semtok.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,29 @@ func (tv *tokenVisitor) inspect(n ast.Node) (descend bool) {
483483
return true
484484
}
485485

486+
func underlineType(obj types.Object) (semtok.TokenType, []string) {
487+
switch obj.Type().Underlying().(type) {
488+
case *types.Interface:
489+
return semtok.TokType, []string{"interface"}
490+
case *types.Struct:
491+
return semtok.TokType, []string{"struct"}
492+
case *types.Pointer:
493+
return semtok.TokType, []string{"pointer"}
494+
case *types.Array:
495+
return semtok.TokType, []string{"array"}
496+
case *types.Map:
497+
return semtok.TokType, []string{"map"}
498+
case *types.Slice:
499+
return semtok.TokType, []string{"slice"}
500+
case *types.Chan:
501+
return semtok.TokType, []string{"chan"}
502+
case *types.Basic:
503+
return semtok.TokType, []string{"defaultLibrary"}
504+
default:
505+
return semtok.TokType, nil
506+
}
507+
}
508+
486509
func (tv *tokenVisitor) ident(id *ast.Ident) {
487510
var obj types.Object
488511

@@ -535,10 +558,9 @@ func (tv *tokenVisitor) ident(id *ast.Ident) {
535558
case *types.TypeName: // could be a TypeParam
536559
if is[*types.TypeParam](aliases.Unalias(obj.Type())) {
537560
emit(semtok.TokTypeParam)
538-
} else if is[*types.Basic](obj.Type()) {
539-
emit(semtok.TokType, "defaultLibrary")
540561
} else {
541-
emit(semtok.TokType)
562+
tok, mods := underlineType(obj)
563+
emit(tok, mods...)
542564
}
543565
case *types.Var:
544566
if is[*types.Signature](aliases.Unalias(obj.Type())) {
@@ -795,11 +817,15 @@ func (tv *tokenVisitor) definitionFor(id *ast.Ident, obj types.Object) (semtok.T
795817
if fld, ok := fldm.(*ast.Field); ok {
796818
// if len(fld.names) == 0 this is a semtok.TokType, being used
797819
if len(fld.Names) == 0 {
798-
return semtok.TokType, nil
820+
tok, mods := underlineType(obj)
821+
modifiers = append(modifiers, mods...)
822+
return tok, modifiers
799823
}
800824
return semtok.TokVariable, modifiers
801825
}
802-
return semtok.TokType, modifiers
826+
tok, mods := underlineType(obj)
827+
modifiers = append(modifiers, mods...)
828+
return tok, modifiers
803829
}
804830
}
805831
// can't happen

gopls/internal/protocol/semantic.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ var (
5252
semanticModifiers = [...]string{
5353
"declaration", "definition", "readonly", "static",
5454
"deprecated", "abstract", "async", "modification", "documentation", "defaultLibrary",
55+
"interface", "struct", "pointer", "array", "map", "slice", "chan",
5556
}
5657
)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ func clientCapabilities(cfg EditorConfig) (protocol.ClientCapabilities, error) {
353353
capabilities.TextDocument.SemanticTokens.TokenModifiers = []string{
354354
"declaration", "definition", "readonly", "static",
355355
"deprecated", "abstract", "async", "modification", "documentation", "defaultLibrary",
356+
// Additional modifiers supported by this client:
357+
"interface", "struct", "pointer", "array", "map", "slice", "chan",
356358
}
357359
// The LSP tests have historically enabled this flag,
358360
// but really we should test both ways for older editors.

0 commit comments

Comments
 (0)