Skip to content

Commit 3e5fc1b

Browse files
committed
final edits, needs better docstring in expectedAssignStmtTypes
1 parent ed3b08f commit 3e5fc1b

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

gopls/internal/golang/completion/completion.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,10 @@ Nodes:
23032303
break Nodes
23042304
}
23052305
case *ast.AssignStmt:
2306-
return expectedAssignStmtCandidate(c, node, inf)
2306+
objType, assignees := expectedAssignStmtTypes(c, node)
2307+
inf.objType = objType
2308+
inf.assignees = assignees
2309+
return inf
23072310
case *ast.ValueSpec:
23082311
inf.objType = expectedValueSpecType(c, node)
23092312
return
@@ -2506,12 +2509,11 @@ func inferExpectedResultTypes(c *completer, callNodeIdx int) []types.Type {
25062509
expectedResults = append(expectedResults, expectedCompositeLiteralType(enclosingCompositeLiteral, c.pos))
25072510
}
25082511
case *ast.AssignStmt:
2509-
inf := expectedAssignStmtCandidate(c, node, candidateInference{})
2510-
if len(inf.assignees) > 0 {
2511-
expectedResults = make([]types.Type, len(inf.assignees))
2512-
copy(expectedResults, inf.assignees)
2513-
} else if inf.objType != nil {
2514-
expectedResults = append(expectedResults, inf.objType)
2512+
objType, assignees := expectedAssignStmtTypes(c, node)
2513+
if len(assignees) > 0 {
2514+
return assignees
2515+
} else if objType != nil {
2516+
expectedResults = append(expectedResults, objType)
25152517
}
25162518
case *ast.ValueSpec:
25172519
if resultType := expectedValueSpecType(c, node); resultType != nil {
@@ -2576,34 +2578,33 @@ func expectedValueSpecType(c *completer, node *ast.ValueSpec) types.Type {
25762578
return nil
25772579
}
25782580

2579-
// expectedAssignStmtCandidate returns information about the expected candidate
2580-
// for a AssignStmt at the query position.
2581-
func expectedAssignStmtCandidate(c *completer, node *ast.AssignStmt, inf candidateInference) candidateInference {
2581+
// expectedAssignStmtTypes returns the inference objType and assignees for the assignment.
2582+
func expectedAssignStmtTypes(c *completer, node *ast.AssignStmt) (objType types.Type, assignees []types.Type) {
25822583
// Only rank completions if you are on the right side of the token.
25832584
if c.pos > node.TokPos {
25842585
i := exprAtPos(c.pos, node.Rhs)
25852586
if i >= len(node.Lhs) {
25862587
i = len(node.Lhs) - 1
25872588
}
25882589
if tv, ok := c.pkg.TypesInfo().Types[node.Lhs[i]]; ok {
2589-
inf.objType = tv.Type
2590+
objType = tv.Type
25902591
}
25912592

25922593
// If we have a single expression on the RHS, record the LHS
25932594
// assignees so we can favor multi-return function calls with
25942595
// matching result values.
25952596
if len(node.Rhs) <= 1 {
25962597
for _, lhs := range node.Lhs {
2597-
inf.assignees = append(inf.assignees, c.pkg.TypesInfo().TypeOf(lhs))
2598+
assignees = append(assignees, c.pkg.TypesInfo().TypeOf(lhs))
25982599
}
25992600
} else {
26002601
// Otherwise, record our single assignee, even if its type is
26012602
// not available. We use this info to downrank functions
26022603
// with the wrong number of result values.
2603-
inf.assignees = append(inf.assignees, c.pkg.TypesInfo().TypeOf(node.Lhs[i]))
2604+
assignees = append(assignees, c.pkg.TypesInfo().TypeOf(node.Lhs[i]))
26042605
}
26052606
}
2606-
return inf
2607+
return objType, assignees
26072608
}
26082609

26092610
// expectedReturnStmtType returns the expected type of a return statement.

0 commit comments

Comments
 (0)