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
158
159
160
161
162
163
164
165
|
package app
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// spellKind distinguishes the popup's action rows.
type spellKind int
const (
spellSuggest spellKind = iota // replace the word with value
spellAdd // add the word to the personal dictionary
spellIgnore // ignore the word for this session
)
// spellOption is one selectable row in the misspelled-word popup.
type spellOption struct {
label string
kind spellKind
value string // replacement word for spellSuggest
}
// spellPopup is the state of the active misspelled-word popup: the flagged word,
// its location, the choice list, and the cursor within it.
type spellPopup struct {
word string
row, start, end int
options []spellOption
sel int
}
// openSpellPopupAt opens the suggestion popup for a flagged word at (row, col),
// returning false (and doing nothing) when no misspelled word sits there.
func (a *App) openSpellPopupAt(row, col int) bool {
word, start, end, ok := a.editor.FlaggedWordAt(row, col)
if !ok {
return false
}
opts := make([]spellOption, 0, 7)
for _, s := range a.editor.Suggest(word, 5) {
opts = append(opts, spellOption{label: s, kind: spellSuggest, value: s})
}
opts = append(opts,
spellOption{label: "Add to dictionary", kind: spellAdd},
spellOption{label: "Ignore", kind: spellIgnore},
)
a.spell = spellPopup{word: word, row: row, start: start, end: end, options: opts}
a.mode = ModeSpell
a.status = ""
return true
}
// openSpellPopup triggers the popup for a flagged word at or next to the cursor
// (the Alt+; handler). It reports whether a popup opened.
func (a *App) openSpellPopup() bool {
if a.mode != ModeEditor {
return false
}
return a.openSpellPopupAt(a.editor.Cursor.Row, a.editor.Cursor.Col)
}
// handleSpellKey drives the popup: arrows/Tab move the selection, a number key
// jumps to and applies that row, Enter applies the selection, Esc dismisses.
func (a *App) handleSpellKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
n := len(a.spell.options)
switch msg.Type {
case tea.KeyEsc:
a.mode = ModeEditor
return a, nil
case tea.KeyUp, tea.KeyShiftTab:
a.spell.sel = (a.spell.sel - 1 + n) % n
return a, nil
case tea.KeyDown, tea.KeyTab:
a.spell.sel = (a.spell.sel + 1) % n
return a, nil
case tea.KeyEnter:
return a.applySpell(a.spell.sel)
case tea.KeyRunes:
if len(msg.Runes) == 1 {
switch r := msg.Runes[0]; {
case r >= '1' && r <= '9':
if i := int(r - '1'); i < n {
return a.applySpell(i)
}
case r == 'a' || r == 'A':
return a.applySpell(a.kindIndex(spellAdd))
case r == 'i' || r == 'I':
return a.applySpell(a.kindIndex(spellIgnore))
}
}
}
return a, nil
}
// kindIndex returns the option index of the first row of the given kind.
func (a *App) kindIndex(k spellKind) int {
for i, o := range a.spell.options {
if o.kind == k {
return i
}
}
return 0
}
// applySpell performs option i — replace, add, or ignore — then closes the popup.
func (a *App) applySpell(i int) (tea.Model, tea.Cmd) {
if i < 0 || i >= len(a.spell.options) {
a.mode = ModeEditor
return a, nil
}
opt := a.spell.options[i]
switch opt.kind {
case spellSuggest:
a.editor.ReplaceWordAt(a.spell.row, a.spell.start, a.spell.end, opt.value)
a.status = "Replaced with " + opt.value
case spellAdd:
if err := a.editor.AddToDictionary(a.spell.word); err != nil {
a.status = "Add to dictionary failed: " + err.Error()
} else {
a.status = "Added " + a.spell.word + " to dictionary"
}
case spellIgnore:
a.editor.IgnoreWord(a.spell.word)
a.status = "Ignored " + a.spell.word
}
a.mode = ModeEditor
return a, nil
}
// spellBar renders the popup as a themed full-width bottom bar: the misspelled
// word, then numbered choices with the current selection highlighted, and the
// add/ignore hints.
func (a *App) spellBar() string {
bar := lipgloss.NewStyle().
Foreground(a.theme.StatusFg).
Background(a.theme.StatusBg).
Width(maxInt(a.width, 1))
selStyle := lipgloss.NewStyle().Foreground(a.theme.SelFg).Background(a.theme.SelBg)
parts := make([]string, 0, len(a.spell.options)+1)
parts = append(parts, "“"+a.spell.word+"” →")
for i, o := range a.spell.options {
var label string
switch o.kind {
case spellSuggest:
label = fmt.Sprintf("%d %s", i+1, o.label)
case spellAdd:
label = "a Add"
case spellIgnore:
label = "i Ignore"
}
if i == a.spell.sel {
label = selStyle.Render(" " + label + " ")
} else {
label = " " + label + " "
}
parts = append(parts, label)
}
parts = append(parts, " Esc")
return bar.Render(" " + strings.Join(parts, " ") + " ")
}
|