From d8aeeaffe67154d9dfb86df09e1a50a8ae7843d7 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Wed, 23 Apr 2025 18:40:56 +0200 Subject: [PATCH] Only pass key and not value to `LRUCache.removeAll(where:)` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The value isn’t needed here and the calls become cleaner if only the key is passed. --- Sources/SKUtilities/LRUCache.swift | 10 +++++----- .../SourceKitLSP/Swift/DiagnosticReportManager.swift | 4 ++-- Sources/SourceKitLSP/Swift/MacroExpansion.swift | 2 +- Sources/SourceKitLSP/Swift/SyntaxTreeManager.swift | 2 +- Tests/SKUtilitiesTests/LRUCacheTests.swift | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/SKUtilities/LRUCache.swift b/Sources/SKUtilities/LRUCache.swift index 3f835816..f20e809a 100644 --- a/Sources/SKUtilities/LRUCache.swift +++ b/Sources/SKUtilities/LRUCache.swift @@ -41,15 +41,15 @@ package struct LRUCache { /// A collection containing just the keys of the cache. /// /// - Note: Keys will **not** be in the same order that they were added to the cache. - package var keys: any Collection { cache.keys } + package var keys: some Collection { cache.keys } /// A collection containing just the values of the cache. /// /// - Note: Values will **not** be in the same order that they were added to the cache. - package var values: any Collection { cache.values } + package var values: some Collection { cache.values } package init(capacity: Int) { - assert(capacity > 0, "LRUCache capacity must be greater than 0") + precondition(capacity > 0, "LRUCache capacity must be greater than 0") self.capacity = capacity self.cache = Dictionary(minimumCapacity: capacity) self.priorities = Dictionary(minimumCapacity: capacity) @@ -101,9 +101,9 @@ package struct LRUCache { } /// Removes all the elements that satisfy the given predicate. - package mutating func removeAll(where shouldBeRemoved: (_: ((key: Key, value: Value)) throws -> Bool)) rethrows { + package mutating func removeAll(where shouldBeRemoved: (_ key: Key) throws -> Bool) rethrows { cache = try cache.filter { entry in - guard try shouldBeRemoved(entry) else { + guard try shouldBeRemoved(entry.key) else { return true } removePriority(forKey: entry.key) diff --git a/Sources/SourceKitLSP/Swift/DiagnosticReportManager.swift b/Sources/SourceKitLSP/Swift/DiagnosticReportManager.swift index 654dea14..0b928c06 100644 --- a/Sources/SourceKitLSP/Swift/DiagnosticReportManager.swift +++ b/Sources/SourceKitLSP/Swift/DiagnosticReportManager.swift @@ -95,7 +95,7 @@ actor DiagnosticReportManager { } func removeItemsFromCache(with uri: DocumentURI) async { - reportTaskCache.removeAll(where: { $0.key.snapshotID.uri == uri }) + reportTaskCache.removeAll(where: { $0.snapshotID.uri == uri }) } private func requestReport( @@ -193,7 +193,7 @@ actor DiagnosticReportManager { reportTask: ReportTask ) { // Remove any reportTasks for old versions of this document. - reportTaskCache.removeAll(where: { $0.key.snapshotID <= snapshotID }) + reportTaskCache.removeAll(where: { $0.snapshotID <= snapshotID }) reportTaskCache[CacheKey(snapshotID: snapshotID, buildSettings: buildSettings)] = reportTask } } diff --git a/Sources/SourceKitLSP/Swift/MacroExpansion.swift b/Sources/SourceKitLSP/Swift/MacroExpansion.swift index 416218d9..8cd170fa 100644 --- a/Sources/SourceKitLSP/Swift/MacroExpansion.swift +++ b/Sources/SourceKitLSP/Swift/MacroExpansion.swift @@ -121,7 +121,7 @@ actor MacroExpansionManager { /// Remove all cached macro expansions for the given primary file, eg. because the macro's plugin might have changed. func purge(primaryFile: DocumentURI) { cache.removeAll { - $0.key.snapshotID.uri.primaryFile ?? $0.key.snapshotID.uri == primaryFile + $0.snapshotID.uri.primaryFile ?? $0.snapshotID.uri == primaryFile } } } diff --git a/Sources/SourceKitLSP/Swift/SyntaxTreeManager.swift b/Sources/SourceKitLSP/Swift/SyntaxTreeManager.swift index bd08e459..567bdff0 100644 --- a/Sources/SourceKitLSP/Swift/SyntaxTreeManager.swift +++ b/Sources/SourceKitLSP/Swift/SyntaxTreeManager.swift @@ -45,7 +45,7 @@ actor SyntaxTreeManager { /// Set the task that computes the syntax tree for the given document snapshot. private func setComputation(for snapshotID: DocumentSnapshot.ID, computation: SyntaxTreeComputation) { // Remove any syntax trees for old versions of this document. - syntaxTreeComputations.removeAll(where: { key, value in key < snapshotID }) + syntaxTreeComputations.removeAll(where: { $0 < snapshotID }) syntaxTreeComputations[snapshotID] = computation } diff --git a/Tests/SKUtilitiesTests/LRUCacheTests.swift b/Tests/SKUtilitiesTests/LRUCacheTests.swift index d04931f7..7ce515be 100644 --- a/Tests/SKUtilitiesTests/LRUCacheTests.swift +++ b/Tests/SKUtilitiesTests/LRUCacheTests.swift @@ -81,7 +81,7 @@ final class LRUCacheTests: XCTestCase { } // Remove all even keys - lruCache.removeAll(where: { $0.key % 2 == 0 }) + lruCache.removeAll(where: { $0 % 2 == 0 }) assertLRUCacheKeys(lruCache, expectedKeys: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]) // Remove all key-value pairs