Made the highlighter code cleaner by unifying the regular expressions into one.

This commit is contained in:
PaddiM8 2020-06-09 15:43:53 +02:00
parent 98fc2bf151
commit a35bcc054a

View File

@ -54,23 +54,24 @@ impl Highlighter for LineHighlighter {
fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> {
let mut coloured = line.to_string(); let mut coloured = line.to_string();
let reg = Regex::new(r"(([^0-9\.,\(\)=\+-/\*\^!_]+(_\d+)?)|[\+-/\*\^!])").unwrap(); let reg = Regex::new(
let unit = Regex::new(r"(deg|rad)").unwrap(); r"(?x)
let identifier = Regex::new(r"[^0-9\.,\(\)=\+-/\*\^!_]+(_\d+)?").unwrap(); (?P<identifier>[^!-@\s_|^]+(_\d+)?) |
let op = Regex::new(r"[\+-/\*\^!]+").unwrap(); (?P<op>[+\-/*^!])",
)
.unwrap();
coloured = reg coloured = reg
.replace_all(&coloured, |caps: &Captures| { .replace_all(&coloured, |caps: &Captures| {
let cap = &caps[0]; if let Some(cap) = caps.name("identifier") {
match cap.as_str() {
if unit.is_match(cap) { "rad" | "deg" | "°" => Colour::Yellow.paint(cap.as_str()).to_string(),
Colour::Yellow.paint(cap).to_string() _ => Colour::Blue.paint(cap.as_str()).to_string(),
} else if identifier.is_match(cap) { }
Colour::Blue.paint(cap).to_string() } else if let Some(cap) = caps.name("op") {
} else if op.is_match(cap) { Colour::Fixed(172).paint(cap.as_str()).to_string()
Colour::Fixed(172).paint(cap).to_string()
} else { } else {
cap.to_string() caps[0].to_string()
} }
}) })
.to_string(); .to_string();