Skip to content

Commit 8669bfc

Browse files
committed
gopls/internal/server: add "View package documentation" code action
This CL adds a "View package documentation" code action to any selection in a Go source file. Its command has two effects: 1) to start a web server associated with the current LSP server, with coupled lifetimes, and register a handler to serve the package documentation; and 2) to direct the client to open a browser page showing the package documentation for the selection. The server is a minimal imitation of pkg.go.dev, working off the cache.Package representation. This means we can display doc markup even for unsaved editor buffers, and show type-derived information such as method sets of each type. Clicking through to the source--which on pkg.go.dev goes to cs.opensource.google--causes the client editor to navigate directly to the source file, using the magic of showDocument requests. The web server is secure in that all its endpoint URLs contain an unguessable secret, so a local process belonging to a different user can't access source code by port scanning. The CL includes a test that (a) the webserver content reflects edits (even unsaved) in the source buffers, and (b) that "clicking" through the source link causes the server to navigate the client editor to the correct source location. A couple of tests that asserted "no code actions" needed to be tweaked, as now there is always at least one code action in any Go source file. Change-Id: I2fe1f97e4e2b0b15cff6a4feb66501fce349b97d Reviewed-on: https://go-review.googlesource.com/c/tools/+/571215 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 5628443 commit 8669bfc

20 files changed

+1022
-24
lines changed

gopls/doc/commands.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,30 @@ Args:
222222
}
223223
```
224224

225+
### **View package documentation.**
226+
Identifier: `gopls.doc`
227+
228+
Opens the Go package documentation page for the current
229+
package in a browser.
230+
231+
Args:
232+
233+
```
234+
{
235+
"uri": string,
236+
"range": {
237+
"start": {
238+
"line": uint32,
239+
"character": uint32,
240+
},
241+
"end": {
242+
"line": uint32,
243+
"character": uint32,
244+
},
245+
},
246+
}
247+
```
248+
225249
### **Run go mod edit -go=version**
226250
Identifier: `gopls.edit_go_directive`
227251

gopls/internal/golang/codeaction.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func CodeActions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
9393
// Code actions requiring type information.
9494
if want[protocol.RefactorRewrite] ||
9595
want[protocol.RefactorInline] ||
96-
want[protocol.GoTest] {
96+
want[protocol.GoTest] ||
97+
want[protocol.GoDoc] {
9798
pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, fh.URI())
9899
if err != nil {
99100
return nil, err
@@ -121,6 +122,19 @@ func CodeActions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
121122
}
122123
actions = append(actions, fixes...)
123124
}
125+
126+
if want[protocol.GoDoc] {
127+
loc := protocol.Location{URI: pgf.URI, Range: rng}
128+
cmd, err := command.NewDocCommand("View package documentation", loc)
129+
if err != nil {
130+
return nil, err
131+
}
132+
actions = append(actions, protocol.CodeAction{
133+
Title: cmd.Title,
134+
Kind: protocol.GoDoc,
135+
Command: &cmd,
136+
})
137+
}
124138
}
125139
return actions, nil
126140
}

0 commit comments

Comments
 (0)