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.

45 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 ss: SyntaxSet = from_binary(include_bytes!("../../../sublime_syntaxes/newlines.packdump"));
  15. RefCell::new((ss, false))
  16. };
  17. }
  18. lazy_static! {
  19. pub static ref THEME_SET: ThemeSet = from_binary(include_bytes!("../../../sublime_themes/all.themedump"));
  20. }
  21. pub fn get_highlighter<'a>(theme: &'a Theme, info: &str, base_path: &Path, extra_syntaxes: &[String]) -> Result<HighlightLines<'a>, LoadingError> {
  22. SYNTAX_SET.with(|rc| {
  23. let (ss, extras_added) = &mut *rc.borrow_mut();
  24. if !*extras_added {
  25. for dir in extra_syntaxes {
  26. ss.load_syntaxes(base_path.join(dir), true)?;
  27. }
  28. ss.link_syntaxes();
  29. *extras_added = true;
  30. }
  31. let syntax = info
  32. .split(' ')
  33. .next()
  34. .and_then(|lang| ss.find_syntax_by_token(lang))
  35. .unwrap_or_else(|| ss.find_syntax_plain_text());
  36. Ok(HighlightLines::new(syntax, theme))
  37. })
  38. }