Skip to content

Commit

Permalink
Remove sorted(_ keyPath:) extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Aug 15, 2024
1 parent e32a513 commit 058d89e
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 78 deletions.
2 changes: 1 addition & 1 deletion CotEditor/Sources/AboutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private struct CreditsView: View {

SectionView(String(localized: "Localization", table: "About", comment: "section heading")) {
Grid(alignment: .leadingFirstTextBaseline, verticalSpacing: 4) {
ForEach(self.credits.localization.sorted(\.key), id: \.key) { item in
ForEach(self.credits.localization.sorted(using: SortDescriptor(\.key)), id: \.key) { item in
GridRow {
Text(Locale.current.localizedString(forIdentifier: item.key)!)
.foregroundStyle(.secondary)
Expand Down
64 changes: 0 additions & 64 deletions CotEditor/Sources/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,6 @@ extension Sequence {



extension Sequence where Element: Equatable {

/// An array consists of unique elements of receiver by keeping ordering.
var uniqued: [Element] {

self.reduce(into: []) { (unique, element) in
guard !unique.contains(element) else { return }

unique.append(element)
}
}
}



extension Array where Element: Equatable {

/// Removes duplicated elements by keeping ordering.
mutating func unique() {

self = self.uniqued
}
}



extension Dictionary {

/// Returns a new dictionary containing the keys transformed by the given closure with the values of this dictionary.
Expand All @@ -125,16 +99,6 @@ extension Dictionary {
}


/// Returns a new dictionary containing the keys transformed by the given keyPath with the values of this dictionary.
///
/// - Parameter keyPath: The keyPath to the value to transform key. Every transformed key must be unique.
/// - Returns: A dictionary containing transformed keys and the values of this dictionary.
func mapKeys<T>(_ keyPath: KeyPath<Key, T>) -> [T: Value] {

self.mapKeys { $0[keyPath: keyPath] }
}


/// Returns a new dictionary containing the keys transformed by the given closure with the values of this dictionary.
///
/// - Parameter transform: A closure that transforms a key. Every transformed key must be unique.
Expand All @@ -158,31 +122,3 @@ extension Dictionary {
set { self[key.rawValue] = newValue }
}
}



// MARK: - Sort

extension Sequence {

/// Returns the elements of the sequence, sorted using the value that the given key path refers as the comparison between elements.
///
/// - Parameter keyPath: The key path to the value to compare.
/// - Returns: A sorted array of the sequence’s elements.
func sorted(_ keyPath: KeyPath<Element, some Comparable>) -> [Element] {

self.sorted { $0[keyPath: keyPath] < $1[keyPath: keyPath] }
}
}


extension MutableCollection where Self: RandomAccessCollection {

/// Sorts the collection in place, using the value that the given key path refers as the comparison between elements.
///
/// - Parameter keyPath: The key path to the value to compare.
mutating func sort(_ keyPath: KeyPath<Element, some Comparable>) {

self.sort { $0[keyPath: keyPath] < $1[keyPath: keyPath] }
}
}
2 changes: 1 addition & 1 deletion CotEditor/Sources/CommandBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct CommandBarView: View {
guard let result = $0.match(command: newValue) else { return nil }
return Candidate(command: $0, matches: result.result, score: result.score)
}
.sorted(\.score)
.sorted(using: SortDescriptor(\.score))
self.selection = self.candidates.first?.id

// post a VoiceOver announcement
Expand Down
3 changes: 2 additions & 1 deletion CotEditor/Sources/KeyBindingManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ import URLUtils
} else {
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
let data = try encoder.encode(self.userKeyBindings.sorted(\.action.description))
let descriptions = self.userKeyBindings.sorted(using: SortDescriptor(\.action.description))
let data = try encoder.encode(descriptions)
let fileURL = self.settingFileURL

try FileManager.default.createIntermediateDirectories(to: fileURL)
Expand Down
2 changes: 1 addition & 1 deletion CotEditor/Sources/NSLayoutManager+SyntaxHighlight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extension NSLayoutManager {
/// - range: The range to update syntax highlight.
@MainActor final func apply(highlights: [Highlight], theme: Theme?, in range: NSRange) {

assert(highlights.sorted(\.range.location) == highlights)
assert(highlights.sorted(using: SortDescriptor(\.range.location)) == highlights)

// skip if never colorized yet to avoid heavy `self.invalidateDisplay(forCharacterRange:)`
guard !highlights.isEmpty || self.hasTemporaryAttribute(.syntaxType, in: range) else { return }
Expand Down
2 changes: 1 addition & 1 deletion CotEditor/Sources/SyntaxParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ extension SyntaxParser {
}

return try await group.reduce(into: []) { $0 += $1 }
.sorted(\.range.location)
.sorted(using: SortDescriptor(\.range.location))
}

await MainActor.run {
Expand Down
8 changes: 0 additions & 8 deletions Tests/ComparableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ struct ComparableTests {
Item(id: 4, bool: true),
]

#expect(items.sorted(\.bool) == [
Item(id: 1, bool: true),
Item(id: 2, bool: true),
Item(id: 4, bool: true),
Item(id: 0, bool: false),
Item(id: 3, bool: false),
])

#expect(items.sorted(using: [KeyPathComparator(\.bool)]) == [
Item(id: 1, bool: true),
Item(id: 2, bool: true),
Expand Down
2 changes: 1 addition & 1 deletion Tests/SyntaxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ actor SyntaxTests {

let outlineItems = try syntax.outlineExtractors
.flatMap { try $0.items(in: source, range: source.nsRange) }
.sorted(\.range.location)
.sorted(using: SortDescriptor(\.range.location))


#expect(outlineItems.count == 3)
Expand Down

0 comments on commit 058d89e

Please sign in to comment.