| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package completion |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"go/types" |
| 11 |
"strings" |
| 12 |
"unicode" |
| 13 |
|
| 14 |
"golang.org/x/tools/gopls/internal/golang" |
| 15 |
"golang.org/x/tools/gopls/internal/golang/completion/snippet" |
| 16 |
"golang.org/x/tools/gopls/internal/protocol" |
| 17 |
"golang.org/x/tools/internal/event" |
| 18 |
"golang.org/x/tools/internal/typesinternal" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
func (c *completer) literal(ctx context.Context, literalType types.Type, imp *importInfo) { |
| 24 |
if !c.opts.snippets { |
| 25 |
return |
| 26 |
} |
| 27 |
|
| 28 |
expType := c.inference.objType |
| 29 |
|
| 30 |
if c.inference.matchesVariadic(literalType) { |
| 31 |
|
| 32 |
|
| 33 |
return |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
if expType != nil && isEmptyInterface(expType) { |
| 40 |
return |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
if is[*types.Named](types.Unalias(literalType)) && |
| 58 |
expType != nil && |
| 59 |
!is[*types.Named](types.Unalias(typesinternal.Unpointer(expType))) { |
| 60 |
|
| 61 |
return |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
cand := candidate{ |
| 66 |
obj: c.fakeObj(literalType), |
| 67 |
} |
| 68 |
|
| 69 |
switch literalType.Underlying().(type) { |
| 70 |
|
| 71 |
|
| 72 |
case *types.Struct, *types.Array, *types.Slice, *types.Map: |
| 73 |
cand.addressable = true |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
if !c.matchingCandidate(&cand) || (cand.convertTo != nil && !c.inference.needsExactType) { |
| 78 |
return |
| 79 |
} |
| 80 |
|
| 81 |
var ( |
| 82 |
qual = c.qual |
| 83 |
sel = enclosingSelector(c.path, c.pos) |
| 84 |
conversion conversionEdits |
| 85 |
) |
| 86 |
|
| 87 |
if cand.convertTo != nil { |
| 88 |
conversion = c.formatConversion(cand.convertTo) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
if sel != nil { |
| 94 |
qual = func(_ *types.Package) string { return "" } |
| 95 |
} |
| 96 |
|
| 97 |
snip, typeName := c.typeNameSnippet(literalType, qual) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
matchName := typeName |
| 103 |
switch t := literalType.(type) { |
| 104 |
case *types.Slice: |
| 105 |
matchName = types.TypeString(t.Elem(), qual) |
| 106 |
case *types.Array: |
| 107 |
matchName = types.TypeString(t.Elem(), qual) |
| 108 |
} |
| 109 |
|
| 110 |
addlEdits, err := c.importEdits(imp) |
| 111 |
if err != nil { |
| 112 |
event.Error(ctx, "error adding import for literal candidate", err) |
| 113 |
return |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
if score := c.matcher.Score(matchName); score > 0 { |
| 118 |
if cand.hasMod(reference) { |
| 119 |
if sel != nil { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
edits, err := c.editText(sel.Pos(), sel.Pos(), "&") |
| 124 |
if err != nil { |
| 125 |
event.Error(ctx, "error making edit for literal pointer completion", err) |
| 126 |
return |
| 127 |
} |
| 128 |
addlEdits = append(addlEdits, edits...) |
| 129 |
} else { |
| 130 |
|
| 131 |
typeName = "&" + typeName |
| 132 |
snip.PrependText("&") |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
switch t := literalType.Underlying().(type) { |
| 137 |
case *types.Struct, *types.Array, *types.Slice, *types.Map: |
| 138 |
item := c.compositeLiteral(t, snip.Clone(), typeName, float64(score), addlEdits) |
| 139 |
item.addConversion(c, conversion) |
| 140 |
c.items = append(c.items, item) |
| 141 |
case *types.Signature: |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
if expType != nil && types.IsInterface(expType) { |
| 146 |
if item, ok := c.basicLiteral(t, snip.Clone(), typeName, float64(score), addlEdits); ok { |
| 147 |
item.addConversion(c, conversion) |
| 148 |
c.items = append(c.items, item) |
| 149 |
} |
| 150 |
} |
| 151 |
case *types.Basic: |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if expType != nil && (types.IsInterface(expType) || types.Identical(expType, literalType)) { |
| 157 |
if item, ok := c.basicLiteral(t, snip.Clone(), typeName, float64(score), addlEdits); ok { |
| 158 |
item.addConversion(c, conversion) |
| 159 |
c.items = append(c.items, item) |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if score := c.matcher.Score("make." + matchName); !cand.hasMod(reference) && score > 0 { |
| 169 |
switch literalType.Underlying().(type) { |
| 170 |
case *types.Slice: |
| 171 |
|
| 172 |
item := c.makeCall(snip.Clone(), typeName, "0", float64(score), addlEdits) |
| 173 |
item.addConversion(c, conversion) |
| 174 |
c.items = append(c.items, item) |
| 175 |
case *types.Map, *types.Chan: |
| 176 |
|
| 177 |
|
| 178 |
item := c.makeCall(snip.Clone(), typeName, "", float64(score), addlEdits) |
| 179 |
item.addConversion(c, conversion) |
| 180 |
c.items = append(c.items, item) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
if score := c.matcher.Score("func"); !cand.hasMod(reference) && score > 0 && (expType == nil || !types.IsInterface(expType)) { |
| 186 |
switch t := literalType.Underlying().(type) { |
| 187 |
case *types.Signature: |
| 188 |
if item, ok := c.functionLiteral(ctx, t, float64(score)); ok { |
| 189 |
item.addConversion(c, conversion) |
| 190 |
c.items = append(c.items, item) |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
const literalCandidateScore = highScore / 2 |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
func (c *completer) functionLiteral(ctx context.Context, sig *types.Signature, matchScore float64) (CompletionItem, bool) { |
| 205 |
snip := &snippet.Builder{} |
| 206 |
snip.WriteText("func(") |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
var ( |
| 213 |
paramNames = make([]string, sig.Params().Len()) |
| 214 |
paramNameCount = make(map[string]int) |
| 215 |
hasTypeParams bool |
| 216 |
) |
| 217 |
for i := range sig.Params().Len() { |
| 218 |
var ( |
| 219 |
p = sig.Params().At(i) |
| 220 |
name = p.Name() |
| 221 |
) |
| 222 |
|
| 223 |
if tp, _ := types.Unalias(p.Type()).(*types.TypeParam); tp != nil && !c.typeParamInScope(tp) { |
| 224 |
hasTypeParams = true |
| 225 |
} |
| 226 |
|
| 227 |
if name == "" { |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
typeName, err := golang.FormatVarType(ctx, c.snapshot, c.pkg, p, |
| 232 |
func(p *types.Package) string { return "" }, |
| 233 |
func(golang.PackageName, golang.ImportPath, golang.PackagePath) string { return "" }) |
| 234 |
if err != nil { |
| 235 |
|
| 236 |
|
| 237 |
if ctx.Err() == nil { |
| 238 |
event.Error(ctx, "formatting var type", err) |
| 239 |
} |
| 240 |
return CompletionItem{}, false |
| 241 |
} |
| 242 |
name = abbreviateTypeName(typeName) |
| 243 |
} |
| 244 |
paramNames[i] = name |
| 245 |
if name != "_" { |
| 246 |
paramNameCount[name]++ |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
for n, c := range paramNameCount { |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
if c >= 2 { |
| 255 |
paramNameCount[n] = 1 |
| 256 |
} else { |
| 257 |
delete(paramNameCount, n) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
for i := range sig.Params().Len() { |
| 262 |
if hasTypeParams && !c.opts.placeholders { |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
snip.WritePlaceholder(nil) |
| 267 |
break |
| 268 |
} |
| 269 |
|
| 270 |
if i > 0 { |
| 271 |
snip.WriteText(", ") |
| 272 |
} |
| 273 |
|
| 274 |
var ( |
| 275 |
p = sig.Params().At(i) |
| 276 |
name = paramNames[i] |
| 277 |
) |
| 278 |
|
| 279 |
|
| 280 |
if idx, found := paramNameCount[name]; found { |
| 281 |
paramNameCount[name]++ |
| 282 |
name = fmt.Sprintf("%s%d", name, idx) |
| 283 |
} |
| 284 |
|
| 285 |
if name != p.Name() && c.opts.placeholders { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
snip.WritePlaceholder(func(b *snippet.Builder) { |
| 290 |
b.WriteText(name) |
| 291 |
}) |
| 292 |
} else { |
| 293 |
snip.WriteText(name) |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
if i == sig.Params().Len()-1 || !types.Identical(p.Type(), sig.Params().At(i+1).Type()) { |
| 300 |
snip.WriteText(" ") |
| 301 |
typeStr, err := golang.FormatVarType(ctx, c.snapshot, c.pkg, p, c.qual, c.mq) |
| 302 |
if err != nil { |
| 303 |
|
| 304 |
|
| 305 |
if ctx.Err() == nil { |
| 306 |
event.Error(ctx, "formatting var type", err) |
| 307 |
} |
| 308 |
return CompletionItem{}, false |
| 309 |
} |
| 310 |
if sig.Variadic() && i == sig.Params().Len()-1 { |
| 311 |
typeStr = strings.Replace(typeStr, "[]", "...", 1) |
| 312 |
} |
| 313 |
|
| 314 |
if tp, ok := types.Unalias(p.Type()).(*types.TypeParam); ok && !c.typeParamInScope(tp) { |
| 315 |
snip.WritePlaceholder(func(snip *snippet.Builder) { |
| 316 |
snip.WriteText(typeStr) |
| 317 |
}) |
| 318 |
} else { |
| 319 |
snip.WriteText(typeStr) |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
snip.WriteText(")") |
| 324 |
|
| 325 |
results := sig.Results() |
| 326 |
if results.Len() > 0 { |
| 327 |
snip.WriteText(" ") |
| 328 |
} |
| 329 |
|
| 330 |
resultsNeedParens := results.Len() > 1 || |
| 331 |
results.Len() == 1 && results.At(0).Name() != "" |
| 332 |
|
| 333 |
var resultHasTypeParams bool |
| 334 |
for v := range results.Variables() { |
| 335 |
if tp, ok := types.Unalias(v.Type()).(*types.TypeParam); ok && !c.typeParamInScope(tp) { |
| 336 |
resultHasTypeParams = true |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if resultsNeedParens { |
| 341 |
snip.WriteText("(") |
| 342 |
} |
| 343 |
for i := range results.Len() { |
| 344 |
if resultHasTypeParams && !c.opts.placeholders { |
| 345 |
|
| 346 |
|
| 347 |
snip.WritePlaceholder(nil) |
| 348 |
break |
| 349 |
} |
| 350 |
|
| 351 |
if i > 0 { |
| 352 |
snip.WriteText(", ") |
| 353 |
} |
| 354 |
r := results.At(i) |
| 355 |
if name := r.Name(); name != "" { |
| 356 |
snip.WriteText(name + " ") |
| 357 |
} |
| 358 |
|
| 359 |
text, err := golang.FormatVarType(ctx, c.snapshot, c.pkg, r, c.qual, c.mq) |
| 360 |
if err != nil { |
| 361 |
|
| 362 |
|
| 363 |
if ctx.Err() == nil { |
| 364 |
event.Error(ctx, "formatting var type", err) |
| 365 |
} |
| 366 |
return CompletionItem{}, false |
| 367 |
} |
| 368 |
if tp, ok := types.Unalias(r.Type()).(*types.TypeParam); ok && !c.typeParamInScope(tp) { |
| 369 |
snip.WritePlaceholder(func(snip *snippet.Builder) { |
| 370 |
snip.WriteText(text) |
| 371 |
}) |
| 372 |
} else { |
| 373 |
snip.WriteText(text) |
| 374 |
} |
| 375 |
} |
| 376 |
if resultsNeedParens { |
| 377 |
snip.WriteText(")") |
| 378 |
} |
| 379 |
|
| 380 |
snip.WriteText(" {") |
| 381 |
snip.WriteFinalTabstop() |
| 382 |
snip.WriteText("}") |
| 383 |
|
| 384 |
return CompletionItem{ |
| 385 |
Label: "func(...) {}", |
| 386 |
Score: matchScore * literalCandidateScore, |
| 387 |
Kind: protocol.VariableCompletion, |
| 388 |
snippet: snip, |
| 389 |
}, true |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
var conventionalAcronyms = map[string]string{ |
| 397 |
"context": "ctx", |
| 398 |
"error": "err", |
| 399 |
"tx": "tx", |
| 400 |
"responsewriter": "w", |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
func abbreviateTypeName(s string) string { |
| 408 |
|
| 409 |
|
| 410 |
var inBracket bool |
| 411 |
s = strings.TrimFunc(s, func(r rune) bool { |
| 412 |
if inBracket { |
| 413 |
inBracket = r != ']' |
| 414 |
return true |
| 415 |
} |
| 416 |
|
| 417 |
if r == '[' { |
| 418 |
inBracket = true |
| 419 |
} |
| 420 |
|
| 421 |
return !unicode.IsLetter(r) |
| 422 |
}) |
| 423 |
|
| 424 |
if acr, ok := conventionalAcronyms[strings.ToLower(s)]; ok { |
| 425 |
return acr |
| 426 |
} |
| 427 |
|
| 428 |
return golang.AbbreviateVarName(s) |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
func (c *completer) compositeLiteral(T types.Type, snip *snippet.Builder, typeName string, matchScore float64, edits []protocol.TextEdit) CompletionItem { |
| 434 |
snip.WriteText("{") |
| 435 |
|
| 436 |
|
| 437 |
if strct, ok := T.(*types.Struct); !ok || fieldsAccessible(strct, c.pkg.Types()) { |
| 438 |
snip.WriteFinalTabstop() |
| 439 |
} |
| 440 |
snip.WriteText("}") |
| 441 |
|
| 442 |
nonSnippet := typeName + "{}" |
| 443 |
|
| 444 |
return CompletionItem{ |
| 445 |
Label: nonSnippet, |
| 446 |
InsertText: nonSnippet, |
| 447 |
Score: matchScore * literalCandidateScore, |
| 448 |
Kind: protocol.VariableCompletion, |
| 449 |
AdditionalTextEdits: edits, |
| 450 |
snippet: snip, |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
func (c *completer) basicLiteral(T types.Type, snip *snippet.Builder, typeName string, matchScore float64, edits []protocol.TextEdit) (CompletionItem, bool) { |
| 459 |
|
| 460 |
if isUntyped(T) { |
| 461 |
return CompletionItem{}, false |
| 462 |
} |
| 463 |
|
| 464 |
snip.WriteText("(") |
| 465 |
snip.WriteFinalTabstop() |
| 466 |
snip.WriteText(")") |
| 467 |
|
| 468 |
nonSnippet := typeName + "()" |
| 469 |
|
| 470 |
return CompletionItem{ |
| 471 |
Label: nonSnippet, |
| 472 |
InsertText: nonSnippet, |
| 473 |
Detail: T.String(), |
| 474 |
Score: matchScore * literalCandidateScore, |
| 475 |
Kind: protocol.VariableCompletion, |
| 476 |
AdditionalTextEdits: edits, |
| 477 |
snippet: snip, |
| 478 |
}, true |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
func (c *completer) makeCall(snip *snippet.Builder, typeName string, secondArg string, matchScore float64, edits []protocol.TextEdit) CompletionItem { |
| 483 |
|
| 484 |
|
| 485 |
snip.PrependText("make(") |
| 486 |
if secondArg != "" { |
| 487 |
snip.WriteText(", ") |
| 488 |
snip.WritePlaceholder(func(b *snippet.Builder) { |
| 489 |
if c.opts.placeholders { |
| 490 |
b.WriteText(secondArg) |
| 491 |
} |
| 492 |
}) |
| 493 |
} |
| 494 |
snip.WriteText(")") |
| 495 |
|
| 496 |
var nonSnippet strings.Builder |
| 497 |
nonSnippet.WriteString("make(" + typeName) |
| 498 |
if secondArg != "" { |
| 499 |
nonSnippet.WriteString(", ") |
| 500 |
nonSnippet.WriteString(secondArg) |
| 501 |
} |
| 502 |
nonSnippet.WriteByte(')') |
| 503 |
|
| 504 |
return CompletionItem{ |
| 505 |
Label: nonSnippet.String(), |
| 506 |
InsertText: nonSnippet.String(), |
| 507 |
|
| 508 |
Score: matchScore * literalCandidateScore * 0.99, |
| 509 |
Kind: protocol.FunctionCompletion, |
| 510 |
AdditionalTextEdits: edits, |
| 511 |
snippet: snip, |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
func (c *completer) typeNameSnippet(literalType types.Type, qual types.Qualifier) (*snippet.Builder, string) { |
| 517 |
var ( |
| 518 |
snip snippet.Builder |
| 519 |
typeName string |
| 520 |
tparams *types.TypeParamList |
| 521 |
) |
| 522 |
|
| 523 |
t, ok := literalType.(typesinternal.NamedOrAlias) |
| 524 |
if ok { |
| 525 |
tparams = t.TypeParams() |
| 526 |
} |
| 527 |
if tparams.Len() > 0 && !c.fullyInstantiated(t) { |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
if pkg := qual(t.Obj().Pkg()); pkg != "" { |
| 533 |
typeName = pkg + "." |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
typeName += t.Obj().Name() |
| 538 |
snip.WriteText(typeName + "[") |
| 539 |
|
| 540 |
if c.opts.placeholders { |
| 541 |
for i := range tparams.Len() { |
| 542 |
if i > 0 { |
| 543 |
snip.WriteText(", ") |
| 544 |
} |
| 545 |
snip.WritePlaceholder(func(snip *snippet.Builder) { |
| 546 |
snip.WriteText(types.TypeString(tparams.At(i), qual)) |
| 547 |
}) |
| 548 |
} |
| 549 |
} else { |
| 550 |
snip.WritePlaceholder(nil) |
| 551 |
} |
| 552 |
snip.WriteText("]") |
| 553 |
typeName += "[...]" |
| 554 |
} else { |
| 555 |
|
| 556 |
typeName = types.TypeString(literalType, qual) |
| 557 |
snip.WriteText(typeName) |
| 558 |
} |
| 559 |
|
| 560 |
return &snip, typeName |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
func (c *completer) fullyInstantiated(t typesinternal.NamedOrAlias) bool { |
| 566 |
targs := t.TypeArgs() |
| 567 |
tparams := t.TypeParams() |
| 568 |
|
| 569 |
if tparams.Len() != targs.Len() { |
| 570 |
return false |
| 571 |
} |
| 572 |
|
| 573 |
for targ := range targs.Types() { |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
switch targ := types.Unalias(targ).(type) { |
| 585 |
case *types.TypeParam: |
| 586 |
|
| 587 |
|
| 588 |
if !c.typeParamInScope(targ) { |
| 589 |
return false |
| 590 |
} |
| 591 |
case *types.Named: |
| 592 |
if !c.fullyInstantiated(targ) { |
| 593 |
return false |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
return true |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
func (c *completer) typeParamInScope(tp *types.TypeParam) bool { |
| 604 |
obj := tp.Obj() |
| 605 |
if obj == nil { |
| 606 |
return false |
| 607 |
} |
| 608 |
|
| 609 |
scope := c.innermostScope() |
| 610 |
if scope == nil { |
| 611 |
return false |
| 612 |
} |
| 613 |
|
| 614 |
_, foundObj := scope.LookupParent(obj.Name(), c.pos) |
| 615 |
return obj == foundObj |
| 616 |
} |