Files
Sprachmittler/docs/CODING_STANDARDS.md

55 lines
3.7 KiB
Markdown

# 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.async` in new code. Use `@MainActor` / `MainActor.run`.
- `NSPasteboard` access is `@MainActor` only. Accessibility calls run on a dedicated serial queue wrapped in an async function; `AXUIElement` values never cross task boundaries as mutable state.
- No `@unchecked Sendable` without 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 are `final class`, `struct` or `actor`.
- The composition root (`AppEnvironment` in the app, `CLIEnvironment` in 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.
- `public` only for API used across modules.
- Model names, tags, prompts and language names only in `SprachmittlerEngines` (lint rule `no_prompt_literals_outside_engines`).
- `NSPasteboard` and Accessibility APIs only in `SprachmittlerCapture` (lint rules).
- `URLSession` / `URLRequest` only in `SprachmittlerOllama` (lint rule).
## Errors and logging
- Throw `SprachmittlerError` across module boundaries. Internal errors may be module-specific, but they are mapped at the boundary.
- `Logger` per category (ARCHITECTURE §15). Never interpolate selected text, translations, pasteboard strings, app names or paths without `privacy: .private`; prefer counts, lengths, short hashes, timings and error kinds.
- `fatalError` / `preconditionFailure` only 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 `defer` where 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 (`MenuBarExtra` menu, `NSPanel`, Services, Accessibility).
- Views have no logic beyond formatting. They bind to `PanelViewModel`, `SettingsViewModel`, `OnboardingViewModel`.
- Every interactive control has an `accessibilityLabel` and 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.activate` or `makeKeyAndOrderFront`.
## 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.