|
@@ -2,16 +2,20 @@ |
|
|
extern crate lazy_static; |
|
|
extern crate lazy_static; |
|
|
extern crate syntect; |
|
|
extern crate syntect; |
|
|
|
|
|
|
|
|
|
|
|
use std::cell::RefCell; |
|
|
|
|
|
|
|
|
|
|
|
use syntect::LoadingError; |
|
|
use syntect::dumps::from_binary; |
|
|
use syntect::dumps::from_binary; |
|
|
use syntect::parsing::SyntaxSet; |
|
|
use syntect::parsing::SyntaxSet; |
|
|
use syntect::highlighting::{ThemeSet, Theme}; |
|
|
use syntect::highlighting::{ThemeSet, Theme}; |
|
|
use syntect::easy::HighlightLines; |
|
|
use syntect::easy::HighlightLines; |
|
|
|
|
|
|
|
|
thread_local! { |
|
|
thread_local! { |
|
|
pub static SYNTAX_SET: SyntaxSet = { |
|
|
|
|
|
|
|
|
/// A pair of the set and whether extras have been added to it. |
|
|
|
|
|
pub static SYNTAX_SET: RefCell<(SyntaxSet, bool)> = { |
|
|
let mut ss: SyntaxSet = from_binary(include_bytes!("../../../sublime_syntaxes/newlines.packdump")); |
|
|
let mut ss: SyntaxSet = from_binary(include_bytes!("../../../sublime_syntaxes/newlines.packdump")); |
|
|
ss.link_syntaxes(); |
|
|
ss.link_syntaxes(); |
|
|
ss |
|
|
|
|
|
|
|
|
RefCell::new((ss, false)) |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -19,14 +23,22 @@ lazy_static! { |
|
|
pub static ref THEME_SET: ThemeSet = from_binary(include_bytes!("../../../sublime_themes/all.themedump")); |
|
|
pub static ref THEME_SET: ThemeSet = from_binary(include_bytes!("../../../sublime_themes/all.themedump")); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_highlighter<'a>(theme: &'a Theme, info: &str, extra_syntaxes: &[String]) -> Result<HighlightLines<'a>, LoadingError> { |
|
|
|
|
|
SYNTAX_SET.with(|rc| { |
|
|
|
|
|
let (ss, extras_added) = &mut *rc.borrow_mut(); |
|
|
|
|
|
if !*extras_added { |
|
|
|
|
|
for dir in extra_syntaxes { |
|
|
|
|
|
ss.load_syntaxes(dir, true)?; |
|
|
|
|
|
} |
|
|
|
|
|
ss.link_syntaxes(); |
|
|
|
|
|
*extras_added = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
pub fn get_highlighter<'a>(theme: &'a Theme, info: &str) -> HighlightLines<'a> { |
|
|
|
|
|
SYNTAX_SET.with(|ss| { |
|
|
|
|
|
let syntax = info |
|
|
let syntax = info |
|
|
.split(' ') |
|
|
.split(' ') |
|
|
.next() |
|
|
.next() |
|
|
.and_then(|lang| ss.find_syntax_by_token(lang)) |
|
|
.and_then(|lang| ss.find_syntax_by_token(lang)) |
|
|
.unwrap_or_else(|| ss.find_syntax_plain_text()); |
|
|
.unwrap_or_else(|| ss.find_syntax_plain_text()); |
|
|
HighlightLines::new(syntax, theme) |
|
|
|
|
|
|
|
|
Ok(HighlightLines::new(syntax, theme)) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |