|
1 | | - |
2 | | - |
3 | | -use eframe::egui::{FontFamily, FontId}; |
4 | 1 | use eframe::egui::{self, text::LayoutJob, Color32, TextFormat}; |
5 | | - |
| 2 | +use eframe::egui::{FontFamily, FontId}; |
6 | 3 |
|
7 | 4 | extern crate regex; |
8 | 5 | use regex::Regex; |
9 | 6 | use regex::RegexSet; |
10 | 7 | const DEFAULT_FONT_ID: FontId = FontId::new(14.0, FontFamily::Monospace); |
11 | 8 |
|
12 | | - |
13 | | -#[derive(Debug)] |
14 | | -#[derive(Clone, Copy)] |
15 | | -pub struct HighLightElement |
16 | | -{ |
17 | | - pos_start:usize, |
18 | | - pos_end:usize, |
19 | | - token_idx:usize |
| 9 | +#[derive(Debug, Clone, Copy)] |
| 10 | +pub struct HighLightElement { |
| 11 | + pos_start: usize, |
| 12 | + pos_end: usize, |
| 13 | + token_idx: usize, |
20 | 14 | } |
21 | | -impl HighLightElement{ |
22 | | - pub fn new(pos_start: usize, pos_end:usize,token_idx:usize) -> Self{ |
23 | | - Self{ |
| 15 | +impl HighLightElement { |
| 16 | + pub fn new(pos_start: usize, pos_end: usize, token_idx: usize) -> Self { |
| 17 | + Self { |
24 | 18 | pos_start, |
25 | 19 | pos_end, |
26 | | - token_idx |
| 20 | + token_idx, |
27 | 21 | } |
28 | 22 | } |
29 | 23 | } |
30 | 24 | pub fn highlight_impl( |
31 | 25 | _ctx: &egui::Context, |
32 | 26 | text: &str, |
33 | 27 | tokens: Vec<String>, |
34 | | - default_color:Color32 |
| 28 | + default_color: Color32, |
35 | 29 | ) -> Option<LayoutJob> { |
36 | 30 | // Extremely simple syntax highlighter for when we compile without syntect |
37 | 31 |
|
38 | | - |
39 | 32 | let mut my_tokens = tokens.clone(); |
40 | | - for token in my_tokens.clone() |
41 | | - { |
42 | | - if token.len() < 1 |
43 | | - { |
| 33 | + for token in my_tokens.clone() { |
| 34 | + if token.is_empty() { |
44 | 35 | let index = my_tokens.iter().position(|x| *x == token).unwrap(); |
45 | 36 | my_tokens.remove(index); |
46 | 37 | } |
47 | 38 | } |
48 | 39 |
|
49 | 40 | let content_string = String::from(text); |
50 | 41 | // let _ = file.read_to_string(&mut isi); |
51 | | - let mut regexs:Vec<Regex> = Vec::new(); |
| 42 | + let mut regexs: Vec<Regex> = Vec::new(); |
52 | 43 | for sentence in my_tokens.clone() { |
53 | | - match Regex::new(&sentence){ |
| 44 | + match Regex::new(&sentence) { |
54 | 45 | Ok(re) => { |
55 | 46 | regexs.push(re); |
56 | | - }, |
57 | | - Err(_err) =>{ |
58 | | - |
59 | | - }, |
| 47 | + } |
| 48 | + Err(_err) => {} |
60 | 49 | }; |
61 | 50 | } |
62 | 51 |
|
63 | | - let mut highlight_list : Vec<HighLightElement> = Vec::<HighLightElement>::new(); |
64 | | - match RegexSet::new(my_tokens.clone()){ |
| 52 | + let mut highlight_list: Vec<HighLightElement> = Vec::<HighLightElement>::new(); |
| 53 | + match RegexSet::new(my_tokens.clone()) { |
65 | 54 | Ok(set) => { |
66 | 55 | for idx in set.matches(&content_string).into_iter() { |
67 | 56 | for caps in regexs[idx].captures_iter(&content_string) { |
68 | 57 | highlight_list.push(HighLightElement::new( |
69 | | - caps.get(0).unwrap().start(), |
| 58 | + caps.get(0).unwrap().start(), |
70 | 59 | caps.get(0).unwrap().end(), |
71 | | - idx)); |
| 60 | + idx, |
| 61 | + )); |
72 | 62 | } |
73 | 63 | } |
74 | | - }, |
75 | | - Err(_err) => { |
76 | | - |
77 | 64 | } |
| 65 | + Err(_err) => {} |
78 | 66 | }; |
79 | 67 |
|
80 | 68 | highlight_list.sort_by_key(|item| (item.pos_start, item.pos_end)); |
81 | 69 |
|
82 | | - |
83 | 70 | let mut job = LayoutJob::default(); |
84 | | - let mut previous = HighLightElement::new(0,0, 0); |
85 | | - for matches in highlight_list |
86 | | - { |
87 | | - if previous.pos_end >= matches.pos_start |
88 | | - { |
89 | | - continue |
| 71 | + let mut previous = HighLightElement::new(0, 0, 0); |
| 72 | + for matches in highlight_list { |
| 73 | + if previous.pos_end >= matches.pos_start { |
| 74 | + continue; |
90 | 75 | } |
91 | | - job.append(&text[previous.pos_end..(matches.pos_start)], 0.0, TextFormat::simple(DEFAULT_FONT_ID, default_color)); |
92 | | - if matches.token_idx == 0 |
93 | | - { |
94 | | - job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(255, 100, 100))); |
95 | | - }else if matches.token_idx == 1 |
96 | | - { |
97 | | - job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(225, 159, 0))); |
98 | | - |
99 | | - }else if matches.token_idx == 2 |
100 | | - { |
101 | | - job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(87, 165, 171))); |
102 | | - }else if matches.token_idx == 3 |
103 | | - { |
104 | | - job.append(&text[matches.pos_start..matches.pos_end], 0.0, TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(109, 147, 226))); |
| 76 | + job.append( |
| 77 | + &text[previous.pos_end..(matches.pos_start)], |
| 78 | + 0.0, |
| 79 | + TextFormat::simple(DEFAULT_FONT_ID, default_color), |
| 80 | + ); |
| 81 | + if matches.token_idx == 0 { |
| 82 | + job.append( |
| 83 | + &text[matches.pos_start..matches.pos_end], |
| 84 | + 0.0, |
| 85 | + TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(255, 100, 100)), |
| 86 | + ); |
| 87 | + } else if matches.token_idx == 1 { |
| 88 | + job.append( |
| 89 | + &text[matches.pos_start..matches.pos_end], |
| 90 | + 0.0, |
| 91 | + TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(225, 159, 0)), |
| 92 | + ); |
| 93 | + } else if matches.token_idx == 2 { |
| 94 | + job.append( |
| 95 | + &text[matches.pos_start..matches.pos_end], |
| 96 | + 0.0, |
| 97 | + TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(87, 165, 171)), |
| 98 | + ); |
| 99 | + } else if matches.token_idx == 3 { |
| 100 | + job.append( |
| 101 | + &text[matches.pos_start..matches.pos_end], |
| 102 | + 0.0, |
| 103 | + TextFormat::simple(DEFAULT_FONT_ID, Color32::from_rgb(109, 147, 226)), |
| 104 | + ); |
105 | 105 | } |
106 | | - previous = matches.clone(); |
| 106 | + previous = matches; |
107 | 107 | } |
108 | | - job.append(&text[previous.pos_end..], 0.0, TextFormat::simple(DEFAULT_FONT_ID, default_color)); |
109 | | - |
| 108 | + job.append( |
| 109 | + &text[previous.pos_end..], |
| 110 | + 0.0, |
| 111 | + TextFormat::simple(DEFAULT_FONT_ID, default_color), |
| 112 | + ); |
110 | 113 |
|
111 | 114 | Some(job) |
112 | 115 | } |
113 | | - |
|
0 commit comments