-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gopls/internal/golang/completion: fix package clause completion suffix
CL 585275 introduced a call to completion.Surrounding.Suffix that exposed a latent bug in package clause completions: the completion content was not being correctly computed as containing the cursor. Fix the heuristics for completing at "package<whitespace>|", so that the surrounding content is correct. As a result of this change the 'editRegexp' from some completion tests now no longer includes the newline, which also seems more correct. Fixes golang/go#68169 Change-Id: I32ea20a7f9f0096aef85ed77c64d3b4dfeedef45 Reviewed-on: https://go-review.googlesource.com/c/tools/+/594796 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com> Auto-Submit: Robert Findley <rfindley@google.com>
- Loading branch information
Showing
3 changed files
with
57 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
gopls/internal/test/integration/completion/fixedbugs_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package completion | ||
|
||
import ( | ||
"testing" | ||
|
||
. "golang.org/x/tools/gopls/internal/test/integration" | ||
) | ||
|
||
func TestPackageCompletionCrash_Issue68169(t *testing.T) { | ||
// This test reproduces the scenario of golang/go#68169, a crash in | ||
// completion.Selection.Suffix. | ||
// | ||
// The file content here is extracted from the issue. | ||
const files = ` | ||
-- go.mod -- | ||
module example.com | ||
go 1.18 | ||
-- playdos/play.go -- | ||
package | ||
` | ||
|
||
Run(t, files, func(t *testing.T, env *Env) { | ||
env.OpenFile("playdos/play.go") | ||
// Previously, this call would crash gopls as it was incorrectly computing | ||
// the surrounding completion suffix. | ||
completions := env.Completion(env.RegexpSearch("playdos/play.go", "package ()")) | ||
if len(completions.Items) == 0 { | ||
t.Fatal("Completion() returned empty results") | ||
} | ||
// Sanity check: we should get package clause compeltion. | ||
if got, want := completions.Items[0].Label, "package playdos"; got != want { | ||
t.Errorf("Completion()[0].Label == %s, want %s", got, want) | ||
} | ||
}) | ||
} |