|
| 1 | + |
| 2 | + |
| 3 | +use eframe::egui::{FontFamily, FontId}; |
| 4 | +use eframe::egui::{self, text::LayoutJob, Color32, TextFormat}; |
| 5 | + |
| 6 | + |
| 7 | +extern crate regex; |
| 8 | +use regex::Regex; |
| 9 | +use regex::RegexSet; |
| 10 | +const DEFAULT_FONT_ID: FontId = FontId::new(14.0, FontFamily::Monospace); |
| 11 | + |
| 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 |
| 20 | +} |
| 21 | +impl HighLightElement{ |
| 22 | + pub fn new(pos_start: usize, pos_end:usize,token_idx:usize) -> Self{ |
| 23 | + Self{ |
| 24 | + pos_start, |
| 25 | + pos_end, |
| 26 | + token_idx |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | +pub fn highlight_impl( |
| 31 | + _ctx: &egui::Context, |
| 32 | + text: &str, |
| 33 | + tokens: Vec<String>, |
| 34 | + default_color:Color32 |
| 35 | +) -> Option<LayoutJob> { |
| 36 | + // Extremely simple syntax highlighter for when we compile without syntect |
| 37 | + |
| 38 | + |
| 39 | + let mut my_tokens = tokens.clone(); |
| 40 | + for token in my_tokens.clone() |
| 41 | + { |
| 42 | + if token.len() < 1 |
| 43 | + { |
| 44 | + let index = my_tokens.iter().position(|x| *x == token).unwrap(); |
| 45 | + my_tokens.remove(index); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + let content_string = String::from(text); |
| 50 | + // let _ = file.read_to_string(&mut isi); |
| 51 | + let mut regexs:Vec<Regex> = Vec::new(); |
| 52 | + for sentence in my_tokens.clone() { |
| 53 | + match Regex::new(&sentence){ |
| 54 | + Ok(re) => { |
| 55 | + regexs.push(re); |
| 56 | + }, |
| 57 | + Err(_err) =>{ |
| 58 | + |
| 59 | + }, |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + let mut highlight_list : Vec<HighLightElement> = Vec::<HighLightElement>::new(); |
| 64 | + match RegexSet::new(my_tokens.clone()){ |
| 65 | + Ok(set) => { |
| 66 | + for idx in set.matches(&content_string).into_iter() { |
| 67 | + for caps in regexs[idx].captures_iter(&content_string) { |
| 68 | + highlight_list.push(HighLightElement::new( |
| 69 | + caps.get(0).unwrap().start(), |
| 70 | + caps.get(0).unwrap().end(), |
| 71 | + idx)); |
| 72 | + } |
| 73 | + } |
| 74 | + }, |
| 75 | + Err(_err) => { |
| 76 | + |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + highlight_list.sort_by_key(|item| (item.pos_start, item.pos_end)); |
| 81 | + |
| 82 | + |
| 83 | + 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 |
| 90 | + } |
| 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))); |
| 105 | + } |
| 106 | + previous = matches.clone(); |
| 107 | + } |
| 108 | + job.append(&text[previous.pos_end..], 0.0, TextFormat::simple(DEFAULT_FONT_ID, default_color)); |
| 109 | + |
| 110 | + |
| 111 | + Some(job) |
| 112 | +} |
| 113 | + |
0 commit comments