-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspellcheck_commands.go
More file actions
332 lines (290 loc) · 9.66 KB
/
Copy pathspellcheck_commands.go
File metadata and controls
332 lines (290 loc) · 9.66 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"fmt"
"strconv"
"strings"
)
// HandleSpellCheckCommand processes spellcheck-related commands
func HandleSpellCheckCommand(args []string) bool {
// Get the SpellChecker instance
sc := GetSpellChecker()
if sc == nil {
fmt.Println("Failed to initialize spell checker")
return true
}
// Initialize if not already done
if !sc.isInitialized {
err := sc.Initialize()
if err != nil {
fmt.Printf("Error initializing spell checker: %v\n", err)
return true
}
}
// Handle commands
if len(args) == 0 {
// Show spellcheck status
showSpellCheckStatus(sc)
return true
}
// Handle special commands
if len(args) >= 1 {
cmd := args[0]
switch cmd {
case "status":
// Show status
showSpellCheckStatus(sc)
return true
case "enable":
// Enable spell checking
sc.EnableSpellChecker()
fmt.Println("Spell checking enabled")
return true
case "disable":
// Disable spell checking
sc.DisableSpellChecker()
fmt.Println("Spell checking disabled")
return true
case "config":
// Show or edit configuration
if len(args) < 2 {
showSpellCheckConfig(sc)
} else {
updateSpellCheckConfig(sc, args[1:])
}
return true
case "add":
// Add a word to the custom dictionary
if len(args) < 2 {
fmt.Println("Usage: :spellcheck add <word>")
return true
}
addToCustomDictionary(sc, args[1])
return true
case "remove":
// Remove a word from the custom dictionary
if len(args) < 2 {
fmt.Println("Usage: :spellcheck remove <word>")
return true
}
removeFromCustomDictionary(sc, args[1])
return true
case "test":
// Test spell checking on a command
if len(args) < 2 {
fmt.Println("Usage: :spellcheck test <command>")
return true
}
testSpellCheck(sc, args[1])
return true
case "help":
// Show help
showSpellCheckHelp()
return true
default:
fmt.Println("Unknown spell check command:", cmd)
fmt.Println("Type :spellcheck help for available commands")
return true
}
}
return true
}
// showSpellCheckStatus displays the current status of the spell checker
func showSpellCheckStatus(sc *SpellChecker) {
fmt.Println("Spell Checker Status")
fmt.Println("====================")
fmt.Printf("Enabled: %v\n", sc.IsEnabled())
fmt.Printf("Dictionary size: %d commands\n", len(sc.internalCommands))
fmt.Printf("Custom entries: %d words\n", len(sc.config.CustomDictionary))
fmt.Printf("Auto-correct: %v (threshold: %.2f)\n", sc.config.AutoCorrect, sc.config.AutoCorrectThreshold)
fmt.Printf("Suggestion threshold: %.2f\n", sc.config.SuggestionThreshold)
}
// showSpellCheckConfig displays the spell checker configuration
func showSpellCheckConfig(sc *SpellChecker) {
fmt.Println("Spell Checker Configuration")
fmt.Println("==========================")
fmt.Printf("Enabled: %v\n", sc.config.Enabled)
fmt.Printf("Suggestion threshold: %.2f\n", sc.config.SuggestionThreshold)
fmt.Printf("Max suggestions: %d\n", sc.config.MaxSuggestions)
fmt.Printf("Auto-correct: %v\n", sc.config.AutoCorrect)
fmt.Printf("Auto-correct threshold: %.2f\n", sc.config.AutoCorrectThreshold)
fmt.Printf("Case sensitive: %v\n", sc.config.CaseSensitive)
// Show custom dictionary
if len(sc.config.CustomDictionary) > 0 {
fmt.Println("\nCustom Dictionary:")
for i, word := range sc.config.CustomDictionary {
fmt.Printf(" %d. %s\n", i+1, word)
}
} else {
fmt.Println("\nCustom Dictionary: (empty)")
}
}
// updateSpellCheckConfig updates the spell checker configuration
func updateSpellCheckConfig(sc *SpellChecker, args []string) {
if len(args) < 1 {
fmt.Println("Usage: :spellcheck config <setting=value>")
fmt.Println("Available settings: enabled, threshold, max_suggestions, auto_correct, auto_threshold, case_sensitive")
return
}
// Process setting=value pairs
for _, arg := range args {
parts := strings.SplitN(arg, "=", 2)
if len(parts) != 2 {
fmt.Printf("Invalid setting format: %s (should be setting=value)\n", arg)
continue
}
setting := parts[0]
value := parts[1]
switch setting {
case "enabled":
if value == "true" || value == "1" || value == "yes" {
sc.config.Enabled = true
} else if value == "false" || value == "0" || value == "no" {
sc.config.Enabled = false
} else {
fmt.Printf("Invalid value for %s: %s (should be true or false)\n", setting, value)
continue
}
case "threshold":
threshold, err := strconv.ParseFloat(value, 64)
if err != nil || threshold < 0 || threshold > 1 {
fmt.Printf("Invalid value for %s: %s (should be between 0.0 and 1.0)\n", setting, value)
continue
}
sc.config.SuggestionThreshold = threshold
case "max_suggestions":
max, err := strconv.Atoi(value)
if err != nil || max < 1 {
fmt.Printf("Invalid value for %s: %s (should be a positive integer)\n", setting, value)
continue
}
sc.config.MaxSuggestions = max
case "auto_correct":
if value == "true" || value == "1" || value == "yes" {
sc.config.AutoCorrect = true
} else if value == "false" || value == "0" || value == "no" {
sc.config.AutoCorrect = false
} else {
fmt.Printf("Invalid value for %s: %s (should be true or false)\n", setting, value)
continue
}
case "auto_threshold":
threshold, err := strconv.ParseFloat(value, 64)
if err != nil || threshold < 0 || threshold > 1 {
fmt.Printf("Invalid value for %s: %s (should be between 0.0 and 1.0)\n", setting, value)
continue
}
sc.config.AutoCorrectThreshold = threshold
case "case_sensitive":
if value == "true" || value == "1" || value == "yes" {
sc.config.CaseSensitive = true
} else if value == "false" || value == "0" || value == "no" {
sc.config.CaseSensitive = false
} else {
fmt.Printf("Invalid value for %s: %s (should be true or false)\n", setting, value)
continue
}
default:
fmt.Printf("Unknown setting: %s\n", setting)
continue
}
fmt.Printf("Updated %s to %s\n", setting, value)
}
// Save the updated configuration
err := sc.UpdateConfig(sc.config)
if err != nil {
fmt.Printf("Error saving configuration: %v\n", err)
}
}
// addToCustomDictionary adds a word to the custom dictionary
func addToCustomDictionary(sc *SpellChecker, word string) {
// Check if word already exists in the dictionary
for _, existingWord := range sc.config.CustomDictionary {
if existingWord == word {
fmt.Printf("'%s' already exists in the custom dictionary\n", word)
return
}
}
// Add the word to the dictionary
sc.config.CustomDictionary = append(sc.config.CustomDictionary, word)
err := sc.UpdateConfig(sc.config)
if err != nil {
fmt.Printf("Error saving configuration: %v\n", err)
return
}
// Reinitialize commands to include the new word
sc.initializeCommands()
fmt.Printf("Added '%s' to the custom dictionary\n", word)
}
// removeFromCustomDictionary removes a word from the custom dictionary
func removeFromCustomDictionary(sc *SpellChecker, word string) {
// Check if word exists in the dictionary
found := false
var newDictionary []string
for _, existingWord := range sc.config.CustomDictionary {
if existingWord == word {
found = true
} else {
newDictionary = append(newDictionary, existingWord)
}
}
if !found {
fmt.Printf("'%s' does not exist in the custom dictionary\n", word)
return
}
// Update the dictionary
sc.config.CustomDictionary = newDictionary
err := sc.UpdateConfig(sc.config)
if err != nil {
fmt.Printf("Error saving configuration: %v\n", err)
return
}
// Reinitialize commands to exclude the removed word
sc.initializeCommands()
fmt.Printf("Removed '%s' from the custom dictionary\n", word)
}
// testSpellCheck tests the spell checker on a command
func testSpellCheck(sc *SpellChecker, command string) {
// Ensure the command has a colon prefix for internal commands
testCmd := command
if !strings.HasPrefix(testCmd, ":") {
testCmd = ":" + testCmd
}
// Check for suggestions
suggestions := sc.CheckCommand(testCmd)
if len(suggestions) == 0 {
fmt.Printf("No suggestions for '%s'\n", command)
return
}
// Display suggestions
fmt.Printf("Suggestions for '%s':\n", command)
for i, suggestion := range suggestions {
fmt.Printf(" %d. '%s' (similarity: %.2f)\n", i+1, suggestion.Command, suggestion.Score)
}
// Check for auto-correction
if sc.ShouldAutoCorrect(suggestions) {
fmt.Printf("Auto-correct would choose: '%s'\n", suggestions[0].Command)
}
}
// showSpellCheckHelp displays help for spell checker commands
func showSpellCheckHelp() {
fmt.Println("Spell Checker Commands")
fmt.Println("=====================")
fmt.Println(" :spellcheck - Show spell checker status")
fmt.Println(" :spellcheck status - Show spell checker status")
fmt.Println(" :spellcheck enable - Enable spell checking")
fmt.Println(" :spellcheck disable - Disable spell checking")
fmt.Println(" :spellcheck config - Show configuration")
fmt.Println(" :spellcheck config <setting=value> - Update configuration")
fmt.Println(" :spellcheck add <word> - Add word to custom dictionary")
fmt.Println(" :spellcheck remove <word> - Remove word from custom dictionary")
fmt.Println(" :spellcheck test <command> - Test spell checking on a command")
fmt.Println(" :spellcheck help - Show this help message")
fmt.Println()
fmt.Println("Available configuration settings:")
fmt.Println(" enabled - Enable/disable spell checking (true/false)")
fmt.Println(" threshold - Suggestion threshold (0.0-1.0)")
fmt.Println(" max_suggestions - Maximum number of suggestions to show")
fmt.Println(" auto_correct - Enable/disable auto-correction (true/false)")
fmt.Println(" auto_threshold - Auto-correction threshold (0.0-1.0)")
fmt.Println(" case_sensitive - Case sensitive matching (true/false)")
}