3.7 KiB
3.7 KiB
Coding standards
Language and concurrency
- Swift 6 language mode, complete strict concurrency. Zero warnings policy: warnings are errors in Release.
- Model shared mutable state as an
actor(TranslationSession,SettingsStore). UI-facing types are@MainActor. - No
DispatchQueue.main.asyncin new code. Use@MainActor/MainActor.run. NSPasteboardaccess is@MainActoronly. Accessibility calls run on a dedicated serial queue wrapped in an async function;AXUIElementvalues never cross task boundaries as mutable state.- No
@unchecked Sendablewithout a comment explaining the invariant. - Never block an actor or the main actor on networking, Accessibility calls or pasteboard snapshots. Every network call is async and cancellable.
- Respect cancellation:
try Task.checkCancellation()in chunk loops, validation and insertion.
Structure
- One primary type per file. The file name matches the type.
- Protocols at module boundaries (
OllamaCatalog,OllamaChatting,TranslationAdapter,PasteboardWatching,TextInserting,ClipboardSnapshotting,FileSystem). Concrete types arefinal class,structoractor. - The composition root (
AppEnvironmentin the app,CLIEnvironmentin the CLI) is the only place that instantiates concrete implementations. - No singletons except Apple-provided ones (
FileManager.default,NSPasteboard.general,NSWorkspace.shared); wrap them behind protocols where tests need them. publiconly for API used across modules.- Model names, tags, prompts and language names only in
SprachmittlerEngines(lint ruleno_prompt_literals_outside_engines). NSPasteboardand Accessibility APIs only inSprachmittlerCapture(lint rules).URLSession/URLRequestonly inSprachmittlerOllama(lint rule).
Errors and logging
- Throw
SprachmittlerErroracross module boundaries. Internal errors may be module-specific, but they are mapped at the boundary. Loggerper category (ARCHITECTURE §15). Never interpolate selected text, translations, pasteboard strings, app names or paths withoutprivacy: .private; prefer counts, lengths, short hashes, timings and error kinds.fatalError/preconditionFailureonly for programmer errors that tests would catch.
Files and persistence
- The app writes only settings (
UserDefaults) and, at release time, the app bundle. It does not write source text or translations anywhere. - Clipboard snapshots live in memory for one replacement. Restore them with
deferwhere possible. - If a future feature needs a file, all writes are atomic: temporary file in the same directory, then
replaceItemAt/ rename. That feature needs an ADR.
UI
- SwiftUI for views; AppKit only where SwiftUI lacks support (
MenuBarExtramenu,NSPanel, Services, Accessibility). - Views have no logic beyond formatting. They bind to
PanelViewModel,SettingsViewModel,OnboardingViewModel. - Every interactive control has an
accessibilityLabeland a keyboard path. - Strings:
String(localized:)from the start. English is the base, German (de) follows in M8. - Never show raw model text as an error message; show
userMessage. - The translation panel never calls
NSApp.activateormakeKeyAndOrderFront.
Style
- SwiftFormat default rules with 4-space indent and 120-column limit (
.swiftformat). - SwiftLint with
.swiftlint.yml. Warnings are fixed, not suppressed. A disable requires a reason comment. - Doc comments (
///) on all public API.
Git
- Small commits; conventional commit messages, e.g.
feat(engines): Hy-MT3 request encoding (T2.2). - Never commit
Sprachmittler.xcodeproj,.build/,DerivedData/,evaluation/corpus/,evaluation/results/*or any private text.