Debian Codegraph @

gopls/doc/features/web.md

raw symbols SCIP
1 ---
2 title: "Gopls: Web-based features"
3 ---
4
5 The LSP
6 [`window.showDocument`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_showDocument) request
7 allows the server to instruct the client to open a file in the editor
8 or a web page in a browser. It is the basis for a number of gopls
9 features that report information about your program through a web
10 interface.
11
12 We recognize that a web interface is not ideal for everyone: some
13 users prefer a full-screen editor layout and dislike switching
14 windows; others may work in a text-only terminal without a window
15 system, perhaps over remote ssh or on the Linux console.
16 Unfortunately, the LSP lacks several natural kinds of extensibility,
17 including the ability for servers to define:
18
19 - queries that [generalize a References
20 query](https://github.com/microsoft/language-server-protocol/issues/1911),
21 displaying results using similar UI elements;
22 - commands that [produce a stream of
23 text](https://github.com/joaotavora/eglot/discussions/1402), like a
24 typical shell command or compiler, that the client can redirect to
25 the editor's usual terminal-like UI element; or
26 - refactoring operations that, like Rename, [prompt the
27 user](https://github.com/microsoft/language-server-protocol/issues/1164)
28 for additional information.
29
30 The web-based UI can help fill these gaps until such time as the LSP
31 provides standard ways of implementing these features.
32
33 Gopls' web server listens on a `localhost` port. For security, all its
34 endpoints include a random string that serves as an authentication
35 token. The client, provided authenticated URLs by the server, will be
36 able to access your source code, but arbitrary processes running on
37 your machine will not.
38 Restarting the gopls process causes this secret to change, rendering
39 all existing previous URLs invalid; existing pages will display a banner
40 indicating that they have become disconnected.
41
42 TODO: combine the web server and the debug server; see https://go.dev/issue/68229.
43
44 Gopls supports two-way communication between the web browser and the
45 client editor. All of the web-based reports contain links to
46 declarations in your source code. Clicking on one of these links
47 causes gopls to send a `showDocument` request to your editor to open
48 the relevant source file at the appropriate line. This works even when
49 your source code has been modified but not saved.
50 (VS Code users: please upvote microsoft/vscode#208093 if you would
51 like your editor to raise its window when handling this event.)
52
53 <a name='doc'></a>
54 ## `source.doc`: Browse package documentation
55
56 In any Go source file, a code action request returns a command to
57 "Browse package documentation". This command opens a browser window
58 showing the documentation for the current Go package, presented using
59 a similar design to https://pkg.go.dev.
60
61 This allows you to preview the documentation for your packages, even
62 internal ones that may be unpublished externally. Reloading the page
63 updates the documentation to reflect your changes. It is not necessary
64 to save modified Go source files.
65
66 <img title="Browse package documentation" src="../assets/browse-pkg-doc.png" width='80%'>
67
68 Clicking on the link for a package-level symbol or method, which in
69 `pkg.go.dev` would ordinarily take you to a source-code viewer such as
70 GitHub or Google Code Search, causes your editor to navigate to the
71 relevant source file and line.
72
73 Client support:
74 - **VS Code**: Use the "Source Action... > Browse documentation for package P" menu.
75 - **Emacs + eglot**: Use `M-x go-browse-doc` in [go-mode](https://github.com/dominikh/go-mode.el).
76 - **Vim + coc.nvim**: ??
77
78
79 <a name='freesymbols'></a>
80 ## `source.freesymbols`: Browse free symbols
81
82 When studying code, either to understand it or to evaluate a different
83 organization or factoring, it is common to need to know what the
84 "inputs" are to a given chunk of code, either because you are
85 considering extracting it into its own function and want to know what
86 parameters it would take, or just to understand how one piece of a long
87 function relates to the preceding pieces.
88
89 If you select a chunk of code, and invoke the "Browse free symbols"
90 [code action](transformation.md#code-actions), your editor will
91 open a browser displaying a report on the free symbols of the
92 selection. A symbol is "free" if it is referenced from within the
93 selection but defined outside of it. In essence, these are the inputs
94 to the selected chunk.
95
96 <img title="Browse free symbols" src="../assets/browse-free-symbols.png" width='80%'>
97
98 The report classifies the symbols into imported, local, and
99 package-level symbols. The imported symbols are grouped by package,
100 and link to the documentation for the package, as described above.
101 Each of the remaining symbols is presented as a link that causes your
102 editor to navigate to its declaration.
103
104 TODO: explain dotted paths.
105
106 Client support:
107 - **VS Code**: Use the "Source Action... > Browse free symbols" menu.
108 - **Emacs + eglot**: Use `M-x go-browse-freesymbols` in [go-mode](https://github.com/dominikh/go-mode.el).
109 - **Vim + coc.nvim**: ??
110
111
112 <a name='assembly'></a>
113 ## `source.assembly`: Browse assembly
114
115 When you're optimizing the performance of your code or investigating
116 an unexpected crash, it may sometimes be helpful to inspect the
117 assembly code produced by the compiler for a given Go function.
118
119 If you position the cursor or selection within a function f,
120 gopls offers the "Browse assembly for f" [code action](transformation.md#code-actions).
121 This opens a web-based listing of the assembly for the function, plus
122 any functions nested within it.
123
124 Each time you edit your source and reload the page, the current
125 package is recompiled and the listing is updated. It is not necessary
126 to save your modified files.
127
128 The compiler's target architecture is the same as the one gopls uses
129 when analyzing the file: typically, this is your machine's GOARCH, but
130 when viewing a file with a build tag, such as one named `foo_amd64.go`
131 or containing the comment `//go:build amd64`, the tags determine the
132 architecture.
133
134 Each instruction is displayed with a link that causes your editor to
135 navigate to the source line responsible for the instruction, according
136 to the debug information.
137
138 <img title="Browse assembly" src="../assets/browse-assembly.png" width="80%">
139
140 The example above shows the arm64 assembly listing of
141 [`time.NewTimer`](https://pkg.go.dev/time#NewTimer).
142 Observe that the indicated instruction links to a source location
143 inside a different function, `syncTimer`, because the compiler
144 inlined the call from `NewTimer` to `syncTimer`.
145
146 Browsing assembly is not yet supported for generic functions, package
147 initializers (`func init`), or functions in test packages.
148 (Contributions welcome!)
149
150 Client support:
151 - **VS Code**: Use the "Source Action... > Browse GOARCH assembly for f" menu.
152 - **Emacs + eglot**: Use `M-x go-browse-assembly` in [go-mode](https://github.com/dominikh/go-mode.el).
153 - **Vim + coc.nvim**: ??
154
155
156 <a name='splitpkg'></a>
157 ## `source.splitPackage`: Split package into components
158
159 The web-based "Split package" tool can help you split a complex
160 package into two or more components, ensuring that the dependencies
161 among those components are acyclic.
162
163 Follow the instructions on the page to choose a set of named components,
164 assign each declaration to the most appropriate component, and then
165 visualize the dependencies between those components created by references
166 from one symbol to another.
167
168 The figure below shows the tool operating on the `fmt` package, which
169 could (in principle) be split into three subpackages, one for
170 formatting (`Printf` and friends), one for scanning (`Scanf`), and one
171 for their common dependencies.
172
173 <img title="Split package 'fmt'" src="../assets/splitpkg.png" width="55%">
174
175 (Try playing with the tool on this package: it's an instructive
176 exercise. The figure below shows the solution.)
177
178 <img title="Split package 'fmt'" src="../assets/splitpkg-deps.png" width="55%">
179
180 The tool does not currently perform the code transformation (moving
181 declarations to new packages, renaming symbols to export them as
182 needed), but we hope to add that in a future release.
183
184 Client support:
185 - **VS Code**: Use the "Source Action... > Split package P" menu.