-
Notifications
You must be signed in to change notification settings - Fork 8
/
token_encoder.go
152 lines (124 loc) · 3.89 KB
/
token_encoder.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
package lsp
import (
"bytes"
lsp "github.com/Azure/azapi-lsp/internal/protocol"
"github.com/Azure/azapi-lsp/internal/source"
"github.com/hashicorp/hcl-lang/lang"
)
type TokenEncoder struct {
Lines source.Lines
Tokens []lang.SemanticToken
ClientCaps lsp.SemanticTokensClientCapabilities
}
func (te *TokenEncoder) Encode() []uint32 {
data := make([]uint32, 0)
for i := range te.Tokens {
data = append(data, te.encodeTokenOfIndex(i)...)
}
return data
}
func (te *TokenEncoder) encodeTokenOfIndex(i int) []uint32 {
token := te.Tokens[i]
var tokenType TokenType
modifiers := make([]TokenModifier, 0)
switch token.Type {
case lang.TokenBlockType:
tokenType = TokenTypeType
case lang.TokenBlockLabel:
tokenType = TokenTypeString
case lang.TokenAttrName:
tokenType = TokenTypeProperty
case lang.TokenBool:
tokenType = TokenTypeKeyword
case lang.TokenNumber:
tokenType = TokenTypeNumber
case lang.TokenString:
tokenType = TokenTypeString
case lang.TokenObjectKey:
tokenType = TokenTypeParameter
case lang.TokenMapKey:
tokenType = TokenTypeParameter
case lang.TokenKeyword:
tokenType = TokenTypeVariable
case lang.TokenTraversalStep:
tokenType = TokenTypeVariable
default:
return []uint32{}
}
if !te.tokenTypeSupported(tokenType) {
return []uint32{}
}
tokenTypeIdx := TokenTypesLegend(te.ClientCaps.TokenTypes).Index(tokenType)
for _, m := range token.Modifiers {
switch m {
case lang.TokenModifierDependent:
if !te.tokenModifierSupported(TokenModifierModification) {
continue
}
modifiers = append(modifiers, TokenModifierModification)
case lang.TokenModifierDeprecated:
if !te.tokenModifierSupported(TokenModifierDeprecated) {
continue
}
modifiers = append(modifiers, TokenModifierDeprecated)
}
}
modifierBitMask := TokenModifiersLegend(te.ClientCaps.TokenModifiers).BitMask(modifiers)
data := make([]uint32, 0)
// Client may not support multiline tokens which would be indicated
// via lsp.SemanticTokensCapabilities.MultilineTokenSupport
// once it becomes available in gopls LSP structs.
//
// For now we just safely assume client does *not* support it.
tokenLineDelta := token.Range.End.Line - token.Range.Start.Line
previousLine := 0
previousStartChar := 0
if i > 0 {
previousLine = te.Tokens[i-1].Range.End.Line - 1
currentLine := te.Tokens[i].Range.End.Line - 1
if currentLine == previousLine {
previousStartChar = te.Tokens[i-1].Range.Start.Column - 1
}
}
if tokenLineDelta == 0 || false /* te.clientCaps.MultilineTokenSupport */ {
deltaLine := token.Range.Start.Line - 1 - previousLine
tokenLength := token.Range.End.Byte - token.Range.Start.Byte
deltaStartChar := token.Range.Start.Column - 1 - previousStartChar
data = append(data, []uint32{
uint32(deltaLine),
uint32(deltaStartChar),
uint32(tokenLength),
uint32(tokenTypeIdx),
uint32(modifierBitMask),
}...)
} else {
// Add entry for each line of a multiline token
for tokenLine := token.Range.Start.Line - 1; tokenLine <= token.Range.End.Line-1; tokenLine++ {
deltaLine := tokenLine - previousLine
deltaStartChar := 0
if tokenLine == token.Range.Start.Line-1 {
deltaStartChar = token.Range.Start.Column - 1 - previousStartChar
}
lineBytes := bytes.TrimRight(te.Lines[tokenLine].Bytes(), "\n\r")
length := len(lineBytes)
if tokenLine == token.Range.End.Line-1 {
length = token.Range.End.Column - 1
}
data = append(data, []uint32{
uint32(deltaLine),
uint32(deltaStartChar),
uint32(length),
uint32(tokenTypeIdx),
uint32(modifierBitMask),
}...)
previousLine = tokenLine
}
}
return data
}
func (te *TokenEncoder) tokenTypeSupported(tokenType TokenType) bool {
return sliceContains(te.ClientCaps.TokenTypes, string(tokenType))
}
func (te *TokenEncoder) tokenModifierSupported(tokenModifier TokenModifier) bool {
return sliceContains(te.ClientCaps.TokenModifiers, string(tokenModifier))
}