/
manual.go
259 lines (242 loc) · 6.42 KB
/
manual.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
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
// Copyright (c) 2018, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lex
import (
"fmt"
"strings"
"unicode"
"cogentcore.org/core/pi/token"
)
// these functions provide "manual" lexing support for specific cases
// such as completion where a string must be processed further.
// FirstWord returns the first contiguous sequence of purely unicode.IsLetter runes
// within given string -- skips over any leading non-letters until a letter is found.
// This does not include numbers -- use FirstWordDigits for that
func FirstWord(str string) string {
rstr := ""
for _, s := range str {
if !IsLetter(s) {
if len(rstr) == 0 {
continue
}
break
}
rstr += string(s)
}
return rstr
}
// FirstWordDigits returns the first contiguous sequence of purely IsLetterOrDigit runes
// within given string -- skips over any leading non-letters until a *letter* (not digit) is found.
func FirstWordDigits(str string) string {
rstr := ""
for _, s := range str {
if !IsLetterOrDigit(s) {
if len(rstr) == 0 {
continue
}
break
}
if len(rstr) == 0 && IsDigit(s) { // can't start with digit
continue
}
rstr += string(s)
}
return rstr
}
// FirstWordApostrophe returns the first contiguous sequence of purely unicode.IsLetter runes.
// that can also contain an apostrophe *within* the word but not at the end
func FirstWordApostrophe(str string) string {
rstr := ""
for _, s := range str {
if !(IsLetter(s) || s == '\'') {
if len(rstr) == 0 {
continue
}
break
}
if len(rstr) == 0 && s == '\'' { // can't start with '
continue
}
rstr += string(s)
}
rstr = strings.TrimRight(rstr, "'") // get rid of any trailing ones!
return rstr
}
// TrimLeftToAlpha returns string without any leading non-alpha runes
func TrimLeftToAlpha(nm string) string {
return strings.TrimLeftFunc(nm, func(r rune) bool {
return !unicode.IsLetter(r)
})
}
// FirstNonSpaceRune returns the index of first non-space rune, -1 if not found
func FirstNonSpaceRune(src []rune) int {
for i, s := range src {
if !unicode.IsSpace(s) {
return i
}
}
return -1
}
// LastNonSpaceRune returns the index of last non-space rune, -1 if not found
func LastNonSpaceRune(src []rune) int {
sz := len(src)
if sz == 0 {
return -1
}
for i := sz - 1; i >= 0; i-- {
s := src[i]
if !unicode.IsSpace(s) {
return i
}
}
return -1
}
// InnerBracketScope returns the inner-scope for given bracket type
// if it is imbalanced -- it is important to do completion based
// just on that inner scope if that is where the user is at.
func InnerBracketScope(str string, brl, brr string) string {
nlb := strings.Count(str, brl)
nrb := strings.Count(str, brr)
if nlb == nrb {
return str
}
if nlb > nrb {
li := strings.LastIndex(str, brl)
if li == len(str)-1 {
return InnerBracketScope(str[:li], brl, brr) // get rid of open ending and try again
}
str = str[li+1:]
ri := strings.Index(str, brr)
if ri < 0 {
return str
}
return str[:ri]
}
// nrb > nlb -- we're missing the left guys -- go to first rb
ri := strings.Index(str, brr)
if ri == 0 {
return InnerBracketScope(str[1:], brl, brr) // get rid of opening and try again
}
str = str[:ri]
li := strings.Index(str, brl)
if li < 0 {
return str
}
return str[li+1:]
}
// LastField returns the last white-space separated string
func LastField(str string) string {
if str == "" {
return ""
}
flds := strings.Fields(str)
return flds[len(flds)-1]
}
// ObjPathAt returns the starting Lex, before given lex,
// that include sequences of PunctSepPeriod and NameTag
// which are used for object paths (e.g., field.field.field)
func ObjPathAt(line Line, lx *Lex) *Lex {
stlx := lx
if lx.St > 1 {
_, lxidx := line.AtPos(lx.St - 1)
for i := lxidx; i >= 0; i-- {
clx := &line[i]
if clx.Tok.Tok == token.PunctSepPeriod || clx.Tok.Tok.InCat(token.Name) {
stlx = clx
} else {
break
}
}
}
return stlx
}
// LastScopedString returns the last white-space separated, and bracket
// enclosed string from given string.
func LastScopedString(str string) string {
str = LastField(str)
bstr := str
str = InnerBracketScope(str, "{", "}")
str = InnerBracketScope(str, "(", ")")
str = InnerBracketScope(str, "[", "]")
if str == "" {
return bstr
}
str = TrimLeftToAlpha(str)
if str == "" {
str = TrimLeftToAlpha(bstr)
if str == "" {
return bstr
}
}
flds := strings.Split(str, ",")
return flds[len(flds)-1]
}
// HasUpperCase returns true if string has an upper-case letter
func HasUpperCase(str string) bool {
for _, r := range str {
if unicode.IsUpper(r) {
return true
}
}
return false
}
// MatchCase uses the source string case (upper / lower) to set corresponding
// case in target string, returning that string.
func MatchCase(src, trg string) string {
rsc := []rune(src)
rtg := []rune(trg)
mx := min(len(rsc), len(rtg))
for i := 0; i < mx; i++ {
t := rtg[i]
if unicode.IsUpper(rsc[i]) {
if !unicode.IsUpper(t) {
rtg[i] = unicode.ToUpper(t)
}
} else {
if !unicode.IsLower(t) {
rtg[i] = unicode.ToLower(t)
}
}
}
return string(rtg)
}
// MarkupPathsAsLinks checks for strings that look like file paths / urls and returns
// the original fields as a byte slice along with a marked-up version of that
// with html link markups for the files (as <a href="file:///...").
// Input is field-parsed already, and maxFlds is the maximum number of fields
// to look for file paths in (e.g., 2 is a reasonable default, to avoid getting
// other false-alarm info later in the text).
// This is mainly used for marking up output from commands, for example.
func MarkupPathsAsLinks(flds []string, maxFlds int) (orig, link []byte) {
mx := min(len(flds), maxFlds)
for i := 0; i < mx; i++ {
ff := flds[i]
if !strings.HasPrefix(ff, "./") && !strings.HasPrefix(ff, "/") && !strings.HasPrefix(ff, "../") {
continue
}
fnflds := strings.Split(ff, ":")
fn := string(fnflds[0])
pos := ""
col := ""
if len(fnflds) > 1 {
pos = string(fnflds[1])
col = ""
if len(fnflds) > 2 {
col = string(fnflds[2])
}
}
lstr := ""
if col != "" {
lstr = fmt.Sprintf(`<a href="file:///%v#L%vC%v">%v</a>`, fn, pos, col, string(ff))
} else if pos != "" {
lstr = fmt.Sprintf(`<a href="file:///%v#L%v">%v</a>`, fn, pos, string(ff))
} else {
lstr = fmt.Sprintf(`<a href="file:///%v">%v</a>`, fn, string(ff))
}
orig = []byte(ff)
link = []byte(lstr)
break
}
return
}