grammargem/Sources/GrammaGem/Grammar/GrammarEngine.swift
BigMarc f5faa27f88 GrammaGem — open-source, on-device macOS writing assistant
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>
2026-06-16 13:56:26 +02:00

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
}
}