| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package completion |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"go/types" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
|
| 13 |
"golang.org/x/tools/gopls/internal/util/typesutil" |
| 14 |
"golang.org/x/tools/internal/packagepath" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
const MaxDeepCompletions = 3 |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
type deepCompletionState struct { |
| 25 |
|
| 26 |
enabled bool |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
queueClosed bool |
| 31 |
|
| 32 |
|
| 33 |
thisQueue []candidate |
| 34 |
|
| 35 |
|
| 36 |
nextQueue []candidate |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
highScores [MaxDeepCompletions]float64 |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
candidateCount int |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func (s *deepCompletionState) enqueue(cand candidate) { |
| 49 |
s.nextQueue = append(s.nextQueue, cand) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
func (s *deepCompletionState) scorePenalty(cand *candidate) float64 { |
| 57 |
var deepPenalty float64 |
| 58 |
for _, dc := range cand.path { |
| 59 |
deepPenalty++ |
| 60 |
|
| 61 |
if !dc.Exported() { |
| 62 |
deepPenalty -= 0.1 |
| 63 |
} |
| 64 |
|
| 65 |
if _, isSig := dc.Type().Underlying().(*types.Signature); isSig { |
| 66 |
deepPenalty += 0.1 |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
return deepPenalty / 10 |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func (s *deepCompletionState) isHighScore(score float64) bool { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
for i, deepScore := range s.highScores { |
| 83 |
if score <= deepScore { |
| 84 |
continue |
| 85 |
} |
| 86 |
|
| 87 |
if deepScore != 0 && i != len(s.highScores)-1 { |
| 88 |
|
| 89 |
|
| 90 |
copy(s.highScores[i+1:], s.highScores[i:]) |
| 91 |
} |
| 92 |
s.highScores[i] = score |
| 93 |
return true |
| 94 |
} |
| 95 |
|
| 96 |
return false |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
func (s *deepCompletionState) newPath(cand candidate, obj types.Object) []types.Object { |
| 102 |
path := make([]types.Object, len(cand.path)+1) |
| 103 |
copy(path, cand.path) |
| 104 |
path[len(path)-1] = obj |
| 105 |
|
| 106 |
return path |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
func (c *completer) deepSearch(ctx context.Context, minDepth int, deadline *time.Time) { |
| 113 |
defer func() { |
| 114 |
|
| 115 |
|
| 116 |
c.deepState.thisQueue = c.deepState.thisQueue[:0] |
| 117 |
c.deepState.nextQueue = c.deepState.nextQueue[:0] |
| 118 |
}() |
| 119 |
|
| 120 |
depth := 0 |
| 121 |
|
| 122 |
stop := func() bool { |
| 123 |
|
| 124 |
|
| 125 |
select { |
| 126 |
case <-ctx.Done(): |
| 127 |
return true |
| 128 |
default: |
| 129 |
} |
| 130 |
|
| 131 |
return depth > minDepth && deadline != nil && time.Now().After(*deadline) |
| 132 |
} |
| 133 |
|
| 134 |
for len(c.deepState.nextQueue) > 0 { |
| 135 |
depth++ |
| 136 |
if stop() { |
| 137 |
return |
| 138 |
} |
| 139 |
c.deepState.thisQueue, c.deepState.nextQueue = c.deepState.nextQueue, c.deepState.thisQueue[:0] |
| 140 |
|
| 141 |
outer: |
| 142 |
for _, cand := range c.deepState.thisQueue { |
| 143 |
obj := cand.obj |
| 144 |
|
| 145 |
if obj == nil { |
| 146 |
continue |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
if len(cand.path) == 0 { |
| 151 |
if c.seen[obj] { |
| 152 |
continue |
| 153 |
} |
| 154 |
c.seen[obj] = true |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
if !c.completionContext.packageCompletion && |
| 161 |
obj.Pkg() != nil && obj.Pkg() != c.pkg.Types() && !obj.Exported() { |
| 162 |
continue |
| 163 |
} |
| 164 |
|
| 165 |
if cand.imp != nil && !packagepath.CanImport(string(c.pkg.Metadata().PkgPath), cand.imp.importPath) { |
| 166 |
continue |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
if c.wantTypeName() && obj.Type() != nil && !isTypeName(obj) && !isPkgName(obj) { |
| 174 |
continue |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
for _, seenObj := range cand.path { |
| 182 |
if seenObj == obj { |
| 183 |
continue outer |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
c.addCandidate(ctx, &cand) |
| 188 |
|
| 189 |
c.deepState.candidateCount++ |
| 190 |
if c.opts.budget > 0 && c.deepState.candidateCount%100 == 0 { |
| 191 |
if stop() { |
| 192 |
return |
| 193 |
} |
| 194 |
spent := float64(time.Since(c.startTime)) / float64(c.opts.budget) |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
if !c.deepState.queueClosed && spent >= 0.85 { |
| 199 |
c.deepState.queueClosed = true |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
if !c.deepState.enabled || c.deepState.queueClosed { |
| 205 |
continue |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
if isTypeName(obj) { |
| 210 |
continue |
| 211 |
} |
| 212 |
if obj.Type() == nil { |
| 213 |
continue |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if v, ok := obj.(*types.Var); ok && v.Embedded() { |
| 219 |
continue |
| 220 |
} |
| 221 |
|
| 222 |
if sig, ok := obj.Type().Underlying().(*types.Signature); ok { |
| 223 |
|
| 224 |
|
| 225 |
if sig.Params().Len() == 0 && sig.Results().Len() == 1 { |
| 226 |
path := c.deepState.newPath(cand, obj) |
| 227 |
|
| 228 |
c.methodsAndFields(sig.Results().At(0).Type(), false, cand.imp, func(newCand candidate) { |
| 229 |
newCand.pathInvokeMask = cand.pathInvokeMask | (1 << uint64(len(cand.path))) |
| 230 |
newCand.path = path |
| 231 |
c.deepState.enqueue(newCand) |
| 232 |
}) |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
path := c.deepState.newPath(cand, obj) |
| 237 |
switch obj := obj.(type) { |
| 238 |
case *types.PkgName: |
| 239 |
c.packageMembers(obj.Imported(), stdScore, cand.imp, func(newCand candidate) { |
| 240 |
newCand.pathInvokeMask = cand.pathInvokeMask |
| 241 |
newCand.path = path |
| 242 |
c.deepState.enqueue(newCand) |
| 243 |
}) |
| 244 |
default: |
| 245 |
c.methodsAndFields(obj.Type(), cand.addressable, cand.imp, func(newCand candidate) { |
| 246 |
newCand.pathInvokeMask = cand.pathInvokeMask |
| 247 |
newCand.path = path |
| 248 |
c.deepState.enqueue(newCand) |
| 249 |
}) |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
func (c *completer) addCandidate(ctx context.Context, cand *candidate) { |
| 258 |
obj := cand.obj |
| 259 |
if obj != nil && obj.Name() == "_" { |
| 260 |
return |
| 261 |
} |
| 262 |
if c.matchingCandidate(cand) { |
| 263 |
cand.score *= highScore |
| 264 |
|
| 265 |
if p := c.penalty(cand); p > 0 { |
| 266 |
cand.score *= (1 - p) |
| 267 |
} |
| 268 |
} else if isTypeName(obj) { |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
if _, isNamed := obj.Type().(*types.Named); isNamed { |
| 274 |
c.literal(ctx, obj.Type(), cand.imp) |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
if cand.hasMod(invoke) { |
| 280 |
if sig, ok := obj.Type().Underlying().(*types.Signature); ok && sig.Recv() != nil { |
| 281 |
cand.score *= 0.9 |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
if !obj.Exported() && obj.Parent() != types.Universe { |
| 287 |
cand.score *= 1.1 |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
if cand.hasMod(index) { |
| 293 |
cand.score *= 0.9 |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
cand.score -= cand.score * c.deepState.scorePenalty(cand) |
| 298 |
|
| 299 |
if cand.score < 0 { |
| 300 |
cand.score = 0 |
| 301 |
} |
| 302 |
|
| 303 |
cand.name = deepCandName(cand) |
| 304 |
if item, err := c.item(ctx, *cand); err == nil { |
| 305 |
c.items = append(c.items, item) |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
func deepCandName(cand *candidate) string { |
| 312 |
totalLen := len(cand.obj.Name()) |
| 313 |
for i, obj := range cand.path { |
| 314 |
totalLen += len(obj.Name()) + 1 |
| 315 |
if cand.pathInvokeMask&(1<<uint16(i)) > 0 { |
| 316 |
totalLen += 2 |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
var buf strings.Builder |
| 321 |
buf.Grow(totalLen) |
| 322 |
|
| 323 |
for i, obj := range cand.path { |
| 324 |
buf.WriteString(obj.Name()) |
| 325 |
if fn, ok := obj.(*types.Func); ok { |
| 326 |
buf.WriteString(typesutil.FormatTypeParams(fn.Signature().TypeParams())) |
| 327 |
} |
| 328 |
if cand.pathInvokeMask&(1<<uint16(i)) > 0 { |
| 329 |
buf.WriteByte('(') |
| 330 |
buf.WriteByte(')') |
| 331 |
} |
| 332 |
buf.WriteByte('.') |
| 333 |
} |
| 334 |
|
| 335 |
buf.WriteString(cand.obj.Name()) |
| 336 |
|
| 337 |
return buf.String() |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
func (c *completer) penalty(cand *candidate) float64 { |
| 344 |
for _, p := range c.inference.penalized { |
| 345 |
if c.objChainMatches(cand, p.objChain) { |
| 346 |
return p.penalty |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
return 0 |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
func (c *completer) objChainMatches(cand *candidate, chain []types.Object) bool { |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
if len(chain) != len(c.inference.objChain)+len(cand.path)+1 { |
| 364 |
return false |
| 365 |
} |
| 366 |
|
| 367 |
if chain[len(chain)-1] != cand.obj { |
| 368 |
return false |
| 369 |
} |
| 370 |
|
| 371 |
for i, o := range c.inference.objChain { |
| 372 |
if chain[i] != o { |
| 373 |
return false |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
for i, o := range cand.path { |
| 378 |
if chain[i+len(c.inference.objChain)] != o { |
| 379 |
return false |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
return true |
| 384 |
} |