Skip to content

Commit 2154cbf

Browse files
committed
gopls/internal/golang: add "Browse gopls features" code action
This command opens the Index of Features doc page: $ gopls codeaction -kind=gopls.doc.features -exec ./gopls/main.go VS Code exposes this new code action through the Quick Fix menu (Command-.) under the section "More actions...". It should probably also be given a top-level command similar to "Go: Add Import", etc. Other editors seem to treat code actions more uniformly, so special handling is unnecessary. Change-Id: I633dd34cdb9005009a098bcd7bb50d0db06044c7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/595557 Commit-Queue: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent ead76ab commit 2154cbf

File tree

15 files changed

+111
-43
lines changed

15 files changed

+111
-43
lines changed

gopls/doc/commands.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ Args:
262262
}
263263
```
264264

265+
<a id='gopls.client_open_url'></a>
266+
## `gopls.client_open_url`: **Request that the client open a URL in a browser.**
267+
268+
269+
270+
Args:
271+
272+
```
273+
string
274+
```
275+
265276
<a id='gopls.diagnose_files'></a>
266277
## `gopls.diagnose_files`: **Cause server to publish diagnostics for the specified files.**
267278

gopls/doc/features/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ when making significant changes to existing features or when adding new ones.
3838
- [Type Definition](navigation.md#type-definition): go to definition of type of selected symbol
3939
- [References](navigation.md#references): list references to selected symbol
4040
- [Implementation](navigation.md#implementation): show "implements" relationships of selected type
41-
- [Document Symbol](passive.md#document-symbol): outline of symbols defined in current file
41+
- [Document Symbol](navigation.md#document-symbol): outline of symbols defined in current file
4242
- [Symbol](navigation.md#symbol): fuzzy search for symbol by name
4343
- [Selection Range](navigation.md#selection-range): select enclosing unit of syntax
4444
- [Call Hierarchy](navigation.md#call-hierarchy): show outgoing/incoming calls to the current function
@@ -59,3 +59,8 @@ when making significant changes to existing features or when adding new ones.
5959
- [go.mod and go.work files](modfiles.md): Go module and workspace manifests
6060
- [Command-line interface](../command-line.md): CLI for debugging and scripting (unstable)
6161
- [Non-standard commands](../commands.md): gopls-specific RPC protocol extensions (unstable)
62+
63+
You can find this page from within your editor by executing the
64+
`gopls.doc.features` [code action](transformation.md#code-actions),
65+
which opens it in a web browser.
66+
In VS Code, you can find it on the Quick fix menu.

gopls/internal/cmd/codeaction.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Valid kinds include:
5858
source.doc
5959
source.freesymbols
6060
goTest
61+
gopls.doc.features
6162
6263
Kinds are hierarchical, so "refactor" includes "refactor.inline".
6364
(Note: actions of kind "goTest" are not returned unless explicitly

gopls/internal/cmd/integration_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,10 +977,13 @@ type C struct{}
977977
}
978978
// list code actions in file, filtering by title
979979
{
980-
res := gopls(t, tree, "codeaction", "-title=Br.wse", "a.go")
980+
res := gopls(t, tree, "codeaction", "-title=Browse.*doc", "a.go")
981981
res.checkExit(true)
982982
got := res.stdout
983-
want := `command "Browse documentation for package a" [source.doc]` + "\n"
983+
want := `command "Browse gopls feature documentation" [gopls.doc.features]` +
984+
"\n" +
985+
`command "Browse documentation for package a" [source.doc]` +
986+
"\n"
984987
if got != want {
985988
t.Errorf("codeaction: got <<%s>>, want <<%s>>\nstderr:\n%s", got, want, res.stderr)
986989
}

gopls/internal/cmd/usage/codeaction.hlp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Valid kinds include:
2929
source.doc
3030
source.freesymbols
3131
goTest
32+
gopls.doc.features
3233

3334
Kinds are hierarchical, so "refactor" includes "refactor.inline".
3435
(Note: actions of kind "goTest" are not returned unless explicitly

gopls/internal/doc/api.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,13 @@
989989
"ArgDoc": "{\n\t// The go.mod file URI.\n\t\"URI\": string,\n\t// The modules to check.\n\t\"Modules\": []string,\n}",
990990
"ResultDoc": ""
991991
},
992+
{
993+
"Command": "gopls.client_open_url",
994+
"Title": "Request that the client open a URL in a browser.",
995+
"Doc": "",
996+
"ArgDoc": "string",
997+
"ResultDoc": ""
998+
},
992999
{
9931000
"Command": "gopls.diagnose_files",
9941001
"Title": "Cause server to publish diagnostics for the specified files.",

gopls/internal/golang/codeaction.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func CodeActions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
4848
if wantQuickFixes ||
4949
want[protocol.SourceOrganizeImports] ||
5050
want[protocol.RefactorExtract] ||
51-
want[settings.GoFreeSymbols] {
51+
want[settings.GoFreeSymbols] ||
52+
want[settings.GoplsDocFeatures] {
5253

5354
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full)
5455
if err != nil {
@@ -115,6 +116,22 @@ func CodeActions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
115116
Command: &cmd,
116117
})
117118
}
119+
120+
if want[settings.GoplsDocFeatures] {
121+
// TODO(adonovan): after the docs are published in gopls/v0.17.0,
122+
// use the gopls release tag instead of master.
123+
cmd, err := command.NewClientOpenURLCommand(
124+
"Browse gopls feature documentation",
125+
"https://github.com/golang/tools/blob/master/gopls/doc/features/README.md")
126+
if err != nil {
127+
return nil, err
128+
}
129+
actions = append(actions, protocol.CodeAction{
130+
Title: cmd.Title,
131+
Kind: settings.GoplsDocFeatures,
132+
Command: &cmd,
133+
})
134+
}
118135
}
119136

120137
// Code actions requiring type information.

gopls/internal/protocol/command/command_gen.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopls/internal/protocol/command/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ type Interface interface {
265265
// The machine architecture is determined by the view.
266266
Assembly(_ context.Context, viewID, packageID, symbol string) error
267267

268+
// ClientOpenURL: Request that the client open a URL in a browser.
269+
ClientOpenURL(_ context.Context, url string) error
270+
268271
// ScanImports: force a sychronous scan of the imports cache.
269272
//
270273
// This command is intended for use by gopls tests only.

gopls/internal/server/code_action.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ func (s *server) CodeAction(ctx context.Context, params *protocol.CodeActionPara
143143
case settings.GoTest,
144144
settings.GoDoc,
145145
settings.GoFreeSymbols,
146-
settings.GoAssembly:
146+
settings.GoAssembly,
147+
settings.GoplsDocFeatures:
147148
return false // read-only query
148149
}
149150
return true // potential write operation

0 commit comments

Comments
 (0)