| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package completion |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"go/ast" |
| 12 |
"go/doc" |
| 13 |
"go/types" |
| 14 |
"strings" |
| 15 |
|
| 16 |
"golang.org/x/tools/gopls/internal/golang" |
| 17 |
"golang.org/x/tools/gopls/internal/golang/completion/snippet" |
| 18 |
"golang.org/x/tools/gopls/internal/protocol" |
| 19 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 20 |
"golang.org/x/tools/gopls/internal/util/typesutil" |
| 21 |
internalastutil "golang.org/x/tools/internal/astutil" |
| 22 |
"golang.org/x/tools/internal/event" |
| 23 |
"golang.org/x/tools/internal/imports" |
| 24 |
) |
| 25 |
|
| 26 |
var ( |
| 27 |
errNoMatch = errors.New("not a surrounding match") |
| 28 |
errLowScore = errors.New("not a high scoring candidate") |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
func (c *completer) item(ctx context.Context, cand candidate) (CompletionItem, error) { |
| 33 |
obj := cand.obj |
| 34 |
|
| 35 |
|
| 36 |
matchScore := c.matcher.Score(cand.name) |
| 37 |
if matchScore <= 0 { |
| 38 |
return CompletionItem{}, errNoMatch |
| 39 |
} |
| 40 |
cand.score *= float64(matchScore) |
| 41 |
|
| 42 |
|
| 43 |
if len(cand.path) != 0 && !c.deepState.isHighScore(cand.score) { |
| 44 |
return CompletionItem{}, errLowScore |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
if obj.Parent() == types.Universe { |
| 49 |
return c.formatBuiltin(ctx, cand) |
| 50 |
} |
| 51 |
|
| 52 |
var ( |
| 53 |
label = cand.name |
| 54 |
detail = types.TypeString(obj.Type(), c.qual) |
| 55 |
insert = label |
| 56 |
kind = protocol.TextCompletion |
| 57 |
snip snippet.Builder |
| 58 |
protocolEdits []protocol.TextEdit |
| 59 |
) |
| 60 |
if obj.Type() == nil { |
| 61 |
detail = "" |
| 62 |
} |
| 63 |
|
| 64 |
type hasTypeParams interface{ TypeParams() *types.TypeParamList } |
| 65 |
if genericType, _ := obj.Type().(hasTypeParams); genericType != nil && isTypeName(obj) && c.wantTypeParams() { |
| 66 |
|
| 67 |
|
| 68 |
tparams := genericType.TypeParams() |
| 69 |
label += typesutil.FormatTypeParams(tparams) |
| 70 |
insert = label |
| 71 |
} |
| 72 |
|
| 73 |
snip.WriteText(insert) |
| 74 |
|
| 75 |
switch obj := obj.(type) { |
| 76 |
case *types.TypeName: |
| 77 |
detail, kind = golang.FormatType(obj.Type(), c.qual) |
| 78 |
case *types.Const: |
| 79 |
kind = protocol.ConstantCompletion |
| 80 |
case *types.Var: |
| 81 |
if _, ok := obj.Type().(*types.Struct); ok { |
| 82 |
detail = "struct{...}" |
| 83 |
} else if obj.IsField() { |
| 84 |
var err error |
| 85 |
detail, err = golang.FormatVarType(ctx, c.snapshot, c.pkg, obj, c.qual, c.mq) |
| 86 |
if err != nil { |
| 87 |
return CompletionItem{}, err |
| 88 |
} |
| 89 |
} |
| 90 |
if obj.IsField() { |
| 91 |
kind = protocol.FieldCompletion |
| 92 |
c.structFieldSnippet(cand, detail, &snip) |
| 93 |
} else { |
| 94 |
kind = protocol.VariableCompletion |
| 95 |
} |
| 96 |
if obj.Type() == nil { |
| 97 |
break |
| 98 |
} |
| 99 |
case *types.Func: |
| 100 |
if obj.Signature().Recv() == nil { |
| 101 |
kind = protocol.FunctionCompletion |
| 102 |
} else { |
| 103 |
kind = protocol.MethodCompletion |
| 104 |
} |
| 105 |
case *types.PkgName: |
| 106 |
kind = protocol.ModuleCompletion |
| 107 |
detail = fmt.Sprintf("%q", obj.Imported().Path()) |
| 108 |
case *types.Label: |
| 109 |
kind = protocol.ConstantCompletion |
| 110 |
detail = "label" |
| 111 |
} |
| 112 |
|
| 113 |
var prefix string |
| 114 |
for _, mod := range cand.mods { |
| 115 |
switch mod { |
| 116 |
case reference: |
| 117 |
prefix = "&" + prefix |
| 118 |
case dereference: |
| 119 |
prefix = "*" + prefix |
| 120 |
case chanRead: |
| 121 |
prefix = "<-" + prefix |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
var ( |
| 126 |
suffix string |
| 127 |
funcType = obj.Type() |
| 128 |
) |
| 129 |
Suffixes: |
| 130 |
for _, mod := range cand.mods { |
| 131 |
switch mod { |
| 132 |
case invoke: |
| 133 |
if sig, ok := funcType.Underlying().(*types.Signature); ok { |
| 134 |
s, err := golang.NewSignature(ctx, c.snapshot, c.pkg, sig, nil, c.qual, c.mq) |
| 135 |
if err != nil { |
| 136 |
return CompletionItem{}, err |
| 137 |
} |
| 138 |
|
| 139 |
tparams := s.TypeParams() |
| 140 |
if len(tparams) > 0 { |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
free := inferableTypeParams(sig) |
| 149 |
for i := sig.TypeParams().Len() - 1; i >= 0; i-- { |
| 150 |
tparam := sig.TypeParams().At(i) |
| 151 |
if !free[tparam] { |
| 152 |
break |
| 153 |
} |
| 154 |
tparams = tparams[:i] |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
c.functionCallSnippet("", tparams, s.Params(), &snip) |
| 159 |
if sig.Results().Len() == 1 { |
| 160 |
funcType = sig.Results().At(0).Type() |
| 161 |
} |
| 162 |
detail = "func" + s.Format() |
| 163 |
} |
| 164 |
|
| 165 |
if !c.opts.snippets { |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
break Suffixes |
| 171 |
} |
| 172 |
case takeSlice: |
| 173 |
suffix += "[:]" |
| 174 |
case takeDotDotDot: |
| 175 |
suffix += "..." |
| 176 |
case index: |
| 177 |
snip.WriteText("[") |
| 178 |
snip.WritePlaceholder(nil) |
| 179 |
snip.WriteText("]") |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
if cand.imp != nil { |
| 186 |
addlEdits, err := c.importEdits(cand.imp) |
| 187 |
|
| 188 |
if err != nil { |
| 189 |
return CompletionItem{}, err |
| 190 |
} |
| 191 |
|
| 192 |
protocolEdits = append(protocolEdits, addlEdits...) |
| 193 |
if kind != protocol.ModuleCompletion { |
| 194 |
if detail != "" { |
| 195 |
detail += " " |
| 196 |
} |
| 197 |
detail += fmt.Sprintf("(from %q)", cand.imp.importPath) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if cand.convertTo != nil { |
| 202 |
conv := c.formatConversion(cand.convertTo) |
| 203 |
prefix = conv.prefix + prefix |
| 204 |
suffix = conv.suffix |
| 205 |
} |
| 206 |
|
| 207 |
if prefix != "" { |
| 208 |
|
| 209 |
if sel := enclosingSelector(c.path, c.pos); sel != nil { |
| 210 |
edits, err := c.editText(sel.Pos(), sel.Pos(), prefix) |
| 211 |
if err != nil { |
| 212 |
return CompletionItem{}, err |
| 213 |
} |
| 214 |
protocolEdits = append(protocolEdits, edits...) |
| 215 |
} else { |
| 216 |
|
| 217 |
insert = prefix + insert |
| 218 |
snip.PrependText(prefix) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if suffix != "" { |
| 223 |
insert += suffix |
| 224 |
snip.WriteText(suffix) |
| 225 |
} |
| 226 |
|
| 227 |
detail = strings.TrimPrefix(detail, "untyped ") |
| 228 |
|
| 229 |
if cand.detail != "" { |
| 230 |
detail = cand.detail |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
if c.completionContext.commentNeedsLeadingSpace { |
| 236 |
insert = " " + insert |
| 237 |
snip.PrependText(" ") |
| 238 |
} |
| 239 |
|
| 240 |
item := CompletionItem{ |
| 241 |
Label: label, |
| 242 |
InsertText: insert, |
| 243 |
AdditionalTextEdits: protocolEdits, |
| 244 |
Detail: detail, |
| 245 |
Kind: kind, |
| 246 |
Score: cand.score, |
| 247 |
Depth: len(cand.path), |
| 248 |
snippet: &snip, |
| 249 |
isSlice: isSlice(obj), |
| 250 |
} |
| 251 |
|
| 252 |
if !c.opts.documentation { |
| 253 |
return item, nil |
| 254 |
} |
| 255 |
pos := safetoken.StartPosition(c.pkg.FileSet(), obj.Pos()) |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
if !pos.IsValid() { |
| 260 |
return item, nil |
| 261 |
} |
| 262 |
|
| 263 |
comment, err := golang.HoverDocForObject(ctx, c.snapshot, c.pkg.FileSet(), obj) |
| 264 |
if err != nil { |
| 265 |
event.Error(ctx, fmt.Sprintf("failed to find Hover for %q", obj.Name()), err) |
| 266 |
return item, nil |
| 267 |
} |
| 268 |
if c.opts.fullDocumentation { |
| 269 |
item.Documentation = comment.Text() |
| 270 |
} else { |
| 271 |
item.Documentation = doc.Synopsis(comment.Text()) |
| 272 |
} |
| 273 |
if internalastutil.Deprecation(comment) != "" { |
| 274 |
if c.snapshot.Options().CompletionTags { |
| 275 |
item.Tags = []protocol.CompletionItemTag{protocol.ComplDeprecated} |
| 276 |
} else if c.snapshot.Options().CompletionDeprecated { |
| 277 |
item.Deprecated = true |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
return item, nil |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
type conversionEdits struct { |
| 287 |
prefix, suffix string |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
func (c *completer) formatConversion(convertTo types.Type) conversionEdits { |
| 295 |
if convertTo == nil { |
| 296 |
return conversionEdits{} |
| 297 |
} |
| 298 |
|
| 299 |
typeName := types.TypeString(convertTo, c.qual) |
| 300 |
switch t := convertTo.(type) { |
| 301 |
|
| 302 |
|
| 303 |
case *types.Pointer, *types.Signature: |
| 304 |
typeName = "(" + typeName + ")" |
| 305 |
case *types.Basic: |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
if t.Info()&types.IsUntyped != 0 { |
| 310 |
typeName = types.TypeString(types.Default(convertTo), c.qual) |
| 311 |
} |
| 312 |
} |
| 313 |
return conversionEdits{prefix: typeName + "(", suffix: ")"} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
func (c *completer) importEdits(imp *importInfo) ([]protocol.TextEdit, error) { |
| 318 |
if imp == nil { |
| 319 |
return nil, nil |
| 320 |
} |
| 321 |
|
| 322 |
pgf, err := c.pkg.File(protocol.URIFromPath(c.filename)) |
| 323 |
if err != nil { |
| 324 |
return nil, err |
| 325 |
} |
| 326 |
|
| 327 |
return golang.ComputeImportFixEdits(c.snapshot.Options().Local, pgf.Src, &imports.ImportFix{ |
| 328 |
StmtInfo: imports.ImportInfo{ |
| 329 |
ImportPath: imp.importPath, |
| 330 |
Name: imp.name, |
| 331 |
}, |
| 332 |
|
| 333 |
FixType: imports.AddImport, |
| 334 |
}) |
| 335 |
} |
| 336 |
|
| 337 |
func (c *completer) formatBuiltin(ctx context.Context, cand candidate) (CompletionItem, error) { |
| 338 |
obj := cand.obj |
| 339 |
item := CompletionItem{ |
| 340 |
Label: obj.Name(), |
| 341 |
InsertText: obj.Name(), |
| 342 |
Score: cand.score, |
| 343 |
} |
| 344 |
switch obj.(type) { |
| 345 |
case *types.Const: |
| 346 |
item.Kind = protocol.ConstantCompletion |
| 347 |
case *types.Builtin: |
| 348 |
item.Kind = protocol.FunctionCompletion |
| 349 |
sig, err := golang.NewBuiltinSignature(ctx, c.snapshot, obj.Name()) |
| 350 |
if err != nil { |
| 351 |
return CompletionItem{}, err |
| 352 |
} |
| 353 |
item.Detail = "func" + sig.Format() |
| 354 |
item.snippet = &snippet.Builder{} |
| 355 |
∅. |
| 356 |
c.functionCallSnippet(obj.Name(), sig.TypeParams(), sig.Params(), item.snippet) |
| 357 |
case *types.TypeName: |
| 358 |
if types.IsInterface(obj.Type()) { |
| 359 |
item.Kind = protocol.InterfaceCompletion |
| 360 |
} else { |
| 361 |
item.Kind = protocol.ClassCompletion |
| 362 |
} |
| 363 |
case *types.Nil: |
| 364 |
item.Kind = protocol.VariableCompletion |
| 365 |
} |
| 366 |
return item, nil |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
func (c *completer) wantTypeParams() bool { |
| 373 |
|
| 374 |
|
| 375 |
start := c.path[0].Pos() |
| 376 |
for i, nd := range c.path { |
| 377 |
if fd, ok := nd.(*ast.FuncDecl); ok { |
| 378 |
if i > 0 && fd.Recv != nil && start < fd.Recv.End() { |
| 379 |
return true |
| 380 |
} else { |
| 381 |
return false |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
return false |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
func inferableTypeParams(sig *types.Signature) map[*types.TypeParam]bool { |
| 391 |
free := make(map[*types.TypeParam]bool) |
| 392 |
|
| 393 |
|
| 394 |
var visit func(t types.Type) |
| 395 |
visit = func(t types.Type) { |
| 396 |
switch t := t.(type) { |
| 397 |
case *types.Array: |
| 398 |
visit(t.Elem()) |
| 399 |
case *types.Chan: |
| 400 |
visit(t.Elem()) |
| 401 |
case *types.Map: |
| 402 |
visit(t.Key()) |
| 403 |
visit(t.Elem()) |
| 404 |
case *types.Pointer: |
| 405 |
visit(t.Elem()) |
| 406 |
case *types.Slice: |
| 407 |
visit(t.Elem()) |
| 408 |
case *types.Interface: |
| 409 |
for method := range t.ExplicitMethods() { |
| 410 |
visit(method.Type()) |
| 411 |
} |
| 412 |
for etyp := range t.EmbeddedTypes() { |
| 413 |
visit(etyp) |
| 414 |
} |
| 415 |
case *types.Union: |
| 416 |
for term := range t.Terms() { |
| 417 |
visit(term.Type()) |
| 418 |
} |
| 419 |
case *types.Signature: |
| 420 |
if tp := t.TypeParams(); tp != nil { |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
for tparam := range tp.TypeParams() { |
| 425 |
visit(tparam.Constraint()) |
| 426 |
} |
| 427 |
} |
| 428 |
visit(t.Params()) |
| 429 |
visit(t.Results()) |
| 430 |
case *types.Tuple: |
| 431 |
for v := range t.Variables() { |
| 432 |
visit(v.Type()) |
| 433 |
} |
| 434 |
case *types.Struct: |
| 435 |
for field := range t.Fields() { |
| 436 |
visit(field.Type()) |
| 437 |
} |
| 438 |
case *types.TypeParam: |
| 439 |
free[t] = true |
| 440 |
case *types.Alias: |
| 441 |
visit(types.Unalias(t)) |
| 442 |
case *types.Named: |
| 443 |
targs := t.TypeArgs() |
| 444 |
for t0 := range targs.Types() { |
| 445 |
visit(t0) |
| 446 |
} |
| 447 |
case *types.Basic: |
| 448 |
|
| 449 |
default: |
| 450 |
panic(t) |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
visit(sig.Params()) |
| 455 |
|
| 456 |
|
| 457 |
restart: |
| 458 |
for tp := range sig.TypeParams().TypeParams() { |
| 459 |
if free[tp] { |
| 460 |
n := len(free) |
| 461 |
visit(tp.Constraint()) |
| 462 |
if len(free) > n { |
| 463 |
goto restart |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
return free |
| 468 |
} |