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.

highlighting.rs 1.4KB

5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. ss
  12. };
  13. pub static ref THEME_SET: ThemeSet =
  14. from_binary(include_bytes!("../../../sublime_themes/all.themedump"));
  15. }
  16. /// Returns the highlighter and whether it was found in the extra or not
  17. pub fn get_highlighter<'a>(info: &str, config: &Config) -> (HighlightLines<'a>, bool) {
  18. let theme = &THEME_SET.themes[&config.highlight_theme];
  19. let mut in_extra = false;
  20. if let Some(ref lang) = info.split(' ').next() {
  21. let syntax = SYNTAX_SET
  22. .find_syntax_by_token(lang)
  23. .or_else(|| {
  24. if let Some(ref extra) = config.extra_syntax_set {
  25. let s = extra.find_syntax_by_token(lang);
  26. if s.is_some() {
  27. in_extra = true;
  28. }
  29. s
  30. } else {
  31. None
  32. }
  33. })
  34. .unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text());
  35. (HighlightLines::new(syntax, theme), in_extra)
  36. } else {
  37. (HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), false)
  38. }
  39. }