Skip to content

Commit 6074529

Browse files
committed
Manually fix swift-format lint rules
- I think there are valid calls with mixing inline and trailing closures (notably `Debouncer`), so I’m considering whether we should disable that rule. - The `forEach` rule is a little annoying because we have `forEach` on `SKDResponseArray`. But it caught two cases of using `forEach` on arrays, so I think it’s worth keeping.
1 parent 98cd30b commit 6074529

File tree

16 files changed

+80
-38
lines changed

16 files changed

+80
-38
lines changed

Sources/BuildServerIntegration/BuildServerManager.swift

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -460,36 +460,38 @@ package actor BuildServerManager: QueueBasedMessageHandler {
460460
// The debounce duration of 500ms was chosen arbitrarily without any measurements.
461461
self.filesDependenciesUpdatedDebouncer = Debouncer(
462462
debounceDuration: .milliseconds(500),
463-
combineResults: { $0.union($1) }
464-
) {
465-
[weak self] (filesWithUpdatedDependencies) in
466-
guard let self, let delegate = await self.delegate else {
467-
logger.fault("Not calling filesDependenciesUpdated because no delegate exists in SwiftPMBuildServer")
468-
return
469-
}
470-
let changedWatchedFiles = await self.watchedFilesReferencing(mainFiles: filesWithUpdatedDependencies)
471-
if !changedWatchedFiles.isEmpty {
472-
await delegate.filesDependenciesUpdated(changedWatchedFiles)
463+
combineResults: { $0.union($1) },
464+
makeCall: {
465+
[weak self] (filesWithUpdatedDependencies) in
466+
guard let self, let delegate = await self.delegate else {
467+
logger.fault("Not calling filesDependenciesUpdated because no delegate exists in SwiftPMBuildServer")
468+
return
469+
}
470+
let changedWatchedFiles = await self.watchedFilesReferencing(mainFiles: filesWithUpdatedDependencies)
471+
if !changedWatchedFiles.isEmpty {
472+
await delegate.filesDependenciesUpdated(changedWatchedFiles)
473+
}
473474
}
474-
}
475+
)
475476

476477
// We don't need a large debounce duration here. It just needs to be big enough to accumulate
477478
// `resultReceivedAfterTimeout` calls for the same document (see comment on `filesBuildSettingsChangedDebouncer`).
478479
// Since they should all come in at the same time, a couple of milliseconds should be sufficient here, an 20ms be
479480
// plenty while still not causing a noticeable delay to the user.
480481
self.filesBuildSettingsChangedDebouncer = Debouncer(
481482
debounceDuration: .milliseconds(20),
482-
combineResults: { $0.union($1) }
483-
) {
484-
[weak self] (filesWithChangedBuildSettings) in
485-
guard let self, let delegate = await self.delegate else {
486-
logger.fault("Not calling fileBuildSettingsChanged because no delegate exists in SwiftPMBuildServer")
487-
return
488-
}
489-
if !filesWithChangedBuildSettings.isEmpty {
490-
await delegate.fileBuildSettingsChanged(filesWithChangedBuildSettings)
483+
combineResults: { $0.union($1) },
484+
makeCall: {
485+
[weak self] (filesWithChangedBuildSettings) in
486+
guard let self, let delegate = await self.delegate else {
487+
logger.fault("Not calling fileBuildSettingsChanged because no delegate exists in SwiftPMBuildServer")
488+
return
489+
}
490+
if !filesWithChangedBuildSettings.isEmpty {
491+
await delegate.fileBuildSettingsChanged(filesWithChangedBuildSettings)
492+
}
491493
}
492-
}
494+
)
493495

494496
// TODO: Forward file watch patterns from this initialize request to the client
495497
// (https://github.com/swiftlang/sourcekit-lsp/issues/1671)

Sources/BuildServerIntegration/CompilationDatabase.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ package struct JSONCompilationDatabase: Equatable, Codable {
140140

141141
package func encode(to encoder: Encoder) throws {
142142
var container = encoder.unkeyedContainer()
143-
try commands.forEach { try container.encode($0) }
143+
for command in commands {
144+
try container.encode(command)
145+
}
144146
}
145147

146148
package subscript(_ uri: DocumentURI) -> [CompilationDatabaseCompileCommand] {

Sources/BuildServerProtocol/Messages/TaskStartNotification.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public struct TaskStartNotification: NotificationType {
4646
/// Message describing the task.
4747
public var message: String?
4848

49-
/** Kind of data to expect in the `data` field. If this field is not set, the kind of data is not specified. */
49+
/// Kind of data to expect in the `data` field. If this field is not set, the kind of data is not specified.
5050
public var dataKind: TaskStartDataKind?
5151

5252
/// Optional metadata about the task.

Sources/CompletionScoring/Text/MatchCollator.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,16 @@ package struct MatchCollator {
415415
influencingTokenizedIdentifiers: influencingTokenizedIdentifiers,
416416
patternUTF8Length: 0,
417417
orderingTiesBy: { _, _ in false },
418-
maximumNumberOfItemsForExpensiveSelection: Self.defaultMaximumNumberOfItemsForExpensiveSelection
419-
) { collator in
420-
return (0..<iterations).reduce(0) { accumulation, _ in
421-
collator.applyInfluence()
422-
return collator.rescoredMatches.reduce(accumulation) { accumulation, match in
423-
accumulation + match.individualScore.value
418+
maximumNumberOfItemsForExpensiveSelection: Self.defaultMaximumNumberOfItemsForExpensiveSelection,
419+
body: { collator in
420+
return (0..<iterations).reduce(0) { accumulation, _ in
421+
collator.applyInfluence()
422+
return collator.rescoredMatches.reduce(accumulation) { accumulation, match in
423+
accumulation + match.individualScore.value
424+
}
424425
}
425426
}
426-
}
427+
)
427428
}
428429

429430
package static func tokenize(

Sources/SKUtilities/Debouncer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ package actor Debouncer<Parameter: Sendable> {
4141
package init(
4242
debounceDuration: Duration,
4343
combineResults: @escaping (Parameter, Parameter) -> Parameter,
44-
_ makeCall: @Sendable @escaping (Parameter) async -> Void
44+
makeCall: @Sendable @escaping (Parameter) async -> Void
4545
) {
4646
self.debounceDuration = debounceDuration
4747
self.combineParameters = combineResults
@@ -84,7 +84,7 @@ package actor Debouncer<Parameter: Sendable> {
8484

8585
extension Debouncer<Void> {
8686
package init(debounceDuration: Duration, _ makeCall: @Sendable @escaping () async -> Void) {
87-
self.init(debounceDuration: debounceDuration, combineResults: { _, _ in }, makeCall)
87+
self.init(debounceDuration: debounceDuration, combineResults: { _, _ in }, makeCall: makeCall)
8888
}
8989

9090
package func scheduleCall() {

Sources/SourceKitD/SKDResponseArray.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ package final class SKDResponseArray: Sendable {
6262
package func map<T>(_ transform: (SKDResponseDictionary) throws -> T) rethrows -> [T] {
6363
var result: [T] = []
6464
result.reserveCapacity(self.count)
65+
// swift-format-ignore: ReplaceForEachWithForLoop
66+
// Reference is to `SKDResponseArray.forEach`, not `Array.forEach`.
6567
try self.forEach { _, element in
6668
result.append(try transform(element))
6769
return true
@@ -71,6 +73,8 @@ package final class SKDResponseArray: Sendable {
7173

7274
package func compactMap<T>(_ transform: (SKDResponseDictionary) throws -> T?) rethrows -> [T] {
7375
var result: [T] = []
76+
// swift-format-ignore: ReplaceForEachWithForLoop
77+
// Reference is to `SKDResponseArray.forEach`, not `Array.forEach`.
7478
try self.forEach { _, element in
7579
if let transformed = try transform(element) {
7680
result.append(transformed)

Sources/SourceKitD/sourcekitd_uids.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package import Csourcekitd
1616

17+
// swift-format-ignore: TypeNamesShouldBeCapitalized
18+
// Matching C style types
1719
package struct sourcekitd_api_keys {
1820
/// `key.version_major`
1921
package let versionMajor: sourcekitd_api_uid_t
@@ -776,6 +778,8 @@ package struct sourcekitd_api_keys {
776778
}
777779
}
778780

781+
// swift-format-ignore: TypeNamesShouldBeCapitalized
782+
// Matching C style types
779783
package struct sourcekitd_api_requests {
780784
/// `source.request.protocol_version`
781785
package let protocolVersion: sourcekitd_api_uid_t
@@ -947,6 +951,8 @@ package struct sourcekitd_api_requests {
947951
}
948952
}
949953

954+
// swift-format-ignore: TypeNamesShouldBeCapitalized
955+
// Matching C style types
950956
package struct sourcekitd_api_values {
951957
/// `source.lang.swift.decl.function.free`
952958
package let declFunctionFree: sourcekitd_api_uid_t

Sources/SourceKitLSP/Swift/CommentXML.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ private struct XMLToMarkdown {
6565

6666
// [XMLNode]? is the type of XMLNode.children.
6767
mutating func toMarkdown(_ nodes: [XMLNode]?, separator: String = "") {
68-
nodes?.forEach {
69-
toMarkdown($0)
68+
guard let nodes else {
69+
return
70+
}
71+
for node in nodes {
72+
toMarkdown(node)
7073
out += separator
7174
}
7275
}

Sources/SourceKitLSP/Swift/Diagnostic.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extension CodeAction {
2626
/// If this is from a note, the note's description should be passed as `fromNote`.
2727
init?(fixits: SKDResponseArray, in snapshot: DocumentSnapshot, fromNote: String?) {
2828
var edits: [TextEdit] = []
29+
// swift-format-ignore: ReplaceForEachWithForLoop
30+
// Reference is to `SKDResponseArray.forEach`, not `Array.forEach`.
2931
let editsMapped = fixits.forEach { (_, skfixit) -> Bool in
3032
if let edit = TextEdit(fixit: skfixit, in: snapshot) {
3133
edits.append(edit)
@@ -204,6 +206,8 @@ extension Diagnostic {
204206
range = Range(snapshot.positionOf(utf8Offset: utf8Offset))
205207
}
206208

209+
// swift-format-ignore: ReplaceForEachWithForLoop
210+
// Reference is to `SKDResponseArray.forEach`, not `Array.forEach`.
207211
// If the diagnostic has a range associated with it that starts at the same location as the diagnostics position, use it to retrieve a proper range for the diagnostic, instead of just reporting a zero-length range.
208212
(diag[keys.ranges] as SKDResponseArray?)?.forEach { index, skRange in
209213
guard let utf8Offset: Int = skRange[keys.offset],
@@ -273,6 +277,8 @@ extension Diagnostic {
273277
var notes: [DiagnosticRelatedInformation]? = nil
274278
if let sknotes: SKDResponseArray = diag[keys.diagnostics] {
275279
notes = []
280+
// swift-format-ignore: ReplaceForEachWithForLoop
281+
// Reference is to `SKDResponseArray.forEach`, not `Array.forEach`.
276282
sknotes.forEach { (_, sknote) -> Bool in
277283
guard
278284
let note = DiagnosticRelatedInformation(

Sources/SourceKitLSP/Swift/RefactoringResponse.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ extension [RefactoringEdit] {
2929

3030
self = []
3131

32+
// swift-format-ignore: ReplaceForEachWithForLoop
33+
// Reference is to `SKDResponseArray.forEach`, not `Array.forEach`.
3234
categorizedEdits.forEach { _, categorizedEdit in
3335
guard let edits: SKDResponseArray = categorizedEdit[keys.edits] else {
3436
logger.fault("edits doesn't exist in categorizedEdit dictionary")
3537
return true
3638
}
39+
// swift-format-ignore: ReplaceForEachWithForLoop
40+
// Reference is to `SKDResponseArray.forEach`, not `Array.forEach`.
3741
edits.forEach { _, edit in
3842
guard let startLine: Int = edit[keys.line],
3943
let startColumn: Int = edit[keys.column],

0 commit comments

Comments
 (0)