Skip to content

Commit 2e68ad7

Browse files
muirdmstamblerre
authored andcommitted
internal/lsp: fix scope of FuncType completion candidates
Fix objects defined in the function signature to only be completable inside the function body. For example: func (dog Dog) bark(d<>) { // Don't complete <> to "dog". d<> // Do complete <> to "dog". } Change-Id: Ic9a2dc2ce6771212780f2d6af2221a67d203f35f Reviewed-on: https://go-review.googlesource.com/c/tools/+/196559 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent ae58c0f commit 2e68ad7

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

internal/lsp/source/completion.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,16 @@ func (c *completer) methodsAndFields(typ types.Type, addressable bool) error {
563563
func (c *completer) lexical() error {
564564
var scopes []*types.Scope // scopes[i], where i<len(path), is the possibly nil Scope of path[i].
565565
for _, n := range c.path {
566+
// Include *FuncType scope if pos is inside the function body.
566567
switch node := n.(type) {
567568
case *ast.FuncDecl:
568-
n = node.Type
569+
if node.Body != nil && nodeContains(node.Body, c.pos) {
570+
n = node.Type
571+
}
569572
case *ast.FuncLit:
570-
n = node.Type
573+
if node.Body != nil && nodeContains(node.Body, c.pos) {
574+
n = node.Type
575+
}
571576
}
572577
scopes = append(scopes, c.pkg.GetTypesInfo().Scopes[n])
573578
}
@@ -654,6 +659,10 @@ func (c *completer) lexical() error {
654659
return nil
655660
}
656661

662+
func nodeContains(n ast.Node, pos token.Pos) bool {
663+
return n != nil && n.Pos() <= pos && pos < n.End()
664+
}
665+
657666
func (c *completer) inConstDecl() bool {
658667
for _, n := range c.path {
659668
if decl, ok := n.(*ast.GenDecl); ok && decl.Tok == token.CONST {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package funcsig
2+
3+
type someType int //@item(sigSomeType, "someType", "int", "type")
4+
5+
// Don't complete "foo" in signature.
6+
func (foo someType) _() { //@item(sigFoo, "foo", "someType", "var"),complete(") {", sigSomeType)
7+
8+
//@complete("", sigFoo, sigSomeType)
9+
}

internal/lsp/tests/tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// We hardcode the expected number of test cases to ensure that all tests
3030
// are being executed. If a test is added, this number must be changed.
3131
const (
32-
ExpectedCompletionsCount = 152
32+
ExpectedCompletionsCount = 154
3333
ExpectedCompletionSnippetCount = 36
3434
ExpectedUnimportedCompletionsCount = 1
3535
ExpectedDeepCompletionsCount = 5

0 commit comments

Comments
 (0)