Skip to content

Commit ad41b8f

Browse files
committed
all: merge master (7404bd2) into gopls-release-branch.0.9
Also add back the gopls/go.mod replace directive, so that tests pass. Merge List: + 2022-06-27 7404bd2 all: gofmt some recent file changes + 2022-06-27 ec0831a refactor/satisfy: don't crash on type parameters + 2022-06-27 66bbba3 internal/memoize: remove unused Store.generations map + 2022-06-27 56116ec internal/memoize: don't destroy reference counted handles + 2022-06-25 10494c7 cmd/digraph: fix typo + 2022-06-24 93a03c2 internal/lsp/cache: invalidate reverse closure when loading packages + 2022-06-24 c36379b internal/lsp/cache: don't set new metadata if existing is valid + 2022-06-24 e1ec1f3 internal/imports: use a module resolver if GOWORK is set + 2022-06-24 60ca636 internal/lsp: use camel case for inlay hint config fields + 2022-06-24 2994e99 internal/persistent: change map to use set/get as method names + 2022-06-23 22ab253 internal/lsp: rename viewport to range + 2022-06-23 3f5f798 internal/lsp/cache: use persistent map for storing files in the snapshot + 2022-06-22 f60e9bc internal/lsp/cache: use persistent map for storing gofiles in the snapshot + 2022-06-22 871637b internal/lsp: add settings for inlay hints and enable + 2022-06-22 a44cc76 cmd/stringer: use explicit NeedX values instead of deprecated LoadSyntax + 2022-06-22 4e231cb internal/lsp/cache: don't prune unreachable metadata on clone + 2022-06-22 a2de635 internal/lsp/cache: honor the go.work for computing workspace packages + 2022-06-22 cbb8e8e internal/span: optimise URI.Filename to avoid allocation + 2022-06-21 63d8015 internal/lsp/cache: minor simplifications to Symbols + 2022-06-21 59bd4fa internal/lsp: find references to package + 2022-06-21 a1303c8 internal/lsp: remove tooltip from inlay hints + 2022-06-21 641b30b internal/lsp: add inlay hints for inferred type params + 2022-06-21 70ccf57 go/packages: fix loading single file when outside of GOPATH, module + 2022-06-17 381ac87 internal/lsp/debug: reduce critical sections in trace + 2022-06-17 1e14d99 internal/lsp: add inlay hints for composite literal types + 2022-06-16 e987015 internal/lsp/cache: symbolize in parallel + 2022-06-16 88325aa internal/memoize: add trace region for Handle.run Change-Id: I7501b2d80d2f6983f6b32ae5733444f0fec52fcc
2 parents 8d69276 + 7404bd2 commit ad41b8f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2670
-615
lines changed

cmd/digraph/digraph.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ The support commands are:
3434
sccs
3535
all strongly connected components (one per line)
3636
scc <node>
37-
the set of nodes nodes strongly connected to the specified one
37+
the set of nodes strongly connected to the specified one
3838
focus <node>
3939
the subgraph containing all directed paths that pass through the specified node
4040

cmd/stringer/stringer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ type Package struct {
217217
// parsePackage exits if there is an error.
218218
func (g *Generator) parsePackage(patterns []string, tags []string) {
219219
cfg := &packages.Config{
220-
Mode: packages.LoadSyntax,
220+
Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax,
221221
// TODO: Need to think about constants in test files. Maybe write type_string_test.go
222222
// in a separate pass? For later.
223223
Tests: false,

go/packages/golist.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,12 @@ func (state *golistState) runContainsQueries(response *responseDeduper, queries
302302
}
303303
dirResponse, err := state.createDriverResponse(pattern)
304304

305-
// If there was an error loading the package, or the package is returned
306-
// with errors, try to load the file as an ad-hoc package.
305+
// If there was an error loading the package, or no packages are returned,
306+
// or the package is returned with errors, try to load the file as an
307+
// ad-hoc package.
307308
// Usually the error will appear in a returned package, but may not if we're
308309
// in module mode and the ad-hoc is located outside a module.
309-
if err != nil || len(dirResponse.Packages) == 1 && len(dirResponse.Packages[0].GoFiles) == 0 &&
310+
if err != nil || len(dirResponse.Packages) == 0 || len(dirResponse.Packages) == 1 && len(dirResponse.Packages[0].GoFiles) == 0 &&
310311
len(dirResponse.Packages[0].Errors) == 1 {
311312
var queryErr error
312313
if dirResponse, queryErr = state.adhocPackage(pattern, query); queryErr != nil {

go/packages/packages_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,6 +2709,31 @@ func TestEmptyEnvironment(t *testing.T) {
27092709
}
27102710
}
27112711

2712+
func TestPackageLoadSingleFile(t *testing.T) {
2713+
tmp, err := ioutil.TempDir("", "a")
2714+
if err != nil {
2715+
t.Fatal(err)
2716+
}
2717+
defer os.RemoveAll(tmp)
2718+
2719+
filename := filepath.Join(tmp, "a.go")
2720+
2721+
if err := ioutil.WriteFile(filename, []byte(`package main; func main() { println("hello world") }`), 0775); err != nil {
2722+
t.Fatal(err)
2723+
}
2724+
2725+
pkgs, err := packages.Load(&packages.Config{Mode: packages.LoadSyntax, Dir: tmp}, "file="+filename)
2726+
if err != nil {
2727+
t.Fatalf("could not load package: %v", err)
2728+
}
2729+
if len(pkgs) != 1 {
2730+
t.Fatalf("expected one package to be loaded, got %d", len(pkgs))
2731+
}
2732+
if len(pkgs[0].CompiledGoFiles) != 1 || pkgs[0].CompiledGoFiles[0] != filename {
2733+
t.Fatalf("expected one compiled go file (%q), got %v", filename, pkgs[0].CompiledGoFiles)
2734+
}
2735+
}
2736+
27122737
func errorMessages(errors []packages.Error) []string {
27132738
var msgs []string
27142739
for _, err := range errors {

gopls/doc/generate.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func doMain(baseDir string, write bool) (bool, error) {
6363
if ok, err := rewriteFile(filepath.Join(baseDir, "gopls/doc/analyzers.md"), api, write, rewriteAnalyzers); !ok || err != nil {
6464
return ok, err
6565
}
66+
if ok, err := rewriteFile(filepath.Join(baseDir, "gopls/doc/inlayHints.md"), api, write, rewriteInlayHints); !ok || err != nil {
67+
return ok, err
68+
}
6669

6770
return true, nil
6871
}
@@ -102,6 +105,7 @@ func loadAPI() (*source.APIJSON, error) {
102105
} {
103106
api.Analyzers = append(api.Analyzers, loadAnalyzers(m)...)
104107
}
108+
api.Hints = loadHints(source.AllInlayHints)
105109
for _, category := range []reflect.Value{
106110
reflect.ValueOf(defaults.UserOptions),
107111
} {
@@ -146,6 +150,14 @@ func loadAPI() (*source.APIJSON, error) {
146150
Default: def,
147151
})
148152
}
153+
case "hints":
154+
for _, a := range api.Hints {
155+
opt.EnumKeys.Keys = append(opt.EnumKeys.Keys, source.EnumKey{
156+
Name: fmt.Sprintf("%q", a.Name),
157+
Doc: a.Doc,
158+
Default: strconv.FormatBool(a.Default),
159+
})
160+
}
149161
}
150162
}
151163
}
@@ -488,6 +500,23 @@ func loadAnalyzers(m map[string]*source.Analyzer) []*source.AnalyzerJSON {
488500
return json
489501
}
490502

503+
func loadHints(m map[string]*source.Hint) []*source.HintJSON {
504+
var sorted []string
505+
for _, h := range m {
506+
sorted = append(sorted, h.Name)
507+
}
508+
sort.Strings(sorted)
509+
var json []*source.HintJSON
510+
for _, name := range sorted {
511+
h := m[name]
512+
json = append(json, &source.HintJSON{
513+
Name: h.Name,
514+
Doc: h.Doc,
515+
})
516+
}
517+
return json
518+
}
519+
491520
func lowerFirst(x string) string {
492521
if x == "" {
493522
return x
@@ -699,6 +728,21 @@ func rewriteAnalyzers(doc []byte, api *source.APIJSON) ([]byte, error) {
699728
return replaceSection(doc, "Analyzers", section.Bytes())
700729
}
701730

731+
func rewriteInlayHints(doc []byte, api *source.APIJSON) ([]byte, error) {
732+
section := bytes.NewBuffer(nil)
733+
for _, hint := range api.Hints {
734+
fmt.Fprintf(section, "## **%v**\n\n", hint.Name)
735+
fmt.Fprintf(section, "%s\n\n", hint.Doc)
736+
switch hint.Default {
737+
case true:
738+
fmt.Fprintf(section, "**Enabled by default.**\n\n")
739+
case false:
740+
fmt.Fprintf(section, "**Disabled by default. Enable it by setting `\"hints\": {\"%s\": true}`.**\n\n", hint.Name)
741+
}
742+
}
743+
return replaceSection(doc, "Hints", section.Bytes())
744+
}
745+
702746
func replaceSection(doc []byte, sectionName string, replacement []byte) ([]byte, error) {
703747
re := regexp.MustCompile(fmt.Sprintf(`(?s)<!-- BEGIN %v.* -->\n(.*?)<!-- END %v.* -->`, sectionName, sectionName))
704748
idx := re.FindSubmatchIndex(doc)

gopls/doc/inlayHints.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Hints
2+
3+
This document describes the inlay hints that `gopls` uses inside the editor.
4+
5+
<!-- BEGIN Hints: DO NOT MANUALLY EDIT THIS SECTION -->
6+
## **assignVariableTypes**
7+
8+
Enable/disable inlay hints for variable types in assign statements:
9+
10+
i/* int/*, j/* int/* := 0, len(r)-1
11+
12+
**Disabled by default. Enable it by setting `"hints": {"assignVariableTypes": true}`.**
13+
14+
## **compositeLiteralFields**
15+
16+
Enable/disable inlay hints for composite literal field names:
17+
18+
{in: "Hello, world", want: "dlrow ,olleH"}
19+
20+
**Disabled by default. Enable it by setting `"hints": {"compositeLiteralFields": true}`.**
21+
22+
## **compositeLiteralTypes**
23+
24+
Enable/disable inlay hints for composite literal types:
25+
26+
for _, c := range []struct {
27+
in, want string
28+
}{
29+
/*struct{ in string; want string }*/{"Hello, world", "dlrow ,olleH"},
30+
}
31+
32+
**Disabled by default. Enable it by setting `"hints": {"compositeLiteralTypes": true}`.**
33+
34+
## **constantValues**
35+
36+
Enable/disable inlay hints for constant values:
37+
38+
const (
39+
KindNone Kind = iota/* = 0*/
40+
KindPrint/* = 1*/
41+
KindPrintf/* = 2*/
42+
KindErrorf/* = 3*/
43+
)
44+
45+
**Disabled by default. Enable it by setting `"hints": {"constantValues": true}`.**
46+
47+
## **functionTypeParameters**
48+
49+
Enable/disable inlay hints for implicit type parameters on generic functions:
50+
51+
myFoo/*[int, string]*/(1, "hello")
52+
53+
**Disabled by default. Enable it by setting `"hints": {"functionTypeParameters": true}`.**
54+
55+
## **parameterNames**
56+
57+
Enable/disable inlay hints for parameter names:
58+
59+
parseInt(/* str: */ "123", /* radix: */ 8)
60+
61+
**Disabled by default. Enable it by setting `"hints": {"parameterNames": true}`.**
62+
63+
## **rangeVariableTypes**
64+
65+
Enable/disable inlay hints for variable types in range statements:
66+
67+
for k/* int*/, v/* string/* := range []string{} {
68+
fmt.Println(k, v)
69+
}
70+
71+
**Disabled by default. Enable it by setting `"hints": {"rangeVariableTypes": true}`.**
72+
73+
<!-- END Hints: DO NOT MANUALLY EDIT THIS SECTION -->

gopls/doc/settings.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ still be able to independently override specific experimental features.
3535
* [Completion](#completion)
3636
* [Diagnostic](#diagnostic)
3737
* [Documentation](#documentation)
38+
* [Inlayhint](#inlayhint)
3839
* [Navigation](#navigation)
3940

4041
### Build
@@ -370,6 +371,18 @@ linksInHover toggles the presence of links to documentation in hover.
370371

371372
Default: `true`.
372373

374+
#### Inlayhint
375+
376+
##### **hints** *map[string]bool*
377+
378+
**This setting is experimental and may be deleted.**
379+
380+
hints specify inlay hints that users want to see.
381+
A full list of hints that gopls uses can be found
382+
[here](https://github.com/golang/tools/blob/master/gopls/doc/inlayHints.md).
383+
384+
Default: `{}`.
385+
373386
#### Navigation
374387

375388
##### **importShortcut** *enum*

gopls/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ require (
2424
golang.org/x/text v0.3.7 // indirect
2525
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
2626
)
27+
28+
replace golang.org/x/tools => ../

gopls/internal/regtest/misc/imports_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,49 @@ func TestA(t *testing.T) {
214214
)
215215
})
216216
}
217+
218+
// Test for golang/go#52784
219+
func TestGoWorkImports(t *testing.T) {
220+
testenv.NeedsGo1Point(t, 18)
221+
const pkg = `
222+
-- go.work --
223+
go 1.19
224+
225+
use (
226+
./caller
227+
./mod
228+
)
229+
-- caller/go.mod --
230+
module caller.com
231+
232+
go 1.18
233+
234+
require mod.com v0.0.0
235+
236+
replace mod.com => ../mod
237+
-- caller/caller.go --
238+
package main
239+
240+
func main() {
241+
a.Test()
242+
}
243+
-- mod/go.mod --
244+
module mod.com
245+
246+
go 1.18
247+
-- mod/a/a.go --
248+
package a
249+
250+
func Test() {
251+
}
252+
`
253+
Run(t, pkg, func(t *testing.T, env *Env) {
254+
env.OpenFile("caller/caller.go")
255+
env.Await(env.DiagnosticAtRegexp("caller/caller.go", "a.Test"))
256+
257+
// Saving caller.go should trigger goimports, which should find a.Test in
258+
// the mod.com module, thanks to the go.work file.
259+
env.SaveBuffer("caller/caller.go")
260+
env.Await(EmptyDiagnostics("caller/caller.go"))
261+
})
262+
}

0 commit comments

Comments
 (0)