| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package completion |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"go/ast" |
| 10 |
"go/token" |
| 11 |
"go/types" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"golang.org/x/tools/gopls/internal/cache" |
| 15 |
"golang.org/x/tools/gopls/internal/golang" |
| 16 |
"golang.org/x/tools/gopls/internal/golang/completion/snippet" |
| 17 |
"golang.org/x/tools/gopls/internal/protocol" |
| 18 |
"golang.org/x/tools/internal/typesinternal" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
func (c *completer) addStatementCandidates() { |
| 24 |
c.addErrCheck() |
| 25 |
c.addAssignAppend() |
| 26 |
c.addReturnZeroValues() |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
func (c *completer) addAssignAppend() { |
| 43 |
if len(c.path) < 3 { |
| 44 |
return |
| 45 |
} |
| 46 |
|
| 47 |
ident, _ := c.path[0].(*ast.Ident) |
| 48 |
if ident == nil { |
| 49 |
return |
| 50 |
} |
| 51 |
|
| 52 |
var ( |
| 53 |
|
| 54 |
|
| 55 |
sliceText string |
| 56 |
|
| 57 |
|
| 58 |
needsLHS = false |
| 59 |
fset = c.pkg.FileSet() |
| 60 |
) |
| 61 |
|
| 62 |
switch n := c.path[1].(type) { |
| 63 |
case *ast.AssignStmt: |
| 64 |
|
| 65 |
if c.matcher.Score("append") <= 0 { |
| 66 |
return |
| 67 |
} |
| 68 |
|
| 69 |
exprIdx := exprAtPos(c.pos, n.Rhs) |
| 70 |
if exprIdx == len(n.Rhs) || exprIdx > len(n.Lhs)-1 { |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
lhsType := c.pkg.TypesInfo().TypeOf(n.Lhs[exprIdx]) |
| 75 |
if lhsType == nil { |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
if _, isSlice := lhsType.Underlying().(*types.Slice); !isSlice { |
| 81 |
return |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
sliceText = golang.FormatNode(fset, n.Lhs[exprIdx]) |
| 86 |
case *ast.SelectorExpr: |
| 87 |
|
| 88 |
if _, parentIsExprtStmt := c.path[2].(*ast.ExprStmt); !parentIsExprtStmt { |
| 89 |
return |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
sliceText = golang.FormatNode(fset, n.X) + "." |
| 96 |
needsLHS = true |
| 97 |
case *ast.ExprStmt: |
| 98 |
needsLHS = true |
| 99 |
default: |
| 100 |
return |
| 101 |
} |
| 102 |
|
| 103 |
var ( |
| 104 |
label string |
| 105 |
snip snippet.Builder |
| 106 |
score = highScore |
| 107 |
) |
| 108 |
|
| 109 |
if needsLHS { |
| 110 |
|
| 111 |
|
| 112 |
bestItem := c.topCandidate() |
| 113 |
if bestItem == nil || !bestItem.isSlice { |
| 114 |
return |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
score = bestItem.Score - 0.01 |
| 120 |
|
| 121 |
|
| 122 |
sliceText += bestItem.Label |
| 123 |
|
| 124 |
|
| 125 |
label = fmt.Sprintf("%s = ", bestItem.Label) |
| 126 |
snip.WriteText(label) |
| 127 |
} |
| 128 |
|
| 129 |
snip.WriteText(fmt.Sprintf("append(%s, ", sliceText)) |
| 130 |
snip.WritePlaceholder(nil) |
| 131 |
snip.WriteText(")") |
| 132 |
|
| 133 |
c.items = append(c.items, CompletionItem{ |
| 134 |
Label: label + fmt.Sprintf("append(%s, )", sliceText), |
| 135 |
Kind: protocol.FunctionCompletion, |
| 136 |
Score: score, |
| 137 |
snippet: &snip, |
| 138 |
}) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
func (c *completer) topCandidate() *CompletionItem { |
| 145 |
var bestItem, secondBestItem *CompletionItem |
| 146 |
for i := range c.items { |
| 147 |
if bestItem == nil || c.items[i].Score > bestItem.Score { |
| 148 |
bestItem = &c.items[i] |
| 149 |
} else if secondBestItem == nil || c.items[i].Score > secondBestItem.Score { |
| 150 |
secondBestItem = &c.items[i] |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if secondBestItem != nil && secondBestItem.Score == bestItem.Score { |
| 157 |
return nil |
| 158 |
} |
| 159 |
|
| 160 |
return bestItem |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
func (c *completer) addErrCheck() { |
| 180 |
if len(c.path) < 2 || c.enclosingFunc == nil || !c.opts.placeholders { |
| 181 |
return |
| 182 |
} |
| 183 |
|
| 184 |
var ( |
| 185 |
errorType = types.Universe.Lookup("error").Type() |
| 186 |
result = c.enclosingFunc.sig.Results() |
| 187 |
testVar = getTestVar(c.enclosingFunc, c.pkg) |
| 188 |
isTest = testVar != "" |
| 189 |
doesNotReturnErr = result.Len() == 0 || !types.Identical(result.At(result.Len()-1).Type(), errorType) |
| 190 |
) |
| 191 |
|
| 192 |
if !isTest && doesNotReturnErr { |
| 193 |
return |
| 194 |
} |
| 195 |
|
| 196 |
prevLine := prevStmt(c.pos, c.path) |
| 197 |
if prevLine == nil { |
| 198 |
return |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
assign, _ := prevLine.(*ast.AssignStmt) |
| 203 |
if assign == nil || len(assign.Lhs) == 0 { |
| 204 |
return |
| 205 |
} |
| 206 |
|
| 207 |
lastAssignee := assign.Lhs[len(assign.Lhs)-1] |
| 208 |
|
| 209 |
|
| 210 |
if !types.Identical(c.pkg.TypesInfo().TypeOf(lastAssignee), errorType) { |
| 211 |
return |
| 212 |
} |
| 213 |
|
| 214 |
var ( |
| 215 |
|
| 216 |
errVar = golang.FormatNode(c.pkg.FileSet(), lastAssignee) |
| 217 |
|
| 218 |
|
| 219 |
needsIf = true |
| 220 |
) |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if errVar == "_" { |
| 226 |
return |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
switch n := c.path[0].(type) { |
| 233 |
case *ast.Ident: |
| 234 |
switch c.path[1].(type) { |
| 235 |
case *ast.ExprStmt: |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
if c.matcher.Score("if") <= 0 { |
| 243 |
return |
| 244 |
} |
| 245 |
case *ast.IfStmt: |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if c.matcher.Score(errVar) <= 0 { |
| 253 |
return |
| 254 |
} |
| 255 |
|
| 256 |
needsIf = false |
| 257 |
default: |
| 258 |
return |
| 259 |
} |
| 260 |
case *ast.IfStmt: |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
if _, bad := n.Cond.(*ast.BadExpr); !bad { |
| 270 |
return |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
needsIf = c.pos == n.Pos()+token.Pos(len("if")) |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
var snip snippet.Builder |
| 286 |
if needsIf { |
| 287 |
snip.WriteText("if ") |
| 288 |
} |
| 289 |
snip.WriteText(fmt.Sprintf("%s != nil {\n\t", errVar)) |
| 290 |
|
| 291 |
var label string |
| 292 |
if isTest { |
| 293 |
snip.WriteText(fmt.Sprintf("%s.Fatal(%s)", testVar, errVar)) |
| 294 |
label = fmt.Sprintf("%[1]s != nil { %[2]s.Fatal(%[1]s) }", errVar, testVar) |
| 295 |
} else { |
| 296 |
snip.WriteText("return ") |
| 297 |
for i := range result.Len() - 1 { |
| 298 |
if zero, isValid := typesinternal.ZeroString(result.At(i).Type(), c.qual); isValid { |
| 299 |
snip.WriteText(zero) |
| 300 |
} |
| 301 |
snip.WriteText(", ") |
| 302 |
} |
| 303 |
snip.WritePlaceholder(func(b *snippet.Builder) { |
| 304 |
b.WriteText(errVar) |
| 305 |
}) |
| 306 |
label = fmt.Sprintf("%[1]s != nil { return %[1]s }", errVar) |
| 307 |
} |
| 308 |
|
| 309 |
snip.WriteText("\n}") |
| 310 |
|
| 311 |
if needsIf { |
| 312 |
label = "if " + label |
| 313 |
} |
| 314 |
|
| 315 |
c.items = append(c.items, CompletionItem{ |
| 316 |
Label: label, |
| 317 |
Kind: protocol.SnippetCompletion, |
| 318 |
Score: highScore, |
| 319 |
snippet: &snip, |
| 320 |
}) |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
func getTestVar(enclosingFunc *funcInfo, pkg *cache.Package) string { |
| 330 |
if enclosingFunc == nil || enclosingFunc.sig == nil { |
| 331 |
return "" |
| 332 |
} |
| 333 |
|
| 334 |
var testingPkg *types.Package |
| 335 |
for _, p := range pkg.Types().Imports() { |
| 336 |
if p.Path() == "testing" { |
| 337 |
testingPkg = p |
| 338 |
break |
| 339 |
} |
| 340 |
} |
| 341 |
if testingPkg == nil { |
| 342 |
return "" |
| 343 |
} |
| 344 |
tbObj := testingPkg.Scope().Lookup("TB") |
| 345 |
if tbObj == nil { |
| 346 |
return "" |
| 347 |
} |
| 348 |
iface, ok := tbObj.Type().Underlying().(*types.Interface) |
| 349 |
if !ok { |
| 350 |
return "" |
| 351 |
} |
| 352 |
|
| 353 |
sig := enclosingFunc.sig |
| 354 |
for param := range sig.Params().Variables() { |
| 355 |
if param.Name() == "_" { |
| 356 |
continue |
| 357 |
} |
| 358 |
if !types.Implements(param.Type(), iface) { |
| 359 |
continue |
| 360 |
} |
| 361 |
return param.Name() |
| 362 |
} |
| 363 |
|
| 364 |
return "" |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
func (c *completer) addReturnZeroValues() { |
| 375 |
if len(c.path) < 2 || c.enclosingFunc == nil || !c.opts.placeholders { |
| 376 |
return |
| 377 |
} |
| 378 |
result := c.enclosingFunc.sig.Results() |
| 379 |
if result.Len() == 0 { |
| 380 |
return |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
var score = stdScore - 0.01 |
| 385 |
switch c.path[0].(type) { |
| 386 |
case *ast.ReturnStmt, *ast.Ident: |
| 387 |
f := c.matcher.Score("return") |
| 388 |
if f <= 0 { |
| 389 |
return |
| 390 |
} |
| 391 |
score *= float64(f) |
| 392 |
default: |
| 393 |
return |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
var snip snippet.Builder |
| 399 |
var label strings.Builder |
| 400 |
snip.WriteText("return ") |
| 401 |
fmt.Fprintf(&label, "return ") |
| 402 |
|
| 403 |
for i := range result.Len() { |
| 404 |
if i > 0 { |
| 405 |
snip.WriteText(", ") |
| 406 |
fmt.Fprintf(&label, ", ") |
| 407 |
} |
| 408 |
|
| 409 |
zero, isValid := typesinternal.ZeroString(result.At(i).Type(), c.qual) |
| 410 |
if !isValid { |
| 411 |
zero = "" |
| 412 |
} |
| 413 |
snip.WritePlaceholder(func(b *snippet.Builder) { |
| 414 |
b.WriteText(zero) |
| 415 |
}) |
| 416 |
fmt.Fprint(&label, zero) |
| 417 |
} |
| 418 |
|
| 419 |
c.items = append(c.items, CompletionItem{ |
| 420 |
Label: label.String(), |
| 421 |
Kind: protocol.SnippetCompletion, |
| 422 |
Score: score, |
| 423 |
snippet: &snip, |
| 424 |
}) |
| 425 |
} |