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.4KB

  1. #[macro_use]
  2. extern crate lazy_static;
  3. extern crate syntect;
  4. use std::cell::RefCell;
  5. use std::path::Path;
  6. use syntect::LoadingError;
  7. use syntect::dumps::from_binary;
  8. use syntect::parsing::SyntaxSet;
  9. use syntect::highlighting::{ThemeSet, Theme};
  10. use syntect::easy::HighlightLines;
  11. thread_local! {
  12. /// A pair of the set and whether extras have been added to it.
  13. pub static SYNTAX_SET: RefCell<(SyntaxSet, bool)> = {
  14. let mut ss: SyntaxSet = from_binary(include_bytes!("../../../sublime_syntaxes/newlines.packdump"));
  15. ss.link_syntaxes();
  16. RefCell::new((ss, false))
  17. };
  18. }
  19. lazy_static! {
  20. pub static ref THEME_SET: ThemeSet = from_binary(include_bytes!("../../../sublime_themes/all.themedump"));
  21. }
  22. pub fn get_highlighter<'a>(theme: &'a Theme, info: &str, base_path: &Path, extra_syntaxes: &[String]) -> Result<HighlightLines<'a>, LoadingError> {
  23. SYNTAX_SET.with(|rc| {
  24. let (ss, extras_added) = &mut *rc.borrow_mut();
  25. if !*extras_added {
  26. for dir in extra_syntaxes {
  27. ss.load_syntaxes(base_path.join(dir), true)?;
  28. }
  29. ss.link_syntaxes();
  30. *extras_added = true;
  31. }
  32. let syntax = info
  33. .split(' ')
  34. .next()
  35. .and_then(|lang| ss.find_syntax_by_token(lang))
  36. .unwrap_or_else(|| ss.find_syntax_plain_text());
  37. Ok(HighlightLines::new(syntax, theme))
  38. })
  39. }