Debian Codegraph @

gopls/internal/template/highlight.go

raw symbols SCIP
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package template
6
7 import (
8 "context"
9 "fmt"
10 "regexp"
11
12 "golang.org/x/tools/gopls/internal/cache"
13 "golang.org/x/tools/gopls/internal/file"
14 "golang.org/x/tools/gopls/internal/protocol"
15 )
16
17 func Highlight(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, rng protocol.Range) ([]protocol.DocumentHighlight, error) {
18 buf, err := fh.Content()
19 if err != nil {
20 return nil, err
21 }
22 p := parseBuffer(fh.URI(), buf)
23 start, end, err := p.mapper.RangeOffsets(rng)
24 if err != nil {
25 return nil, err
26 }
27
28 if p.parseErr == nil {
29 for _, s := range p.symbols {
30 if s.start <= start && end < s.start+s.len {
31 return markSymbols(p, s)
32 }
33 }
34 }
35
36 // these tokens exist whether or not there was a parse error
37 // (symbols require a successful parse)
38 for _, tok := range p.tokens {
39 if tok.start <= start && end < tok.end {
40 wordAt := wordAt(p.buf, start)
41 if len(wordAt) > 0 {
42 return markWordInToken(p, wordAt)
43 }
44 }
45 }
46
47 // TODO: find the 'word' at pos, etc: someday
48 // until then we get the default action, which doesn't respect word boundaries
49 return nil, nil
50 }
51
52 func markSymbols(p *parsed, sym symbol) ([]protocol.DocumentHighlight, error) {
53 var ans []protocol.DocumentHighlight
54 for _, s := range p.symbols {
55 if s.name == sym.name {
56 kind := protocol.Read
57 if s.vardef {
58 kind = protocol.Write
59 }
60 rng, err := p.mapper.OffsetRange(s.offsets())
61 if err != nil {
62 return nil, err
63 }
64 ans = append(ans, protocol.DocumentHighlight{
65 Range: rng,
66 Kind: kind,
67 })
68 }
69 }
70 return ans, nil
71 }
72
73 // A token is {{...}}, and this marks words in the token that equal the give word
74 func markWordInToken(p *parsed, wordAt string) ([]protocol.DocumentHighlight, error) {
75 var ans []protocol.DocumentHighlight
76 pat, err := regexp.Compile(fmt.Sprintf(`\b%s\b`, wordAt))
77 if err != nil {
78 return nil, fmt.Errorf("%q: unmatchable word (%v)", wordAt, err)
79 }
80 for _, tok := range p.tokens {
81 matches := pat.FindAllIndex(p.buf[tok.start:tok.end], -1)
82 for _, match := range matches {
83 rng, err := p.mapper.OffsetRange(match[0], match[1])
84 if err != nil {
85 return nil, err
86 }
87 ans = append(ans, protocol.DocumentHighlight{
88 Range: rng,
89 Kind: protocol.Text,
90 })
91 }
92 }
93 return ans, nil
94 }
95
96 // wordAt returns the word the cursor is in (meaning in or just before)
97 func wordAt(buf []byte, pos int) string {
98 if pos >= len(buf) {
99 return ""
100 }
101 after := moreRe.Find(buf[pos:])
102 if len(after) == 0 {
103 return "" // end of the word
104 }
105 got := wordRe.Find(buf[:pos+len(after)])
106 return string(got)
107 }
108
109 var (
110 wordRe = regexp.MustCompile(`[$]?\w+$`)
111 moreRe = regexp.MustCompile(`^[$]?\w+`)
112 )