-
Notifications
You must be signed in to change notification settings - Fork 7
/
spell_lib.go
152 lines (133 loc) · 3.24 KB
/
spell_lib.go
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
// +build spell
package main
import (
"fmt"
"regexp"
"strings"
)
type Spellcheck struct {
speller Speller
toks map[string]struct{}
check CheckFunc
strips []*regexp.Regexp
}
// isCodeRE matches assignment, function calls, imports, and braces.
var isCodeRE *regexp.Regexp = regexp.MustCompile(
"(:=|[a-zA-Z1-9]\\(|{|}|[a-z]/[a-z]|\\[)")
func NewSpellcheck(ts TokenSet, ignoreFile string) (sc *Spellcheck, err error) {
igns, err := WithPassIgnores(ignoreFile)
if err != nil {
return nil, err
}
strips := []*regexp.Regexp{
regexp.MustCompile("http(s|):[^ ]*"),
regexp.MustCompile("TODO[ ]*\\([a-z]*"),
}
hsp := NewHunSpeller()
if hsp == nil {
return nil, fmt.Errorf("bad hunspell")
}
asp, err := NewASpeller()
if err != nil {
return nil, err
}
sc = &Spellcheck{
strips: strips,
toks: ts,
speller: NewMultiSpeller(asp, hsp),
}
checks := []CheckFunc{
sc.WithPassTokens(),
igns,
WithPassNumbers(),
sc.WithSpeller(),
}
sc.check = func(w string) bool {
for _, chk := range checks {
if chk(w) {
return true
}
}
return false
}
return sc, nil
}
func (sc *Spellcheck) Close() { sc.speller.Close() }
func (sc *Spellcheck) WithPassTokens() CheckFunc {
return func(word string) bool {
_, ok := sc.toks[word]
return ok
}
}
func (sc *Spellcheck) WithSpeller() CheckFunc {
return func(word string) bool { return sc.speller.Check(word) }
}
func (sc *Spellcheck) Check() CheckPipe {
return func(lc <-chan *Lexeme, outc chan<- *CheckedLexeme) {
sc.checkComments(lc, outc)
}
}
func (sc *Spellcheck) checkComments(lc <-chan *Lexeme, outc chan<- *CheckedLexeme) {
ch := Filter(lc, CommentFilter)
for comm := range ch {
if isCodeComment(comm.lit) {
continue
}
if ct := sc.checkLexeme(comm); ct != nil {
outc <- ct
}
}
}
// isCodeComment finds comments that contain example code as indented blocks:
// abc, def := indented.with(spaceTab)
// a, b := tabOnly()
// if xyz != nil { panic(indentWithSpace) }
// Since it's an example, sometimes the identifiers aren't in the
// code, which will cause a spell check false positive.
func isCodeComment(s string) bool {
hasPfx := false
for _, pfx := range []string{"// ", "// \t", "//\t"} {
if hasPfx = strings.HasPrefix(s, pfx); hasPfx {
break
}
}
// Check if line seems to be code to avoid false negatives on
// indented comments like lists.
return hasPfx && isCodeRE.MatchString(s)
}
func (sc *Spellcheck) suggest(word string) string {
if sc.check(word) {
return ""
}
if sugg := sc.speller.Suggest(word); len(sugg) > 0 {
return sugg[0]
}
return ""
}
func (sc *Spellcheck) tokenize(s string) []string {
for _, re := range sc.strips {
s = string(re.ReplaceAll([]byte(s), []byte("")))
}
x := []string{
".", "`", "\"", ",", "!", "?",
";", ")", "(", "/", ":", "=",
"*", "-", ">", "]", "[",
"|", "{", "}", "+", "\t", "' ",
" '", "&", "<", "'s "}
for _, v := range x {
s = strings.Replace(s, v, " ", -1)
}
return strings.Fields(s)
}
func (sc *Spellcheck) checkLexeme(ct *Lexeme) (ret *CheckedLexeme) {
for _, word := range sc.tokenize(ct.lit) {
if sc.check(word) {
continue
}
if ret == nil {
ret = &CheckedLexeme{ct, "spell", nil}
}
ret.words = append(ret.words, CheckedWord{word, sc.suggest(word)})
}
return ret
}