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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
//! Syntax highlighting with tree-sitter
//!
//! # Design
//! - **Viewport-only parsing**: Only highlights visible lines for instant performance with large files
//! - **Incremental updates**: Re-parses only edited regions
//! - **Lazy initialization**: Parsing happens on first render
//!
//! # Performance
//! Must work instantly when loading a 1GB file and jumping to an arbitrary offset.
//! This is achieved by only parsing the visible viewport (~50 lines), not the entire file.
use crate::config::LARGE_FILE_THRESHOLD_BYTES;
use crate::model::buffer::Buffer;
use crate::view::theme::Theme;
use ratatui::style::Color;
use std::ops::Range;
use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter as TSHighlighter};
/// Maximum bytes to parse in a single operation (for viewport highlighting)
const MAX_PARSE_BYTES: usize = LARGE_FILE_THRESHOLD_BYTES as usize; // 1MB
/// Highlight category names used for default languages.
/// The order matches the `configure()` call in `highlight_config()`.
/// Index 0 = attribute, 1 = comment, 2 = constant, 3 = function, 4 = keyword,
/// 5 = number, 6 = operator, 7 = property, 8 = string, 9 = type, 10 = variable
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HighlightCategory {
Attribute,
Comment,
Constant,
Function,
Keyword,
Number,
Operator,
Property,
String,
Type,
Variable,
}
impl HighlightCategory {
/// Map a default language highlight index to a category
fn from_default_index(index: usize) -> Option<Self> {
match index {
0 => Some(Self::Attribute),
1 => Some(Self::Comment),
2 => Some(Self::Constant),
3 => Some(Self::Function),
4 => Some(Self::Keyword),
5 => Some(Self::Number),
6 => Some(Self::Operator),
7 => Some(Self::Property),
8 => Some(Self::String),
9 => Some(Self::Type),
10 => Some(Self::Variable),
_ => None,
}
}
/// Map a TypeScript highlight index to a category.
/// TypeScript has more categories; we map them to the closest theme color.
fn from_typescript_index(index: usize) -> Option<Self> {
match index {
0 => Some(Self::Attribute), // attribute
1 => Some(Self::Comment), // comment
2 => Some(Self::Constant), // constant
3 => Some(Self::Constant), // constant.builtin
4 => Some(Self::Type), // constructor
5 => Some(Self::String), // embedded (template substitutions)
6 => Some(Self::Function), // function
7 => Some(Self::Function), // function.builtin
8 => Some(Self::Function), // function.method
9 => Some(Self::Keyword), // keyword
10 => Some(Self::Number), // number
11 => Some(Self::Operator), // operator
12 => Some(Self::Property), // property
13 => Some(Self::Operator), // punctuation.bracket
14 => Some(Self::Operator), // punctuation.delimiter
15 => Some(Self::Constant), // punctuation.special (template ${})
16 => Some(Self::String), // string
17 => Some(Self::String), // string.special (regex)
18 => Some(Self::Type), // type
19 => Some(Self::Type), // type.builtin
20 => Some(Self::Variable), // variable
21 => Some(Self::Constant), // variable.builtin (this, super, arguments)
22 => Some(Self::Variable), // variable.parameter
_ => None,
}
}
/// Get the color for this category from the theme
pub fn color(&self, theme: &Theme) -> Color {
match self {
Self::Attribute => theme.syntax_constant, // No specific attribute color, use constant
Self::Comment => theme.syntax_comment,
Self::Constant => theme.syntax_constant,
Self::Function => theme.syntax_function,
Self::Keyword => theme.syntax_keyword,
Self::Number => theme.syntax_constant,
Self::Operator => theme.syntax_operator,
Self::Property => theme.syntax_variable, // Properties are like variables
Self::String => theme.syntax_string,
Self::Type => theme.syntax_type,
Self::Variable => theme.syntax_variable,
}
}
}
/// A highlighted span of text
#[derive(Debug, Clone)]
pub struct HighlightSpan {
/// Byte range in the buffer
pub range: Range<usize>,
/// Color for this span
pub color: Color,
}
/// Internal span used for caching (stores category instead of color)
#[derive(Debug, Clone)]
struct CachedSpan {
/// Byte range in the buffer
range: Range<usize>,
/// Highlight category for this span
category: HighlightCategory,
}
/// Language configuration for syntax highlighting
#[derive(Debug, Clone, Copy)]
pub enum Language {
Rust,
Python,
JavaScript,
TypeScript,
HTML,
CSS,
C,
Cpp,
Go,
Json,
Java,
CSharp,
Php,
Ruby,
Bash,
Lua,
// Markdown, // Disabled due to tree-sitter version conflict
}
impl Language {
/// Detect language from file extension
pub fn from_path(path: &std::path::Path) -> Option<Self> {
match path.extension()?.to_str()? {
"rs" => Some(Language::Rust),
"py" => Some(Language::Python),
"js" | "jsx" => Some(Language::JavaScript),
"ts" | "tsx" => Some(Language::TypeScript),
"html" => Some(Language::HTML),
"css" => Some(Language::CSS),
"c" | "h" => Some(Language::C),
"cpp" | "hpp" | "cc" | "hh" | "cxx" | "hxx" => Some(Language::Cpp),
"go" => Some(Language::Go),
"json" => Some(Language::Json),
"java" => Some(Language::Java),
"cs" => Some(Language::CSharp),
"php" => Some(Language::Php),
"rb" => Some(Language::Ruby),
"sh" | "bash" => Some(Language::Bash),
"lua" => Some(Language::Lua),
// "md" => Some(Language::Markdown), // Disabled
_ => None,
}
}
/// Get tree-sitter highlight configuration for this language
fn highlight_config(&self) -> Result<HighlightConfiguration, String> {
match self {
Language::Rust => {
let mut config = HighlightConfiguration::new(
tree_sitter_rust::LANGUAGE.into(),
"rust",
tree_sitter_rust::HIGHLIGHTS_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create Rust highlight config: {e}"))?;
// Configure highlight names
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::Python => {
let mut config = HighlightConfiguration::new(
tree_sitter_python::LANGUAGE.into(),
"python",
tree_sitter_python::HIGHLIGHTS_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create Python highlight config: {e}"))?;
// Configure highlight names
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::JavaScript => {
let mut config = HighlightConfiguration::new(
tree_sitter_javascript::LANGUAGE.into(),
"javascript",
tree_sitter_javascript::HIGHLIGHT_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create JavaScript highlight config: {e}"))?;
// Configure highlight names
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::TypeScript => {
// TypeScript extends JavaScript, so we need to combine queries
// TypeScript-specific highlights come first (higher priority),
// followed by JavaScript base highlights
let combined_highlights = format!(
"{}\n{}",
tree_sitter_typescript::HIGHLIGHTS_QUERY,
tree_sitter_javascript::HIGHLIGHT_QUERY
);
let mut config = HighlightConfiguration::new(
tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
"typescript",
&combined_highlights,
"", // injections query
tree_sitter_typescript::LOCALS_QUERY, // locals query for proper scoping
)
.map_err(|e| format!("Failed to create TypeScript highlight config: {e}"))?;
// Configure highlight names - must include all captures from both JS and TS queries
config.configure(&[
"attribute",
"comment",
"constant",
"constant.builtin",
"constructor",
"embedded",
"function",
"function.builtin",
"function.method",
"keyword",
"number",
"operator",
"property",
"punctuation.bracket",
"punctuation.delimiter",
"punctuation.special",
"string",
"string.special",
"type",
"type.builtin",
"variable",
"variable.builtin",
"variable.parameter",
]);
Ok(config)
}
Language::HTML => {
let mut config = HighlightConfiguration::new(
tree_sitter_html::LANGUAGE.into(),
"html",
tree_sitter_html::HIGHLIGHTS_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create HTML highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::CSS => {
let mut config = HighlightConfiguration::new(
tree_sitter_css::LANGUAGE.into(),
"css",
tree_sitter_css::HIGHLIGHTS_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create CSS highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::C => {
let mut config = HighlightConfiguration::new(
tree_sitter_c::LANGUAGE.into(),
"c",
tree_sitter_c::HIGHLIGHT_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create C highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::Cpp => {
let mut config = HighlightConfiguration::new(
tree_sitter_cpp::LANGUAGE.into(),
"cpp",
tree_sitter_cpp::HIGHLIGHT_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create C++ highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::Go => {
let mut config = HighlightConfiguration::new(
tree_sitter_go::LANGUAGE.into(),
"go",
tree_sitter_go::HIGHLIGHTS_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create Go highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::Json => {
let mut config = HighlightConfiguration::new(
tree_sitter_json::LANGUAGE.into(),
"json",
tree_sitter_json::HIGHLIGHTS_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create JSON highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::Java => {
let mut config = HighlightConfiguration::new(
tree_sitter_java::LANGUAGE.into(),
"java",
tree_sitter_java::HIGHLIGHTS_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create Java highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::CSharp => {
// Note: tree-sitter-c-sharp doesn't export HIGHLIGHTS_QUERY
// Using empty query for now - basic parsing still works
let mut config = HighlightConfiguration::new(
tree_sitter_c_sharp::LANGUAGE.into(),
"c_sharp",
"", // No HIGHLIGHTS_QUERY exported in 0.23.1
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create C# highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::Php => {
let mut config = HighlightConfiguration::new(
tree_sitter_php::LANGUAGE_PHP.into(),
"php",
tree_sitter_php::HIGHLIGHTS_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create PHP highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::Ruby => {
let mut config = HighlightConfiguration::new(
tree_sitter_ruby::LANGUAGE.into(),
"ruby",
tree_sitter_ruby::HIGHLIGHTS_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create Ruby highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::Bash => {
let mut config = HighlightConfiguration::new(
tree_sitter_bash::LANGUAGE.into(),
"bash",
tree_sitter_bash::HIGHLIGHT_QUERY, // Note: singular, not plural
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create Bash highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
}
Language::Lua => {
let mut config = HighlightConfiguration::new(
tree_sitter_lua::LANGUAGE.into(),
"lua",
tree_sitter_lua::HIGHLIGHTS_QUERY,
"", // injections query
"", // locals query
)
.map_err(|e| format!("Failed to create Lua highlight config: {e}"))?;
config.configure(&[
"attribute",
"comment",
"constant",
"function",
"keyword",
"number",
"operator",
"property",
"string",
"type",
"variable",
]);
Ok(config)
} // Language::Markdown => {
// // Disabled due to tree-sitter version conflict
// Err("Markdown highlighting not available".to_string())
// }
}
}
/// Map tree-sitter highlight index to a highlight category
fn highlight_category(&self, index: usize) -> Option<HighlightCategory> {
match self {
Language::TypeScript => HighlightCategory::from_typescript_index(index),
_ => HighlightCategory::from_default_index(index),
}
}
}
/// Cache of highlighted spans for a specific byte range
#[derive(Debug, Clone)]
struct HighlightCache {
/// Byte range this cache covers
range: Range<usize>,
/// Highlighted spans within this range (stores categories for theme-independent caching)
spans: Vec<CachedSpan>,
}
/// Syntax highlighter with incremental viewport-based parsing
pub struct Highlighter {
/// Tree-sitter highlighter instance
ts_highlighter: TSHighlighter,
/// Language being highlighted
language: Language,
/// Highlight configuration for the language
config: HighlightConfiguration,
/// Cache of highlighted spans (only for visible viewport)
cache: Option<HighlightCache>,
/// Last known buffer length (for detecting complete buffer changes)
last_buffer_len: usize,
}
impl Highlighter {
/// Create a new highlighter for the given language
pub fn new(language: Language) -> Result<Self, String> {
let config = language.highlight_config()?;
Ok(Self {
ts_highlighter: TSHighlighter::new(),
language,
config,
cache: None,
last_buffer_len: 0,
})
}
/// Highlight the visible viewport range
///
/// This only parses the visible lines for instant performance with large files.
/// Returns highlighted spans for the requested byte range, colored according to the theme.
///
/// `context_bytes` controls how far before/after the viewport to parse for accurate
/// highlighting of multi-line constructs (strings, comments, nested blocks).
pub fn highlight_viewport(
&mut self,
buffer: &Buffer,
viewport_start: usize,
viewport_end: usize,
theme: &Theme,
context_bytes: usize,
) -> Vec<HighlightSpan> {
// Check if cache is valid for this range
if let Some(cache) = &self.cache {
if cache.range.start <= viewport_start
&& cache.range.end >= viewport_end
&& self.last_buffer_len == buffer.len()
{
// Cache hit! Filter spans to the requested range and resolve colors from theme
return cache
.spans
.iter()
.filter(|span| {
span.range.start < viewport_end && span.range.end > viewport_start
})
.map(|span| HighlightSpan {
range: span.range.clone(),
color: span.category.color(theme),
})
.collect();
}
}
// Cache miss - need to parse
// Extend range for context (helps with multi-line constructs like strings, comments, nested blocks)
let parse_start = viewport_start.saturating_sub(context_bytes);
let parse_end = (viewport_end + context_bytes).min(buffer.len());
let parse_range = parse_start..parse_end;
// Limit parse size for safety
if parse_range.len() > MAX_PARSE_BYTES {
tracing::warn!(
"Parse range too large: {} bytes, truncating to {}",
parse_range.len(),
MAX_PARSE_BYTES
);
// Just return empty spans if the range is too large
return Vec::new();
}
// Extract source bytes from buffer
let source = buffer.slice_bytes(parse_range.clone());
// Highlight the source - store categories for theme-independent caching
let mut cached_spans = Vec::new();
match self.ts_highlighter.highlight(
&self.config,
&source,
None, // cancellation flag
|_| None, // injection callback
) {
Ok(highlights) => {
let mut current_highlight: Option<usize> = None;
for event in highlights {
match event {
Ok(HighlightEvent::Source { start, end }) => {
let span_start = parse_start + start;
let span_end = parse_start + end;
if let Some(highlight_idx) = current_highlight {
if let Some(category) =
self.language.highlight_category(highlight_idx)
{
cached_spans.push(CachedSpan {
range: span_start..span_end,
category,
});
}
}
}
Ok(HighlightEvent::HighlightStart(s)) => {
current_highlight = Some(s.0);
}
Ok(HighlightEvent::HighlightEnd) => {
current_highlight = None;
}
Err(e) => {
tracing::warn!("Highlight error: {}", e);
break;
}
}
}
}
Err(e) => {
tracing::error!("Failed to highlight: {}", e);
}
}
// Update cache
self.cache = Some(HighlightCache {
range: parse_range,
spans: cached_spans.clone(),
});
self.last_buffer_len = buffer.len();
// Filter to requested viewport and resolve colors from theme
cached_spans
.into_iter()
.filter(|span| span.range.start < viewport_end && span.range.end > viewport_start)
.map(|span| HighlightSpan {
range: span.range,
color: span.category.color(theme),
})
.collect()
}
/// Invalidate cache for an edited range
///
/// Call this when the buffer is edited to mark the cache as stale.
pub fn invalidate_range(&mut self, edit_range: Range<usize>) {
if let Some(cache) = &self.cache {
// If edit intersects cache, invalidate it
if edit_range.start < cache.range.end && edit_range.end > cache.range.start {
self.cache = None;
}
}
}
/// Invalidate entire cache
pub fn invalidate_all(&mut self) {
self.cache = None;
}
/// Get the current language
pub fn language(&self) -> &Language {
&self.language
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::buffer::Buffer;
#[test]
fn test_language_detection() {
let path = std::path::Path::new("test.rs");
assert!(matches!(Language::from_path(path), Some(Language::Rust)));
let path = std::path::Path::new("test.py");
assert!(matches!(Language::from_path(path), Some(Language::Python)));
let path = std::path::Path::new("test.js");
assert!(matches!(
Language::from_path(path),
Some(Language::JavaScript)
));
let path = std::path::Path::new("test.jsx");
assert!(matches!(
Language::from_path(path),
Some(Language::JavaScript)
));
let path = std::path::Path::new("test.ts");
assert!(matches!(
Language::from_path(path),
Some(Language::TypeScript)
));
let path = std::path::Path::new("test.tsx");
assert!(matches!(
Language::from_path(path),
Some(Language::TypeScript)
));
let path = std::path::Path::new("test.html");
assert!(matches!(Language::from_path(path), Some(Language::HTML)));
let path = std::path::Path::new("test.css");
assert!(matches!(Language::from_path(path), Some(Language::CSS)));
let path = std::path::Path::new("test.c");
assert!(matches!(Language::from_path(path), Some(Language::C)));
let path = std::path::Path::new("test.h");
assert!(matches!(Language::from_path(path), Some(Language::C)));
let path = std::path::Path::new("test.cpp");
assert!(matches!(Language::from_path(path), Some(Language::Cpp)));
let path = std::path::Path::new("test.hpp");
assert!(matches!(Language::from_path(path), Some(Language::Cpp)));
let path = std::path::Path::new("test.cc");
assert!(matches!(Language::from_path(path), Some(Language::Cpp)));
let path = std::path::Path::new("test.hh");
assert!(matches!(Language::from_path(path), Some(Language::Cpp)));
let path = std::path::Path::new("test.cxx");
assert!(matches!(Language::from_path(path), Some(Language::Cpp)));
let path = std::path::Path::new("test.hxx");
assert!(matches!(Language::from_path(path), Some(Language::Cpp)));
let path = std::path::Path::new("test.go");
assert!(matches!(Language::from_path(path), Some(Language::Go)));
let path = std::path::Path::new("test.json");
assert!(matches!(Language::from_path(path), Some(Language::Json)));
let path = std::path::Path::new("test.java");
assert!(matches!(Language::from_path(path), Some(Language::Java)));
let path = std::path::Path::new("test.cs");
assert!(matches!(Language::from_path(path), Some(Language::CSharp)));
let path = std::path::Path::new("test.php");
assert!(matches!(Language::from_path(path), Some(Language::Php)));
let path = std::path::Path::new("test.rb");
assert!(matches!(Language::from_path(path), Some(Language::Ruby)));
let path = std::path::Path::new("test.sh");
assert!(matches!(Language::from_path(path), Some(Language::Bash)));
let path = std::path::Path::new("test.bash");
assert!(matches!(Language::from_path(path), Some(Language::Bash)));
let path = std::path::Path::new("test.lua");
assert!(matches!(Language::from_path(path), Some(Language::Lua)));
// Markdown disabled due to tree-sitter version conflict
// let path = std::path::Path::new("test.md");
// assert!(matches!(Language::from_path(path), Some(Language::Markdown)));
let path = std::path::Path::new("test.txt");
assert!(Language::from_path(path).is_none());
}
#[test]
fn test_highlighter_basic() {
let buffer = Buffer::from_str_test("fn main() {\n println!(\"Hello\");\n}");
let mut highlighter = Highlighter::new(Language::Rust).unwrap();
let theme = Theme::dark();
// Highlight entire buffer
let spans = highlighter.highlight_viewport(&buffer, 0, buffer.len(), &theme, 100_000);
// Should have some highlighted spans
assert!(!spans.is_empty());
// Keywords like "fn" should be highlighted with the theme's keyword color
let has_keyword = spans.iter().any(|s| s.color == theme.syntax_keyword);
assert!(has_keyword, "Should highlight keywords");
}
#[test]
fn test_highlighter_viewport_only() {
// Create a large buffer
let mut content = String::new();
for i in 0..1000 {
content.push_str(&format!("fn function_{i}() {{}}\n"));
}
let buffer = Buffer::from_str_test(&content);
let mut highlighter = Highlighter::new(Language::Rust).unwrap();
let theme = Theme::dark();
// Highlight only a small viewport in the middle
let viewport_start = 10000;
let viewport_end = 10500;
let spans =
highlighter.highlight_viewport(&buffer, viewport_start, viewport_end, &theme, 100_000);
// Should have some spans in the viewport
assert!(!spans.is_empty());
// All spans should be within or near the viewport
for span in &spans {
assert!(
span.range.start < viewport_end + 2000,
"Span start {} should be near viewport end {}",
span.range.start,
viewport_end
);
}
}
#[test]
fn test_cache_invalidation() {
let buffer = Buffer::from_str_test("fn main() {\n println!(\"Hello\");\n}");
let mut highlighter = Highlighter::new(Language::Rust).unwrap();
let theme = Theme::dark();
// First highlight
highlighter.highlight_viewport(&buffer, 0, buffer.len(), &theme, 100_000);
assert!(highlighter.cache.is_some());
// Invalidate a range
highlighter.invalidate_range(5..10);
assert!(highlighter.cache.is_none());
// Highlight again to rebuild cache
highlighter.highlight_viewport(&buffer, 0, buffer.len(), &theme, 100_000);
assert!(highlighter.cache.is_some());
// Invalidate all
highlighter.invalidate_all();
assert!(highlighter.cache.is_none());
}
#[test]
fn test_theme_affects_colors() {
let buffer = Buffer::from_str_test("fn main() {\n println!(\"Hello\");\n}");
let mut highlighter = Highlighter::new(Language::Rust).unwrap();
// Highlight with dark theme
let dark_theme = Theme::dark();
let dark_spans =
highlighter.highlight_viewport(&buffer, 0, buffer.len(), &dark_theme, 100_000);
// Highlight with light theme (cache should still work, colors should change)
let light_theme = Theme::light();
let light_spans =
highlighter.highlight_viewport(&buffer, 0, buffer.len(), &light_theme, 100_000);
// Both should have spans
assert!(!dark_spans.is_empty());
assert!(!light_spans.is_empty());
// Keywords should have different colors in different themes
let dark_keyword = dark_spans
.iter()
.find(|s| s.color == dark_theme.syntax_keyword);
let light_keyword = light_spans
.iter()
.find(|s| s.color == light_theme.syntax_keyword);
assert!(dark_keyword.is_some(), "Dark theme should have keyword");
assert!(light_keyword.is_some(), "Light theme should have keyword");
// The keyword colors should be different between themes
assert_ne!(
dark_theme.syntax_keyword, light_theme.syntax_keyword,
"Themes should have different keyword colors"
);
}
}