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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use std::cell::RefCell;
use std::num::NonZero;
use log::warn;
use lru::LruCache;
use lsp_types::{SemanticToken, SemanticTokens, SemanticTokensResult, Url};
use shader_sense::symbols::symbols::ShaderSymbolMode;
use shader_sense::{position::ShaderPosition, symbols::symbols::ShaderSymbolData};
use crate::server::common::ServerLanguageError;
use crate::server::ServerLanguage;
impl ServerLanguage {
fn get_regex<'a>(
label: &String,
regex_cache: &'a mut LruCache<String, regex::Regex>,
) -> &'a regex::Regex {
regex_cache.get_or_insert(label.clone(), || {
regex::Regex::new(format!("\\b({})\\b", regex::escape(label)).as_str()).unwrap()
})
}
fn find_macros(&mut self, uri: &Url) -> Vec<SemanticToken> {
let cached_file = self.watched_files.files.get(uri).unwrap();
let symbols = self.watched_files.get_all_symbols(&uri);
let file_path = uri.to_file_path().unwrap();
let content = &RefCell::borrow(&cached_file.shader_module).content;
symbols
.macros
.iter()
.map(|symbol| {
let byte_offset_start = match &symbol.mode {
ShaderSymbolMode::Runtime(runtime) => {
if runtime.file_path.as_os_str() == file_path.as_os_str() {
runtime.range.start.to_byte_offset(content).unwrap()
} else {
match cached_file
.data
.as_ref()
.unwrap()
.symbol_cache
.find_direct_includer(&runtime.file_path)
{
Some(include) => {
include.get_range().start.to_byte_offset(content).unwrap()
}
None => 0, // Included from another file, but not found...
}
}
}
_ => 0, // Not runtime means no range means its everywhere
};
// Need to ignore comment aswell... Might need tree sitter instead.
// Looking for preproc_arg & identifier might be enough.
// Need to check for regions too...
let reg = Self::get_regex(&symbol.label, &mut self.regex_cache);
let word_byte_offsets: Vec<usize> = reg
.captures_iter(&content[byte_offset_start..])
.map(|e| e.get(0).unwrap().range().start + byte_offset_start)
.collect();
word_byte_offsets
.iter()
.filter_map(|byte_offset| {
match ShaderPosition::from_byte_offset(&content, *byte_offset) {
Ok(position) => {
Some(SemanticToken {
delta_line: position.line,
delta_start: position.pos,
length: symbol.label.len() as u32,
token_type: 0, // SemanticTokenType::MACRO, view registration
token_modifiers_bitset: 0,
})
}
Err(_) => None,
}
})
.collect()
})
.collect::<Vec<Vec<SemanticToken>>>()
.concat()
}
fn find_enum(&mut self, uri: &Url) -> Vec<SemanticToken> {
let cached_file = self.watched_files.files.get(uri).unwrap();
let symbols = self.watched_files.get_all_symbols(&uri);
let file_path = uri.to_file_path().unwrap();
let content = &RefCell::borrow(&cached_file.shader_module).content;
symbols
.types
.iter()
.filter_map(|symbol| match &symbol.data {
ShaderSymbolData::Enum { values } => {
let byte_offset_start = match &symbol.mode {
ShaderSymbolMode::Runtime(runtime) => {
if runtime.file_path.as_os_str() == file_path.as_os_str() {
runtime.range.start.to_byte_offset(content).unwrap()
} else {
match cached_file
.data
.as_ref()
.unwrap()
.symbol_cache
.find_direct_includer(&runtime.file_path)
{
Some(include) => {
include.get_range().start.to_byte_offset(content).unwrap()
}
None => 0, // Included from another file, but not found...
}
}
}
_ => 0, // Not runtime means no range means its everywhere
};
let mut tokens = Vec::new();
// Add enum label aswell.
let reg = Self::get_regex(&symbol.label, &mut self.regex_cache);
let word_byte_offsets: Vec<usize> = reg
.captures_iter(&content[byte_offset_start..])
.map(|c| c.get(0).unwrap().range().start + byte_offset_start)
.collect();
tokens.extend(
word_byte_offsets
.iter()
.filter_map(|byte_offset| {
match ShaderPosition::from_byte_offset(&content, *byte_offset) {
Ok(position) => {
Some(SemanticToken {
delta_line: position.line,
delta_start: position.pos,
length: symbol.label.len() as u32,
token_type: 3, // SemanticTokenType::ENUM, view registration
token_modifiers_bitset: 0,
})
}
Err(_) => None,
}
})
.collect::<Vec<SemanticToken>>(),
);
// Collect enum member now.
for value in values {
let reg = Self::get_regex(&value.label, &mut self.regex_cache);
let word_byte_offsets: Vec<usize> = reg
.captures_iter(&content[byte_offset_start..])
.map(|e| e.get(0).unwrap().range().start + byte_offset_start)
.collect();
tokens.extend(
word_byte_offsets
.iter()
.filter_map(|byte_offset| {
match ShaderPosition::from_byte_offset(&content, *byte_offset) {
Ok(position) => {
Some(SemanticToken {
delta_line: position.line,
delta_start: position.pos,
length: value.label.len() as u32,
token_type: 2, // SemanticTokenType::ENUM_MEMBER, view registration
token_modifiers_bitset: 0,
})
}
Err(_) => None,
}
})
.collect::<Vec<SemanticToken>>(),
);
}
Some(tokens)
}
_ => None,
})
.collect::<Vec<Vec<SemanticToken>>>()
.concat()
}
fn find_parameters_variables(&mut self, uri: &Url) -> Vec<SemanticToken> {
let cached_file = self.watched_files.files.get(uri).unwrap();
let symbols = self.watched_files.get_all_symbols(&uri);
let file_path = uri.to_file_path().unwrap();
let content = &RefCell::borrow(&cached_file.shader_module).content;
symbols
.functions
.iter()
.map(|symbol| {
let mut tokens = Vec::new();
// If we own a scope and have a range.
if let ShaderSymbolMode::Runtime(runtime) = &symbol.mode {
if let Some(scope) = &runtime.scope {
if runtime.file_path.as_os_str() == file_path.as_os_str() {
let content_start = scope.start.to_byte_offset(&content).unwrap();
let content_end = scope.end.to_byte_offset(&content).unwrap();
match &symbol.data {
ShaderSymbolData::Functions { signatures } => {
assert!(
signatures.len() == 1,
"Should have only one signature"
);
for parameter in &signatures[0].parameters {
match ¶meter.range {
Some(range) => tokens.push(SemanticToken {
delta_line: range.start.line,
delta_start: range.start.pos,
length: parameter.label.len() as u32,
token_type: 1, // SemanticTokenType::PARAMETER, view registration
token_modifiers_bitset: 0,
}),
None => continue, // Should not happen for local symbol, but skip it to be sure...
}
// Push occurences in scope
let reg = Self::get_regex(
¶meter.label,
&mut self.regex_cache,
);
let word_byte_offsets: Vec<usize> = reg
.captures_iter(&content[content_start..content_end])
.map(|e| {
e.get(0).unwrap().range().start + content_start
})
.collect();
tokens.extend(
word_byte_offsets
.iter()
.filter_map(|byte_offset| {
if *byte_offset > 0
&& &content[byte_offset - 1..*byte_offset]
== "."
{
None // Skip struct param with same name.
} else {
match ShaderPosition::from_byte_offset(
&content,
*byte_offset,
) {
Ok(position) => {
Some(SemanticToken {
delta_line: position.line,
delta_start: position.pos,
length: parameter.label.len()
as u32,
token_type: 1, // SemanticTokenType::PARAMETER, view registration
token_modifiers_bitset: 0,
})
}
Err(_) => None,
}
}
})
.collect::<Vec<SemanticToken>>(),
);
}
}
_ => {} // Nothing to push
}
} else {
// Nothing to push
}
} else {
// Nothing to push
}
} else {
// Nothing to push
}
tokens
})
.collect::<Vec<Vec<SemanticToken>>>()
.concat()
}
pub fn recolt_semantic_tokens(
&mut self,
uri: &Url,
) -> Result<SemanticTokensResult, ServerLanguageError> {
// Ensure valid file input.
let _cached_file = self.get_cachable_file(&uri)?;
// Find occurences of tokens to paint
let mut tokens = Vec::new();
tokens.extend(self.find_macros(uri));
tokens.extend(self.find_parameters_variables(uri));
tokens.extend(self.find_enum(uri));
// Increase cache size if we couldnt fit all tokens.
if tokens.len() > self.regex_cache.cap().get() {
let scale_factor = 1.2; // Allocate a bit more than the max.
let max_size = 2000; // Avoid using too much memory.
if tokens.len() <= max_size {
let new_cap =
std::cmp::min((tokens.len() as f32 * scale_factor) as usize, max_size);
warn!(
"Too many tokens found for single file {} ({}). Extending regex cache size to {}.",
uri,
tokens.len(),
new_cap
);
let old_cache = std::mem::replace(
&mut self.regex_cache,
LruCache::new(NonZero::new(new_cap).unwrap()),
);
for (old_key, old_regex) in old_cache {
self.regex_cache.put(old_key, old_regex);
}
} else {
warn!(
"Too many tokens found for single file {} ({}), maximum limit {} reached.",
uri,
tokens.len(),
max_size
);
}
}
// Sort by positions
tokens.sort_by(|lhs, rhs| {
(&lhs.delta_line, &lhs.delta_start).cmp(&(&rhs.delta_line, &rhs.delta_start))
});
// Compute delta from position
let mut delta_line = 0;
let mut delta_pos = 0;
for token in &mut tokens {
// Reset pos on new line.
if token.delta_line != delta_line {
delta_pos = 0;
}
let line = token.delta_line;
let pos = token.delta_start;
token.delta_line = line - delta_line;
token.delta_start = pos - delta_pos;
delta_line = line;
delta_pos = pos;
}
Ok(SemanticTokensResult::Tokens(SemanticTokens {
result_id: None,
data: tokens,
}))
}
}