@@ -425,21 +425,45 @@ class SourceKitDocument {
425
425
}
426
426
427
427
private func checkExpectedCompletionResult( _ expected: ExpectedResult , in response: SourceKitdResponse , info: RequestInfo ) throws {
428
+ /// Struct that defines a code completion result to determine if there are duplicate results.
429
+ struct CodeCompletionResultSpec : Hashable {
430
+ let description : String
431
+ let type : String
432
+ let usr : String
433
+
434
+ init ( _ result: SourceKitdResponse . Dictionary ) {
435
+ self . description = result. getString ( . key_Description)
436
+ self . type = result. getString ( . key_TypeName)
437
+ self . usr = result. getString ( . key_AssociatedUSRs)
438
+ }
439
+ }
428
440
let matcher = CompletionMatcher ( for: expected)
429
441
var found = false
442
+ var foundDuplicate : CodeCompletionResultSpec ? = nil
443
+ var seenResults = Set < CodeCompletionResultSpec > ( )
430
444
response. value. getArray ( . key_Results) . enumerate { ( _, item) -> Bool in
431
445
let result = item. getDictionary ( )
432
446
found = matcher. match ( result. getString ( . key_Name) , ignoreArgLabels: shouldIgnoreArgs ( of: expected, for: result) )
447
+ let resultSpec = CodeCompletionResultSpec ( result)
448
+ if foundDuplicate == nil , !seenResults. insert ( resultSpec) . inserted {
449
+ foundDuplicate = resultSpec
450
+ }
433
451
return !found
434
452
}
435
- if !found {
453
+ lazy var truncatedResponseText = {
436
454
// FIXME: code completion responses can be huge, truncate them for now.
437
455
let maxSize = 25_000
438
456
var responseText = response. description
439
457
if responseText. count > maxSize {
440
458
responseText = responseText. prefix ( maxSize) + " [truncated] "
441
459
}
442
- throw SourceKitError . failed ( . missingExpectedResult, request: info, response: responseText. trimmingCharacters ( in: . newlines) )
460
+ return responseText
461
+ } ( )
462
+ if !found {
463
+ throw SourceKitError . failed ( . missingExpectedResult, request: info, response: truncatedResponseText. trimmingCharacters ( in: . newlines) )
464
+ }
465
+ if let foundDuplicate = foundDuplicate {
466
+ throw SourceKitError . failed ( . duplicateResult( name: foundDuplicate. description) , request: info, response: truncatedResponseText. trimmingCharacters ( in: . newlines) )
443
467
}
444
468
}
445
469
0 commit comments