Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Code highlighting #26

Merged
merged 14 commits into from
Mar 3, 2022
89 changes: 48 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions codectrl-gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ lazy_static = "1.4"
regex = "1.5"
serde = { version = "1.0", features = ["derive"] }
serde_cbor = "0.11"
# serde_json = "1.0"
# tree-sitter-highlight = "0.20.1"
# tree-sitter-cpp = "0.19.0"
# tree-sitter-rust = "0.20.1"
# tree-sitter-python = "0.19.1"
syntect = { version = "4.2", default-features = false, features = ["default-fancy"]}
xxhash-rust = { version = "0.8.3", features = ["xxh3"]}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ use syntect::{
util::LinesWithEndings,
};

fn lang_to_short(lang: &str) -> &str {
// Add more language here
match lang {
"rust" => "rs",
"python" => "py",
_ => "",
}
}

Copy link
Member

@STBoyden STBoyden Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at previous comment to see why possibly unnecessary.

pub fn code_highlighter(code: &str, lang: &str) -> egui::text::LayoutJob {
STBoyden marked this conversation as resolved.
Show resolved Hide resolved
let ps = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();
let syntax = ps.find_syntax_by_extension("py").unwrap();
let syntax = ps.find_syntax_by_extension(lang_to_short(lang)).unwrap();
Copy link
Member

@STBoyden STBoyden Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.rs/syntect/4.6.0/syntect/parsing/struct.SyntaxSet.html#method.find_syntax_by_name

You should be able to use that and just pass lang into it, rather than creating a new function for it.

Something like this:

let syntax =  ps.find_syntax_by_name(lang).unwrap();

let mut h = HighlightLines::new(syntax, &ts.themes["base16-mocha.dark"]);
let mut job = egui::text::LayoutJob::default();
for line in LinesWithEndings::from(code) {
Expand All @@ -31,4 +40,4 @@ pub fn code_highlighter(code: &str, lang: &str) -> egui::text::LayoutJob {
}
}
job
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,12 @@ fn code_scroll(
let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
// TODO: Hardcoded Langauge should be accepted from the log
let hash: u128 = xxhash(string.as_bytes());
let mut layout_job: egui::text::LayoutJob =
egui::text::LayoutJob::default();
let mut layout_job: egui::text::LayoutJob;

if *code_hash == hash {
layout_job = egui::text::LayoutJob::from(code_job.clone());
} else {
let temp_job = code_highlighter(string, "rust"); // Change rust to log.language or something
let temp_job = code_highlighter(string, &log.language); // Change rust to log.language or something
*code_job = temp_job.clone();
layout_job = temp_job;
*code_hash = xxhash(string.as_bytes());
Expand Down