Skip to content

Commit e22a80d

Browse files
committed
extension/tools/goplssetting: update copy of gopls json-api types
The types are moved to tools/gopls/internal/doc/api.go. Remove `JSON` suffix from each name. Change-Id: Ic5dae90f83f32f241a1e128aecd7def92aff34bc Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/616678 kokoro-CI: kokoro <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Commit-Queue: Hyang-Ah Hana Kim <[email protected]>
1 parent 538900f commit e22a80d

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

extension/tools/goplssetting/goplssetting.go

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func Generate(inputFile string, skipCleanup bool) ([]byte, error) {
6666
}
6767

6868
// readGoplsAPI returns the output of `gopls api-json`.
69-
func readGoplsAPI() (*APIJSON, error) {
69+
func readGoplsAPI() (*API, error) {
7070
version, err := exec.Command("gopls", "-v", "version").Output()
7171
if err != nil {
7272
return nil, fmt.Errorf("failed to check gopls version: %v", err)
@@ -78,7 +78,7 @@ func readGoplsAPI() (*APIJSON, error) {
7878
return nil, fmt.Errorf("failed to run gopls: %v", err)
7979
}
8080

81-
api := &APIJSON{}
81+
api := &API{}
8282
if err := json.Unmarshal(out, api); err != nil {
8383
return nil, fmt.Errorf("failed to unmarshal: %v", err)
8484
}
@@ -87,37 +87,37 @@ func readGoplsAPI() (*APIJSON, error) {
8787

8888
// extractOptions extracts the options from APIJSON.
8989
// It may rearrange the ordering and documentation for better presentation.
90-
func extractOptions(api *APIJSON) ([]*OptionJSON, error) {
90+
func extractOptions(api *API) ([]*Option, error) {
9191
type sortableOptionJSON struct {
92-
*OptionJSON
92+
*Option
9393
section string
9494
}
9595
options := []sortableOptionJSON{}
9696
for k, v := range api.Options {
9797
for _, o := range v {
98-
options = append(options, sortableOptionJSON{OptionJSON: o, section: k})
98+
options = append(options, sortableOptionJSON{Option: o, section: k})
9999
}
100100
}
101101
sort.SliceStable(options, func(i, j int) bool {
102-
pi := priority(options[i].OptionJSON)
103-
pj := priority(options[j].OptionJSON)
102+
pi := priority(options[i].Option)
103+
pj := priority(options[j].Option)
104104
if pi == pj {
105105
return options[i].Name < options[j].Name
106106
}
107107
return pi < pj
108108
})
109109

110-
opts := []*OptionJSON{}
110+
opts := []*Option{}
111111
for _, v := range options {
112-
if name := statusName(v.OptionJSON); name != "" {
113-
v.OptionJSON.Doc = name + " " + v.OptionJSON.Doc
112+
if name := statusName(v.Option); name != "" {
113+
v.Option.Doc = name + " " + v.Option.Doc
114114
}
115-
opts = append(opts, v.OptionJSON)
115+
opts = append(opts, v.Option)
116116
}
117117
return opts, nil
118118
}
119119

120-
func priority(opt *OptionJSON) int {
120+
func priority(opt *Option) int {
121121
switch toStatus(opt.Status) {
122122
case Experimental:
123123
return 10
@@ -127,7 +127,7 @@ func priority(opt *OptionJSON) int {
127127
return 1000
128128
}
129129

130-
func statusName(opt *OptionJSON) string {
130+
func statusName(opt *Option) string {
131131
switch toStatus(opt.Status) {
132132
case Experimental:
133133
return "(Experimental)"
@@ -171,8 +171,8 @@ func rewritePackageJSON(newSettings, inFile string) ([]byte, error) {
171171

172172
// asVSCodeSettings converts the given options to match the VS Code settings
173173
// format.
174-
func asVSCodeSettings(options []*OptionJSON) ([]byte, error) {
175-
seen := map[string][]*OptionJSON{}
174+
func asVSCodeSettings(options []*Option) ([]byte, error) {
175+
seen := map[string][]*Option{}
176176
for _, opt := range options {
177177
seen[opt.Hierarchy] = append(seen[opt.Hierarchy], opt)
178178
}
@@ -195,7 +195,7 @@ func asVSCodeSettings(options []*OptionJSON) ([]byte, error) {
195195
return json.Marshal(goProperties)
196196
}
197197

198-
func collectProperties(m map[string][]*OptionJSON) (goplsProperties, goProperties map[string]*Object, err error) {
198+
func collectProperties(m map[string][]*Option) (goplsProperties, goProperties map[string]*Object, err error) {
199199
var sorted []string
200200
var containsEmpty bool
201201
for k := range m {
@@ -248,7 +248,7 @@ func collectProperties(m map[string][]*OptionJSON) (goplsProperties, goPropertie
248248
return goplsProperties, goProperties, nil
249249
}
250250

251-
func toObject(opt *OptionJSON) (*Object, error) {
251+
func toObject(opt *Option) (*Object, error) {
252252
doc := opt.Doc
253253
if mappedTo, ok := associatedToExtensionProperties[opt.Name]; ok {
254254
doc = fmt.Sprintf("%v\nIf unspecified, values of `%v` will be propagated.\n", doc, strings.Join(mappedTo, ", "))
@@ -295,7 +295,7 @@ func toObject(opt *OptionJSON) (*Object, error) {
295295
return obj, nil
296296
}
297297

298-
func formatOptionDefault(opt *OptionJSON) interface{} {
298+
func formatOptionDefault(opt *Option) interface{} {
299299
// Each key will have its own default value, instead of one large global
300300
// one. (Alternatively, we can build the default from the keys.)
301301
if len(opt.EnumKeys.Keys) > 0 {
@@ -388,18 +388,18 @@ const (
388388
None
389389
)
390390

391-
// APIJSON is the output json type of `gopls api-json`.
392-
// Types copied from golang.org/x/tools/internal/lsp/source/options.go.
393-
type APIJSON struct {
394-
Options map[string][]*OptionJSON
395-
Commands []*CommandJSON
396-
Lenses []*LensJSON
397-
Analyzers []*AnalyzerJSON
391+
// API is a JSON-encodable representation of gopls' public interfaces.
392+
//
393+
// Types are copied from golang.org/x/tools/gopls/internal/doc/api.go.
394+
type API struct {
395+
Options map[string][]*Option
396+
Lenses []*Lens
397+
Analyzers []*Analyzer
398398
}
399399

400-
type OptionJSON struct {
400+
type Option struct {
401401
Name string
402-
Type string
402+
Type string // T = bool | string | int | enum | any | []T | map[T]T | time.Duration
403403
Doc string
404404
EnumKeys EnumKeys
405405
EnumValues []EnumValue
@@ -414,29 +414,32 @@ type EnumKeys struct {
414414
}
415415

416416
type EnumKey struct {
417-
Name string
417+
Name string // in JSON syntax (quoted)
418418
Doc string
419419
Default string
420420
}
421421

422422
type EnumValue struct {
423-
Value string
424-
Doc string
423+
Value string // in JSON syntax (quoted)
424+
Doc string // doc comment; always starts with `Value`
425425
}
426426

427-
type CommandJSON struct {
428-
Command string
429-
Title string
430-
Doc string
427+
type Lens struct {
428+
FileType string // e.g. "Go", "go.mod"
429+
Lens string
430+
Title string
431+
Doc string
432+
Default bool
431433
}
432434

433-
type LensJSON struct {
434-
Lens string
435-
Title string
436-
Doc string
435+
type Analyzer struct {
436+
Name string
437+
Doc string // from analysis.Analyzer.Doc ("title: summary\ndescription"; not Markdown)
438+
URL string
439+
Default bool
437440
}
438441

439-
type AnalyzerJSON struct {
442+
type Hint struct {
440443
Name string
441444
Doc string
442445
Default bool

extension/tools/goplssetting/goplssetting_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ func TestWriteAsVSCodeSettings(t *testing.T) {
3333
}
3434
testCases := []struct {
3535
name string
36-
in *OptionJSON
36+
in *Option
3737
out string
3838
}{
3939
{
4040
name: "boolean",
41-
in: &OptionJSON{
41+
in: &Option{
4242
Name: "verboseOutput",
4343
Type: "bool",
4444
Doc: "verboseOutput enables additional debug logging.\n",
@@ -53,7 +53,7 @@ func TestWriteAsVSCodeSettings(t *testing.T) {
5353
},
5454
{
5555
name: "time",
56-
in: &OptionJSON{
56+
in: &Option{
5757
Name: "completionBudget",
5858
Type: "time.Duration",
5959
Default: "\"100ms\"",
@@ -66,7 +66,7 @@ func TestWriteAsVSCodeSettings(t *testing.T) {
6666
},
6767
{
6868
name: "map",
69-
in: &OptionJSON{
69+
in: &Option{
7070
Name: "analyses",
7171
Type: "map[string]bool",
7272
Default: "{}",
@@ -78,7 +78,7 @@ func TestWriteAsVSCodeSettings(t *testing.T) {
7878
},
7979
{
8080
name: "enum",
81-
in: &OptionJSON{
81+
in: &Option{
8282
Name: "matcher",
8383
Type: "enum",
8484
EnumValues: []EnumValue{
@@ -107,7 +107,7 @@ func TestWriteAsVSCodeSettings(t *testing.T) {
107107
},
108108
{
109109
name: "array",
110-
in: &OptionJSON{
110+
in: &Option{
111111
Name: "directoryFilters",
112112
Type: "[]string",
113113
Default: "[\"-node_modules\", \"-vendor\"]",
@@ -122,7 +122,7 @@ func TestWriteAsVSCodeSettings(t *testing.T) {
122122

123123
for _, tc := range testCases {
124124
t.Run(tc.name, func(t *testing.T) {
125-
options := []*OptionJSON{tc.in}
125+
options := []*Option{tc.in}
126126
b, err := asVSCodeSettings(options)
127127
if err != nil {
128128
t.Fatal(err)

0 commit comments

Comments
 (0)