| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package completion |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"go/ast" |
| 11 |
"go/token" |
| 12 |
"go/types" |
| 13 |
"log" |
| 14 |
"reflect" |
| 15 |
"strings" |
| 16 |
"sync" |
| 17 |
"text/template" |
| 18 |
|
| 19 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 20 |
"golang.org/x/tools/gopls/internal/golang" |
| 21 |
"golang.org/x/tools/gopls/internal/golang/completion/snippet" |
| 22 |
"golang.org/x/tools/gopls/internal/protocol" |
| 23 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 24 |
"golang.org/x/tools/internal/event" |
| 25 |
"golang.org/x/tools/internal/imports" |
| 26 |
"golang.org/x/tools/internal/typesinternal" |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
type postfixTmpl struct { |
| 36 |
|
| 37 |
label string |
| 38 |
|
| 39 |
|
| 40 |
details string |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
body string |
| 45 |
|
| 46 |
tmpl *template.Template |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
type postfixTmplArgs struct { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
StmtOK bool |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
X string |
| 65 |
|
| 66 |
|
| 67 |
Obj types.Object |
| 68 |
|
| 69 |
|
| 70 |
Type types.Type |
| 71 |
|
| 72 |
|
| 73 |
FuncResults []*types.Var |
| 74 |
|
| 75 |
sel *ast.SelectorExpr |
| 76 |
scope *types.Scope |
| 77 |
snip snippet.Builder |
| 78 |
importIfNeeded func(pkgPath string, scope *types.Scope) (name string, edits []protocol.TextEdit, err error) |
| 79 |
edits []protocol.TextEdit |
| 80 |
qual types.Qualifier |
| 81 |
varNames map[string]bool |
| 82 |
placeholders bool |
| 83 |
currentTabStop int |
| 84 |
} |
| 85 |
|
| 86 |
var postfixTmpls = []postfixTmpl{{ |
| 87 |
label: "sort", |
| 88 |
details: "sort.Slice()", |
| 89 |
body: `{{if and (eq .Kind "slice") .StmtOK -}} |
| 90 |
{{.Import "sort"}}.Slice({{.X}}, func({{.VarName nil "i"}}, {{.VarName nil "j"}} int) bool { |
| 91 |
{{.Cursor}} |
| 92 |
}) |
| 93 |
{{- end}}`, |
| 94 |
}, { |
| 95 |
label: "last", |
| 96 |
details: "s[len(s)-1]", |
| 97 |
body: `{{if and (eq .Kind "slice") .Obj -}} |
| 98 |
{{.X}}[len({{.X}})-1] |
| 99 |
{{- end}}`, |
| 100 |
}, { |
| 101 |
label: "reverse", |
| 102 |
details: "reverse slice", |
| 103 |
body: `{{if and (eq .Kind "slice") .StmtOK -}} |
| 104 |
{{.Import "slices"}}.Reverse({{.X}}) |
| 105 |
{{- end}}`, |
| 106 |
}, { |
| 107 |
label: "range", |
| 108 |
details: "range over slice", |
| 109 |
body: `{{if and (eq .Kind "slice") .StmtOK -}} |
| 110 |
for {{.VarName nil "i" | .Placeholder }}, {{.VarName .ElemType "v" | .Placeholder}} := range {{.X}} { |
| 111 |
{{.Cursor}} |
| 112 |
} |
| 113 |
{{- end}}`, |
| 114 |
}, { |
| 115 |
label: "for", |
| 116 |
details: "range over slice by index", |
| 117 |
body: `{{if and (eq .Kind "slice") .StmtOK -}} |
| 118 |
for {{ .VarName nil "i" | .Placeholder }} := range {{.X}} { |
| 119 |
{{.Cursor}} |
| 120 |
} |
| 121 |
{{- end}}`, |
| 122 |
}, { |
| 123 |
label: "forr", |
| 124 |
details: "range over slice by index and value", |
| 125 |
body: `{{if and (eq .Kind "slice") .StmtOK -}} |
| 126 |
for {{.VarName nil "i" | .Placeholder }}, {{.VarName .ElemType "v" | .Placeholder }} := range {{.X}} { |
| 127 |
{{.Cursor}} |
| 128 |
} |
| 129 |
{{- end}}`, |
| 130 |
}, { |
| 131 |
label: "append", |
| 132 |
details: "append and re-assign slice", |
| 133 |
body: `{{if and (eq .Kind "slice") .StmtOK .Obj -}} |
| 134 |
{{.X}} = append({{.X}}, {{.Cursor}}) |
| 135 |
{{- end}}`, |
| 136 |
}, { |
| 137 |
label: "append", |
| 138 |
details: "append to slice", |
| 139 |
body: `{{if and (eq .Kind "slice") (not .StmtOK) -}} |
| 140 |
append({{.X}}, {{.Cursor}}) |
| 141 |
{{- end}}`, |
| 142 |
}, { |
| 143 |
label: "copy", |
| 144 |
details: "duplicate slice", |
| 145 |
body: `{{if and (eq .Kind "slice") .StmtOK .Obj -}} |
| 146 |
{{$v := (.VarName nil (printf "%sCopy" .X))}}{{$v}} := make([]{{.TypeName .ElemType}}, len({{.X}})) |
| 147 |
copy({{$v}}, {{.X}}) |
| 148 |
{{end}}`, |
| 149 |
}, { |
| 150 |
label: "range", |
| 151 |
details: "range over map", |
| 152 |
body: `{{if and (eq .Kind "map") .StmtOK -}} |
| 153 |
for {{.VarName .KeyType "k" | .Placeholder}}, {{.VarName .ElemType "v" | .Placeholder}} := range {{.X}} { |
| 154 |
{{.Cursor}} |
| 155 |
} |
| 156 |
{{- end}}`, |
| 157 |
}, { |
| 158 |
label: "for", |
| 159 |
details: "range over map by key", |
| 160 |
body: `{{if and (eq .Kind "map") .StmtOK -}} |
| 161 |
for {{.VarName .KeyType "k" | .Placeholder}} := range {{.X}} { |
| 162 |
{{.Cursor}} |
| 163 |
} |
| 164 |
{{- end}}`, |
| 165 |
}, { |
| 166 |
label: "forr", |
| 167 |
details: "range over map by key and value", |
| 168 |
body: `{{if and (eq .Kind "map") .StmtOK -}} |
| 169 |
for {{.VarName .KeyType "k" | .Placeholder}}, {{.VarName .ElemType "v" | .Placeholder}} := range {{.X}} { |
| 170 |
{{.Cursor}} |
| 171 |
} |
| 172 |
{{- end}}`, |
| 173 |
}, { |
| 174 |
label: "clear", |
| 175 |
details: "clear map contents", |
| 176 |
body: `{{if and (eq .Kind "map") .StmtOK -}} |
| 177 |
{{$k := (.VarName .KeyType "k")}}for {{$k}} := range {{.X}} { |
| 178 |
delete({{.X}}, {{$k}}) |
| 179 |
} |
| 180 |
{{end}}`, |
| 181 |
}, { |
| 182 |
label: "keys", |
| 183 |
details: "create slice of keys", |
| 184 |
body: `{{if and (eq .Kind "map") .StmtOK -}} |
| 185 |
{{$keysVar := (.VarName nil "keys")}}{{$keysVar}} := make([]{{.TypeName .KeyType}}, 0, len({{.X}})) |
| 186 |
{{$k := (.VarName .KeyType "k")}}for {{$k}} := range {{.X}} { |
| 187 |
{{$keysVar}} = append({{$keysVar}}, {{$k}}) |
| 188 |
} |
| 189 |
{{end}}`, |
| 190 |
}, { |
| 191 |
label: "range", |
| 192 |
details: "range over channel", |
| 193 |
body: `{{if and (eq .Kind "chan") .StmtOK -}} |
| 194 |
for {{.VarName .ElemType "e" | .Placeholder}} := range {{.X}} { |
| 195 |
{{.Cursor}} |
| 196 |
} |
| 197 |
{{- end}}`, |
| 198 |
}, { |
| 199 |
label: "for", |
| 200 |
details: "range over channel", |
| 201 |
body: `{{if and (eq .Kind "chan") .StmtOK -}} |
| 202 |
for {{.VarName .ElemType "e" | .Placeholder}} := range {{.X}} { |
| 203 |
{{.Cursor}} |
| 204 |
} |
| 205 |
{{- end}}`, |
| 206 |
}, { |
| 207 |
label: "var", |
| 208 |
details: "assign to variables", |
| 209 |
body: `{{if and (eq .Kind "tuple") .StmtOK -}} |
| 210 |
{{$a := .}}{{range $i, $v := .Tuple}}{{if $i}}, {{end}}{{$a.VarName $v.Type $v.Name | $a.Placeholder }}{{end}} := {{.X}} |
| 211 |
{{- end}}`, |
| 212 |
}, { |
| 213 |
label: "var", |
| 214 |
details: "assign to variable", |
| 215 |
body: `{{if and (ne .Kind "tuple") .StmtOK -}} |
| 216 |
{{.VarName .Type "" | .Placeholder }} := {{.X}} |
| 217 |
{{- end}}`, |
| 218 |
}, { |
| 219 |
label: "print", |
| 220 |
details: "print to stdout", |
| 221 |
body: `{{if and (ne .Kind "tuple") .StmtOK -}} |
| 222 |
{{.Import "fmt"}}.Printf("{{.EscapeQuotes .X}}: %v\n", {{.X}}) |
| 223 |
{{- end}}`, |
| 224 |
}, { |
| 225 |
label: "print", |
| 226 |
details: "print to stdout", |
| 227 |
body: `{{if and (eq .Kind "tuple") .StmtOK -}} |
| 228 |
{{.Import "fmt"}}.Println({{.X}}) |
| 229 |
{{- end}}`, |
| 230 |
}, { |
| 231 |
label: "split", |
| 232 |
details: "split string", |
| 233 |
body: `{{if (eq (.TypeName .Type) "string") -}} |
| 234 |
{{.Import "strings"}}.Split({{.X}}, "{{.Cursor}}") |
| 235 |
{{- end}}`, |
| 236 |
}, { |
| 237 |
label: "join", |
| 238 |
details: "join string slice", |
| 239 |
body: `{{if and (eq .Kind "slice") (eq (.TypeName .ElemType) "string") -}} |
| 240 |
{{.Import "strings"}}.Join({{.X}}, "{{.Cursor}}") |
| 241 |
{{- end}}`, |
| 242 |
}, { |
| 243 |
label: "ifnotnil", |
| 244 |
details: "if expr != nil", |
| 245 |
body: `{{if and (or (eq .Kind "pointer") (eq .Kind "chan") (eq .Kind "signature") (eq .Kind "interface") (eq .Kind "map") (eq .Kind "slice")) .StmtOK -}} |
| 246 |
if {{.X}} != nil { |
| 247 |
{{.Cursor}} |
| 248 |
} |
| 249 |
{{- end}}`, |
| 250 |
}, { |
| 251 |
label: "len", |
| 252 |
details: "len(s)", |
| 253 |
body: `{{if (eq .Kind "slice" "map" "array" "chan") -}} |
| 254 |
len({{.X}}) |
| 255 |
{{- end}}`, |
| 256 |
}, { |
| 257 |
label: "iferr", |
| 258 |
details: "check error and return", |
| 259 |
body: `{{if and .StmtOK (eq (.TypeName .Type) "error") -}} |
| 260 |
{{- $errName := (or (and .IsIdent .X) "err") -}} |
| 261 |
if {{if not .IsIdent}}err := {{.X}}; {{end}}{{$errName}} != nil { |
| 262 |
return {{$a := .}}{{range $i, $v := .FuncResults}} |
| 263 |
{{- if $i}}, {{end -}} |
| 264 |
{{- if eq ($a.TypeName $v.Type) "error" -}} |
| 265 |
{{$a.Placeholder $errName}} |
| 266 |
{{- else -}} |
| 267 |
{{$a.Zero $v.Type}} |
| 268 |
{{- end -}} |
| 269 |
{{end}} |
| 270 |
} |
| 271 |
{{end}}`, |
| 272 |
}, { |
| 273 |
label: "iferr", |
| 274 |
details: "check error and return", |
| 275 |
body: `{{if and .StmtOK (eq .Kind "tuple") (len .Tuple) (eq (.TypeName .TupleLast.Type) "error") -}} |
| 276 |
{{- $a := . -}} |
| 277 |
if {{range $i, $v := .Tuple}}{{if $i}}, {{end}}{{if and (eq ($a.TypeName $v.Type) "error") (eq (inc $i) (len $a.Tuple))}}err{{else}}_{{end}}{{end}} := {{.X -}} |
| 278 |
; err != nil { |
| 279 |
return {{range $i, $v := .FuncResults}} |
| 280 |
{{- if $i}}, {{end -}} |
| 281 |
{{- if eq ($a.TypeName $v.Type) "error" -}} |
| 282 |
{{$a.Placeholder "err"}} |
| 283 |
{{- else -}} |
| 284 |
{{$a.Zero $v.Type}} |
| 285 |
{{- end -}} |
| 286 |
{{end}} |
| 287 |
} |
| 288 |
{{end}}`, |
| 289 |
}, { |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
label: "variferr", |
| 295 |
details: "assign variables and check error", |
| 296 |
body: `{{if and .StmtOK (eq .Kind "tuple") (len .Tuple) (eq (.TypeName .TupleLast.Type) "error") -}} |
| 297 |
{{- $a := . -}} |
| 298 |
{{- $errName := "err" -}} |
| 299 |
{{- range $i, $v := .Tuple -}} |
| 300 |
{{- if $i}}, {{end -}} |
| 301 |
{{- if and (eq ($a.TypeName $v.Type) "error") (eq (inc $i) (len $a.Tuple)) -}} |
| 302 |
{{$errName | $a.SpecifiedPlaceholder (len $a.Tuple)}} |
| 303 |
{{- else -}} |
| 304 |
{{$a.VarName $v.Type $v.Name | $a.Placeholder}} |
| 305 |
{{- end -}} |
| 306 |
{{- end}} := {{.X}} |
| 307 |
if {{$errName | $a.SpecifiedPlaceholder (len $a.Tuple)}} != nil { |
| 308 |
return {{range $i, $v := .FuncResults}} |
| 309 |
{{- if $i}}, {{end -}} |
| 310 |
{{- if eq ($a.TypeName $v.Type) "error" -}} |
| 311 |
{{$errName | $a.SpecifiedPlaceholder (len $a.Tuple) | |
| 312 |
$a.SpecifiedPlaceholder (inc (len $a.Tuple))}} |
| 313 |
{{- else -}} |
| 314 |
{{$a.Zero $v.Type}} |
| 315 |
{{- end -}} |
| 316 |
{{end}} |
| 317 |
} |
| 318 |
{{end}}`, |
| 319 |
}, { |
| 320 |
label: "variferr", |
| 321 |
details: "assign variables and check error", |
| 322 |
body: `{{if and .StmtOK (eq (.TypeName .Type) "error") -}} |
| 323 |
{{- $a := . -}} |
| 324 |
{{- $errName := .VarName nil "err" -}} |
| 325 |
{{$errName | $a.SpecifiedPlaceholder 1}} := {{.X}} |
| 326 |
if {{$errName | $a.SpecifiedPlaceholder 1}} != nil { |
| 327 |
return {{range $i, $v := .FuncResults}} |
| 328 |
{{- if $i}}, {{end -}} |
| 329 |
{{- if eq ($a.TypeName $v.Type) "error" -}} |
| 330 |
{{$errName | $a.SpecifiedPlaceholder 1 | $a.SpecifiedPlaceholder 2}} |
| 331 |
{{- else -}} |
| 332 |
{{$a.Zero $v.Type}} |
| 333 |
{{- end -}} |
| 334 |
{{end}} |
| 335 |
} |
| 336 |
{{end}}`, |
| 337 |
}, |
| 338 |
{ |
| 339 |
label: "tostring", |
| 340 |
details: "[]byte to string", |
| 341 |
body: `{{if (eq (.TypeName .Type) "[]byte") -}} |
| 342 |
string({{.X}}) |
| 343 |
{{- end}}`, |
| 344 |
}, |
| 345 |
{ |
| 346 |
label: "tostring", |
| 347 |
details: "int to string", |
| 348 |
body: `{{if (eq (.TypeName .Type) "int") -}} |
| 349 |
{{.Import "strconv"}}.Itoa({{.X}}) |
| 350 |
{{- end}}`, |
| 351 |
}, |
| 352 |
{ |
| 353 |
label: "tobytes", |
| 354 |
details: "string to []byte", |
| 355 |
body: `{{if (eq (.TypeName .Type) "string") -}} |
| 356 |
[]byte({{.X}}) |
| 357 |
{{- end}}`, |
| 358 |
}, |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
func (a *postfixTmplArgs) Cursor() string { |
| 364 |
return "$0" |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
func (a *postfixTmplArgs) Placeholder(placeholder string) string { |
| 370 |
if !a.placeholders { |
| 371 |
placeholder = "" |
| 372 |
} |
| 373 |
return fmt.Sprintf("${%d:%s}", a.nextTabStop(), placeholder) |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
func (a *postfixTmplArgs) nextTabStop() int { |
| 378 |
|
| 379 |
a.currentTabStop++ |
| 380 |
return a.currentTabStop |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
func (a *postfixTmplArgs) SpecifiedPlaceholder(tabStop int, placeholder string) string { |
| 387 |
if !a.placeholders { |
| 388 |
placeholder = "" |
| 389 |
} |
| 390 |
return fmt.Sprintf("${%d:%s}", tabStop, placeholder) |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
func (a *postfixTmplArgs) Import(path string) (string, error) { |
| 396 |
name, edits, err := a.importIfNeeded(path, a.scope) |
| 397 |
if err != nil { |
| 398 |
return "", fmt.Errorf("couldn't import %q: %w", path, err) |
| 399 |
} |
| 400 |
a.edits = append(a.edits, edits...) |
| 401 |
|
| 402 |
return name, nil |
| 403 |
} |
| 404 |
|
| 405 |
func (a *postfixTmplArgs) EscapeQuotes(v string) string { |
| 406 |
return strings.ReplaceAll(v, `"`, `\\"`) |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
func (a *postfixTmplArgs) ElemType() types.Type { |
| 411 |
type hasElem interface{ Elem() types.Type } |
| 412 |
if e, ok := a.Type.Underlying().(hasElem); ok { |
| 413 |
return e.Elem() |
| 414 |
} |
| 415 |
return nil |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
func (a *postfixTmplArgs) Kind() string { |
| 421 |
t := reflect.TypeOf(a.Type.Underlying()) |
| 422 |
return strings.ToLower(strings.TrimPrefix(t.String(), "*types.")) |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
func (a *postfixTmplArgs) KeyType() types.Type { |
| 428 |
return a.Type.Underlying().(*types.Map).Key() |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
func (a *postfixTmplArgs) Tuple() []*types.Var { |
| 433 |
tuple, _ := a.Type.(*types.Tuple) |
| 434 |
typs := make([]*types.Var, tuple.Len()) |
| 435 |
for i := range typs { |
| 436 |
typs[i] = tuple.At(i) |
| 437 |
} |
| 438 |
return typs |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
func (a *postfixTmplArgs) TupleLast() *types.Var { |
| 443 |
tuple, _ := a.Type.(*types.Tuple) |
| 444 |
if tuple.Len() == 0 { |
| 445 |
return nil |
| 446 |
} |
| 447 |
return tuple.At(tuple.Len() - 1) |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
func (a *postfixTmplArgs) TypeName(t types.Type) (string, error) { |
| 452 |
if t == nil || t == types.Typ[types.Invalid] { |
| 453 |
return "", fmt.Errorf("invalid type: %v", t) |
| 454 |
} |
| 455 |
return types.TypeString(t, a.qual), nil |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
func (a *postfixTmplArgs) Zero(t types.Type) string { |
| 460 |
zero, _ := typesinternal.ZeroString(t, a.qual) |
| 461 |
return zero |
| 462 |
} |
| 463 |
|
| 464 |
func (a *postfixTmplArgs) IsIdent() bool { |
| 465 |
_, ok := a.sel.X.(*ast.Ident) |
| 466 |
return ok |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
func (a *postfixTmplArgs) VarName(t types.Type, nonNamedDefault string) string { |
| 475 |
if t == nil { |
| 476 |
t = types.Typ[types.Invalid] |
| 477 |
} |
| 478 |
|
| 479 |
var name string |
| 480 |
|
| 481 |
if !types.Identical(t, types.Typ[types.Invalid]) && types.Implements(t, errorIntf) { |
| 482 |
name = "err" |
| 483 |
} else if !is[*types.Named](types.Unalias(typesinternal.Unpointer(t))) { |
| 484 |
name = nonNamedDefault |
| 485 |
} |
| 486 |
|
| 487 |
if name == "" { |
| 488 |
name = types.TypeString(t, func(p *types.Package) string { |
| 489 |
return "" |
| 490 |
}) |
| 491 |
name = abbreviateTypeName(name) |
| 492 |
} |
| 493 |
|
| 494 |
if dot := strings.LastIndex(name, "."); dot > -1 { |
| 495 |
name = name[dot+1:] |
| 496 |
} |
| 497 |
|
| 498 |
uniqueName := name |
| 499 |
for i := 2; ; i++ { |
| 500 |
if s, _ := a.scope.LookupParent(uniqueName, token.NoPos); s == nil && !a.varNames[uniqueName] { |
| 501 |
break |
| 502 |
} |
| 503 |
uniqueName = fmt.Sprintf("%s%d", name, i) |
| 504 |
} |
| 505 |
|
| 506 |
a.varNames[uniqueName] = true |
| 507 |
|
| 508 |
return uniqueName |
| 509 |
} |
| 510 |
|
| 511 |
func (c *completer) addPostfixSnippetCandidates(ctx context.Context, sel *ast.SelectorExpr) { |
| 512 |
if !c.opts.postfix { |
| 513 |
return |
| 514 |
} |
| 515 |
|
| 516 |
initPostfixRules() |
| 517 |
|
| 518 |
if sel == nil || sel.Sel == nil { |
| 519 |
return |
| 520 |
} |
| 521 |
|
| 522 |
selType := c.pkg.TypesInfo().TypeOf(sel.X) |
| 523 |
if selType == nil { |
| 524 |
return |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
if tuple, ok := selType.(*types.Tuple); ok && tuple == nil { |
| 529 |
return |
| 530 |
} |
| 531 |
|
| 532 |
tokFile := c.pkg.FileSet().File(c.pos) |
| 533 |
|
| 534 |
|
| 535 |
var stmtOK bool |
| 536 |
for i, n := range c.path { |
| 537 |
if n == sel && i < len(c.path)-1 { |
| 538 |
switch p := c.path[i+1].(type) { |
| 539 |
case *ast.ExprStmt: |
| 540 |
stmtOK = true |
| 541 |
case *ast.AssignStmt: |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
stmtOK = safetoken.Line(tokFile, c.pos) < safetoken.Line(tokFile, p.TokPos) |
| 550 |
} |
| 551 |
break |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
var funcResults []*types.Var |
| 556 |
if c.enclosingFunc != nil { |
| 557 |
results := c.enclosingFunc.sig.Results() |
| 558 |
if results != nil { |
| 559 |
funcResults = make([]*types.Var, results.Len()) |
| 560 |
for i := range results.Len() { |
| 561 |
funcResults[i] = results.At(i) |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
scope := c.pkg.Types().Scope().Innermost(c.pos) |
| 567 |
if scope == nil { |
| 568 |
return |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
afterDot := sel.Sel.Pos() |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
if startLine := safetoken.Line(tokFile, sel.Pos()); startLine != safetoken.Line(tokFile, afterDot) { |
| 583 |
if safetoken.Line(tokFile, c.pos) != startLine { |
| 584 |
return |
| 585 |
} |
| 586 |
afterDot = c.pos |
| 587 |
} |
| 588 |
|
| 589 |
for _, rule := range postfixTmpls { |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
edits, err := c.editText(sel.Pos(), afterDot, "") |
| 594 |
if err != nil { |
| 595 |
event.Error(ctx, "error calculating postfix edits", err) |
| 596 |
return |
| 597 |
} |
| 598 |
|
| 599 |
tmplArgs := postfixTmplArgs{ |
| 600 |
X: golang.FormatNode(c.pkg.FileSet(), sel.X), |
| 601 |
StmtOK: stmtOK, |
| 602 |
Obj: exprObj(c.pkg.TypesInfo(), sel.X), |
| 603 |
Type: selType, |
| 604 |
FuncResults: funcResults, |
| 605 |
sel: sel, |
| 606 |
qual: c.qual, |
| 607 |
importIfNeeded: c.importIfNeeded, |
| 608 |
scope: scope, |
| 609 |
varNames: make(map[string]bool), |
| 610 |
placeholders: c.opts.placeholders, |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
err = rule.tmpl.Execute(&tmplArgs.snip, &tmplArgs) |
| 616 |
if err != nil { |
| 617 |
event.Error(ctx, "error executing postfix template", err) |
| 618 |
continue |
| 619 |
} |
| 620 |
|
| 621 |
if strings.TrimSpace(tmplArgs.snip.String()) == "" { |
| 622 |
continue |
| 623 |
} |
| 624 |
|
| 625 |
score := c.matcher.Score(rule.label) |
| 626 |
if score <= 0 { |
| 627 |
continue |
| 628 |
} |
| 629 |
|
| 630 |
c.items = append(c.items, CompletionItem{ |
| 631 |
Label: rule.label + "!", |
| 632 |
Detail: rule.details, |
| 633 |
Score: float64(score) * 0.01, |
| 634 |
Kind: protocol.SnippetCompletion, |
| 635 |
snippet: &tmplArgs.snip, |
| 636 |
AdditionalTextEdits: append(edits, tmplArgs.edits...), |
| 637 |
}) |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
var postfixRulesOnce sync.Once |
| 642 |
|
| 643 |
func initPostfixRules() { |
| 644 |
postfixRulesOnce.Do(func() { |
| 645 |
var idx int |
| 646 |
for _, rule := range postfixTmpls { |
| 647 |
var err error |
| 648 |
rule.tmpl, err = template.New("postfix_snippet").Funcs(template.FuncMap{ |
| 649 |
"inc": inc, |
| 650 |
}).Parse(rule.body) |
| 651 |
if err != nil { |
| 652 |
log.Panicf("error parsing postfix snippet template: %v", err) |
| 653 |
} |
| 654 |
postfixTmpls[idx] = rule |
| 655 |
idx++ |
| 656 |
} |
| 657 |
postfixTmpls = postfixTmpls[:idx] |
| 658 |
}) |
| 659 |
} |
| 660 |
|
| 661 |
func inc(i int) int { |
| 662 |
return i + 1 |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
func (c *completer) importIfNeeded(pkgPath string, scope *types.Scope) (string, []protocol.TextEdit, error) { |
| 668 |
defaultName := imports.ImportPathToAssumedName(pkgPath) |
| 669 |
|
| 670 |
|
| 671 |
for _, s := range c.pgf.File.Imports { |
| 672 |
|
| 673 |
|
| 674 |
if string(metadata.UnquoteImportPath(s)) == pkgPath { |
| 675 |
if s.Name == nil { |
| 676 |
return defaultName, nil, nil |
| 677 |
} |
| 678 |
if s.Name.Name != "_" { |
| 679 |
return s.Name.Name, nil, nil |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
if _, obj := scope.LookupParent(defaultName, token.NoPos); obj != nil { |
| 686 |
return "", nil, fmt.Errorf("import name %q of %q already in use", defaultName, pkgPath) |
| 687 |
} |
| 688 |
|
| 689 |
edits, err := c.importEdits(&importInfo{ |
| 690 |
importPath: pkgPath, |
| 691 |
}) |
| 692 |
if err != nil { |
| 693 |
return "", nil, err |
| 694 |
} |
| 695 |
|
| 696 |
return defaultName, edits, nil |
| 697 |
} |