oak_python/highlighter/
mod.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum HighlightKind {
3 Keyword,
4 String,
5 Number,
6 Comment,
7 Identifier,
8 Decorator,
9}
10
11pub trait Highlighter {
13 fn highlight(&self, text: &str) -> Vec<(usize, usize, HighlightKind)>;
15}
16
17pub struct PythonHighlighter {
18 pub use_parser: bool,
19}
20
21impl Default for PythonHighlighter {
22 fn default() -> Self {
23 Self { use_parser: false }
24 }
25}
26
27impl PythonHighlighter {
28 pub fn new() -> Self {
29 Self::default()
30 }
31
32 pub fn with_parser() -> Self {
33 Self { use_parser: true }
34 }
35
36 fn highlight_keywords(&self, text: &str) -> Vec<(usize, usize, HighlightKind)> {
37 let mut highlights = Vec::new();
38 let keywords = [
39 "False", "None", "True", "and", "as", "assert", "async", "await", "break", "class", "continue", "def", "del", "elif", "else", "except", "finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass",
40 "raise", "return", "try", "while", "with", "yield",
41 ];
42
43 for keyword in &keywords {
44 let mut start = 0;
45 while let Some(pos) = text[start..].find(keyword) {
46 let absolute_pos = start + pos;
47 let end_pos = absolute_pos + keyword.len();
48
49 let is_word_boundary_before = absolute_pos == 0 || !text.chars().nth(absolute_pos - 1).unwrap_or(' ').is_alphanumeric();
50 let is_word_boundary_after = end_pos >= text.len() || !text.chars().nth(end_pos).unwrap_or(' ').is_alphanumeric();
51
52 if is_word_boundary_before && is_word_boundary_after {
53 highlights.push((absolute_pos, end_pos, HighlightKind::Keyword));
54 }
55
56 start = absolute_pos + 1;
57 }
58 }
59
60 highlights
61 }
62 fn highlight_strings(&self, text: &str) -> Vec<(usize, usize, HighlightKind)> {
63 let mut highlights = Vec::new();
64 let mut chars = text.char_indices().peekable();
65
66 while let Some((i, ch)) = chars.next() {
67 match ch {
68 '"' | '\'' => {
69 let quote = ch;
70 let start = i;
71 let mut escaped = false;
72 let mut found_end = false;
73
74 while let Some((j, next_ch)) = chars.next() {
75 if escaped {
76 escaped = false;
77 }
78 else if next_ch == '\\' {
79 escaped = true;
80 }
81 else if next_ch == quote {
82 highlights.push((start, j + 1, HighlightKind::String));
83 found_end = true;
84 break;
85 }
86 }
87 if !found_end {
88 highlights.push((start, text.len(), HighlightKind::String));
89 }
90 }
91 _ => {}
92 }
93 }
94 highlights
95 }
96
97 fn highlight_numbers(&self, text: &str) -> Vec<(usize, usize, HighlightKind)> {
98 let mut highlights = Vec::new();
99 let mut start = None;
100
101 for (i, ch) in text.char_indices() {
102 if ch.is_ascii_digit() {
103 if start.is_none() {
104 start = Some(i);
105 }
106 }
107 else {
108 if let Some(s) = start {
109 highlights.push((s, i, HighlightKind::Number));
110 start = None;
111 }
112 }
113 }
114 if let Some(s) = start {
115 highlights.push((s, text.len(), HighlightKind::Number));
116 }
117 highlights
118 }
119
120 fn highlight_comments(&self, text: &str) -> Vec<(usize, usize, HighlightKind)> {
121 let mut highlights = Vec::new();
122 let mut start = 0;
123 while let Some(pos) = text[start..].find('#') {
124 let absolute_pos = start + pos;
125 let end_pos = text[absolute_pos..].find('\n').map(|n| absolute_pos + n).unwrap_or(text.len());
126 highlights.push((absolute_pos, end_pos, HighlightKind::Comment));
127 start = end_pos;
128 }
129 highlights
130 }
131}
132
133impl Highlighter for PythonHighlighter {
134 fn highlight(&self, text: &str) -> Vec<(usize, usize, HighlightKind)> {
135 let mut highlights = self.highlight_keywords(text);
136 highlights.extend(self.highlight_strings(text));
137 highlights.extend(self.highlight_numbers(text));
138 highlights.extend(self.highlight_comments(text));
139 highlights.sort_by_key(|h| h.0);
140 highlights
141 }
142}