Skip to content

Commit b6f9914

Browse files
authored
Populate SymbolInformation.Kind field (#213)
Map Go type-checker objects to SCIP symbol kinds: - *types.Func → Function / Method / MethodSpecification - *types.TypeName → Struct / Interface / TypeAlias / TypeParameter / Type - *types.Var → Field / Variable - *types.Const → Constant - *types.PkgName → Package Fixes #176
1 parent 39c31fb commit b6f9914

70 files changed

Lines changed: 587 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

internal/document/document.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/scip-code/scip/bindings/go/scip"
1818
"github.com/sourcegraph/scip-go/internal/lookup"
19+
"github.com/sourcegraph/scip-go/internal/symbols"
1920
"golang.org/x/tools/go/packages"
2021
)
2122

@@ -94,11 +95,13 @@ func (d *Document) SetNewSymbolForPos(
9495
var displayName string
9596
var documentation []string
9697
var sigDoc *scip.Document
98+
var def types.Object
9799

98100
if ident != nil {
99101
displayName = ident.Name
100102

101-
if def := d.pkg.TypesInfo.Defs[ident]; def != nil {
103+
def = d.pkg.TypesInfo.Defs[ident]
104+
if def != nil {
102105
if signature := typeStringForObject(def); signature != "" {
103106
sigDoc = &scip.Document{
104107
Language: "go",
@@ -119,6 +122,7 @@ func (d *Document) SetNewSymbolForPos(
119122

120123
d.pkgSymbols.Set(pos, &scip.SymbolInformation{
121124
Symbol: symbol,
125+
Kind: symbols.KindForObject(def),
122126
DisplayName: displayName,
123127
Documentation: documentation,
124128
SignatureDocumentation: sigDoc,

internal/index/scip.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func indexVisitPackages(
210210

211211
symInfo := &scip.SymbolInformation{
212212
Symbol: pkgSymbol,
213+
Kind: scip.SymbolInformation_Package,
213214
DisplayName: pkg.Name,
214215
Documentation: findPackageDocs(pkg),
215216
SignatureDocumentation: &scip.Document{

internal/symbols/kind.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package symbols
2+
3+
import (
4+
"go/types"
5+
6+
"github.com/scip-code/scip/bindings/go/scip"
7+
)
8+
9+
// KindForObject maps a Go types.Object to the corresponding SCIP SymbolInformation_Kind.
10+
func KindForObject(obj types.Object) scip.SymbolInformation_Kind {
11+
if obj == nil {
12+
return scip.SymbolInformation_UnspecifiedKind
13+
}
14+
15+
switch obj := obj.(type) {
16+
case *types.Func:
17+
sig, ok := obj.Type().(*types.Signature)
18+
if !ok {
19+
return scip.SymbolInformation_Function
20+
}
21+
if sig.Recv() == nil {
22+
return scip.SymbolInformation_Function
23+
}
24+
if types.IsInterface(sig.Recv().Type()) {
25+
return scip.SymbolInformation_MethodSpecification
26+
}
27+
return scip.SymbolInformation_Method
28+
29+
case *types.TypeName:
30+
if obj.IsAlias() {
31+
return scip.SymbolInformation_TypeAlias
32+
}
33+
switch obj.Type().Underlying().(type) {
34+
case *types.Struct:
35+
return scip.SymbolInformation_Struct
36+
case *types.Interface:
37+
return scip.SymbolInformation_Interface
38+
case *types.TypeParam:
39+
return scip.SymbolInformation_TypeParameter
40+
default:
41+
return scip.SymbolInformation_Type
42+
}
43+
44+
case *types.Var:
45+
if obj.IsField() {
46+
return scip.SymbolInformation_Field
47+
}
48+
return scip.SymbolInformation_Variable
49+
50+
case *types.Const:
51+
return scip.SymbolInformation_Constant
52+
53+
case *types.PkgName:
54+
return scip.SymbolInformation_Package
55+
56+
default:
57+
return scip.SymbolInformation_UnspecifiedKind
58+
}
59+
}

internal/testdata/snapshots/output/alias/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package main
22
// ^^^^ definition github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/
3+
// kind Package
34
// display_name main
45
// signature_documentation
56
// > package main
@@ -9,6 +10,7 @@
910
type (
1011
T struct{}
1112
// ^ definition github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/T#
13+
// kind Struct
1214
// display_name T
1315
// signature_documentation
1416
// > type T struct{}
@@ -17,6 +19,7 @@
1719
// > Copied from https://github.com/golang/go/issues/68877#issuecomment-2290000187
1820
U = T
1921
// ^ definition github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/U#
22+
// kind TypeAlias
2023
// display_name U
2124
// signature_documentation
2225
// > type U = T
@@ -26,6 +29,7 @@
2629
// ^ reference github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/T#
2730
V = U
2831
// ^ definition github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/V#
32+
// kind TypeAlias
2933
// display_name V
3034
// signature_documentation
3135
// > type V = U
@@ -35,6 +39,7 @@
3539
// ^ reference github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/U#
3640
S U
3741
// ^ definition github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/S#
42+
// kind Struct
3843
// display_name S
3944
// signature_documentation
4045
// > type S struct{}
@@ -44,6 +49,7 @@
4449
// ^ reference github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/U#
4550
Z int32
4651
// ^ definition github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/Z#
52+
// kind Type
4753
// display_name Z
4854
// signature_documentation
4955
// > type Z int32
@@ -55,10 +61,12 @@
5561
//⌄ enclosing_range_start github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/f().
5662
func f(u U) {}
5763
// ^ definition github.com/sourcegraph/scip-go 0.1.test `github.com/sourcegraph/scip-go/internal/testdata/snapshots/input/alias`/f().
64+
// kind Function
5865
// display_name f
5966
// signature_documentation
6067
// > func f(u U)
6168
// ^ definition local 0
69+
// kind Variable
6270
// display_name u
6371
// signature_documentation
6472
// > var u U

internal/testdata/snapshots/output/embedded/embedded.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package embedded
22
// ^^^^^^^^ definition 0.1.test `sg/embedded`/
3+
// kind Package
34
// display_name embedded
45
// signature_documentation
56
// > package embedded
@@ -13,6 +14,7 @@
1314

1415
type osExecCommand struct {
1516
// ^^^^^^^^^^^^^ definition 0.1.test `sg/embedded`/osExecCommand#
17+
// kind Struct
1618
// display_name osExecCommand
1719
// signature_documentation
1820
// > type osExecCommand struct{ *exec.Cmd }
@@ -22,6 +24,7 @@
2224
*exec.Cmd
2325
// ^^^^ reference github.com/golang/go/src go1.22 `os/exec`/
2426
// ^^^ definition 0.1.test `sg/embedded`/osExecCommand#Cmd.
27+
// kind Field
2528
// display_name Cmd
2629
// signature_documentation
2730
// > struct field Cmd *exec.Cmd
@@ -31,10 +34,12 @@
3134
//⌄ enclosing_range_start 0.1.test `sg/embedded`/wrapExecCommand().
3235
func wrapExecCommand(c *exec.Cmd) {
3336
// ^^^^^^^^^^^^^^^ definition 0.1.test `sg/embedded`/wrapExecCommand().
37+
// kind Function
3438
// display_name wrapExecCommand
3539
// signature_documentation
3640
// > func wrapExecCommand(c *exec.Cmd)
3741
// ^ definition local 0
42+
// kind Variable
3843
// display_name c
3944
// signature_documentation
4045
// > var c *Cmd
@@ -49,6 +54,7 @@
4954

5055
type Inner struct {
5156
// ^^^^^ definition 0.1.test `sg/embedded`/Inner#
57+
// kind Struct
5258
// display_name Inner
5359
// signature_documentation
5460
// > type Inner struct {
@@ -58,23 +64,27 @@
5864
// > }
5965
X int
6066
// ^ definition 0.1.test `sg/embedded`/Inner#X.
67+
// kind Field
6168
// display_name X
6269
// signature_documentation
6370
// > struct field X int
6471
Y int
6572
// ^ definition 0.1.test `sg/embedded`/Inner#Y.
73+
// kind Field
6674
// display_name Y
6775
// signature_documentation
6876
// > struct field Y int
6977
Z int
7078
// ^ definition 0.1.test `sg/embedded`/Inner#Z.
79+
// kind Field
7180
// display_name Z
7281
// signature_documentation
7382
// > struct field Z int
7483
}
7584

7685
type Outer struct {
7786
// ^^^^^ definition 0.1.test `sg/embedded`/Outer#
87+
// kind Struct
7888
// display_name Outer
7989
// signature_documentation
8090
// > type Outer struct {
@@ -83,12 +93,14 @@
8393
// > }
8494
Inner
8595
// ^^^^^ definition 0.1.test `sg/embedded`/Outer#Inner.
96+
// kind Field
8697
// display_name Inner
8798
// signature_documentation
8899
// > struct field Inner Inner
89100
// ^^^^^ reference 0.1.test `sg/embedded`/Inner#
90101
W int
91102
// ^ definition 0.1.test `sg/embedded`/Outer#W.
103+
// kind Field
92104
// display_name W
93105
// signature_documentation
94106
// > struct field W int
@@ -97,11 +109,13 @@
97109
//⌄ enclosing_range_start 0.1.test `sg/embedded`/useOfCompositeStructs().
98110
func useOfCompositeStructs() {
99111
// ^^^^^^^^^^^^^^^^^^^^^ definition 0.1.test `sg/embedded`/useOfCompositeStructs().
112+
// kind Function
100113
// display_name useOfCompositeStructs
101114
// signature_documentation
102115
// > func useOfCompositeStructs()
103116
o := Outer{
104117
// ^ definition local 1
118+
// kind Variable
105119
// display_name o
106120
// signature_documentation
107121
// > var o Outer

internal/testdata/snapshots/output/embedded/internal/nested.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package nested_internal
22
// ^^^^^^^^^^^^^^^ definition 0.1.test `sg/embedded/internal`/
3+
// kind Package
34
// display_name nested_internal
45
// signature_documentation
56
// > package nested_internal
@@ -14,24 +15,28 @@
1415
//⌄ enclosing_range_start 0.1.test `sg/embedded/internal`/Something().
1516
func Something(recent embedded.RecentCommittersResults) {
1617
// ^^^^^^^^^ definition 0.1.test `sg/embedded/internal`/Something().
18+
// kind Function
1719
// display_name Something
1820
// signature_documentation
1921
// > func Something(recent embedded.RecentCommittersResults)
2022
// ^^^^^^ definition local 0
23+
// kind Variable
2124
// display_name recent
2225
// signature_documentation
2326
// > var recent RecentCommittersResults
2427
// ^^^^^^^^ reference 0.1.test `sg/embedded`/
2528
// ^^^^^^^^^^^^^^^^^^^^^^^ reference 0.1.test `sg/embedded`/RecentCommittersResults#
2629
for _, commit := range recent.Nodes {
2730
// ^^^^^^ definition local 1
31+
// kind Variable
2832
// display_name commit
2933
// signature_documentation
3034
// > var commit struct{Authors struct{Nodes []struct{Date string; Email string; Name string; User struct{Login string}; AvatarURL string}}}
3135
// ^^^^^^ reference local 0
3236
// ^^^^^ reference 0.1.test `sg/embedded`/RecentCommittersResults#Nodes.
3337
for _, author := range commit.Authors.Nodes {
3438
// ^^^^^^ definition local 2
39+
// kind Variable
3540
// display_name author
3641
// signature_documentation
3742
// > var author struct{Date string; Email string; Name string; User struct{Login string}; AvatarURL string}

internal/testdata/snapshots/output/embedded/nested.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
type NestedHandler struct {
88
// ^^^^^^^^^^^^^ definition 0.1.test `sg/embedded`/NestedHandler#
9+
// kind Struct
910
// display_name NestedHandler
1011
// signature_documentation
1112
// > type NestedHandler struct {
@@ -16,6 +17,7 @@
1617
http.Handler
1718
// ^^^^ reference github.com/golang/go/src go1.22 `net/http`/
1819
// ^^^^^^^ definition 0.1.test `sg/embedded`/NestedHandler#Handler.
20+
// kind Field
1921
// display_name Handler
2022
// signature_documentation
2123
// > struct field Handler http.Handler
@@ -24,6 +26,7 @@
2426
// Wow, a great thing for integers
2527
Other int
2628
// ^^^^^ definition 0.1.test `sg/embedded`/NestedHandler#Other.
29+
// kind Field
2730
// display_name Other
2831
// signature_documentation
2932
// > struct field Other int
@@ -34,10 +37,12 @@
3437
//⌄ enclosing_range_start 0.1.test `sg/embedded`/NestedExample().
3538
func NestedExample(n NestedHandler) {
3639
// ^^^^^^^^^^^^^ definition 0.1.test `sg/embedded`/NestedExample().
40+
// kind Function
3741
// display_name NestedExample
3842
// signature_documentation
3943
// > func NestedExample(n NestedHandler)
4044
// ^ definition local 0
45+
// kind Variable
4146
// display_name n
4247
// signature_documentation
4348
// > var n NestedHandler

0 commit comments

Comments
 (0)