Unnamed repository; edit this file 'description' to name the repository.
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
pub use super::highlighter2::*;

// use std::borrow::Cow;
// use std::cell::RefCell;
// use std::sync::atomic::{self, AtomicUsize};
// use std::{fmt, iter, mem, ops};

// use ropey::RopeSlice;
// use tree_sitter::{QueryCaptures, QueryCursor, Tree};

// use crate::{byte_range_to_str, Error, HighlightConfiguration, Syntax, TREE_SITTER_MATCH_LIMIT};

// const CANCELLATION_CHECK_INTERVAL: usize = 100;

// /// Indicates which highlight should be applied to a region of source code.
// #[derive(Copy, Clone, Debug, PartialEq, Eq)]
// pub struct Highlight(pub usize);

// /// Represents a single step in rendering a syntax-highlighted document.
// #[derive(Copy, Clone, Debug)]
// pub enum HighlightEvent {
//     Source { start: usize, end: usize },
//     HighlightStart(Highlight),
//     HighlightEnd,
// }

// #[derive(Debug)]
// struct LocalDef<'a> {
//     name: Cow<'a, str>,
//     value_range: ops::Range<usize>,
//     highlight: Option<Highlight>,
// }

// #[derive(Debug)]
// struct LocalScope<'a> {
//     inherits: bool,
//     range: ops::Range<usize>,
//     local_defs: Vec<LocalDef<'a>>,
// }

// #[derive(Debug)]
// struct HighlightIter<'a> {
//     source: RopeSlice<'a>,
//     byte_offset: usize,
//     cancellation_flag: Option<&'a AtomicUsize>,
//     layers: Vec<HighlightIterLayer<'a>>,
//     iter_count: usize,
//     next_event: Option<HighlightEvent>,
//     last_highlight_range: Option<(usize, usize, u32)>,
// }

// struct HighlightIterLayer<'a> {
//     _tree: Option<Tree>,
//     cursor: QueryCursor,
//     captures: RefCell<iter::Peekable<QueryCaptures<'a, 'a, RopeProvider<'a>, &'a [u8]>>>,
//     config: &'a HighlightConfiguration,
//     highlight_end_stack: Vec<usize>,
//     scope_stack: Vec<LocalScope<'a>>,
//     depth: u32,
// }

// impl<'a> fmt::Debug for HighlightIterLayer<'a> {
//     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//         f.debug_struct("HighlightIterLayer").finish()
//     }
// }

// impl<'a> HighlightIterLayer<'a> {
//     // First, sort scope boundaries by their byte offset in the document. At a
//     // given position, emit scope endings before scope beginnings. Finally, emit
//     // scope boundaries from deeper layers first.
//     fn sort_key(&self) -> Option<(usize, bool, isize)> {
//         let depth = -(self.depth as isize);
//         let next_start = self
//             .captures
//             .borrow_mut()
//             .peek()
//             .map(|(m, i)| m.captures[*i].node.start_byte());
//         let next_end = self.highlight_end_stack.last().cloned();
//         match (next_start, next_end) {
//             (Some(start), Some(end)) => {
//                 if start < end {
//                     Some((start, true, depth))
//                 } else {
//                     Some((end, false, depth))
//                 }
//             }
//             (Some(i), None) => Some((i, true, depth)),
//             (None, Some(j)) => Some((j, false, depth)),
//             _ => None,
//         }
//     }
// }

// impl<'a> HighlightIter<'a> {
//     fn emit_event(
//         &mut self,
//         offset: usize,
//         event: Option<HighlightEvent>,
//     ) -> Option<Result<HighlightEvent, Error>> {
//         let result;
//         if self.byte_offset < offset {
//             result = Some(Ok(HighlightEvent::Source {
//                 start: self.byte_offset,
//                 end: offset,
//             }));
//             self.byte_offset = offset;
//             self.next_event = event;
//         } else {
//             result = event.map(Ok);
//         }
//         self.sort_layers();
//         result
//     }

//     fn sort_layers(&mut self) {
//         while !self.layers.is_empty() {
//             if let Some(sort_key) = self.layers[0].sort_key() {
//                 let mut i = 0;
//                 while i + 1 < self.layers.len() {
//                     if let Some(next_offset) = self.layers[i + 1].sort_key() {
//                         if next_offset < sort_key {
//                             i += 1;
//                             continue;
//                         }
//                     } else {
//                         let layer = self.layers.remove(i + 1);
//                         PARSER.with(|ts_parser| {
//                             let highlighter = &mut ts_parser.borrow_mut();
//                             highlighter.cursors.push(layer.cursor);
//                         });
//                     }
//                     break;
//                 }
//                 if i > 0 {
//                     self.layers[0..(i + 1)].rotate_left(1);
//                 }
//                 break;
//             } else {
//                 let layer = self.layers.remove(0);
//                 PARSER.with(|ts_parser| {
//                     let highlighter = &mut ts_parser.borrow_mut();
//                     highlighter.cursors.push(layer.cursor);
//                 });
//             }
//         }
//     }
// }

// impl<'a> Iterator for HighlightIter<'a> {
//     type Item = Result<HighlightEvent, Error>;

//     fn next(&mut self) -> Option<Self::Item> {
//         'main: loop {
//             // If we've already determined the next highlight boundary, just return it.
//             if let Some(e) = self.next_event.take() {
//                 return Some(Ok(e));
//             }

//             // Periodically check for cancellation, returning `Cancelled` error if the
//             // cancellation flag was flipped.
//             if let Some(cancellation_flag) = self.cancellation_flag {
//                 self.iter_count += 1;
//                 if self.iter_count >= CANCELLATION_CHECK_INTERVAL {
//                     self.iter_count = 0;
//                     if cancellation_flag.load(atomic::Ordering::Relaxed) != 0 {
//                         return Some(Err(Error::Cancelled));
//                     }
//                 }
//             }

//             // If none of the layers have any more highlight boundaries, terminate.
//             if self.layers.is_empty() {
//                 let len = self.source.len_bytes();
//                 return if self.byte_offset < len {
//                     let result = Some(Ok(HighlightEvent::Source {
//                         start: self.byte_offset,
//                         end: len,
//                     }));
//                     self.byte_offset = len;
//                     result
//                 } else {
//                     None
//                 };
//             }

//             // Get the next capture from whichever layer has the earliest highlight boundary.
//             let range;
//             let layer = &mut self.layers[0];
//             let captures = layer.captures.get_mut();
//             if let Some((next_match, capture_index)) = captures.peek() {
//                 let next_capture = next_match.captures[*capture_index];
//                 range = next_capture.node.byte_range();

//                 // If any previous highlight ends before this node starts, then before
//                 // processing this capture, emit the source code up until the end of the
//                 // previous highlight, and an end event for that highlight.
//                 if let Some(end_byte) = layer.highlight_end_stack.last().cloned() {
//                     if end_byte <= range.start {
//                         layer.highlight_end_stack.pop();
//                         return self.emit_event(end_byte, Some(HighlightEvent::HighlightEnd));
//                     }
//                 }
//             }
//             // If there are no more captures, then emit any remaining highlight end events.
//             // And if there are none of those, then just advance to the end of the document.
//             else if let Some(end_byte) = layer.highlight_end_stack.last().cloned() {
//                 layer.highlight_end_stack.pop();
//                 return self.emit_event(end_byte, Some(HighlightEvent::HighlightEnd));
//             } else {
//                 return self.emit_event(self.source.len_bytes(), None);
//             };

//             let (mut match_, capture_index) = captures.next().unwrap();
//             let mut capture = match_.captures[capture_index];

//             // Remove from the local scope stack any local scopes that have already ended.
//             while range.start > layer.scope_stack.last().unwrap().range.end {
//                 layer.scope_stack.pop();
//             }

//             // If this capture is for tracking local variables, then process the
//             // local variable info.
//             let mut reference_highlight = None;
//             let mut definition_highlight = None;
//             while match_.pattern_index < layer.config.highlights_pattern_index {
//                 // If the node represents a local scope, push a new local scope onto
//                 // the scope stack.
//                 if Some(capture.index) == layer.config.local_scope_capture_index {
//                     definition_highlight = None;
//                     let mut scope = LocalScope {
//                         inherits: true,
//                         range: range.clone(),
//                         local_defs: Vec::new(),
//                     };
//                     for prop in layer.config.query.property_settings(match_.pattern_index) {
//                         if let "local.scope-inherits" = prop.key.as_ref() {
//                             scope.inherits =
//                                 prop.value.as_ref().map_or(true, |r| r.as_ref() == "true");
//                         }
//                     }
//                     layer.scope_stack.push(scope);
//                 }
//                 // If the node represents a definition, add a new definition to the
//                 // local scope at the top of the scope stack.
//                 else if Some(capture.index) == layer.config.local_def_capture_index {
//                     reference_highlight = None;
//                     let scope = layer.scope_stack.last_mut().unwrap();

//                     let mut value_range = 0..0;
//                     for capture in match_.captures {
//                         if Some(capture.index) == layer.config.local_def_value_capture_index {
//                             value_range = capture.node.byte_range();
//                         }
//                     }

//                     let name = byte_range_to_str(range.clone(), self.source);
//                     scope.local_defs.push(LocalDef {
//                         name,
//                         value_range,
//                         highlight: None,
//                     });
//                     definition_highlight = scope.local_defs.last_mut().map(|s| &mut s.highlight);
//                 }
//                 // If the node represents a reference, then try to find the corresponding
//                 // definition in the scope stack.
//                 else if Some(capture.index) == layer.config.local_ref_capture_index
//                     && definition_highlight.is_none()
//                 {
//                     definition_highlight = None;
//                     let name = byte_range_to_str(range.clone(), self.source);
//                     for scope in layer.scope_stack.iter().rev() {
//                         if let Some(highlight) = scope.local_defs.iter().rev().find_map(|def| {
//                             if def.name == name && range.start >= def.value_range.end {
//                                 Some(def.highlight)
//                             } else {
//                                 None
//                             }
//                         }) {
//                             reference_highlight = highlight;
//                             break;
//                         }
//                         if !scope.inherits {
//                             break;
//                         }
//                     }
//                 }

//                 // Continue processing any additional matches for the same node.
//                 if let Some((next_match, next_capture_index)) = captures.peek() {
//                     let next_capture = next_match.captures[*next_capture_index];
//                     if next_capture.node == capture.node {
//                         capture = next_capture;
//                         match_ = captures.next().unwrap().0;
//                         continue;
//                     }
//                 }

//                 self.sort_layers();
//                 continue 'main;
//             }

//             // Otherwise, this capture must represent a highlight.
//             // If this exact range has already been highlighted by an earlier pattern, or by
//             // a different layer, then skip over this one.
//             if let Some((last_start, last_end, last_depth)) = self.last_highlight_range {
//                 if range.start == last_start && range.end == last_end && layer.depth < last_depth {
//                     self.sort_layers();
//                     continue 'main;
//                 }
//             }

//             // If the current node was found to be a local variable, then skip over any
//             // highlighting patterns that are disabled for local variables.
//             if definition_highlight.is_some() || reference_highlight.is_some() {
//                 while layer.config.non_local_variable_patterns[match_.pattern_index] {
//                     match_.remove();
//                     if let Some((next_match, next_capture_index)) = captures.peek() {
//                         let next_capture = next_match.captures[*next_capture_index];
//                         if next_capture.node == capture.node {
//                             capture = next_capture;
//                             match_ = captures.next().unwrap().0;
//                             continue;
//                         }
//                     }

//                     self.sort_layers();
//                     continue 'main;
//                 }
//             }

//             // Once a highlighting pattern is found for the current node, skip over
//             // any later highlighting patterns that also match this node. Captures
//             // for a given node are ordered by pattern index, so these subsequent
//             // captures are guaranteed to be for highlighting, not injections or
//             // local variables.
//             while let Some((next_match, next_capture_index)) = captures.peek() {
//                 let next_capture = next_match.captures[*next_capture_index];
//                 if next_capture.node == capture.node {
//                     captures.next();
//                 } else {
//                     break;
//                 }
//             }

//             let current_highlight = layer.config.highlight_indices.load()[capture.index as usize];

//             // If this node represents a local definition, then store the current
//             // highlight value on the local scope entry representing this node.
//             if let Some(definition_highlight) = definition_highlight {
//                 *definition_highlight = current_highlight;
//             }

//             // Emit a scope start event and push the node's end position to the stack.
//             if let Some(highlight) = reference_highlight.or(current_highlight) {
//                 self.last_highlight_range = Some((range.start, range.end, layer.depth));
//                 layer.highlight_end_stack.push(range.end);
//                 return self
//                     .emit_event(range.start, Some(HighlightEvent::HighlightStart(highlight)));
//             }

//             self.sort_layers();
//         }
//     }
// }

// impl Syntax {
//     /// Iterate over the highlighted regions for a given slice of source code.
//     pub fn highlight_iter<'a>(
//         &'a self,
//         source: RopeSlice<'a>,
//         range: Option<std::ops::Range<usize>>,
//         cancellation_flag: Option<&'a AtomicUsize>,
//     ) -> impl Iterator<Item = Result<HighlightEvent, Error>> + 'a {
//         let mut layers = self
//             .layers
//             .iter()
//             .filter_map(|(_, layer)| {
//                 // TODO: if range doesn't overlap layer range, skip it

//                 // Reuse a cursor from the pool if available.
//                 let mut cursor = PARSER.with(|ts_parser| {
//                     let highlighter = &mut ts_parser.borrow_mut();
//                     highlighter.cursors.pop().unwrap_or_else(QueryCursor::new)
//                 });

//                 // The `captures` iterator borrows the `Tree` and the `QueryCursor`, which
//                 // prevents them from being moved. But both of these values are really just
//                 // pointers, so it's actually ok to move them.
//                 let cursor_ref =
//                     unsafe { mem::transmute::<_, &'static mut QueryCursor>(&mut cursor) };

//                 // if reusing cursors & no range this resets to whole range
//                 cursor_ref.set_byte_range(range.clone().unwrap_or(0..usize::MAX));
//                 cursor_ref.set_match_limit(TREE_SITTER_MATCH_LIMIT);

//                 let mut captures = cursor_ref
//                     .captures(
//                         &layer.config.query,
//                         layer.tree().root_node(),
//                         RopeProvider(source),
//                     )
//                     .peekable();

//                 // If there's no captures, skip the layer
//                 captures.peek()?;

//                 Some(HighlightIterLayer {
//                     highlight_end_stack: Vec::new(),
//                     scope_stack: vec![LocalScope {
//                         inherits: false,
//                         range: 0..usize::MAX,
//                         local_defs: Vec::new(),
//                     }],
//                     cursor,
//                     _tree: None,
//                     captures: RefCell::new(captures),
//                     config: layer.config.as_ref(), // TODO: just reuse `layer`
//                     depth: layer.depth,            // TODO: just reuse `layer`
//                 })
//             })
//             .collect::<Vec<_>>();

//         layers.sort_unstable_by_key(|layer| layer.sort_key());

//         let mut result = HighlightIter {
//             source,
//             byte_offset: range.map_or(0, |r| r.start),
//             cancellation_flag,
//             iter_count: 0,
//             layers,
//             next_event: None,
//             last_highlight_range: None,
//         };
//         result.sort_layers();
//         result
//     }
// }