Skip to content

Commit b992291

Browse files
committed
gopls/internal/template: unexport Parse et al
Also: - prefer use utf8.RuneCountInString over RuneCount - move some declarations to more logical places No functional changes. Change-Id: I6c2c4cf6b55b66bc8bec0287a0d6b499f56ded28 Reviewed-on: https://go-review.googlesource.com/c/tools/+/688935 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 3e9ce40 commit b992291

File tree

7 files changed

+182
-156
lines changed

7 files changed

+182
-156
lines changed

gopls/internal/template/completion.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"context"
1010
"fmt"
1111
"go/scanner"
12-
"go/token"
12+
gotoken "go/token"
1313
"strings"
1414

1515
"golang.org/x/tools/gopls/internal/cache"
@@ -19,18 +19,18 @@ import (
1919

2020
// information needed for completion
2121
type completer struct {
22-
p *Parsed
22+
p *parsed
2323
pos protocol.Position
2424
offset int // offset of the start of the Token
2525
ctx protocol.CompletionContext
2626
syms map[string]symbol
2727
}
2828

2929
func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pos protocol.Position, context protocol.CompletionContext) (*protocol.CompletionList, error) {
30-
all := New(snapshot.Templates())
30+
all := parseSet(snapshot.Templates())
3131
var start int // the beginning of the Token (completed or not)
3232
syms := make(map[string]symbol)
33-
var p *Parsed
33+
var p *parsed
3434
for fn, fc := range all.files {
3535
// collect symbols from all template files
3636
filterSyms(syms, fc.symbols)
@@ -49,7 +49,7 @@ func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
4949
c := completer{
5050
p: p,
5151
pos: pos,
52-
offset: start + len(Left),
52+
offset: start + len(lbraces),
5353
ctx: context,
5454
syms: syms,
5555
}
@@ -75,29 +75,29 @@ func filterSyms(syms map[string]symbol, ns []symbol) {
7575
}
7676

7777
// return the starting position of the enclosing token, or -1 if none
78-
func inTemplate(fc *Parsed, pos protocol.Position) int {
78+
func inTemplate(fc *parsed, pos protocol.Position) int {
7979
// pos is the pos-th character. if the cursor is at the beginning
8080
// of the file, pos is 0. That is, we've only seen characters before pos
8181
// 1. pos might be in a Token, return tk.Start
8282
// 2. pos might be after an elided but before a Token, return elided
8383
// 3. return -1 for false
84-
offset := fc.FromPosition(pos)
84+
offset := fc.fromPosition(pos)
8585
// this could be a binary search, as the tokens are ordered
8686
for _, tk := range fc.tokens {
87-
if tk.Start+len(Left) <= offset && offset+len(Right) <= tk.End {
88-
return tk.Start
87+
if tk.start+len(lbraces) <= offset && offset+len(rbraces) <= tk.end {
88+
return tk.start
8989
}
9090
}
9191
for _, x := range fc.elided {
92-
if x+len(Left) > offset {
92+
if x+len(lbraces) > offset {
9393
// fc.elided is sorted, and x is the position where a '{{' was replaced
9494
// by ' '. We consider only cases where the replaced {{ is to the left
9595
// of the cursor.
9696
break
9797
}
9898
// If the interval [x,offset] does not contain Left or Right
9999
// then provide completions. (do we need the test for Right?)
100-
if !bytes.Contains(fc.buf[x:offset], Left) && !bytes.Contains(fc.buf[x:offset], Right) {
100+
if !bytes.Contains(fc.buf[x:offset], lbraces) && !bytes.Contains(fc.buf[x:offset], rbraces) {
101101
return x
102102
}
103103
}
@@ -115,7 +115,7 @@ var (
115115
// The error return is always nil.
116116
func (c *completer) complete() (*protocol.CompletionList, error) {
117117
ans := &protocol.CompletionList{IsIncomplete: true, Items: []protocol.CompletionItem{}}
118-
start := c.p.FromPosition(c.pos)
118+
start := c.p.fromPosition(c.pos)
119119
sofar := c.p.buf[c.offset:start]
120120
if len(sofar) == 0 || sofar[len(sofar)-1] == ' ' || sofar[len(sofar)-1] == '\t' {
121121
return ans, nil
@@ -194,22 +194,22 @@ func (c *completer) complete() (*protocol.CompletionList, error) {
194194

195195
// version of c.analyze that uses go/scanner.
196196
func scan(buf []byte) []string {
197-
fset := token.NewFileSet()
197+
fset := gotoken.NewFileSet()
198198
fp := fset.AddFile("", -1, len(buf))
199199
var sc scanner.Scanner
200-
sc.Init(fp, buf, func(pos token.Position, msg string) {}, scanner.ScanComments)
200+
sc.Init(fp, buf, func(pos gotoken.Position, msg string) {}, scanner.ScanComments)
201201
ans := make([]string, 0, 10) // preallocating gives a measurable savings
202202
for {
203203
_, tok, lit := sc.Scan() // tok is an int
204-
if tok == token.EOF {
204+
if tok == gotoken.EOF {
205205
break // done
206-
} else if tok == token.SEMICOLON && lit == "\n" {
206+
} else if tok == gotoken.SEMICOLON && lit == "\n" {
207207
continue // don't care, but probably can't happen
208-
} else if tok == token.PERIOD {
208+
} else if tok == gotoken.PERIOD {
209209
ans = append(ans, ".") // lit is empty
210-
} else if tok == token.IDENT && len(ans) > 0 && ans[len(ans)-1] == "." {
210+
} else if tok == gotoken.IDENT && len(ans) > 0 && ans[len(ans)-1] == "." {
211211
ans[len(ans)-1] = "." + lit
212-
} else if tok == token.IDENT && len(ans) > 0 && ans[len(ans)-1] == "$" {
212+
} else if tok == gotoken.IDENT && len(ans) > 0 && ans[len(ans)-1] == "$" {
213213
ans[len(ans)-1] = "$" + lit
214214
} else if lit != "" {
215215
ans = append(ans, lit)

gopls/internal/template/completion_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ func testCompleter(t *testing.T, tx tparse) *completer {
8282
buf := strings.Replace(tx.marked, "^", "", 1)
8383
p := parseBuffer([]byte(buf))
8484
pos := protocol.Position{Line: 0, Character: uint32(col)}
85-
if p.ParseErr != nil {
86-
log.Printf("%q: %v", tx.marked, p.ParseErr)
85+
if p.parseErr != nil {
86+
log.Printf("%q: %v", tx.marked, p.parseErr)
8787
}
8888
offset := inTemplate(p, pos)
8989
if offset == -1 {
@@ -94,7 +94,7 @@ func testCompleter(t *testing.T, tx tparse) *completer {
9494
c := &completer{
9595
p: p,
9696
pos: protocol.Position{Line: 0, Character: uint32(col)},
97-
offset: offset + len(Left),
97+
offset: offset + len(lbraces),
9898
ctx: protocol.CompletionContext{TriggerKind: protocol.Invoked},
9999
syms: syms,
100100
}

gopls/internal/template/highlight.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ func Highlight(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, lo
2020
return nil, err
2121
}
2222
p := parseBuffer(buf)
23-
pos := p.FromPosition(loc)
23+
pos := p.fromPosition(loc)
2424
var ans []protocol.DocumentHighlight
25-
if p.ParseErr == nil {
25+
if p.parseErr == nil {
2626
for _, s := range p.symbols {
2727
if s.start <= pos && pos < s.start+s.length {
2828
return markSymbols(p, s)
@@ -32,7 +32,7 @@ func Highlight(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, lo
3232
// these tokens exist whether or not there was a parse error
3333
// (symbols require a successful parse)
3434
for _, tok := range p.tokens {
35-
if tok.Start <= pos && pos < tok.End {
35+
if tok.start <= pos && pos < tok.end {
3636
wordAt := findWordAt(p, pos)
3737
if len(wordAt) > 0 {
3838
return markWordInToken(p, wordAt)
@@ -44,7 +44,7 @@ func Highlight(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, lo
4444
return ans, nil
4545
}
4646

47-
func markSymbols(p *Parsed, sym symbol) ([]protocol.DocumentHighlight, error) {
47+
func markSymbols(p *parsed, sym symbol) ([]protocol.DocumentHighlight, error) {
4848
var ans []protocol.DocumentHighlight
4949
for _, s := range p.symbols {
5050
if s.name == sym.name {
@@ -53,7 +53,7 @@ func markSymbols(p *Parsed, sym symbol) ([]protocol.DocumentHighlight, error) {
5353
kind = protocol.Write
5454
}
5555
ans = append(ans, protocol.DocumentHighlight{
56-
Range: p.Range(s.start, s.length),
56+
Range: p._range(s.start, s.length),
5757
Kind: kind,
5858
})
5959
}
@@ -62,17 +62,17 @@ func markSymbols(p *Parsed, sym symbol) ([]protocol.DocumentHighlight, error) {
6262
}
6363

6464
// A token is {{...}}, and this marks words in the token that equal the give word
65-
func markWordInToken(p *Parsed, wordAt string) ([]protocol.DocumentHighlight, error) {
65+
func markWordInToken(p *parsed, wordAt string) ([]protocol.DocumentHighlight, error) {
6666
var ans []protocol.DocumentHighlight
6767
pat, err := regexp.Compile(fmt.Sprintf(`\b%s\b`, wordAt))
6868
if err != nil {
6969
return nil, fmt.Errorf("%q: unmatchable word (%v)", wordAt, err)
7070
}
7171
for _, tok := range p.tokens {
72-
got := pat.FindAllIndex(p.buf[tok.Start:tok.End], -1)
72+
got := pat.FindAllIndex(p.buf[tok.start:tok.end], -1)
7373
for i := range got {
7474
ans = append(ans, protocol.DocumentHighlight{
75-
Range: p.Range(got[i][0], got[i][1]-got[i][0]),
75+
Range: p._range(got[i][0], got[i][1]-got[i][0]),
7676
Kind: protocol.Text,
7777
})
7878
}
@@ -84,7 +84,7 @@ var wordRe = regexp.MustCompile(`[$]?\w+$`)
8484
var moreRe = regexp.MustCompile(`^[$]?\w+`)
8585

8686
// findWordAt finds the word the cursor is in (meaning in or just before)
87-
func findWordAt(p *Parsed, pos int) string {
87+
func findWordAt(p *parsed, pos int) string {
8888
if pos >= len(p.buf) {
8989
return "" // can't happen, as we are called with pos < tok.End
9090
}

gopls/internal/template/implementations.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ func diagnoseOne(fh file.Handle) []*cache.Diagnostic {
4343
return []*cache.Diagnostic{&d}
4444
}
4545
p := parseBuffer(buf)
46-
if p.ParseErr == nil {
46+
if p.parseErr == nil {
4747
return nil
4848
}
4949
unknownError := func(msg string) []*cache.Diagnostic {
50-
s := fmt.Sprintf("malformed template error %q: %s", p.ParseErr.Error(), msg)
50+
s := fmt.Sprintf("malformed template error %q: %s", p.parseErr.Error(), msg)
5151
d := cache.Diagnostic{
52-
Message: s, Severity: protocol.SeverityError, Range: p.Range(p.nls[0], 1),
52+
Message: s, Severity: protocol.SeverityError, Range: p._range(p.nls[0], 1),
5353
URI: fh.URI(), Source: cache.TemplateError}
5454
return []*cache.Diagnostic{&d}
5555
}
5656
// errors look like `template: :40: unexpected "}" in operand`
5757
// so the string needs to be parsed
58-
matches := errRe.FindStringSubmatch(p.ParseErr.Error())
58+
matches := errRe.FindStringSubmatch(p.parseErr.Error())
5959
if len(matches) != 3 {
6060
msg := fmt.Sprintf("expected 3 matches, got %d (%v)", len(matches), matches)
6161
return unknownError(msg)
@@ -71,9 +71,9 @@ func diagnoseOne(fh file.Handle) []*cache.Diagnostic {
7171
start := p.nls[lineno-1]
7272
if lineno < len(p.nls) {
7373
size := p.nls[lineno] - start
74-
d.Range = p.Range(start, size)
74+
d.Range = p._range(start, size)
7575
} else {
76-
d.Range = p.Range(start, 1)
76+
d.Range = p._range(start, 1)
7777
}
7878
return []*cache.Diagnostic{&d}
7979
}
@@ -90,13 +90,13 @@ func Definition(snapshot *cache.Snapshot, fh file.Handle, loc protocol.Position)
9090
sym := x.name
9191
ans := []protocol.Location{}
9292
// PJW: this is probably a pattern to abstract
93-
a := New(snapshot.Templates())
93+
a := parseSet(snapshot.Templates())
9494
for k, p := range a.files {
9595
for _, s := range p.symbols {
9696
if !s.vardef || s.name != sym {
9797
continue
9898
}
99-
ans = append(ans, k.Location(p.Range(s.start, s.length)))
99+
ans = append(ans, k.Location(p._range(s.start, s.length)))
100100
}
101101
}
102102
return ans, nil
@@ -107,7 +107,7 @@ func Hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, positi
107107
if sym == nil || err != nil {
108108
return nil, err
109109
}
110-
ans := protocol.Hover{Range: p.Range(sym.start, sym.length), Contents: protocol.MarkupContent{Kind: protocol.Markdown}}
110+
ans := protocol.Hover{Range: p._range(sym.start, sym.length), Contents: protocol.MarkupContent{Kind: protocol.Markdown}}
111111
switch sym.kind {
112112
case protocol.Function:
113113
ans.Contents.Value = fmt.Sprintf("function: %s", sym.name)
@@ -140,7 +140,7 @@ func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
140140
}
141141
ans := []protocol.Location{}
142142

143-
a := New(snapshot.Templates())
143+
a := parseSet(snapshot.Templates())
144144
for k, p := range a.files {
145145
for _, s := range p.symbols {
146146
if s.name != sym.name {
@@ -149,7 +149,7 @@ func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
149149
if s.vardef && !params.Context.IncludeDeclaration {
150150
continue
151151
}
152-
ans = append(ans, k.Location(p.Range(s.start, s.length)))
152+
ans = append(ans, k.Location(p._range(s.start, s.length)))
153153
}
154154
}
155155
// do these need to be sorted? (a.files is a map)
@@ -181,22 +181,22 @@ func SemanticTokens(ctx context.Context, snapshot *cache.Snapshot, spn protocol.
181181
})
182182
}
183183

184-
for _, t := range p.Tokens() {
185-
if t.Multiline {
186-
la, ca := p.LineCol(t.Start)
187-
lb, cb := p.LineCol(t.End)
188-
add(la, ca, p.RuneCount(la, ca, 0))
184+
for _, t := range p.tokens {
185+
if t.multiline {
186+
la, ca := p.lineCol(t.start)
187+
lb, cb := p.lineCol(t.end)
188+
add(la, ca, p.runeCount(la, ca, 0))
189189
for l := la + 1; l < lb; l++ {
190-
add(l, 0, p.RuneCount(l, 0, 0))
190+
add(l, 0, p.runeCount(l, 0, 0))
191191
}
192-
add(lb, 0, p.RuneCount(lb, 0, cb))
192+
add(lb, 0, p.runeCount(lb, 0, cb))
193193
continue
194194
}
195-
sz, err := p.TokenSize(t)
195+
sz, err := p.tokenSize(t)
196196
if err != nil {
197197
return nil, err
198198
}
199-
line, col := p.LineCol(t.Start)
199+
line, col := p.lineCol(t.start)
200200
add(line, col, uint32(sz))
201201
}
202202
ans := &protocol.SemanticTokens{

0 commit comments

Comments
 (0)