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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
use std::str::FromStr;

use crate::{
    buffer::BufferContent,
    buffer_position::{BufferPosition, BufferPositionIndex, BufferRange},
    glob::{Glob, InvalidGlobError},
    pattern::{MatchResult, Pattern, PatternError, PatternState},
};

#[cfg(not(debug_assertions))]
const MAX_HIGHLIGHT_BYTE_COUNT: usize = 128 * 1024;
#[cfg(debug_assertions)]
const MAX_HIGHLIGHT_BYTE_COUNT: usize = 8 * 1024;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenKind {
    Keyword,
    Type,
    Symbol,
    Literal,
    String,
    Comment,
    Text,
    Whitespace,
}
impl FromStr for TokenKind {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "keywords" => Ok(Self::Keyword),
            "types" => Ok(Self::Type),
            "symbols" => Ok(Self::Symbol),
            "literals" => Ok(Self::Literal),
            "strings" => Ok(Self::String),
            "comments" => Ok(Self::Comment),
            "texts" => Ok(Self::Text),
            _ => Err(()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Token {
    pub kind: TokenKind,
    pub from: BufferPositionIndex,
    pub to: BufferPositionIndex,
}
impl Token {
    pub fn contains(&self, column_byte_index: BufferPositionIndex) -> bool {
        self.from <= column_byte_index && column_byte_index < self.to
    }
}
impl Default for Token {
    fn default() -> Self {
        Self {
            kind: TokenKind::Text,
            from: 0,
            to: 0,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LineParseState {
    Dirty,
    Finished,
    Unfinished(TokenKind, PatternState),
}

impl Default for LineParseState {
    fn default() -> Self {
        Self::Dirty
    }
}

pub struct Syntax {
    glob: Glob,
    rules: [Pattern; 7],
}

impl Syntax {
    pub fn new() -> Self {
        let mut text_pattern = Pattern::new();
        let _ = text_pattern.compile("%a{%w_}|_{%w_}");
        Self {
            glob: Glob::default(),
            rules: [
                Pattern::new(),
                Pattern::new(),
                Pattern::new(),
                Pattern::new(),
                Pattern::new(),
                Pattern::new(),
                text_pattern,
            ],
        }
    }

    fn set_glob(&mut self, glob: &str) -> Result<(), InvalidGlobError> {
        self.glob.compile(glob)
    }

    pub fn set_rule(&mut self, kind: TokenKind, pattern: &str) -> Result<(), PatternError> {
        self.rules[kind as usize].compile(pattern)
    }

    fn parse_line(
        &self,
        line: &str,
        previous_parse_state: LineParseState,
        tokens: &mut Vec<Token>,
    ) -> LineParseState {
        tokens.clear();

        let mut index = 0;

        match previous_parse_state {
            LineParseState::Dirty => unreachable!(),
            LineParseState::Finished => (),
            LineParseState::Unfinished(kind, state) => {
                match self.rules[kind as usize].matches_with_state(line, 0, state) {
                    MatchResult::Ok(end) => {
                        tokens.push(Token {
                            kind,
                            from: 0,
                            to: end as _,
                        });
                        index = end;
                    }
                    MatchResult::Err => (),
                    MatchResult::Pending(state) => {
                        tokens.push(Token {
                            kind,
                            from: 0,
                            to: line.len() as _,
                        });
                        return LineParseState::Unfinished(kind, state);
                    }
                }
            }
        }

        while index < line.len() {
            let from = index;
            index += line[from..]
                .bytes()
                .take_while(u8::is_ascii_whitespace)
                .count();

            let mut best_pattern_kind = TokenKind::Text;
            let mut max_end = index;

            static ALL_NON_WHITESPACE_TOKEN_KINDS: [TokenKind; 7] = [
                TokenKind::Keyword,
                TokenKind::Type,
                TokenKind::Symbol,
                TokenKind::Literal,
                TokenKind::String,
                TokenKind::Comment,
                TokenKind::Text,
            ];

            for kind in ALL_NON_WHITESPACE_TOKEN_KINDS {
                let pattern = &self.rules[kind as usize];
                match pattern.matches(line, index) {
                    MatchResult::Ok(end) => {
                        if end > max_end {
                            max_end = end;
                            best_pattern_kind = kind;
                        }
                    }
                    MatchResult::Err => (),
                    MatchResult::Pending(state) => {
                        tokens.push(Token {
                            kind,
                            from: from as _,
                            to: line.len() as _,
                        });
                        return LineParseState::Unfinished(kind, state);
                    }
                }
            }

            let mut kind = best_pattern_kind;

            if max_end == index {
                kind = TokenKind::Text;
                max_end += line.as_bytes()[index..]
                    .iter()
                    .take_while(|b| b.is_ascii_alphanumeric())
                    .count()
                    .max(1);

                max_end = max_end.min(line.len());
                while !line.is_char_boundary(max_end) {
                    max_end += 1;
                }
            }

            index = max_end;

            tokens.push(Token {
                kind,
                from: from as _,
                to: index as _,
            });
        }

        LineParseState::Finished
    }
}

#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct SyntaxHandle(u32);

pub struct SyntaxCollection {
    syntaxes: Vec<Syntax>,
}

impl SyntaxCollection {
    pub fn new() -> Self {
        Self {
            syntaxes: vec![Syntax::new()],
        }
    }

    pub fn find_handle_by_path(&self, path: &str) -> Option<SyntaxHandle> {
        for (i, syntax) in self.syntaxes.iter().enumerate().rev() {
            if syntax.glob.matches(path) {
                return Some(SyntaxHandle(i as _));
            }
        }

        None
    }

    pub(crate) fn add_from_glob(&mut self, glob: &str) -> Result<(), InvalidGlobError> {
        let mut syntax = Syntax::new();
        syntax.set_glob(glob)?;
        self.syntaxes.push(syntax);
        Ok(())
    }

    pub(crate) fn get_last(&mut self) -> &mut Syntax {
        self.syntaxes.last_mut().unwrap()
    }

    pub fn get(&self, handle: SyntaxHandle) -> &Syntax {
        &self.syntaxes[handle.0 as usize]
    }
}

#[derive(Default)]
struct HighlightedLine {
    parse_state: LineParseState,
    tokens: Vec<Token>,
}

pub enum HighlightResult {
    Complete,
    Pending,
}

pub struct HighlightedBuffer {
    highlighted_len: usize,
    lines: Vec<HighlightedLine>,
    dirty_line_indexes: Vec<BufferPositionIndex>,
}

impl HighlightedBuffer {
    pub fn new() -> Self {
        Self {
            highlighted_len: 1,
            lines: vec![HighlightedLine::default()],
            dirty_line_indexes: Vec::new(),
        }
    }

    pub fn clear(&mut self) {
        self.highlighted_len = 1;
        self.dirty_line_indexes.clear();
    }

    pub fn insert_range(&mut self, range: BufferRange) {
        if self.highlighted_len <= range.from.line_index as _ {
            return;
        }

        let insert_line_count = range.to.line_index - range.from.line_index;
        if insert_line_count > 0 {
            let previous_highlighted_len = self.highlighted_len;
            self.highlighted_len += insert_line_count as usize;
            if self.highlighted_len > self.lines.len() {
                for line in &mut self.lines[previous_highlighted_len..] {
                    line.parse_state = LineParseState::Dirty;
                }
                self.lines
                    .resize_with(self.highlighted_len, HighlightedLine::default);
            } else {
                for line in &mut self.lines[previous_highlighted_len..self.highlighted_len] {
                    line.parse_state = LineParseState::Dirty;
                }
            }

            let insert_index = range.from.line_index + 1;
            self.lines[insert_index as usize..self.highlighted_len as usize]
                .rotate_right(insert_line_count as _);

            for index in &mut self.dirty_line_indexes {
                if insert_index <= *index {
                    *index += insert_line_count;
                }
            }
        }

        self.lines[range.from.line_index as usize].parse_state = LineParseState::Dirty;
        self.dirty_line_indexes.push(range.from.line_index);
    }

    pub fn delete_range(&mut self, range: BufferRange) {
        if self.lines.len() <= range.to.line_index as _ {
            return;
        }

        self.lines[range.from.line_index as usize].parse_state = LineParseState::Dirty;

        let delete_line_count = range.to.line_index - range.from.line_index;
        if delete_line_count > 0 {
            self.highlighted_len -= delete_line_count as usize;
            let delete_index = range.from.line_index + 1;
            self.lines[delete_index as usize..].rotate_left(delete_line_count as _);

            for index in &mut self.dirty_line_indexes {
                if range.to.line_index <= *index {
                    *index -= delete_line_count;
                } else if delete_index <= *index {
                    *index = range.from.line_index;
                }
            }
        }

        self.dirty_line_indexes.push(range.from.line_index);
    }

    pub fn highlight_dirty_lines(
        &mut self,
        syntax: &Syntax,
        buffer: &BufferContent,
    ) -> HighlightResult {
        let buffer_lines = buffer.lines();
        if self.highlighted_len < buffer_lines.len() {
            self.insert_range(BufferRange::between(
                BufferPosition::line_col((self.highlighted_len - 1) as _, 0),
                BufferPosition::line_col((buffer_lines.len() - 1) as _, 0),
            ));
        }

        if self.dirty_line_indexes.is_empty() {
            return HighlightResult::Complete;
        }

        self.dirty_line_indexes.sort_unstable();

        let mut index = self.dirty_line_indexes[0];
        let mut previous_dirty_index = BufferPositionIndex::MAX;

        let mut previous_parse_state = match index.checked_sub(1) {
            Some(i) => self.lines[i as usize].parse_state,
            None => LineParseState::Finished,
        };

        let mut i = 0;
        let mut highlighted_byte_count = 0;
        while i < self.dirty_line_indexes.len() {
            let dirty_index = self.dirty_line_indexes[i];
            i += 1;

            if dirty_index < index || dirty_index == previous_dirty_index {
                continue;
            }

            index = dirty_index;
            previous_dirty_index = dirty_index;

            while index < self.highlighted_len as _ {
                let bline = buffer_lines[index as usize].as_str();
                let hline = &mut self.lines[index as usize];

                let previous_state = hline.parse_state;
                previous_parse_state =
                    syntax.parse_line(bline, previous_parse_state, &mut hline.tokens);
                hline.parse_state = previous_parse_state;

                index += 1;
                highlighted_byte_count += bline.len();

                if MAX_HIGHLIGHT_BYTE_COUNT < highlighted_byte_count {
                    i -= 1;
                    self.dirty_line_indexes[i] = index;
                    self.dirty_line_indexes.drain(..i);

                    return HighlightResult::Pending;
                }

                if previous_state == LineParseState::Finished
                    && previous_parse_state == LineParseState::Finished
                {
                    break;
                }
            }
        }

        self.dirty_line_indexes.clear();
        HighlightResult::Complete
    }

    pub fn line_tokens(&self, line_index: usize) -> &[Token] {
        if line_index < self.highlighted_len {
            &self.lines[line_index].tokens
        } else {
            &[]
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::ops::Range;

    use crate::buffer_position::BufferPosition;

    fn assert_next_token<'a, I>(iter: &mut I, kind: TokenKind, range: Range<usize>)
    where
        I: Iterator<Item = &'a Token>,
    {
        let expect = Some(Token {
            kind,
            from: range.start as _,
            to: range.end as _,
        });
        assert_eq!(expect, iter.next().cloned());
    }

    fn highlighted_tokens(highlighted: &HighlightedBuffer) -> impl Iterator<Item = &Token> {
        highlighted.lines[..highlighted.highlighted_len]
            .iter()
            .flat_map(|l| l.tokens.iter())
    }

    fn assert_token(slice: &str, kind: TokenKind, line: &str, token: &Token) {
        assert_eq!(kind, token.kind);
        assert_eq!(slice, &line[token.from as usize..token.to as usize]);
    }

    #[test]
    fn no_syntax() {
        let syntax = Syntax::new();
        let mut tokens = Vec::new();
        let line = " fn main() ;  ";
        let parse_state = syntax.parse_line(line, LineParseState::Finished, &mut tokens);

        assert_eq!(LineParseState::Finished, parse_state);
        assert_eq!(6, tokens.len());
        assert_token(" fn", TokenKind::Text, line, &tokens[0]);
        assert_token(" main", TokenKind::Text, line, &tokens[1]);
        assert_token("(", TokenKind::Text, line, &tokens[2]);
        assert_token(")", TokenKind::Text, line, &tokens[3]);
        assert_token(" ;", TokenKind::Text, line, &tokens[4]);
        assert_token("  ", TokenKind::Text, line, &tokens[5]);
    }

    #[test]
    fn one_rule_syntax() {
        let mut syntax = Syntax::new();
        syntax.set_rule(TokenKind::Symbol, ";").unwrap();

        let mut tokens = Vec::new();
        let line = " fn main() ;  ";
        let parse_state = syntax.parse_line(line, LineParseState::Finished, &mut tokens);

        assert_eq!(LineParseState::Finished, parse_state);
        assert_eq!(6, tokens.len());
        assert_token(" fn", TokenKind::Text, line, &tokens[0]);
        assert_token(" main", TokenKind::Text, line, &tokens[1]);
        assert_token("(", TokenKind::Text, line, &tokens[2]);
        assert_token(")", TokenKind::Text, line, &tokens[3]);
        assert_token(" ;", TokenKind::Symbol, line, &tokens[4]);
        assert_token("  ", TokenKind::Text, line, &tokens[5]);
    }

    #[test]
    fn simple_syntax() {
        let mut syntax = Syntax::new();
        syntax.set_rule(TokenKind::Keyword, "fn").unwrap();
        syntax.set_rule(TokenKind::Symbol, "%(|%)").unwrap();

        let mut tokens = Vec::new();
        let line = " fn main() ;  ";
        let parse_state = syntax.parse_line(line, LineParseState::Finished, &mut tokens);

        assert_eq!(LineParseState::Finished, parse_state);
        assert_eq!(6, tokens.len());
        assert_token(" fn", TokenKind::Keyword, line, &tokens[0]);
        assert_token(" main", TokenKind::Text, line, &tokens[1]);
        assert_token("(", TokenKind::Symbol, line, &tokens[2]);
        assert_token(")", TokenKind::Symbol, line, &tokens[3]);
        assert_token(" ;", TokenKind::Text, line, &tokens[4]);
        assert_token("  ", TokenKind::Text, line, &tokens[5]);
    }

    #[test]
    fn beginning_anchor_syntax() {
        let mut syntax = Syntax::new();
        syntax.set_rule(TokenKind::Keyword, "^{%w}").unwrap();

        let mut tokens = Vec::new();
        let line = "first second";
        let parse_state = syntax.parse_line(line, LineParseState::Finished, &mut tokens);

        assert_eq!(LineParseState::Finished, parse_state);
        assert_eq!(2, tokens.len());
        assert_token("first", TokenKind::Keyword, line, &tokens[0]);
        assert_token(" second", TokenKind::Text, line, &tokens[1]);
    }

    #[test]
    fn multiline_syntax() {
        let mut syntax = Syntax::new();
        syntax.set_rule(TokenKind::Comment, "/*{!(*/).$}").unwrap();

        let mut tokens = Vec::new();
        let line0 = "before /* comment";
        let line1 = "only comment";
        let line2 = "still comment */ after";

        let line0_kind = syntax.parse_line(line0, LineParseState::Finished, &mut tokens);
        match line0_kind {
            LineParseState::Unfinished(i, _) => assert_eq!(TokenKind::Comment, i),
            _ => panic!("{:?}", line0_kind),
        }
        assert_eq!(2, tokens.len());
        assert_token("before", TokenKind::Text, line0, &tokens[0]);
        assert_token(" /* comment", TokenKind::Comment, line0, &tokens[1]);

        let line1_kind = syntax.parse_line(line1, line0_kind, &mut tokens);
        match line1_kind {
            LineParseState::Unfinished(i, _) => assert_eq!(TokenKind::Comment, i),
            _ => panic!("{:?}", line1_kind),
        }
        assert_eq!(1, tokens.len());
        assert_token("only comment", TokenKind::Comment, line1, &tokens[0]);

        let line2_kind = syntax.parse_line(line2, line1_kind, &mut tokens);
        assert_eq!(LineParseState::Finished, line2_kind);
        assert_eq!(2, tokens.len());
        assert_token("still comment */", TokenKind::Comment, line2, &tokens[0]);
        assert_token(" after", TokenKind::Text, line2, &tokens[1]);
    }

    #[test]
    fn editing_highlighted_buffer() {
        let mut syntax = Syntax::new();
        syntax.set_rule(TokenKind::Comment, "/*{!(*/).$}").unwrap();
        syntax.set_rule(TokenKind::String, "'{!'.$}").unwrap();

        let mut buffer = BufferContent::new();
        let mut highlighted = HighlightedBuffer::new();

        let range = buffer.insert_text(BufferPosition::zero(), "/*\n*/");
        highlighted.insert_range(range);
        highlighted.highlight_dirty_lines(&syntax, &buffer);
        assert_eq!(buffer.lines().len(), highlighted.lines.len());

        {
            let mut tokens = highlighted_tokens(&highlighted);
            assert_next_token(&mut tokens, TokenKind::Comment, 0..2);
            assert_next_token(&mut tokens, TokenKind::Comment, 0..2);
            assert_eq!(None, tokens.next());
        }

        let range = buffer.insert_text(BufferPosition::line_col(1, 0), "'");
        highlighted.insert_range(range);
        highlighted.highlight_dirty_lines(&syntax, &buffer);

        {
            let mut tokens = highlighted_tokens(&highlighted);
            assert_next_token(&mut tokens, TokenKind::Comment, 0..2);
            assert_next_token(&mut tokens, TokenKind::Comment, 0..3);
            assert_eq!(None, tokens.next());
        }
    }

    #[test]
    fn highlight_range_after_unfinished_line() {
        let mut syntax = Syntax::new();
        syntax.set_rule(TokenKind::Comment, "/*{!(*/).$}").unwrap();

        let mut buffer = BufferContent::new();
        let mut highlighted = HighlightedBuffer::new();

        let range = buffer.insert_text(BufferPosition::zero(), "/*\n\n\n*/");
        highlighted.insert_range(range);
        highlighted.highlight_dirty_lines(&syntax, &buffer);
        assert_eq!(buffer.lines().len(), highlighted.lines.len());

        let mut tokens = highlighted_tokens(&highlighted);
        assert_next_token(&mut tokens, TokenKind::Comment, 0..2);
        assert_next_token(&mut tokens, TokenKind::Comment, 0..0);
        assert_next_token(&mut tokens, TokenKind::Comment, 0..0);
        assert_next_token(&mut tokens, TokenKind::Comment, 0..2);
        assert_eq!(None, tokens.next());
    }

    #[test]
    fn highlight_lines_after_unfinished_to_finished() {
        let mut syntax = Syntax::new();
        syntax.set_rule(TokenKind::Comment, "/*{!(*/).$}").unwrap();

        let mut buffer = BufferContent::new();
        let mut highlighted = HighlightedBuffer::new();

        let range = buffer.insert_text(BufferPosition::zero(), "/*\n* /\n*/");
        highlighted.insert_range(range);
        highlighted.highlight_dirty_lines(&syntax, &buffer);

        let range = BufferRange::between(
            BufferPosition::line_col(1, 1),
            BufferPosition::line_col(1, 2),
        );
        buffer.delete_range(range);
        highlighted.delete_range(range);
        highlighted.highlight_dirty_lines(&syntax, &buffer);

        let mut parse_states = highlighted.lines[..highlighted.highlighted_len]
            .iter()
            .map(|l| l.parse_state);
        assert!(matches!(
            parse_states.next(),
            Some(LineParseState::Unfinished(_, _))
        ));
        assert_eq!(Some(LineParseState::Finished), parse_states.next());
        assert_eq!(Some(LineParseState::Finished), parse_states.next());
        assert_eq!(None, parse_states.next());

        {
            let mut tokens = highlighted_tokens(&highlighted);
            assert_next_token(&mut tokens, TokenKind::Comment, 0..2);
            assert_next_token(&mut tokens, TokenKind::Comment, 0..2);
            assert_next_token(&mut tokens, TokenKind::Text, 0..1);
            assert_next_token(&mut tokens, TokenKind::Text, 1..2);
            assert_eq!(None, tokens.next());
        }
    }

    #[test]
    fn highlight_lines_after_became_unfinished() {
        let mut syntax = Syntax::new();
        syntax.set_rule(TokenKind::Comment, "/*{!(*/).$}").unwrap();

        let mut buffer = BufferContent::new();
        let mut highlighted = HighlightedBuffer::new();

        let range = buffer.insert_text(BufferPosition::zero(), "/ *\na\n*/");
        highlighted.insert_range(range);
        highlighted.highlight_dirty_lines(&syntax, &buffer);

        let range = BufferRange::between(
            BufferPosition::line_col(0, 1),
            BufferPosition::line_col(0, 2),
        );
        buffer.delete_range(range);
        highlighted.delete_range(range);
        highlighted.highlight_dirty_lines(&syntax, &buffer);

        let mut tokens = highlighted_tokens(&highlighted);
        assert_next_token(&mut tokens, TokenKind::Comment, 0..2);
        assert_next_token(&mut tokens, TokenKind::Comment, 0..1);
        assert_next_token(&mut tokens, TokenKind::Comment, 0..2);
        assert_eq!(None, tokens.next());
    }

    #[test]
    fn highlight_unfinished_lines_on_multiline_delete() {
        let mut syntax = Syntax::new();
        syntax.set_rule(TokenKind::Comment, "/*{!(*/).$}").unwrap();

        let mut buffer = BufferContent::new();
        let mut highlighted = HighlightedBuffer::new();

        let range = buffer.insert_text(BufferPosition::zero(), "a\n/*\nb\nc*/");
        highlighted.insert_range(range);
        highlighted.highlight_dirty_lines(&syntax, &buffer);
        assert_eq!(buffer.lines().len(), highlighted.highlighted_len);

        {
            let mut tokens = highlighted_tokens(&highlighted);
            assert_next_token(&mut tokens, TokenKind::Text, 0..1);
            assert_next_token(&mut tokens, TokenKind::Comment, 0..2);
            assert_next_token(&mut tokens, TokenKind::Comment, 0..1);
            assert_next_token(&mut tokens, TokenKind::Comment, 0..3);
            assert_eq!(None, tokens.next());
        }

        let range = BufferRange::between(BufferPosition::zero(), BufferPosition::line_col(1, 1));
        buffer.delete_range(range);
        highlighted.delete_range(range);
        highlighted.highlight_dirty_lines(&syntax, &buffer);
        assert_eq!(buffer.lines().len(), highlighted.highlighted_len);

        {
            let mut tokens = highlighted_tokens(&highlighted);
            assert_next_token(&mut tokens, TokenKind::Text, 0..1);
            assert_next_token(&mut tokens, TokenKind::Text, 0..1);
            assert_next_token(&mut tokens, TokenKind::Text, 0..1);
            assert_next_token(&mut tokens, TokenKind::Text, 1..2);
            assert_next_token(&mut tokens, TokenKind::Text, 2..3);
            assert_eq!(None, tokens.next());
        }
    }
}