1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
package editor
import (
"regexp"
"strings"
"glint/internal/spell"
)
// urlRe matches URLs and bare emails so their letters aren't spellchecked.
var urlRe = regexp.MustCompile(`(?:https?://|www\.|mailto:)\S+|\S+@\S+\.\S+`)
// wordRe matches a candidate word: a letter followed by letters or apostrophes
// (straight or curly), so possessives ride along with their base.
var wordRe = regexp.MustCompile(`\p{L}[\p{L}'’]*`)
// SetDict gives the editor a loaded dictionary; spellcheck is inert until one is
// set. SetSpell toggles the user preference.
func (e *Editor) SetDict(d *spell.Dict) {
e.dict = d
e.spellCache = map[string]bool{}
}
// SetSpell sets whether spellcheck is enabled for the session.
func (e *Editor) SetSpell(on bool) { e.spellOn = on }
// ToggleSpell flips spellcheck and returns the new state.
func (e *Editor) ToggleSpell() bool { e.spellOn = !e.spellOn; return e.spellOn }
// SpellEnabled reports the user's session toggle (independent of whether a dict
// is loaded or the file is code).
func (e *Editor) SpellEnabled() bool { return e.spellOn }
// spellActive reports whether spellcheck should actually run: enabled, a
// dictionary is loaded, and the buffer is prose (not a recognized code file,
// which reuses the TASK-018 extension routing via codeFile).
func (e *Editor) spellActive() bool {
return e.spellOn && e.dict != nil && e.codeFile == ""
}
// AddToDictionary adds the word to the personal dictionary and invalidates the
// spell cache so its underline clears on the next render.
func (e *Editor) AddToDictionary(word string) error {
if e.dict == nil {
return nil
}
err := e.dict.Add(word)
e.spellCache = map[string]bool{} // membership changed; drop cached verdicts
return err
}
// spellKnown is a cached membership test; the cache is dropped when the personal
// dictionary changes.
func (e *Editor) spellKnown(word string) bool {
lw := strings.ToLower(word)
if v, ok := e.spellCache[lw]; ok {
return v
}
k := e.dict.Known(lw)
if e.spellCache == nil {
e.spellCache = map[string]bool{}
}
e.spellCache[lw] = k
return k
}
// spellPass splits every prose span into words and re-emits misspelled ones as
// Wavy (undercurl) spans, leaving all other spans untouched. The per-line span
// text is unchanged, so the markup-visible invariant holds.
func (e *Editor) spellPass(all [][]Span) [][]Span {
for li, spans := range all {
var out []Span
changed := false
for _, sp := range spans {
if !sp.Prose {
out = append(out, sp)
continue
}
split := e.splitProse(sp)
if len(split) != 1 {
changed = true
}
out = append(out, split...)
}
if changed {
all[li] = out
}
}
return all
}
// splitProse partitions one prose span's text, marking misspelled words with the
// theme's undercurl. Words inside URLs/emails, acronyms, camel/brand casing, and
// words under three letters are left alone. When nothing is misspelled the
// original span is returned unchanged.
func (e *Editor) splitProse(sp Span) []Span {
text := sp.Text
skips := urlRe.FindAllStringIndex(text, -1)
var out []Span
last := 0
emit := func(s string, wavy bool) {
if s == "" {
return
}
ns := sp
ns.Text = s
ns.Wavy = wavy
if wavy {
ns.UnderColor = e.theme.Spell
}
out = append(out, ns)
}
for _, w := range wordRe.FindAllStringIndex(text, -1) {
word := text[w[0]:w[1]]
if overlapsAny(w, skips) || !checkableWord(word) || e.spellKnown(word) {
continue
}
emit(text[last:w[0]], false)
emit(word, true)
last = w[1]
}
if last == 0 {
return []Span{sp} // nothing flagged
}
emit(text[last:], false)
return out
}
// overlapsAny reports whether byte range r overlaps any range in rs.
func overlapsAny(r []int, rs [][]int) bool {
for _, s := range rs {
if r[0] < s[1] && s[0] < r[1] {
return true
}
}
return false
}
// checkableWord filters out tokens that aren't ordinary prose words: anything
// under three letters, all-caps acronyms (NASA, API), and internal-capital
// brand/camelCase (iOS, GitHub). Only the first letter may be uppercase.
func checkableWord(w string) bool {
r := []rune(w)
if len(r) < 3 {
return false
}
allUpper := true
for i, c := range r {
if c >= 'a' && c <= 'z' || c == '\'' || c == '’' {
allUpper = false
}
if i > 0 && c >= 'A' && c <= 'Z' {
return false // internal uppercase: brand / camelCase
}
}
return !allUpper
}
|