Skip to content

Commit 2d104ec

Browse files
committed
gopls/doc/generate: treat LinksInHover as an enum
This CL defines a type for the true|false|"gopls" type used by linksInHover, and adds a special case to the doc+api generator to treat it as an enum, so that VS Code will present a better value-chooser UI for it. Also, document the type grammar used in the docs. Updates golang/go#68057 Change-Id: I9e334fbc94dcbdc70657d8e64f67fb807e69cbf8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/593656 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent b994346 commit 2d104ec

File tree

5 files changed

+47
-29
lines changed

5 files changed

+47
-29
lines changed

gopls/doc/generate/generate.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
// Run it with this command:
1414
//
15-
// $ cd gopls/doc && go generate
15+
// $ cd gopls/internal/doc && go generate
1616
package main
1717

1818
import (
@@ -323,7 +323,7 @@ func loadOptions(category reflect.Value, optsType types.Object, pkg *packages.Pa
323323

324324
// loadEnums returns a description of gopls' settings enum types based on static analysis.
325325
func loadEnums(pkg *packages.Package) (map[types.Type][]doc.EnumValue, error) {
326-
enums := map[types.Type][]doc.EnumValue{}
326+
enums := make(map[types.Type][]doc.EnumValue)
327327
for _, name := range pkg.Types.Scope().Names() {
328328
obj := pkg.Types.Scope().Lookup(name)
329329
cnst, ok := obj.(*types.Const)
@@ -344,6 +344,15 @@ func loadEnums(pkg *packages.Package) (map[types.Type][]doc.EnumValue, error) {
344344
}
345345
enums[obj.Type()] = append(enums[obj.Type()], v)
346346
}
347+
348+
// linksInHover is a one-off edge case (true | false | "gopls")
349+
// that doesn't warrant a general solution (e.g. struct tag).
350+
enums[pkg.Types.Scope().Lookup("LinksInHoverEnum").Type()] = []doc.EnumValue{
351+
{Value: "false", Doc: "false: do not show links"},
352+
{Value: "true", Doc: "true: show links to the `linkTarget` domain"},
353+
{Value: `"gopls"`, Doc: "`\"gopls\"`: show links to gopls' internal documentation viewer"},
354+
}
355+
347356
return enums, nil
348357
}
349358

@@ -693,14 +702,6 @@ func rewriteSettings(prevContent []byte, api *doc.API) ([]byte, error) {
693702

694703
// heading
695704
//
696-
// TODO(adonovan): We should display not the Go type (e.g.
697-
// `time.Duration`, `map[Enum]bool`) for each setting,
698-
// but its JSON type, since that's the actual interface.
699-
// We need a better way to derive accurate JSON type descriptions
700-
// from Go types. eg. "a string parsed as if by
701-
// `time.Duration.Parse`". (`time.Duration` is an integer, not
702-
// a string!)
703-
//
704705
// We do not display the undocumented dotted-path alias
705706
// (h.title + "." + opt.Name) used by VS Code only.
706707
fmt.Fprintf(&buf, "### `%s` *%v*\n\n", opt.Name, opt.Type)

gopls/doc/settings.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,15 @@ documentation links in hover.
439439
Default: `"pkg.go.dev"`.
440440

441441
<a id='linksInHover'></a>
442-
### `linksInHover` *any*
442+
### `linksInHover` *enum*
443443

444-
linksInHover controls the presence of documentation links
445-
in hover markdown.
444+
linksInHover controls the presence of documentation links in hover markdown.
446445

447-
Its legal values are:
448-
- `false`, for no links;
449-
- `true`, for links to the `linkTarget` domain; or
450-
- `"gopls"`, for links to gopls' internal documentation viewer.
446+
Must be one of:
447+
448+
* false: do not show links
449+
* true: show links to the `linkTarget` domain
450+
* `"gopls"`: show links to gopls' internal documentation viewer
451451

452452
Default: `true`.
453453

gopls/internal/doc/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type API struct {
2929

3030
type Option struct {
3131
Name string
32-
Type string
32+
Type string // T = bool | string | int | enum | any | []T | map[T]T | time.Duration
3333
Doc string
3434
EnumKeys EnumKeys
3535
EnumValues []EnumValue

gopls/internal/doc/api.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,26 @@
154154
},
155155
{
156156
"Name": "linksInHover",
157-
"Type": "any",
158-
"Doc": "linksInHover controls the presence of documentation links\nin hover markdown.\n\nIts legal values are:\n- `false`, for no links;\n- `true`, for links to the `linkTarget` domain; or\n- `\"gopls\"`, for links to gopls' internal documentation viewer.\n",
157+
"Type": "enum",
158+
"Doc": "linksInHover controls the presence of documentation links in hover markdown.\n",
159159
"EnumKeys": {
160160
"ValueType": "",
161161
"Keys": null
162162
},
163-
"EnumValues": null,
163+
"EnumValues": [
164+
{
165+
"Value": "false",
166+
"Doc": "false: do not show links"
167+
},
168+
{
169+
"Value": "true",
170+
"Doc": "true: show links to the `linkTarget` domain"
171+
},
172+
{
173+
"Value": "\"gopls\"",
174+
"Doc": "`\"gopls\"`: show links to gopls' internal documentation viewer"
175+
}
176+
],
164177
"Default": "true",
165178
"Status": "",
166179
"Hierarchy": "ui.documentation"

gopls/internal/settings/settings.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,20 @@ type DocumentationOptions struct {
355355
// documentation links in hover.
356356
LinkTarget string
357357

358-
// LinksInHover controls the presence of documentation links
359-
// in hover markdown.
360-
//
361-
// Its legal values are:
362-
// - `false`, for no links;
363-
// - `true`, for links to the `linkTarget` domain; or
364-
// - `"gopls"`, for links to gopls' internal documentation viewer.
365-
LinksInHover any
358+
// LinksInHover controls the presence of documentation links in hover markdown.
359+
LinksInHover LinksInHoverEnum
366360
}
367361

362+
// LinksInHoverEnum has legal values:
363+
//
364+
// - `false`, for no links;
365+
// - `true`, for links to the `linkTarget` domain; or
366+
// - `"gopls"`, for links to gopls' internal documentation viewer.
367+
//
368+
// Note: this type has special logic in loadEnums in generate.go.
369+
// Be sure to reflect enum and doc changes there!
370+
type LinksInHoverEnum any
371+
368372
// Note: FormattingOptions must be comparable with reflect.DeepEqual.
369373
type FormattingOptions struct {
370374
// Local is the equivalent of the `goimports -local` flag, which puts

0 commit comments

Comments
 (0)