aboutsummaryrefslogtreecommitdiff
path: root/internal/lsp/template/highlight.go
blob: a45abaf50200f7f5f4905d2681c38a2a18563d97 (plain)
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
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package template

import (
	"context"
	"fmt"
	"regexp"

	"golang.org/x/tools/internal/lsp/protocol"
	"golang.org/x/tools/internal/lsp/source"
)

func Highlight(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle, loc protocol.Position) ([]protocol.DocumentHighlight, error) {
	buf, err := fh.Read()
	if err != nil {
		return nil, err
	}
	p := parseBuffer(buf)
	pos := p.FromPosition(loc)
	var ans []protocol.DocumentHighlight
	if p.ParseErr == nil {
		for _, s := range p.symbols {
			if s.start <= pos && pos < s.start+s.length {
				return markSymbols(p, s)
			}
		}
	}
	// these tokens exist whether or not there was a parse error
	// (symbols require a successful parse)
	for _, tok := range p.tokens {
		if tok.Start <= pos && pos < tok.End {
			wordAt := findWordAt(p, pos)
			if len(wordAt) > 0 {
				return markWordInToken(p, wordAt)
			}
		}
	}
	// find the 'word' at pos, etc: someday
	// until then we get the default action, which doesn't respect word boundaries
	return ans, nil
}

func markSymbols(p *Parsed, sym symbol) ([]protocol.DocumentHighlight, error) {
	var ans []protocol.DocumentHighlight
	for _, s := range p.symbols {
		if s.name == sym.name {
			kind := protocol.Read
			if s.vardef {
				kind = protocol.Write
			}
			ans = append(ans, protocol.DocumentHighlight{
				Range: p.Range(s.start, s.length),
				Kind:  kind,
			})
		}
	}
	return ans, nil
}

// A token is {{...}}, and this marks words in the token that equal the give word
func markWordInToken(p *Parsed, wordAt string) ([]protocol.DocumentHighlight, error) {
	var ans []protocol.DocumentHighlight
	pat, err := regexp.Compile(fmt.Sprintf(`\b%s\b`, wordAt))
	if err != nil {
		return nil, fmt.Errorf("%q: unmatchable word (%v)", wordAt, err)
	}
	for _, tok := range p.tokens {
		got := pat.FindAllIndex(p.buf[tok.Start:tok.End], -1)
		for i := 0; i < len(got); i++ {
			ans = append(ans, protocol.DocumentHighlight{
				Range: p.Range(got[i][0], got[i][1]-got[i][0]),
				Kind:  protocol.Text,
			})
		}
	}
	return ans, nil
}

var wordRe = regexp.MustCompile(`[$]?\w+$`)
var moreRe = regexp.MustCompile(`^[$]?\w+`)

// findWordAt finds the word the cursor is in (meaning in or just before)
func findWordAt(p *Parsed, pos int) string {
	if pos >= len(p.buf) {
		return "" // can't happen, as we are called with pos < tok.End
	}
	after := moreRe.Find(p.buf[pos:])
	if len(after) == 0 {
		return "" // end of the word
	}
	got := wordRe.Find(p.buf[:pos+len(after)])
	return string(got)
}