Skip to content

Commit ec1a81b

Browse files
committed
gopls/doc/features: more doc tweaks
Change-Id: Ia4f473b9ef7fe10463e7bb768801a63aac84e89a Reviewed-on: https://go-review.googlesource.com/c/tools/+/596796 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 2fb3ebe commit ec1a81b

File tree

4 files changed

+59
-41
lines changed

4 files changed

+59
-41
lines changed

gopls/doc/features/diagnostics.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Diagnostics come from two main sources: compilation errors and analysis findings
4040
`fmt.Printf("%d", "three")`.
4141

4242
Gopls provides dozens of analyzers aggregated from a variety of
43-
suites. See [Analyzers](../analyzers.md) for the complete list. The
43+
suites; see [Analyzers](../analyzers.md) for the complete list. The
4444
`source` field of each diagnostic produced by an analyzer records
4545
the name of the analyzer that produced it.
4646

@@ -89,8 +89,14 @@ parameter" analysis diagnostic with two alternative fixes.
8989

9090
Suggested fixes that are indisputably safe are [code
9191
actions](transformation.md#code-actions) whose kind is
92-
`"source.fixAll"`. Many client editors have a shortcut to apply all
93-
such fixes.
92+
`"source.fixAll"`.
93+
Many client editors have a shortcut to apply all such fixes.
94+
<!-- Note: each Code Action has exactly one kind, so a server
95+
must offer each "safe" action twice, once with its usual kind
96+
and once with kind "source.fixAll".
97+
The only ones in gopls are simplify{compositelit,range,slice},
98+
which are fixAll + quickfix.
99+
-->
94100

95101
TODO(adonovan): audit all the analyzers to ensure that their
96102
documentation is up-to-date w.r.t. any fixes they suggest.
@@ -105,9 +111,9 @@ Settings:
105111
the base URI for Go package links in the Diagnostic.CodeDescription field.
106112

107113
Client support:
108-
- **VS Code**: Diagnostics appear as a squiggly underline.
114+
- **VS Code**: Each diagnostic appears as a squiggly underline.
109115
Hovering reveals the details, along with any suggested fixes.
110-
- **Emacs + eglot**: Diagnostics appear as a squiggly underline.
116+
- **Emacs + eglot**: Each diagnostic appears as a squiggly underline.
111117
Hovering reveals the details. Use `M-x eglot-code-action-quickfix`
112118
to apply available fixes; it will prompt if there are more than one.
113119
- **Vim + coc.nvim**: ??

gopls/doc/features/navigation.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
This page documents gopls features for navigating your source code.
44

5+
<!-- TODO: screenshots -->
6+
57
## Definition
68

79
The LSP [`textDocument/definition`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_definition)
@@ -35,7 +37,7 @@ Client support:
3537
## References
3638

3739
The LSP [`textDocument/references`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_references)
38-
request returns the locations of all identifiers that refers to the symbol under the cursor.
40+
request returns the locations of all identifiers that refer to the symbol under the cursor.
3941

4042
The references algorithm handles various parts of syntax as follows:
4143

@@ -169,7 +171,7 @@ LSP query searches an index of all the symbols in the workspace.
169171

170172
The default symbol matching algorithm (`fastFuzzy`), inspired by the
171173
popular fuzzy matcher [FZF](https://github.com/junegunn/fzf), attempts
172-
a variety of inexact matches to correct for misspellings in your
174+
a variety of inexact matches to correct for misspellings or abbreviations in your
173175
query. For example, it considers `DocSym` a match for `DocumentSymbol`.
174176

175177
<!--
@@ -185,8 +187,6 @@ However, VS Code does its own fuzzy matching afterward, so these
185187
aren't effective in that client; see golang/vscode-go#647.
186188
-->
187189

188-
TODO: screenshot
189-
190190
Settings:
191191
- The [`symbolMatcher`](../settings.md#symbolMatcher) setting controls the algorithm used for symbol matching.
192192
- The [`symbolStyle`](../settings.md#symbolStyle) setting controls how symbols are qualified in symbol responses.

gopls/doc/features/passive.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ considered passive features.
1313
## Hover
1414

1515
The LSP [`textDocument/hover`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover)
16-
query returns description of the code currently under the cursor, such
16+
query returns a description of the code currently under the cursor, such
1717
as its name, kind, type, value (for a constant), abbreviated
18-
declaration (for a type), its doc comment, if any, and a link to the
18+
declaration (for a type), doc comment (if any), and a link to the
1919
symbol's documentation on `pkg.go.dev`. The client may request either
2020
plain text or Markdown.
2121

2222
<img src='../assets/hover-basic.png'>
2323

24-
Depending on the selection, it may include additional information.
24+
Depending on the selection, the response may include additional information.
2525
For example, hovering over a type shows its declared methods,
2626
plus any methods promoted from embedded fields.
2727

@@ -132,8 +132,8 @@ select any one member, gopls will highlight the complete set:
132132
- the `switch` and `break` tokens of the same switch statement;
133133
- the `func` keyword of a function and all of its `return` statements.
134134

135-
More than one of these rules may be activated by the same selection,
136-
for example, an identifier that is also a return operand.
135+
More than one of these rules may be activated by a single selection,
136+
for example, by an identifier that is also a return operand.
137137

138138
<img src='../assets/document-highlight.png'>
139139

@@ -155,7 +155,7 @@ that reveal implicit information.
155155

156156
Examples:
157157

158-
- In a function call `f(1, 2)`, gopls will provide hints for the
158+
- In a function call `f(1, 2)`, hints provide the
159159
names of the parameters (`parameterNames`), as in the screenshot above.
160160
- In a call to a generic function, hints provide the type arguments
161161
(`functionTypeParameters`).
@@ -172,10 +172,12 @@ Examples:
172172

173173
See [Inlay hints](../inlayHints.md) for a complete list with examples.
174174

175+
<!--
175176
TODO: Do we really need that separate doc? We could put all the
176177
information here with much less fuss. It changes so rarely that a
177178
culture of "update the tests and docs in every CL" should be sufficient.
178179
IIUC, VS Code needs only the api-json representation.
180+
-->
179181

180182
Settings:
181183
- The [`hints`](../settings.md#hints) setting indicates the desired set of hints.
@@ -202,7 +204,7 @@ a portion of it.
202204
The client may use this information to provide syntax highlighting
203205
that conveys semantic distinctions between, for example, functions and
204206
types, constants and variables, or library functions and built-ins.
205-
The client specifies the sets of types and modifiers they are interested in.
207+
The client specifies the sets of types and modifiers it is interested in.
206208

207209
Settings:
208210
- The [`semanticTokens`](../settings.md#semanticTokens) setting determines whether
@@ -249,7 +251,7 @@ Client support:
249251

250252
The LSP [`textDocument/documentLink`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_documentLink)
251253
query uses heuristics to extracts URLs from doc comments and string
252-
literals in the current file so that client can present them as
254+
literals in the current file so that the client can present them as
253255
clickable links.
254256

255257
<img src='../assets/documentlink.png'>

gopls/doc/features/transformation.md

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This document describes gopls' features for code transformation, which
44
include a range of behavior-preserving changes (refactorings,
55
formatting, simplifications), code repair (fixes), and editing support
6-
(filling in struct literals, switch statements).
6+
(filling in struct literals and switch statements).
77

88
Code transformations are not a single category in the LSP:
99
- a few, such as Formatting and Rename, are primary operations in the
@@ -24,7 +24,7 @@ The VS Code manual describes code actions as
2424
"[Quick fixes + Refactorings](https://code.visualstudio.com/docs/editor/refactoring#_code-actions-quick-fixes-and-refactorings)".
2525

2626
A `codeAction` request delivers the menu, so to speak, but it does
27-
not order the meal. When an action is chosen, one of two things happens.
27+
not order the meal. Once the user chooses an action, one of two things happens.
2828
In trivial cases, the action itself contains an edit that the
2929
client can directly apply to the file.
3030
But in most cases, the action contains a [command](../commands.md)
@@ -36,24 +36,33 @@ The server may then compute the edit and send the client a
3636
[`workspace/applyEdit`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_applyEdit)
3737
request to patch the files.
3838
Not all code actions' commands have an `applyEdit` side effect: some
39-
may change the state of the server, for example, to toggle a
40-
variable, or cause the server to send other requests to the client,
41-
such as as a `showDocument` request to open a report in a web browser.
39+
may change the state of the server, for example to toggle a variable
40+
or to cause the server to send other requests to the client,
41+
such as a `showDocument` request to open a report in a web browser.
4242

4343
The main difference between code lenses and code actions is this:
44-
- `codeLens` requests commands for the entire file.
44+
- a `codeLens` request obtains commands for the entire file.
4545
Each command specifies its applicable source range,
4646
and typically appears as an annotation on that source range.
47-
- `codeAction` requests commands only for a particular range: the current selection.
47+
- a `codeAction` request obtains commands only for a particular range: the current selection.
4848
All the commands are presented together in a menu at that location.
4949

5050
Each action has a _kind_,
5151
which is a hierarchical identifier such as `refactor.inline`.
5252
Clients may filter actions based on their kind.
53-
For example, VS Code has two menus, "Refactor..." and "Source action...",
54-
each populated by different kinds of code actions (`refactor.*` and `source.*`),
55-
plus a lightbulb icon that triggers a menu of "quick fixes" (of kind `quickfix.*`),
56-
which are commands deemed unambiguously safe to apply.
53+
For example, VS Code has:
54+
two menus, "Refactor..." and "Source action...", each populated by
55+
different kinds of code actions (`refactor.*` and `source.*`);
56+
a lightbulb icon that triggers a menu of "quick fixes" (of kind `quickfix.*`);
57+
and a "Fix All" command that executes all code actions of
58+
kind `source.fixAll`, which are those deemed unambiguously safe to apply.
59+
60+
Gopls reports some code actions twice, with two different kinds, so
61+
that they appear in multiple UI elements: simplifications,
62+
for example from `for _ = range m` to `for range m`,
63+
have kinds `quickfix` and `source.fixAll`,
64+
so they appear in the "Quick Fix" menu and
65+
are activated by the "Fix All" command.
5766

5867
<!-- In principle the filter may include the trigger event
5968
e.g. auto (cursor motion) vs. invoked (open a menu)
@@ -71,7 +80,7 @@ for the current selection.
7180

7281
Caveats:
7382
- Many of gopls code transformations are limited by Go's syntax tree
74-
representation, which currently records comments not in the tree,
83+
representation, which currently records comments not in the tree
7584
but in a side table; consequently, transformations such as Extract
7685
and Inline are prone to losing comments. This is issue
7786
golang/go#20744, and it is a priority for us to fix in 2024.
@@ -88,6 +97,7 @@ Client support for code actions:
8897
the "Source action..." menu,
8998
the 💡 (light bulb) icon's menu, or
9099
the "Quick fix" (`⌘.`) menu.
100+
The "Fix All" command applies all actions of kind `source.fixAll`.
91101
- **Emacs + eglot**: Code actions are invisible.
92102
Use `M-x eglot-code-actions` to select one from those that are
93103
available (if there are multiple) and execute it.
@@ -126,8 +136,8 @@ deleting existing imports that are duplicate or unused,
126136
adding new ones for undefined symbols,
127137
and sorting them into the conventional order.
128138

129-
The addition of new imports is based on heuristics that depends on
130-
your workspace and the contents of your GOMODCACHE directory; it may
139+
The addition of new imports is based on heuristics that depend on
140+
your workspace and the contents of your GOMODCACHE directory; they may
131141
sometimes make surprising choices.
132142

133143
Many editors automatically organize imports and format the code before
@@ -207,7 +217,7 @@ invoking the rename operation on the type.
207217
Renaming should never introduce a compilation error, but it may
208218
introduce dynamic errors. For example, in a method renaming, if there
209219
is no direct conversion of the affected type to the interface type,
210-
but there is an intermediate conversion to `interface{}` followed by a
220+
but there is an intermediate conversion to a broader type (such as `any`) followed by a
211221
type assertion to the interface type, then gopls may proceed to rename
212222
the method, causing the type assertion to fail at run time.
213223
Similar problems may arise with packages that use reflection, such as
@@ -275,8 +285,8 @@ newly created declaration that contains the selected code:
275285
If the default name for the new declaration is already in use, gopls
276286
generates a fresh name.
277287

278-
Extraction is a challenging problem requiring consideration of a
279-
variety of aspects such as identifier scope and shadowing, control
288+
Extraction is a challenging problem requiring consideration of
289+
identifier scope and shadowing, control
280290
flow such as `break`/`continue` in a loop or `return` in a
281291
function, cardinality of variables, and even subtle issues of style.
282292
In each case, the tool will try to update the extracted statements
@@ -314,9 +324,9 @@ If you select one or more top-level declarations, gopls will offer an
314324
"Extract declarations to new file" code action that moves the selected
315325
declarations into a new file whose name is based on the first declared
316326
symbol.
327+
Import declarations are created as needed.
317328
Gopls also offers this code action when the selection is just the
318329
first token of the declaration, such as `func` or `type`.
319-
Import declarations are created as needed.
320330

321331
![Before: select the declarations to move](../assets/extract-to-new-file-before.png)
322332
![After: the new file is based on the first symbol name](../assets/extract-to-new-file-after.png)
@@ -414,7 +424,7 @@ As users report inputs that cause the compiler to emit suboptimal
414424
code, the compiler is improved to recognize more cases, or more rules,
415425
and more exceptions to rules---but this process has no end.
416426
Inlining is similar, except that "better" code means tidier code.
417-
The most conservative translation provides a simple but (hopefully!)
427+
The most conservative translation provides a simple but (hopefully)
418428
correct foundation, on top of which endless rules, and exceptions to
419429
rules, can embellish and improve the quality of the output.
420430

@@ -447,7 +457,7 @@ Here are some of the technical challenges involved in sound inlining:
447457
its argument expression, we must ensure that any names in the
448458
argument expression continue to refer to the same thing---not to a
449459
different declaration in the callee function body that happens to
450-
use the same name! The inliner must replace local references such as
460+
use the same name. The inliner must replace local references such as
451461
`Printf` by qualified references such as `fmt.Printf`, and add an
452462
import of package `fmt` as needed.
453463

@@ -468,7 +478,7 @@ Here are some of the technical challenges involved in sound inlining:
468478
and its corresponding parameter is never used, the expression
469479
may be eliminated. However, if the expression contains the last
470480
reference to a local variable at the caller, this may cause a compile
471-
error because the variable is now unused! So the inliner must be
481+
error because the variable is now unused. So the inliner must be
472482
cautious about eliminating references to local variables.
473483

474484
This is just a taste of the problem domain. If you're curious, the
@@ -524,12 +534,12 @@ call, the argument 2, a constant, was safely deleted.
524534

525535
When the selection is a string literal, gopls offers a code action
526536
to convert the string between raw form (`` `abc` ``) and interpreted
527-
form (`"abc"`):
537+
form (`"abc"`) where this is possible:
528538

529539
![Convert to interpreted](../assets/convert-string-interpreted.png)
530540
![Convert to raw](../assets/convert-string-raw.png)
531541

532-
Apply the code action a second time to revert back to the original
542+
Applying the code action a second time reverts back to the original
533543
form.
534544

535545
### Invert 'if' condition
@@ -549,7 +559,7 @@ blocks.
549559

550560
### Split elements into separate lines
551561

552-
When the selection is within a bracketed list such as:
562+
When the selection is within a bracketed list of items such as:
553563

554564
- the **elements** of a composite literal, `[]T{a, b, c}`,
555565
- the **arguments** of a function call, `f(a, b, c)`,

0 commit comments

Comments
 (0)