mirror of
https://github.com/BigMarc/grammargem.git
synced 2026-10-13 17:01:55 +02:00
Native SwiftUI menu-bar app: global hotkey → capture selected text (AX API / clipboard fallback) → grammar (Harper) / local AI (MLX) → replace in place. Free/paid entitlement gate + Lemon Squeezy device-cap licensing, $0 marginal cost per user. Heavyweight engines behind protocol stubs so it builds out of the box. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
31 lines
1.2 KiB
Swift
31 lines
1.2 KiB
Swift
import Foundation
|
|
|
|
/// Layer 1 — deterministic, instant, offline grammar checking.
|
|
///
|
|
/// In production this is backed by **Harper** (Rust, Apache-2.0) via a C-FFI
|
|
/// static lib (see `harper-ffi/`) or a bundled `harper-ls` subprocess. The
|
|
/// protocol keeps the rest of the app independent of that integration.
|
|
protocol GrammarEngine: AnyObject {
|
|
/// Return suggestions over `text` (non-destructive; UI draws underlines).
|
|
func check(_ text: String) -> [Suggestion]
|
|
|
|
/// Apply every suggestion to produce a corrected string (used by the
|
|
/// "fix in place" hotkey path).
|
|
func correct(_ text: String) -> String
|
|
|
|
/// Words the engine should never flag (names, brand terms, jargon).
|
|
var ignoreList: Set<String> { get set }
|
|
}
|
|
|
|
extension GrammarEngine {
|
|
/// Default correction: apply suggestions right-to-left so offsets stay valid.
|
|
func correct(_ text: String) -> String {
|
|
let ns = NSMutableString(string: text)
|
|
for s in check(text).sorted(by: { $0.location > $1.location }) {
|
|
guard s.location + s.length <= ns.length else { continue }
|
|
ns.replaceCharacters(in: s.range, with: s.replacement)
|
|
}
|
|
return ns as String
|
|
}
|
|
}
|