▍ humdrum codex / glint v1.1.2
license AGPL-3.0
8.8 KB raw
  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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package app

import (
	"fmt"
	"sort"
	"strings"

	"glint/internal/grammar"

	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
	spellToggle                    // turn spellcheck on/off for the session
	grammarToggle                  // turn Harper grammar checking on/off (TASK-043)
	grammarFix                     // apply a Harper replacement (edits) (TASK-044)
	grammarIgnore                  // ignore this grammar lint for the session (TASK-044)
)

// spellOption is one selectable row in the misspelled-word popup.
type spellOption struct {
	label string
	kind  spellKind
	value string             // replacement word for spellSuggest
	edits []grammar.TextEdit // buffer edits for grammarFix (Harper's replacement)
}

// spellPopup is the state of the active proofing popup: the flagged word (or grammar
// span text), its location, the grammar rule code (for grammarIgnore), the choice
// list, and the cursor within it.
type spellPopup struct {
	word            string
	code            string // Harper rule code, set for grammar popups
	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 {
		// Spelling wins on overlap; with no misspelling here, try a grammar span.
		return a.openGrammarPopupAt(row, col)
	}
	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},
		spellOption{label: "Toggle spellcheck", kind: spellToggle},
		spellOption{label: "Toggle grammar", kind: grammarToggle},
	)
	a.spell = spellPopup{word: word, row: row, start: start, end: end, options: opts}
	a.mode = ModeSpell
	a.status = ""
	return true
}

// openGrammarPopupAt opens the proofing popup on a grammar span at (row, col): it
// asks harper for the fixes on that span and lists them as replacement rows, followed
// by Ignore and the grammar toggle. Returns false when grammar is off or no grammar
// span sits there. Fixes may be empty (harper offered none, or the request timed out)
// — the popup still opens so the span can be ignored.
func (a *App) openGrammarPopupAt(row, col int) bool {
	if a.grammar == nil {
		return false
	}
	start, end, ok := a.editor.GrammarSpanAt(row, col)
	if !ok {
		return false
	}
	text := a.editor.RuneRangeText(row, start, end)
	actions, _ := a.grammar.CodeActions(a.grammarPath, row, start, end)
	opts := make([]spellOption, 0, len(actions)+2)
	for _, ac := range actions {
		opts = append(opts, spellOption{label: ac.Title, kind: grammarFix, edits: ac.Edits})
	}
	opts = append(opts,
		spellOption{label: "Ignore", kind: grammarIgnore},
		spellOption{label: "Toggle grammar", kind: grammarToggle},
	)
	a.spell = spellPopup{
		word:    text,
		code:    a.grammarCodeAt(row, start, end),
		row:     row,
		start:   start,
		end:     end,
		options: opts,
	}
	a.mode = ModeSpell
	a.status = ""
	return true
}

// grammarCodeAt returns the Harper rule code of the retained diagnostic matching the
// span (row, [start,end)), or "" if none — used to key the session ignore.
func (a *App) grammarCodeAt(row, start, end int) string {
	for _, d := range a.lastGrammarDiags {
		if d.Line == row && d.StartCol == start && d.EndCol == end {
			return d.Code
		}
	}
	return ""
}

// openSpellPopup is the Alt+; handler: it opens the full popup on a flagged word
// at the cursor, or, with no misspelling there, a minimal popup offering just the
// session toggle (so spellcheck can always be turned back on even when no
// underlines are visible). It always opens something, so it never reports false.
func (a *App) openSpellPopup() bool {
	if a.mode != ModeEditor {
		return false
	}
	if a.openSpellPopupAt(a.editor.Cursor.Row, a.editor.Cursor.Col) {
		return true
	}
	a.spell = spellPopup{options: []spellOption{
		{label: "Toggle spellcheck", kind: spellToggle},
		{label: "Toggle grammar", kind: grammarToggle},
	}}
	a.mode = ModeSpell
	a.status = ""
	return true
}

// 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':
				if idx := a.kindIndex(spellIgnore); idx >= 0 {
					return a.applySpell(idx)
				}
				return a.applySpell(a.kindIndex(grammarIgnore))
			case r == 't' || r == 'T':
				return a.applySpell(a.kindIndex(spellToggle))
			case r == 'g' || r == 'G':
				return a.applySpell(a.kindIndex(grammarToggle))
			}
		}
	}
	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 -1
}

// 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
	case spellToggle:
		if on := a.editor.ToggleSpell(); on {
			a.status = "Spellcheck on"
		} else {
			a.status = "Spellcheck off"
		}
	case grammarFix:
		a.applyGrammarFix(opt.edits)
		a.status = "Applied: " + opt.label
	case grammarIgnore:
		a.ignoreGrammar(a.spell.code, a.spell.word)
		a.status = "Ignored grammar hint"
	case grammarToggle:
		cmd := a.toggleGrammar()
		a.mode = ModeEditor
		return a, cmd
	}
	a.mode = ModeEditor
	return a, nil
}

// applyGrammarFix applies Harper's replacement edits to the buffer. Edits are applied
// last-first (by document position) so earlier edits don't shift later ranges.
func (a *App) applyGrammarFix(edits []grammar.TextEdit) {
	sorted := make([]grammar.TextEdit, len(edits))
	copy(sorted, edits)
	sort.Slice(sorted, func(i, j int) bool {
		if sorted[i].StartLine != sorted[j].StartLine {
			return sorted[i].StartLine > sorted[j].StartLine
		}
		return sorted[i].StartCol > sorted[j].StartCol
	})
	for _, e := range sorted {
		a.editor.ReplaceRuneRange(e.StartLine, e.StartCol, e.EndLine, e.EndCol, e.NewText)
	}
}

// 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)
	if a.spell.word != "" {
		parts = append(parts, "“"+a.spell.word+"” →")
	} else {
		parts = append(parts, "Spellcheck:")
	}
	for i, o := range a.spell.options {
		var label string
		switch o.kind {
		case spellSuggest, grammarFix:
			label = fmt.Sprintf("%d %s", i+1, o.label)
		case spellAdd:
			label = "a Add"
		case spellIgnore, grammarIgnore:
			label = "i Ignore"
		case spellToggle:
			label = "t Spell"
		case grammarToggle:
			label = "g Grammar"
		}
		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, " ") + " ")
}