-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathdocument_highlight.v
More file actions
280 lines (269 loc) · 8.38 KB
/
Copy pathdocument_highlight.v
File metadata and controls
280 lines (269 loc) · 8.38 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) 2025 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module main
import json2
import os
// DocumentHighlightKind values (LSP §3.17): 1 = Text, 2 = Read, 3 = Write.
const doc_highlight_read = 2
const doc_highlight_write = 3
// Each semantic highlight candidate requires a serial compiler definition
// lookup. Above this bound, return no highlights rather than conflate symbols
// with the same spelling from different scopes.
const document_highlight_semantic_max_candidates = 48
struct DocumentHighlightCandidate {
line_idx int
start_byte int
end_byte int
}
// highlight_has_word reports whether `text` contains `word` at identifier
// boundaries.
fn highlight_has_word(text string, word string) bool {
if word == '' || text.len < word.len {
return false
}
for i := 0; i + word.len <= text.len; i++ {
if text[i..i + word.len] != word {
continue
}
left_ok := i == 0 || !is_ident_char(text[i - 1])
right_ok := i + word.len == text.len || !is_ident_char(text[i + word.len])
if left_ok && right_ok {
return true
}
}
return false
}
// is_for_binding_highlight reports whether the occurrence is on the binding
// side of `for name in values` (including `for key, value in map`).
fn is_for_binding_highlight(line string, start_byte int, end_byte int) bool {
prefix := line[..start_byte]
mut for_byte := -1
for i := 0; i + 3 <= prefix.len; i++ {
if prefix[i..i + 3] != 'for' {
continue
}
left_ok := i == 0 || !is_ident_char(prefix[i - 1])
right_ok := i + 3 == prefix.len || !is_ident_char(prefix[i + 3])
if left_ok && right_ok {
for_byte = i
}
}
if for_byte < 0 {
return false
}
binding_prefix := prefix[for_byte + 3..]
if binding_prefix.contains('{') || binding_prefix.contains(';')
|| highlight_has_word(binding_prefix, 'in') {
return false
}
mut binding_suffix := line[end_byte..]
brace_byte := binding_suffix.index_any('{;')
if brace_byte >= 0 {
binding_suffix = binding_suffix[..brace_byte]
}
return highlight_has_word(binding_suffix, 'in')
}
// is_fn_parameter_highlight reports whether an occurrence is a receiver or
// parameter name in a function signature and is followed by its type.
fn is_fn_parameter_highlight(line string, start_byte int, end_byte int) bool {
mut type_byte := end_byte
for type_byte < line.len && (line[type_byte] == ` ` || line[type_byte] == `\t`) {
type_byte++
}
if type_byte == end_byte || type_byte >= line.len {
return false
}
type_start := line[type_byte]
if !is_ident_char(type_start) && type_start !in [`[`, `?`, `&`, `.`] {
return false
}
prefix := line[..start_byte]
mut fn_byte := -1
for i := 0; i + 2 <= prefix.len; i++ {
if prefix[i..i + 2] != 'fn' {
continue
}
left_ok := i == 0 || !is_ident_char(prefix[i - 1])
right_ok := i + 2 == prefix.len || !is_ident_char(prefix[i + 2])
if left_ok && right_ok {
fn_byte = i
}
}
if fn_byte < 0 {
return false
}
mut paren_depth := 0
for i := fn_byte + 2; i < prefix.len; i++ {
if prefix[i] == `(` {
paren_depth++
} else if prefix[i] == `)` {
paren_depth--
}
}
return paren_depth > 0
}
// classify_highlight_kind classifies an identifier occurrence between byte
// offsets `start_byte` and `end_byte` on `line` as a Write or a Read. This is a
// syntactic heuristic (P2-03): declarations, assignments (including shifts),
// and `name++`/`name--` are writes; comparisons and other uses are reads.
fn classify_highlight_kind(line string, start_byte int, end_byte int) int {
if is_for_binding_highlight(line, start_byte, end_byte)
|| is_fn_parameter_highlight(line, start_byte, end_byte) {
return doc_highlight_write
}
mut i := end_byte
for i < line.len && (line[i] == ` ` || line[i] == `\t`) {
i++
}
if i >= line.len {
return doc_highlight_read
}
rest := line[i..]
if rest.starts_with(':=') {
return doc_highlight_write
}
// Distinguish assignment `=` from comparison `==`/`=>`.
if rest.starts_with('==') || rest.starts_with('=>') {
return doc_highlight_read
}
if rest.starts_with('=') {
return doc_highlight_write
}
if rest.starts_with('++') || rest.starts_with('--') {
return doc_highlight_write
}
if rest.starts_with('<<=') || rest.starts_with('>>=') {
return doc_highlight_write
}
// Compound assignment: += -= *= /= %= &= |= ^= (op followed by '=', not '==').
if rest.len >= 2 && rest[1] == `=` && (rest.len < 3 || rest[2] != `=`)
&& rest[0] in [`+`, `-`, `*`, `/`, `%`, `&`, `|`, `^`] {
return doc_highlight_write
}
return doc_highlight_read
}
// collect_document_highlight_candidates reuses the reference tokenizer so
// comments, literal text, and executable string interpolations are treated
// consistently by highlighting and rename.
fn collect_document_highlight_candidates(content string, lines []string, symbol string, enc PositionEncoding) []DocumentHighlightCandidate {
occurrences := extract_identifier_occurrences(content, enc)
positions := occurrences[symbol] or { return []DocumentHighlightCandidate{} }
mut candidates := []DocumentHighlightCandidate{cap: positions.len}
for position in positions {
if position.line < 0 || position.line >= lines.len {
continue
}
line := lines[position.line]
candidates << DocumentHighlightCandidate{
line_idx: position.line
start_byte: encoded_col_to_byte(line, position.start_char, enc)
end_byte: encoded_col_to_byte(line, position.end_char, enc)
}
}
return candidates
}
// handle_document_highlight handles textDocument/documentHighlight.
// It finds all occurrences of the identifier under the cursor within the current
// document and returns them as a DocumentHighlight list.
fn (mut app App) handle_document_highlight(request Request) Response {
params := json2.decode[DocumentHighlightParams](request.params) or {
$if debug { log('Failed to decode DocumentHighlightParams: ${err}') }
return Response{
id: request.id
result: []DocumentHighlight{}
}
}
uri := params.text_document.uri
content := app.open_files[uri] or { os.read_file(uri_to_path(uri)) or { '' } }
if content == '' {
return Response{
id: request.id
result: []DocumentHighlight{}
}
}
lines := content.split_into_lines()
if params.position.line < 0 || params.position.line >= lines.len {
return Response{
id: request.id
result: []DocumentHighlight{}
}
}
line_text := lines[params.position.line]
start, end := find_word_bounds_at_col(line_text, params.position.char, app.position_encoding)
if start < 0 || end <= start {
return Response{
id: request.id
result: []DocumentHighlight{}
}
}
symbol := substr_by_char_bounds(line_text, start, end, app.position_encoding)
if symbol == '' {
return Response{
id: request.id
result: []DocumentHighlight{}
}
}
candidates := collect_document_highlight_candidates(content, lines, symbol,
app.position_encoding)
if candidates.len > document_highlight_semantic_max_candidates {
return Response{
id: request.id
result: []DocumentHighlight{}
}
}
anchor := app.resolve_symbol_anchor(uri, params.position.line, start)
mut anchor_cache := map[string]?Location{}
if a := anchor {
// The initial lookup already resolved the selected occurrence.
anchor_cache[anchor_cache_key(uri, params.position.line, start)] = a
}
mut highlights := []DocumentHighlight{cap: candidates.len}
for candidate in candidates {
line := lines[candidate.line_idx]
start_char := byte_to_encoded_col(line, candidate.start_byte, app.position_encoding)
end_char := byte_to_encoded_col(line, candidate.end_byte, app.position_encoding)
if a := anchor {
if resolved := app.resolve_symbol_anchor_cached(uri, candidate.line_idx, start_char, mut
anchor_cache)
{
if !same_anchor_location(resolved, a) {
continue
}
} else {
continue
}
}
mut kind := classify_highlight_kind(line, candidate.start_byte, candidate.end_byte)
if a := anchor {
occurrence := Location{
uri: uri
range: LSPRange{
start: Position{
line: candidate.line_idx
char: start_char
}
}
}
if same_anchor_location(occurrence, a) {
kind = doc_highlight_write
}
}
highlights << DocumentHighlight{
range: LSPRange{
start: Position{
line: candidate.line_idx
char: start_char
}
end: Position{
line: candidate.line_idx
char: end_char
}
}
kind: kind // Read/Write (P2-03)
}
}
return Response{
id: request.id
result: highlights
}
}