Skip to content

Commit

Permalink
Fix SwiftLint violations
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Sep 10, 2024
1 parent 749b87e commit c1c68ba
Show file tree
Hide file tree
Showing 18 changed files with 198 additions and 56 deletions.
6 changes: 6 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
disabled_rules:
- multiple_closures_with_trailing_closure
- non_optional_string_data_conversion
- todo
line_length:
ignores_comments: true
2 changes: 1 addition & 1 deletion Maccy/FloatingPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class FloatingPanel<Content: View>: NSPanel, NSWindowDelegate {
newSize.height = min(newHeight, newSize.height)

var newOrigin = frame.origin
newOrigin.y = newOrigin.y + (frame.height - newSize.height)
newOrigin.y += (frame.height - newSize.height)

NSAnimationContext.runAnimationGroup { (context) in
context.duration = 0.2
Expand Down
2 changes: 1 addition & 1 deletion Maccy/Intents/AppIntentError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enum AppIntentError: Swift.Error, CustomLocalizedStringResourceConvertible {

var localizedStringResource: LocalizedStringResource {
switch self {
case .notFound: return "Clipboard item not found"
case .notFound: return "Clipboard item not found"
}
}
}
5 changes: 4 additions & 1 deletion Maccy/Intents/Get.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ struct Get: AppIntent, CustomIntentMigratedAppIntent {
static let intentClassName = "GetIntent"

static var title: LocalizedStringResource = "Get Item from Clipboard History"
static var description = IntentDescription("Gets an item from Maccy clipboard history. The returned item can be used to access its plain/rich/HTML text, image contents or file location.")
static var description = IntentDescription("""
Gets an item from Maccy clipboard history.
The returned item can be used to access its plain/rich/HTML text, image contents or file location.
""")

@Parameter(title: "Selected", default: true)
var selected: Bool
Expand Down
5 changes: 4 additions & 1 deletion Maccy/Intents/Select.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ struct Select: AppIntent, CustomIntentMigratedAppIntent {
static let intentClassName = "SelectIntent"

static var title: LocalizedStringResource = "Select Item in Clipboard History"
static var description = IntentDescription("Selects an item in Maccy clipboard history. Depending on Maccy settings, it might trigger pasting of the selected item.")
static var description = IntentDescription("""
Selects an item in Maccy clipboard history.
Depending on Maccy settings, it might trigger pasting of the selected item.
""")

static var parameterSummary: some ParameterSummary {
Summary("Select \(\.$number) Item in Clipboard History")
Expand Down
3 changes: 1 addition & 2 deletions Maccy/KeyChord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ enum KeyChord: CaseIterable {
)
}

init(_ key: Key, _ modifierFlags: NSEvent.ModifierFlags) {
init(_ key: Key, _ modifierFlags: NSEvent.ModifierFlags) { // swiftlint:disable:this cyclomatic_complexity
switch (key, modifierFlags) {
case (.delete, [.command, .option]):
self = .clearHistory
Expand Down Expand Up @@ -91,5 +91,4 @@ enum KeyChord: CaseIterable {
self = .unknown
}
}
// swiftlint:enable cyclomatic_complexity
}
7 changes: 4 additions & 3 deletions Maccy/KeyShortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import Sauce

struct KeyShortcut: Identifiable {
static func create(character: String) -> [KeyShortcut] {
let key = Key(character: character, virtualKeyCode: nil)
return [
KeyShortcut(key: Key(character: character, virtualKeyCode: nil)),
KeyShortcut(key: Key(character: character, virtualKeyCode: nil), modifierFlags: [.option]),
KeyShortcut(key: Key(character: character, virtualKeyCode: nil), modifierFlags: [Defaults[.pasteByDefault] ? .command : .option, .shift])
KeyShortcut(key: key),
KeyShortcut(key: key, modifierFlags: [.option]),
KeyShortcut(key: key, modifierFlags: [Defaults[.pasteByDefault] ? .command : .option, .shift])
]
}

Expand Down
4 changes: 2 additions & 2 deletions Maccy/Models/HistoryItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class HistoryItem {
let descriptor = FetchDescriptor<HistoryItem>(
predicate: #Predicate { $0.pin != nil }
)
let pins = try! Storage.shared.context.fetch(descriptor).compactMap({ $0.pin })
let assignedPins = Set(pins)
let pins = try? Storage.shared.context.fetch(descriptor).compactMap({ $0.pin })
let assignedPins = Set(pins ?? [])
return Array(supportedPins.subtracting(assignedPins))
}

Expand Down
5 changes: 3 additions & 2 deletions Maccy/Observables/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class AppState: Sendable {
}

var menuIconText: String {
var title = history.unpinnedItems.first?.text.shortened(to: 100).trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
var title = history.unpinnedItems.first?.text.shortened(to: 100)
.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
title.unicodeScalars.removeAll(where: CharacterSet.newlines.contains)
return title.shortened(to: 20)
}
Expand Down Expand Up @@ -131,7 +132,7 @@ class AppState: Sendable {
}

@MainActor
func openPreferences() {
func openPreferences() { // swiftlint:disable:this function_body_length
if settingsWindowController == nil {
settingsWindowController = SettingsWindowController(
panes: [
Expand Down
4 changes: 2 additions & 2 deletions Maccy/Observables/Footer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Footer {
confirmation: .init(
message: "clear_alert_message",
comment: "clear_alert_comment",
ok: "clear_alert_confirm",
confirm: "clear_alert_confirm",
cancel: "clear_alert_cancel"
),
suppressConfirmation: suppressClearAlert
Expand All @@ -53,7 +53,7 @@ class Footer {
confirmation: .init(
message: "clear_alert_message",
comment: "clear_alert_comment",
ok: "clear_alert_confirm",
confirm: "clear_alert_confirm",
cancel: "clear_alert_cancel"
),
suppressConfirmation: suppressClearAlert
Expand Down
2 changes: 1 addition & 1 deletion Maccy/Observables/FooterItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class FooterItem: Equatable, Identifiable {
struct Confirmation {
var message: LocalizedStringKey
var comment: LocalizedStringKey
var ok: LocalizedStringKey
var confirm: LocalizedStringKey
var cancel: LocalizedStringKey
}

Expand Down
4 changes: 2 additions & 2 deletions Maccy/Observables/History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Settings
import SwiftData

@Observable
class History {
class History { // swiftlint:disable:this type_body_length
static let shared = History()

var items: [HistoryItemDecorator] = []
Expand Down Expand Up @@ -132,7 +132,7 @@ class History {
item.contents = existingHistoryItem.contents
}
item.firstCopiedAt = existingHistoryItem.firstCopiedAt
item.numberOfCopies = item.numberOfCopies + existingHistoryItem.numberOfCopies
item.numberOfCopies += existingHistoryItem.numberOfCopies
item.pin = existingHistoryItem.pin
item.title = existingHistoryItem.title
if !item.fromMaccy {
Expand Down
3 changes: 1 addition & 2 deletions Maccy/PopupPosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ enum PopupPosition: String, CaseIterable, Identifiable, CustomStringConvertible,
}

var point = NSEvent.mouseLocation
point.y = point.y - size.height
point.y -= size.height
return point
}

}
2 changes: 2 additions & 0 deletions Maccy/Sorter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AppKit
import Defaults

// swiftlint:disable identifier_name
// swiftlint:disable type_name
class Sorter {
enum By: String, CaseIterable, Identifiable, CustomStringConvertible, Defaults.Serializable {
case lastCopiedAt
Expand Down Expand Up @@ -48,3 +49,4 @@ class Sorter {
}
}
// swiftlint:enable identifier_name
// swiftlint:enable type_name
6 changes: 5 additions & 1 deletion Maccy/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Storage {

init() {
let config = ModelConfiguration(url: URL.applicationSupportDirectory.appending(path: "Maccy/Storage.sqlite"))
container = try! ModelContainer(for: HistoryItem.self, configurations: config)
do {
container = try ModelContainer(for: HistoryItem.self, configurations: config)
} catch let error {
fatalError("Cannot load database: \(error.localizedDescription).")
}
}
}
2 changes: 1 addition & 1 deletion Maccy/Views/ConfirmationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct ConfirmationView<Content: View>: View {
}
.confirmationDialog(confirmation.message, isPresented: $item.showConfirmation) {
Text(confirmation.comment)
Button(confirmation.ok, role: .destructive) {
Button(confirmation.confirm, role: .destructive) {
item.action()
}
Button(confirmation.cancel, role: .cancel) {}
Expand Down
16 changes: 13 additions & 3 deletions MaccyTests/HistoryDecoratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ class HistoryItemDecoratorTests: XCTestCase {

func testHighlight() {
let itemDecorator = historyItemDecorator("foo bar baz")
itemDecorator.highlight("random", [range(from: 1, to: 2, in: itemDecorator), range(from: 8, to: 10, in: itemDecorator)])
itemDecorator.highlight("random", [
range(from: 1, to: 2, in: itemDecorator),
range(from: 8, to: 10, in: itemDecorator)
])
var expectedTitle = AttributedString("foo bar baz")
expectedTitle[expectedTitle.range(of: "oo")!].font = .bold(.body)()
expectedTitle[expectedTitle.range(of: "baz")!].font = .bold(.body)()
Expand All @@ -131,7 +134,10 @@ class HistoryItemDecoratorTests: XCTestCase {
XCTAssertEqual(itemDecorator.attributedTitle, nil)
}

private func historyItemDecorator(_ value: String?, application: String? = "com.apple.finder") -> HistoryItemDecorator {
private func historyItemDecorator(
_ value: String?,
application: String? = "com.apple.finder"
) -> HistoryItemDecorator {
let contents = [
HistoryItemContent(
type: NSPasteboard.PasteboardType.string.rawValue,
Expand All @@ -149,7 +155,10 @@ class HistoryItemDecoratorTests: XCTestCase {
return HistoryItemDecorator(item)
}

private func historyItemDecorator(_ value: Data?, _ type: NSPasteboard.PasteboardType) -> HistoryItemDecorator {
private func historyItemDecorator(
_ value: Data?,
_ type: NSPasteboard.PasteboardType
) -> HistoryItemDecorator {
let contents = [
HistoryItemContent(
type: type.rawValue,
Expand Down Expand Up @@ -210,6 +219,7 @@ class HistoryItemDecoratorTests: XCTestCase {
return HistoryItemDecorator(item)
}

// swiftlint:disable:next identifier_name
private func range(from: Int, to: Int, in item: HistoryItemDecorator) -> Range<String.Index> {
let startIndex = item.title.startIndex
let lowerBound = item.title.index(startIndex, offsetBy: from)
Expand Down
Loading

0 comments on commit c1c68ba

Please sign in to comment.