2019-09-18 13:26:39 -06:00
|
|
|
// Copyright 2019 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 source
|
|
|
|
|
|
|
|
import (
|
2020-04-05 23:18:15 -06:00
|
|
|
"context"
|
2019-09-18 13:26:39 -06:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2019-12-27 13:45:09 -07:00
|
|
|
"math"
|
2019-09-18 13:26:39 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type labelType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
labelNone labelType = iota
|
|
|
|
labelBreak
|
|
|
|
labelContinue
|
|
|
|
labelGoto
|
|
|
|
)
|
|
|
|
|
|
|
|
// wantLabelCompletion returns true if we want (only) label
|
|
|
|
// completions at the position.
|
|
|
|
func (c *completer) wantLabelCompletion() labelType {
|
|
|
|
if _, ok := c.path[0].(*ast.Ident); ok && len(c.path) > 1 {
|
|
|
|
// We want a label if we are an *ast.Ident child of a statement
|
|
|
|
// that accepts a label, e.g. "break Lo<>".
|
|
|
|
return takesLabel(c.path[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
return labelNone
|
|
|
|
}
|
|
|
|
|
|
|
|
// takesLabel returns the corresponding labelType if n is a statement
|
|
|
|
// that accepts a label, otherwise labelNone.
|
|
|
|
func takesLabel(n ast.Node) labelType {
|
|
|
|
if bs, ok := n.(*ast.BranchStmt); ok {
|
|
|
|
switch bs.Tok {
|
|
|
|
case token.BREAK:
|
|
|
|
return labelBreak
|
|
|
|
case token.CONTINUE:
|
|
|
|
return labelContinue
|
|
|
|
case token.GOTO:
|
|
|
|
return labelGoto
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return labelNone
|
|
|
|
}
|
|
|
|
|
|
|
|
// labels adds completion items for labels defined in the enclosing
|
|
|
|
// function.
|
2020-04-05 23:18:15 -06:00
|
|
|
func (c *completer) labels(ctx context.Context, lt labelType) {
|
2019-09-18 13:26:39 -06:00
|
|
|
if c.enclosingFunc == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-27 13:45:09 -07:00
|
|
|
addLabel := func(score float64, l *ast.LabeledStmt) {
|
2019-09-18 13:26:39 -06:00
|
|
|
labelObj := c.pkg.GetTypesInfo().ObjectOf(l.Label)
|
|
|
|
if labelObj != nil {
|
2020-04-05 23:18:15 -06:00
|
|
|
c.found(ctx, candidate{obj: labelObj, score: score})
|
2019-09-18 13:26:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch lt {
|
|
|
|
case labelBreak, labelContinue:
|
|
|
|
// "break" and "continue" only accept labels from enclosing statements.
|
|
|
|
|
2019-12-27 13:45:09 -07:00
|
|
|
for i, p := range c.path {
|
2019-09-18 13:26:39 -06:00
|
|
|
switch p := p.(type) {
|
|
|
|
case *ast.FuncLit:
|
|
|
|
// Labels are function scoped, so don't continue out of functions.
|
|
|
|
return
|
|
|
|
case *ast.LabeledStmt:
|
|
|
|
switch p.Stmt.(type) {
|
2019-10-22 15:13:24 -06:00
|
|
|
case *ast.ForStmt, *ast.RangeStmt:
|
2019-09-18 13:26:39 -06:00
|
|
|
// Loop labels can be used for "break" or "continue".
|
2019-12-27 13:45:09 -07:00
|
|
|
addLabel(highScore*math.Pow(.99, float64(i)), p)
|
2019-10-22 15:13:24 -06:00
|
|
|
case *ast.SwitchStmt, *ast.SelectStmt, *ast.TypeSwitchStmt:
|
2019-09-18 13:26:39 -06:00
|
|
|
// Switch and select labels can be used only for "break".
|
|
|
|
if lt == labelBreak {
|
2019-12-27 13:45:09 -07:00
|
|
|
addLabel(highScore*math.Pow(.99, float64(i)), p)
|
2019-09-18 13:26:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case labelGoto:
|
|
|
|
// Goto accepts any label in the same function not in a nested
|
|
|
|
// block. It also doesn't take labels that would jump across
|
|
|
|
// variable definitions, but ignore that case for now.
|
|
|
|
ast.Inspect(c.enclosingFunc.body, func(n ast.Node) bool {
|
|
|
|
if n == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch n := n.(type) {
|
|
|
|
// Only search into block-like nodes enclosing our "goto".
|
|
|
|
// This prevents us from finding labels in nested blocks.
|
|
|
|
case *ast.BlockStmt, *ast.CommClause, *ast.CaseClause:
|
|
|
|
for _, p := range c.path {
|
|
|
|
if n == p {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
case *ast.LabeledStmt:
|
2019-12-27 13:45:09 -07:00
|
|
|
addLabel(highScore, n)
|
2019-09-18 13:26:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|