You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.6KB

  1. use lazy_static::lazy_static;
  2. use syntect::dumps::from_binary;
  3. use syntect::easy::HighlightLines;
  4. use syntect::highlighting::ThemeSet;
  5. use syntect::parsing::SyntaxSet;
  6. use crate::config::Config;
  7. lazy_static! {
  8. pub static ref SYNTAX_SET: SyntaxSet = {
  9. let ss: SyntaxSet =
  10. //from_binary(include_bytes!("../../../sublime_syntaxes/newlines.packdump"));
  11. from_binary(include_bytes!("../sublime/newlines.packdump"));
  12. ss
  13. };
  14. pub static ref THEME_SET: ThemeSet =
  15. //from_binary(include_bytes!("../../../sublime_themes/all.themedump"));
  16. from_binary(include_bytes!("../sublime/all.themedump"));
  17. }
  18. /// Returns the highlighter and whether it was found in the extra or not
  19. pub fn get_highlighter<'a>(info: &str, config: &Config) -> (HighlightLines<'a>, bool) {
  20. let theme = &THEME_SET.themes[&config.highlight_theme];
  21. let mut in_extra = false;
  22. if let Some(ref lang) = info.split(' ').next() {
  23. let syntax = SYNTAX_SET
  24. .find_syntax_by_token(lang)
  25. .or_else(|| {
  26. if let Some(ref extra) = config.extra_syntax_set {
  27. let s = extra.find_syntax_by_token(lang);
  28. if s.is_some() {
  29. in_extra = true;
  30. }
  31. s
  32. } else {
  33. None
  34. }
  35. })
  36. .unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text());
  37. (HighlightLines::new(syntax, theme), in_extra)
  38. } else {
  39. (HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), false)
  40. }
  41. }