-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
location.go
99 lines (85 loc) · 2.31 KB
/
location.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
package core
import (
"regexp"
"strings"
"github.com/errata-ai/vale/v3/internal/nlp"
)
// initialPosition calculates the position of a match (given by the location in
// the reference document, `loc`) in the source document (`ctx`).
func initialPosition(ctx, txt string, a Alert) (int, string) {
var idx int
var pat *regexp.Regexp
if a.Match == "" {
// We have nothing to look for -- assume the rule applies to the entire
// document (e.g., readability).
return 1, ""
}
offset := strings.Index(ctx, txt)
if offset >= 0 {
ctx, _ = Substitute(ctx, ctx[:offset], '@')
}
sub := strings.ToValidUTF8(a.Match, "")
pat = regexp.MustCompile(`(?:^|\b|_)` + regexp.QuoteMeta(sub) + `(?:_|\b|$)`)
fsi := pat.FindAllStringIndex(ctx, -1)
if len(fsi) == 0 {
idx = strings.Index(ctx, sub)
if idx < 0 {
// This should only happen if we're in a scope that contains inline
// markup (e.g., a sentence with code spans).
return guessLocation(ctx, txt, sub)
}
} else {
idx = fsi[0][0]
// NOTE: This is a workaround for #673.
//
// In cases where we have more than one match, we skip any that look
// like they're inside inline code (e.g., `code`).
//
// This is a bit of a hack: ideally, we'd handle this at the AST level
// by ignoring these inline code spans.
//
// TODO: What about `scope: raw`?
size := len(ctx)
for _, fs := range fsi {
start := fs[0] - 1
end := fs[1] + 1
if start > 0 && (ctx[start] == '`' || ctx[start] == '-') {
continue
} else if end < size && (ctx[end] == '`' || ctx[end] == '-') {
continue
}
idx = fs[0]
break
}
}
if strings.HasPrefix(ctx[idx:], "_") {
idx++ // We don't want to include the underscore boundary.
}
return nlp.StrLen(ctx[:idx]) + 1, sub
}
func guessLocation(ctx, sub, match string) (int, string) {
target := ""
for _, s := range nlp.SentenceTokenizer.Segment(sub) {
if s == match || strings.Index(s, match) > 0 {
target = s
}
}
if target == "" {
return -1, sub
}
tokens := nlp.WordTokenizer.Tokenize(target)
for _, text := range strings.Split(ctx, "\n") {
if allStringsInString(tokens, text) {
return strings.Index(ctx, text) + 1, text
}
}
return -1, sub
}
func allStringsInString(subs []string, s string) bool {
for _, sub := range subs {
if !strings.Contains(s, sub) {
return false
}
}
return true
}