TableTower/ui/spellcheck.gd
RobinFiredrake f2cd982e43 Spellcheck in Build: red underlines, suggestions, spread dictionary
Titles, result text and details (card, inspector, Lore Entries) get red
wavy lines under misspelled words; right-click gives suggestions, Add to
dictionary (saved with the spread) and Ignore (this session). Uses the
Godot-Hunspell add-on's SpellChecker when it is installed; off on Android
and when the add-on is missing. English (en_US) dictionary included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:50:14 -06:00

104 lines
3 KiB
GDScript

class_name Spellcheck
extends RefCounted
## English spellchecking for Build's prose fields (titles, result text,
## details), through Hunspell (the Godot-Hunspell add-on's SpellChecker class
## in addons/hunspell). Words the spread accepts live in TowerBoard.dictionary;
## Ignore lasts for this session. Off on Android (its keyboards check spelling)
## and whenever the add-on isn't there; then fields simply get no underlines.
const AFF := "res://addons/hunspell/dictionaries/en_US.aff"
const DIC := "res://addons/hunspell/dictionaries/en_US.dic"
const MAX_SUGGESTIONS := 5
## The checker (spell(word) -> bool, suggest(word) -> PackedStringArray), or
## null. Tests set a stand-in with use_backend().
static var backend: Object = null
static var _tried := false
static var _cache: Dictionary = {}
## Lower-case words ignored until the app closes.
static var ignored: Dictionary = {}
static func available() -> bool:
if OS.get_name() == "Android":
return false
_load()
return backend != null
static func _load() -> void:
if _tried:
return
_tried = true
if not ClassDB.class_exists("SpellChecker"):
return
var checker: Object = ClassDB.instantiate("SpellChecker")
if checker and checker.call("load_dictionary", AFF, DIC):
backend = checker
## Uses `checker` instead of Hunspell (tests); null turns spellchecking off.
static func use_backend(checker: Object) -> void:
backend = checker
_tried = true
_cache.clear()
ignored.clear()
## True when `word` is spelled right, ignored, or in the spread's dictionary.
static func is_ok(word: String, board: TowerBoard) -> bool:
if backend == null or ignored.has(word.to_lower()) or in_dictionary(board, word):
return true
if not _cache.has(word):
_cache[word] = bool(backend.call("spell", word))
return _cache[word]
## The word ranges in `text` that are spelled wrong.
static func misspelled(text: String, board: TowerBoard) -> Array[Vector2i]:
var out: Array[Vector2i] = []
if backend == null:
return out
for r in SpellWords.ranges(text):
if not is_ok(text.substr(r.x, r.y - r.x), board):
out.append(r)
return out
static func suggestions(word: String) -> PackedStringArray:
if backend == null:
return PackedStringArray()
var all: PackedStringArray = backend.call("suggest", word)
return all.slice(0, MAX_SUGGESTIONS)
static func in_dictionary(board: TowerBoard, word: String) -> bool:
if board == null:
return false
var low := word.to_lower()
for w in board.dictionary:
if w.to_lower() == low:
return true
return false
## "Add to dictionary": the spread accepts the word from now on (an undo step).
static func add_to_dictionary(m: Main, word: String) -> void:
if in_dictionary(m.data, word):
return
m.undo_redo._begin_edit()
m.data.dictionary.append(word)
m.mark_dirty()
m.undo_redo._end_edit()
recheck_all(m)
## "Ignore": accepted until the app closes, in every spread.
static func ignore(m: Main, word: String) -> void:
ignored[word.to_lower()] = true
recheck_all(m)
static func recheck_all(m: Main) -> void:
if m.is_inside_tree():
m.get_tree().call_group(SpellUnderline.GROUP, "recheck")