1
0
mirror of https://github.com/golang/go synced 2024-11-18 21:24:44 -07:00
go/internal/lsp/source/completion_keywords.go
Muir Manders 8a925fa4c0 internal/lsp/source: improve completions at file scope
Add support for var/func/const/type/import keywords at the file scope.
I left out "package" because, currently, if you are completing
something that means you must already have a package declaration. The
main hurdle was that anything other than a decl keyword shows up as
an *ast.BadDecl at the file scope. To properly detect the prefix we
manually scan for the token containing the position.

I also made a couple small drive-by improvements:
 - Also suggest "goto" and "type" keywords in functions.
 - Allow completing directly before a comment, e.g. "foo<>//". I
   needed this for a test that would have been annoying to write
   otherwise.

Updates golang/go#34009.

Change-Id: I290e7bdda9e66a16f996cdc291985a54bf375231
Reviewed-on: https://go-review.googlesource.com/c/tools/+/217500
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2020-02-20 22:48:06 +00:00

117 lines
3.1 KiB
Go

package source
import (
"go/ast"
"golang.org/x/tools/internal/lsp/protocol"
)
const (
BREAK = "break"
CASE = "case"
CHAN = "chan"
CONST = "const"
CONTINUE = "continue"
DEFAULT = "default"
DEFER = "defer"
ELSE = "else"
FALLTHROUGH = "fallthrough"
FOR = "for"
FUNC = "func"
GO = "go"
GOTO = "goto"
IF = "if"
IMPORT = "import"
INTERFACE = "interface"
MAP = "map"
PACKAGE = "package"
RANGE = "range"
RETURN = "return"
SELECT = "select"
STRUCT = "struct"
SWITCH = "switch"
TYPE = "type"
VAR = "var"
)
// addKeywordCompletions offers keyword candidates appropriate at the position.
func (c *completer) addKeywordCompletions() {
const keywordScore = 0.9
seen := make(map[string]bool)
// addKeywords dedupes and adds completion items for the specified
// keywords with the specified score.
addKeywords := func(score float64, kws ...string) {
for _, kw := range kws {
if seen[kw] {
continue
}
seen[kw] = true
if c.matcher.Score(kw) > 0 {
c.items = append(c.items, CompletionItem{
Label: kw,
Kind: protocol.KeywordCompletion,
InsertText: kw,
Score: score,
})
}
}
}
// If we are at the file scope, only offer decl keywords. We don't
// get *ast.Idents at the file scope because non-keyword identifiers
// turn into *ast.BadDecl, not *ast.Ident.
if len(c.path) == 1 || isASTFile(c.path[1]) {
addKeywords(keywordScore, TYPE, CONST, VAR, FUNC, IMPORT)
return
} else if _, ok := c.path[0].(*ast.Ident); !ok {
// Otherwise only offer keywords if the client is completing an identifier.
return
}
// Only suggest keywords if we are beginning a statement.
switch c.path[1].(type) {
case *ast.BlockStmt, *ast.CommClause, *ast.CaseClause, *ast.ExprStmt:
default:
return
}
// Filter out keywords depending on scope
// Skip the first one because we want to look at the enclosing scopes
path := c.path[1:]
for i, n := range path {
switch node := n.(type) {
case *ast.CaseClause:
// only recommend "fallthrough" and "break" within the bodies of a case clause
if c.pos > node.Colon {
addKeywords(keywordScore, BREAK)
// "fallthrough" is only valid in switch statements.
// A case clause is always nested within a block statement in a switch statement,
// that block statement is nested within either a TypeSwitchStmt or a SwitchStmt.
if i+2 >= len(path) {
continue
}
if _, ok := path[i+2].(*ast.SwitchStmt); ok {
addKeywords(keywordScore, FALLTHROUGH)
}
}
case *ast.CommClause:
if c.pos > node.Colon {
addKeywords(keywordScore, BREAK)
}
case *ast.TypeSwitchStmt, *ast.SelectStmt, *ast.SwitchStmt:
addKeywords(keywordScore+lowScore, CASE, DEFAULT)
case *ast.ForStmt:
addKeywords(keywordScore, BREAK, CONTINUE)
// This is a bit weak, functions allow for many keywords
case *ast.FuncDecl:
if node.Body != nil && c.pos > node.Body.Lbrace {
addKeywords(keywordScore-lowScore, DEFER, RETURN, FOR, GO, SWITCH, SELECT, IF, ELSE, VAR, CONST, GOTO, TYPE)
}
}
}
}