@@ -1,15 +1,12 @@
1−use std::borrow::Cow;
21use std::collections::BTreeMap;
32use std::fs::{self, File};
43use std::io::BufReader;
5−use std::path::{Path, PathBuf};
4+use std::path::Path;
65
76use syntect::dumps::{dump_to_file, from_binary, from_reader};
87use syntect::highlighting::{Theme, ThemeSet};
98use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
109
11−use crate::dirs::PROJECT_DIRS;
12−
1310use crate::errors::*;
1411use crate::inputfile::{InputFile, InputFileReader};
1512use crate::syntax_mapping::SyntaxMapping;
@@ -23,13 +20,7 @@ pub struct HighlightingAssets {
2320}
2421
2522impl HighlightingAssets {
26− pub fn new() -> Self {
27− Self::from_cache().unwrap_or_else(|_| Self::from_binary())
28− }
29−
30− pub fn from_files(dir: Option<&Path>, start_empty: bool) -> Result<Self> {
31− let source_dir = dir.unwrap_or_else(|| PROJECT_DIRS.config_dir());
32−
23+ pub fn from_files(source_dir: &Path, start_empty: bool) -> Result<Self> {
3324 let mut theme_set = if start_empty {
3425 ThemeSet {
3526 themes: BTreeMap::new(),
@@ -72,12 +63,11 @@ impl HighlightingAssets {
7263 })
7364 }
7465
75− fn from_cache() -> Result<Self> {
76− let theme_set_path = theme_set_path();
77− let syntax_set_file = File::open(&syntax_set_path()).chain_err(|| {
66+ pub fn from_cache(theme_set_path: &Path, syntax_set_path: &Path) -> Result<Self> {
67+ let syntax_set_file = File::open(syntax_set_path).chain_err(|| {
7868 format!(
7969 "Could not load cached syntax set '{}'",
80− syntax_set_path().to_string_lossy()
70+ syntax_set_path.to_string_lossy()
8171 )
8272 })?;
8373 let syntax_set: SyntaxSet = from_reader(BufReader::new(syntax_set_file))
@@ -106,7 +96,7 @@ impl HighlightingAssets {
10696 from_binary(include_bytes!("../assets/themes.bin"))
10797 }
10898
109− fn from_binary() -> Self {
99+ pub fn from_binary() -> Self {
110100 let syntax_set = Self::get_integrated_syntaxset();
111101 let theme_set = Self::get_integrated_themeset();
112102
@@ -116,8 +106,7 @@ impl HighlightingAssets {
116106 }
117107 }
118108
119− pub fn save(&self, dir: Option<&Path>) -> Result<()> {
120− let target_dir = dir.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
109+ pub fn save(&self, target_dir: &Path) -> Result<()> {
121110 let _ = fs::create_dir_all(target_dir);
122111 let theme_set_path = target_dir.join("themes.bin");
123112 let syntax_set_path = target_dir.join("syntaxes.bin");
@@ -203,30 +192,4 @@ impl HighlightingAssets {
203192
204193 syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
205194 }
206−}
207−
208−fn theme_set_path() -> PathBuf {
209− PROJECT_DIRS.cache_dir().join("themes.bin")
210−}
211−
212−fn syntax_set_path() -> PathBuf {
213− PROJECT_DIRS.cache_dir().join("syntaxes.bin")
214−}
215−
216−pub fn config_dir() -> Cow<'static, str> {
217− PROJECT_DIRS.config_dir().to_string_lossy()
218−}
219−
220−pub fn cache_dir() -> Cow<'static, str> {
221− PROJECT_DIRS.cache_dir().to_string_lossy()
222−}
223−
224−pub fn clear_assets() {
225− print!("Clearing theme set cache ... ");
226− fs::remove_file(theme_set_path()).ok();
227− println!("okay");
228−
229− print!("Clearing syntax set cache ... ");
230− fs::remove_file(syntax_set_path()).ok();
231− println!("okay");
232−}
195+}