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