Skip to content

Commit 312b6d1

Browse files
authored
Merge pull request #273 from hamishknight/exclude-completions
Exclude expected completions in a couple of places
2 parents 3f5f866 + 1548bf0 commit 312b6d1

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

SourceKitStressTester/Sources/StressTester/ActionGenerators.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,15 @@ private struct ActionToken {
503503
return nil
504504
}
505505
}
506+
507+
// Avoid matching for underscored attributes since we hide completions for
508+
// those.
509+
if let attr = parent.ancestorOrSelf(mapping: { $0.as(AttributeSyntax.self) }),
510+
let ident = attr.attributeName.as(IdentifierTypeSyntax.self),
511+
ident.name.text.hasPrefix("_") {
512+
return nil
513+
}
514+
506515
if let parent = parent.as(DeclReferenceExprSyntax.self), parent.baseName == token {
507516
if let refArgs = parent.argumentNames {
508517
let name = SwiftName(base: token.text, labels: refArgs.arguments.map{ $0.name.text })

SourceKitStressTester/Sources/StressTester/SourceKitDocument.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,22 @@ public struct CompletionMatcher {
718718
private func matchesCall(paramLabels: [String]) -> Bool {
719719
var remainingArgLabels = expected.name.argLabels[...]
720720

721+
func skippingIgnorableArgLabels(skipEmpty: Bool = false) -> ArraySlice<String> {
722+
var remainingArgLabels = remainingArgLabels
723+
if skipEmpty || remainingArgLabels.count < expected.name.argLabels.count {
724+
// If previous param was matched, allow consuming unlabeled args to
725+
// handle variadic cases.
726+
remainingArgLabels = remainingArgLabels.drop { $0.isEmpty }
727+
}
728+
// Ignore `file` and `line` since we don't include `#file` and `#line`
729+
// default args for completion.
730+
// FIXME: We ought to have this be configurable and enable it for the stress
731+
// tester.
732+
return remainingArgLabels.drop { $0 == "file" || $0 == "line" }
733+
}
734+
721735
guard !paramLabels.isEmpty else {
722-
return remainingArgLabels.allSatisfy { $0.isEmpty }
736+
return skippingIgnorableArgLabels(skipEmpty: true).isEmpty
723737
}
724738
for nextParamLabel in paramLabels {
725739
if nextParamLabel.isEmpty {
@@ -732,12 +746,9 @@ public struct CompletionMatcher {
732746
continue
733747
}
734748
} else {
735-
// Has param label
736-
if remainingArgLabels.count < expected.name.argLabels.count {
737-
// A previous param was matched, so assume it was variadic and consume
738-
// any leading unlabelled args so the next arg is labelled
739-
remainingArgLabels = remainingArgLabels.drop{ $0.isEmpty }
740-
}
749+
// Has param label, skip any ignorable labels before matching.
750+
remainingArgLabels = skippingIgnorableArgLabels()
751+
741752
guard let nextArgLabel = remainingArgLabels.first else {
742753
// Assume any unprocessed parameters are defaulted
743754
return true
@@ -750,9 +761,6 @@ public struct CompletionMatcher {
750761
// Else assume this param was defaulted and skip it.
751762
}
752763
}
753-
// If at least one arglabel was matched, allow for it being variadic
754-
let hadMatch = remainingArgLabels.count < expected.name.argLabels.count
755-
return remainingArgLabels.isEmpty || hadMatch &&
756-
remainingArgLabels.allSatisfy { $0.isEmpty }
764+
return skippingIgnorableArgLabels().isEmpty
757765
}
758766
}

SourceKitStressTester/Tests/StressTesterToolTests/ActionGeneratorTests.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ class ActionGeneratorTests: XCTestCase {
9797

9898
// FIXME: completion doesn't suggest anonymous closure params (e.g. $0)
9999
(#line, "$0.first", [/*"$0",*/ "first"]),
100-
(#line, "[1,2,4].filter{ $0 == 4 }", ["call:filter"/*, "$0"*/])
100+
(#line, "[1,2,4].filter{ $0 == 4 }", ["call:filter"/*, "$0"*/]),
101+
102+
// We don't include completions for underscored attributes.
103+
(#line, "@_underscored(a: Foo)", []),
104+
(#line, "@_spi(Foo)", []),
101105
]
102106

103107
for test in cases {
@@ -153,7 +157,17 @@ class ActionGeneratorTests: XCTestCase {
153157
(match: .pattern, of: "first(_:z:)", against: "first(x:y:)", ignoreArgs: false, result: false),
154158
(match: .pattern, of: "first(_:)", against: "first", ignoreArgs: false, result: false),
155159
(match: .pattern, of: "first(_:)", against: "first()", ignoreArgs: false, result: true),
156-
(match: .pattern, of: "first(_:_:)", against: "first()", ignoreArgs: false, result: true)
160+
(match: .pattern, of: "first(_:_:)", against: "first()", ignoreArgs: false, result: true),
161+
162+
// We don't do matching for `file` and `line`.
163+
(match: .call, of: "foo(x:file:line:)", against: "foo(x:)", ignoreArgs: false, result: true),
164+
(match: .call, of: "foo(x:file:line:)", against: "foo(x:file:line:)", ignoreArgs: false, result: true),
165+
(match: .call, of: "foo(x:file:line:y:)", against: "foo(x:y:)", ignoreArgs: false, result: true),
166+
(match: .call, of: "foo(file:line:y:)", against: "foo(file:line:x:y:z:)", ignoreArgs: false, result: true),
167+
(match: .call, of: "foo(x:file:line:y:)", against: "foo(x:y:z:)", ignoreArgs: false, result: true),
168+
(match: .call, of: "foo(file:line:y:)", against: "foo(x:y:)", ignoreArgs: false, result: true),
169+
(match: .call, of: "foo(file:line:)", against: "foo(_:)", ignoreArgs: false, result: true),
170+
(match: .call, of: "foo(file:line:)", against: "foo()", ignoreArgs: false, result: true),
157171
]
158172

159173
for test in cases {

0 commit comments

Comments
 (0)