1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
// Copyright 2020 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 (
"context"
"fmt"
"go/ast"
"go/token"
"go/types"
"log"
"reflect"
"strings"
"sync"
"text/template"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/snippet"
"golang.org/x/tools/gopls/internal/lsp/source"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/imports"
)
// Postfix snippets are artificial methods that allow the user to
// compose common operations in an "argument oriented" fashion. For
// example, instead of "sort.Slice(someSlice, ...)" a user can expand
// "someSlice.sort!".
// postfixTmpl represents a postfix snippet completion candidate.
type postfixTmpl struct {
// label is the completion candidate's label presented to the user.
label string
// details is passed along to the client as the candidate's details.
details string
// body is the template text. See postfixTmplArgs for details on the
// facilities available to the template.
body string
tmpl *template.Template
}
// postfixTmplArgs are the template execution arguments available to
// the postfix snippet templates.
type postfixTmplArgs struct {
// StmtOK is true if it is valid to replace the selector with a
// statement. For example:
//
// func foo() {
// bar.sort! // statement okay
//
// someMethod(bar.sort!) // statement not okay
// }
StmtOK bool
// X is the textual SelectorExpr.X. For example, when completing
// "foo.bar.print!", "X" is "foo.bar".
X string
// Obj is the types.Object of SelectorExpr.X, if any.
Obj types.Object
// Type is the type of "foo.bar" in "foo.bar.print!".
Type types.Type
scope *types.Scope
snip snippet.Builder
importIfNeeded func(pkgPath string, scope *types.Scope) (name string, edits []protocol.TextEdit, err error)
edits []protocol.TextEdit
qf types.Qualifier
varNames map[string]bool
}
var postfixTmpls = []postfixTmpl{{
label: "sort",
details: "sort.Slice()",
body: `{{if and (eq .Kind "slice") .StmtOK -}}
{{.Import "sort"}}.Slice({{.X}}, func({{.VarName nil "i"}}, {{.VarName nil "j"}} int) bool {
{{.Cursor}}
})
{{- end}}`,
}, {
label: "last",
details: "s[len(s)-1]",
body: `{{if and (eq .Kind "slice") .Obj -}}
{{.X}}[len({{.X}})-1]
{{- end}}`,
}, {
label: "reverse",
details: "reverse slice",
body: `{{if and (eq .Kind "slice") .StmtOK -}}
{{$i := .VarName nil "i"}}{{$j := .VarName nil "j" -}}
for {{$i}}, {{$j}} := 0, len({{.X}})-1; {{$i}} < {{$j}}; {{$i}}, {{$j}} = {{$i}}+1, {{$j}}-1 {
{{.X}}[{{$i}}], {{.X}}[{{$j}}] = {{.X}}[{{$j}}], {{.X}}[{{$i}}]
}
{{end}}`,
}, {
label: "range",
details: "range over slice",
body: `{{if and (eq .Kind "slice") .StmtOK -}}
for {{.VarName nil "i"}}, {{.VarName .ElemType "v"}} := range {{.X}} {
{{.Cursor}}
}
{{- end}}`,
}, {
label: "append",
details: "append and re-assign slice",
body: `{{if and (eq .Kind "slice") .StmtOK .Obj -}}
{{.X}} = append({{.X}}, {{.Cursor}})
{{- end}}`,
}, {
label: "append",
details: "append to slice",
body: `{{if and (eq .Kind "slice") (not .StmtOK) -}}
append({{.X}}, {{.Cursor}})
{{- end}}`,
}, {
label: "copy",
details: "duplicate slice",
body: `{{if and (eq .Kind "slice") .StmtOK .Obj -}}
{{$v := (.VarName nil (printf "%sCopy" .X))}}{{$v}} := make([]{{.TypeName .ElemType}}, len({{.X}}))
copy({{$v}}, {{.X}})
{{end}}`,
}, {
label: "range",
details: "range over map",
body: `{{if and (eq .Kind "map") .StmtOK -}}
for {{.VarName .KeyType "k"}}, {{.VarName .ElemType "v"}} := range {{.X}} {
{{.Cursor}}
}
{{- end}}`,
}, {
label: "clear",
details: "clear map contents",
body: `{{if and (eq .Kind "map") .StmtOK -}}
{{$k := (.VarName .KeyType "k")}}for {{$k}} := range {{.X}} {
delete({{.X}}, {{$k}})
}
{{end}}`,
}, {
label: "keys",
details: "create slice of keys",
body: `{{if and (eq .Kind "map") .StmtOK -}}
{{$keysVar := (.VarName nil "keys")}}{{$keysVar}} := make([]{{.TypeName .KeyType}}, 0, len({{.X}}))
{{$k := (.VarName .KeyType "k")}}for {{$k}} := range {{.X}} {
{{$keysVar}} = append({{$keysVar}}, {{$k}})
}
{{end}}`,
}, {
label: "range",
details: "range over channel",
body: `{{if and (eq .Kind "chan") .StmtOK -}}
for {{.VarName .ElemType "e"}} := range {{.X}} {
{{.Cursor}}
}
{{- end}}`,
}, {
label: "var",
details: "assign to variables",
body: `{{if and (eq .Kind "tuple") .StmtOK -}}
{{$a := .}}{{range $i, $v := .Tuple}}{{if $i}}, {{end}}{{$a.VarName $v.Type $v.Name}}{{end}} := {{.X}}
{{- end}}`,
}, {
label: "var",
details: "assign to variable",
body: `{{if and (ne .Kind "tuple") .StmtOK -}}
{{.VarName .Type ""}} := {{.X}}
{{- end}}`,
}, {
label: "print",
details: "print to stdout",
body: `{{if and (ne .Kind "tuple") .StmtOK -}}
{{.Import "fmt"}}.Printf("{{.EscapeQuotes .X}}: %v\n", {{.X}})
{{- end}}`,
}, {
label: "print",
details: "print to stdout",
body: `{{if and (eq .Kind "tuple") .StmtOK -}}
{{.Import "fmt"}}.Println({{.X}})
{{- end}}`,
}, {
label: "split",
details: "split string",
body: `{{if (eq (.TypeName .Type) "string") -}}
{{.Import "strings"}}.Split({{.X}}, "{{.Cursor}}")
{{- end}}`,
}, {
label: "join",
details: "join string slice",
body: `{{if and (eq .Kind "slice") (eq (.TypeName .ElemType) "string") -}}
{{.Import "strings"}}.Join({{.X}}, "{{.Cursor}}")
{{- end}}`,
}}
// Cursor indicates where the client's cursor should end up after the
// snippet is done.
func (a *postfixTmplArgs) Cursor() string {
a.snip.WriteFinalTabstop()
return ""
}
// Import makes sure the package corresponding to path is imported,
// returning the identifier to use to refer to the package.
func (a *postfixTmplArgs) Import(path string) (string, error) {
name, edits, err := a.importIfNeeded(path, a.scope)
if err != nil {
return "", fmt.Errorf("couldn't import %q: %w", path, err)
}
a.edits = append(a.edits, edits...)
return name, nil
}
func (a *postfixTmplArgs) EscapeQuotes(v string) string {
return strings.ReplaceAll(v, `"`, `\\"`)
}
// ElemType returns the Elem() type of xType, if applicable.
func (a *postfixTmplArgs) ElemType() types.Type {
if e, _ := a.Type.(interface{ Elem() types.Type }); e != nil {
return e.Elem()
}
return nil
}
// Kind returns the underlying kind of type, e.g. "slice", "struct",
// etc.
func (a *postfixTmplArgs) Kind() string {
t := reflect.TypeOf(a.Type.Underlying())
return strings.ToLower(strings.TrimPrefix(t.String(), "*types."))
}
// KeyType returns the type of X's key. KeyType panics if X is not a
// map.
func (a *postfixTmplArgs) KeyType() types.Type {
return a.Type.Underlying().(*types.Map).Key()
}
// Tuple returns the tuple result vars if X is a call expression.
func (a *postfixTmplArgs) Tuple() []*types.Var {
tuple, _ := a.Type.(*types.Tuple)
if tuple == nil {
return nil
}
typs := make([]*types.Var, 0, tuple.Len())
for i := 0; i < tuple.Len(); i++ {
typs = append(typs, tuple.At(i))
}
return typs
}
// TypeName returns the textual representation of type t.
func (a *postfixTmplArgs) TypeName(t types.Type) (string, error) {
if t == nil || t == types.Typ[types.Invalid] {
return "", fmt.Errorf("invalid type: %v", t)
}
return types.TypeString(t, a.qf), nil
}
// VarName returns a suitable variable name for the type t. If t
// implements the error interface, "err" is used. If t is not a named
// type then nonNamedDefault is used. Otherwise a name is made by
// abbreviating the type name. If the resultant name is already in
// scope, an integer is appended to make a unique name.
func (a *postfixTmplArgs) VarName(t types.Type, nonNamedDefault string) string {
if t == nil {
t = types.Typ[types.Invalid]
}
var name string
// go/types predicates are undefined on types.Typ[types.Invalid].
if !types.Identical(t, types.Typ[types.Invalid]) && types.Implements(t, errorIntf) {
name = "err"
} else if _, isNamed := source.Deref(t).(*types.Named); !isNamed {
name = nonNamedDefault
}
if name == "" {
name = types.TypeString(t, func(p *types.Package) string {
return ""
})
name = abbreviateTypeName(name)
}
if dot := strings.LastIndex(name, "."); dot > -1 {
name = name[dot+1:]
}
uniqueName := name
for i := 2; ; i++ {
if s, _ := a.scope.LookupParent(uniqueName, token.NoPos); s == nil && !a.varNames[uniqueName] {
break
}
uniqueName = fmt.Sprintf("%s%d", name, i)
}
a.varNames[uniqueName] = true
return uniqueName
}
func (c *completer) addPostfixSnippetCandidates(ctx context.Context, sel *ast.SelectorExpr) {
if !c.opts.postfix {
return
}
initPostfixRules()
if sel == nil || sel.Sel == nil {
return
}
selType := c.pkg.GetTypesInfo().TypeOf(sel.X)
if selType == nil {
return
}
// Skip empty tuples since there is no value to operate on.
if tuple, ok := selType.Underlying().(*types.Tuple); ok && tuple == nil {
return
}
tokFile := c.pkg.FileSet().File(c.pos)
// Only replace sel with a statement if sel is already a statement.
var stmtOK bool
for i, n := range c.path {
if n == sel && i < len(c.path)-1 {
switch p := c.path[i+1].(type) {
case *ast.ExprStmt:
stmtOK = true
case *ast.AssignStmt:
// In cases like:
//
// foo.<>
// bar = 123
//
// detect that "foo." makes up the entire statement since the
// apparent selector spans lines.
stmtOK = tokFile.Line(c.pos) < tokFile.Line(p.TokPos)
}
break
}
}
scope := c.pkg.GetTypes().Scope().Innermost(c.pos)
if scope == nil {
return
}
// afterDot is the position after selector dot, e.g. "|" in
// "foo.|print".
afterDot := sel.Sel.Pos()
// We must detect dangling selectors such as:
//
// foo.<>
// bar
//
// and adjust afterDot so that we don't mistakenly delete the
// newline thinking "bar" is part of our selector.
if startLine := tokFile.Line(sel.Pos()); startLine != tokFile.Line(afterDot) {
if tokFile.Line(c.pos) != startLine {
return
}
afterDot = c.pos
}
for _, rule := range postfixTmpls {
// When completing foo.print<>, "print" is naturally overwritten,
// but we need to also remove "foo." so the snippet has a clean
// slate.
edits, err := c.editText(sel.Pos(), afterDot, "")
if err != nil {
event.Error(ctx, "error calculating postfix edits", err)
return
}
tmplArgs := postfixTmplArgs{
X: source.FormatNode(c.pkg.FileSet(), sel.X),
StmtOK: stmtOK,
Obj: exprObj(c.pkg.GetTypesInfo(), sel.X),
Type: selType,
qf: c.qf,
importIfNeeded: c.importIfNeeded,
scope: scope,
varNames: make(map[string]bool),
}
// Feed the template straight into the snippet builder. This
// allows templates to build snippets as they are executed.
err = rule.tmpl.Execute(&tmplArgs.snip, &tmplArgs)
if err != nil {
event.Error(ctx, "error executing postfix template", err)
continue
}
if strings.TrimSpace(tmplArgs.snip.String()) == "" {
continue
}
score := c.matcher.Score(rule.label)
if score <= 0 {
continue
}
c.items = append(c.items, CompletionItem{
Label: rule.label + "!",
Detail: rule.details,
Score: float64(score) * 0.01,
Kind: protocol.SnippetCompletion,
snippet: &tmplArgs.snip,
AdditionalTextEdits: append(edits, tmplArgs.edits...),
})
}
}
var postfixRulesOnce sync.Once
func initPostfixRules() {
postfixRulesOnce.Do(func() {
var idx int
for _, rule := range postfixTmpls {
var err error
rule.tmpl, err = template.New("postfix_snippet").Parse(rule.body)
if err != nil {
log.Panicf("error parsing postfix snippet template: %v", err)
}
postfixTmpls[idx] = rule
idx++
}
postfixTmpls = postfixTmpls[:idx]
})
}
// importIfNeeded returns the package identifier and any necessary
// edits to import package pkgPath.
func (c *completer) importIfNeeded(pkgPath string, scope *types.Scope) (string, []protocol.TextEdit, error) {
defaultName := imports.ImportPathToAssumedName(pkgPath)
// Check if file already imports pkgPath.
for _, s := range c.file.Imports {
// TODO(adonovan): what if pkgPath has a vendor/ suffix?
// This may be the cause of go.dev/issue/56291.
if source.UnquoteImportPath(s) == source.ImportPath(pkgPath) {
if s.Name == nil {
return defaultName, nil, nil
}
if s.Name.Name != "_" {
return s.Name.Name, nil, nil
}
}
}
// Give up if the package's name is already in use by another object.
if _, obj := scope.LookupParent(defaultName, token.NoPos); obj != nil {
return "", nil, fmt.Errorf("import name %q of %q already in use", defaultName, pkgPath)
}
edits, err := c.importEdits(&importInfo{
importPath: pkgPath,
})
if err != nil {
return "", nil, err
}
return defaultName, edits, nil
}
|