Skip to content
This repository has been archived by the owner on Feb 12, 2018. It is now read-only.

Commit

Permalink
Merge pull request trishume#5 from trishume/theme-dumps
Browse files Browse the repository at this point in the history
Internal theme dumps
  • Loading branch information
trishume authored Jun 13, 2016
2 parents bbf58cf + 78bf6e2 commit 776e739
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 148 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
[submodule "testdata/Packages"]
path = testdata/Packages
url = https://github.com/sublimehq/Packages
[submodule "testdata/themes.tmbundle"]
path = testdata/themes.tmbundle
url = https://github.com/textmate/themes.tmbundle.git
[submodule "testdata/InspiredGitHub.tmtheme"]
path = testdata/InspiredGitHub.tmtheme
url = https://github.com/sethlopezme/InspiredGitHub.tmtheme.git
Expand Down
Binary file added assets/default.themedump
Binary file not shown.
3 changes: 2 additions & 1 deletion benches/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate syntect;
use test::Bencher;

use syntect::package_set::PackageSet;
use syntect::theme_set::ThemeSet;
use syntect::scope::ScopeStack;
use syntect::parser::*;
use syntect::theme::highlighter::*;
Expand All @@ -15,7 +16,7 @@ use std::io::Read;

fn highlight_file(b: &mut Bencher, path_s: &str) {
let ps = PackageSet::load_from_folder("testdata/Packages").unwrap();
let highlighter = Highlighter::new(PackageSet::get_theme("testdata/spacegray/base16-ocean.\
let highlighter = Highlighter::new(ThemeSet::get_theme("testdata/spacegray/base16-ocean.\
dark.tmTheme")
.unwrap());
let path = Path::new(path_s);
Expand Down
19 changes: 14 additions & 5 deletions benches/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@ extern crate syntect;
use test::Bencher;

use syntect::package_set::PackageSet;
use syntect::theme_set::ThemeSet;

#[bench]
fn bench_load_syntax_dump(b: &mut Bencher) {
fn bench_load_internal_dump(b: &mut Bencher) {
b.iter(|| {
let ps = PackageSet::from_dump_file("assets/default_newlines.packdump");
let ps = PackageSet::load_defaults_newlines();
test::black_box(&ps);
});
}

#[bench]
fn bench_load_internal_dump(b: &mut Bencher) {
fn bench_load_internal_themes(b: &mut Bencher) {
b.iter(|| {
let ps = PackageSet::load_defaults_newlines();
test::black_box(&ps);
let ts = ThemeSet::load_defaults();
test::black_box(&ts);
});
}

#[bench]
fn bench_load_theme(b: &mut Bencher) {
b.iter(|| {
let theme = ThemeSet::get_theme("testdata/spacegray/base16-ocean.dark.tmTheme");
test::black_box(&theme);
});
}

Expand Down
9 changes: 7 additions & 2 deletions examples/gendata.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
extern crate syntect;
use syntect::package_set::PackageSet;
use syntect::theme_set::ThemeSet;
use syntect::dumps::*;

fn main() {
let mut ps = PackageSet::new();
ps.load_syntaxes("testdata/Packages", true).unwrap();
ps.dump_to_file("assets/default_newlines.packdump").unwrap();
dump_to_file(&ps, "assets/default_newlines.packdump").unwrap();

let mut ps2 = PackageSet::new();
ps2.load_syntaxes("testdata/Packages", false).unwrap();
ps2.dump_to_file("assets/default_nonewlines.packdump").unwrap();
dump_to_file(&ps2, "assets/default_nonewlines.packdump").unwrap();

let ts = ThemeSet::load_from_folder("testdata").unwrap();
dump_to_file(&ts, "assets/default.themedump").unwrap();
}
6 changes: 3 additions & 3 deletions examples/syncat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate syntect;
use syntect::scope::ScopeStack;
use syntect::package_set::PackageSet;
use syntect::theme_set::ThemeSet;
use syntect::parser::*;
use syntect::theme::highlighter::*;
use syntect::theme::style::*;
Expand All @@ -13,9 +14,8 @@ use std::fs::File;

fn main() {
let ps = PackageSet::load_defaults_nonewlines();
let highlighter = Highlighter::new(PackageSet::get_theme("testdata/spacegray/base16-ocean.\
dark.tmTheme")
.unwrap());
let ts = ThemeSet::load_defaults();
let highlighter = Highlighter::new(&ts.themes["base16-ocean.dark"]);

let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
Expand Down
83 changes: 48 additions & 35 deletions src/dumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,42 @@ use bincode::rustc_serialize::*;
use std::fs::File;
use std::io::{BufReader, BufWriter};
use package_set::PackageSet;
use theme_set::ThemeSet;
use std::path::Path;
use flate2::write::ZlibEncoder;
use flate2::read::ZlibDecoder;
use flate2::Compression;
use rustc_serialize::{Encodable, Decodable};

pub fn dump_binary<T: Encodable>(o: &T) -> Vec<u8> {
let mut v = Vec::new();
{
let mut encoder = ZlibEncoder::new(&mut v, Compression::Best);
encode_into(o, &mut encoder, SizeLimit::Infinite).unwrap();
}
v
}

pub fn dump_to_file<T: Encodable, P: AsRef<Path>>(o: &T, path: P) -> EncodingResult<()> {
let f = BufWriter::new(try!(File::create(path).map_err(EncodingError::IoError)));
let mut encoder = ZlibEncoder::new(f, Compression::Best);
encode_into(o, &mut encoder, SizeLimit::Infinite)
}

/// Returns a fully loaded and linked package set from
/// a binary dump. Panics if the dump is invalid.
pub fn from_binary<T: Decodable>(v: &[u8]) -> T {
let mut decoder = ZlibDecoder::new(v);
decode_from(&mut decoder, SizeLimit::Infinite).unwrap()
}

/// Returns a fully loaded and linked package set from
/// a binary dump file.
pub fn from_dump_file<T: Decodable, P: AsRef<Path>>(path: P) -> DecodingResult<T> {
let f = try!(File::open(path).map_err(DecodingError::IoError));
let mut decoder = ZlibDecoder::new(BufReader::new(f));
decode_from(&mut decoder, SizeLimit::Infinite)
}

impl PackageSet {
/// Instantiates a new package set from a binary dump of
Expand All @@ -22,7 +54,8 @@ impl PackageSet {
/// use the fact that SyntaxDefinitions are serializable with
/// the bincode crate to cache dumps of additional syntaxes yourself.
pub fn load_defaults_nonewlines() -> PackageSet {
let mut ps = Self::from_binary(include_bytes!("../assets/default_nonewlines.packdump"));
let mut ps: PackageSet = from_binary(include_bytes!("../assets/default_nonewlines.\
packdump"));
ps.link_syntaxes();
ps
}
Expand All @@ -31,55 +64,35 @@ impl PackageSet {
/// These are separate methods because thanks to linker garbage collection, only the serialized
/// dumps for the method(s) you call will be included in the binary (each is ~200kb for now).
pub fn load_defaults_newlines() -> PackageSet {
let mut ps = Self::from_binary(include_bytes!("../assets/default_newlines.packdump"));
ps.link_syntaxes();
ps
}

pub fn dump_binary(&self) -> Vec<u8> {
assert!(!self.is_linked);
let mut v = Vec::new();
{
let mut encoder = ZlibEncoder::new(&mut v, Compression::Best);
encode_into(self, &mut encoder, SizeLimit::Infinite).unwrap();
}
v
}

pub fn dump_to_file<P: AsRef<Path>>(&self, path: P) -> EncodingResult<()> {
let f = BufWriter::new(try!(File::create(path).map_err(EncodingError::IoError)));
let mut encoder = ZlibEncoder::new(f, Compression::Best);
encode_into(self, &mut encoder, SizeLimit::Infinite)
}

/// Returns a fully loaded and linked package set from
/// a binary dump. Panics if the dump is invalid.
pub fn from_binary(v: &[u8]) -> PackageSet {
let mut decoder = ZlibDecoder::new(v);
let mut ps: PackageSet = decode_from(&mut decoder, SizeLimit::Infinite).unwrap();
let mut ps: PackageSet = from_binary(include_bytes!("../assets/default_newlines.packdump"));
ps.link_syntaxes();
ps
}
}

/// Returns a fully loaded and linked package set from
/// a binary dump file.
pub fn from_dump_file<P: AsRef<Path>>(path: P) -> DecodingResult<PackageSet> {
let f = try!(File::open(path).map_err(DecodingError::IoError));
let mut decoder = ZlibDecoder::new(BufReader::new(f));
decode_from(&mut decoder, SizeLimit::Infinite)
impl ThemeSet {
/// Loads the set of default themes
/// Currently includes Solarized light/dark, Base16 ocean/mocha/eighties and InspiredGithub
pub fn load_defaults() -> ThemeSet {
from_binary(include_bytes!("../assets/default.themedump"))
}
}

#[cfg(test)]
mod tests {
use package_set::PackageSet;
use theme_set::ThemeSet;
use dumps::*;
#[test]
fn can_dump_and_load() {
let mut ps = PackageSet::new();
ps.load_syntaxes("testdata/Packages", false).unwrap();

let bin = ps.dump_binary();
let ps2 = PackageSet::from_binary(&bin[..]);
let bin = dump_binary(&ps);
let ps2: PackageSet = from_binary(&bin[..]);
assert_eq!(ps.syntaxes.len(), ps2.syntaxes.len());

let themes = ThemeSet::load_defaults();
assert!(themes.themes.len() > 4);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern crate flate2;
pub mod syntax_definition;
pub mod yaml_load;
pub mod package_set;
pub mod theme_set;
pub mod scope;
pub mod parser;
pub mod theme;
Expand Down
74 changes: 2 additions & 72 deletions src/package_set.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use syntax_definition::*;
use scope::*;
use yaml_load::*;
use theme::theme::{Theme, ParseThemeError};
use theme::settings::*;

use std::path::{Path, PathBuf};
use std::io::{Error as IoError, BufReader};
use std::path::Path;
use std::io::Error as IoError;
use walkdir::WalkDir;
use std::io::{self, Read};
use std::fs::File;
Expand Down Expand Up @@ -33,14 +31,6 @@ pub enum PackageError {
WalkDir(walkdir::Error),
Io(io::Error),
ParseSyntax(ParseSyntaxError),
ParseTheme(ParseThemeError),
ReadSettings(SettingsError),
}

impl From<SettingsError> for PackageError {
fn from(error: SettingsError) -> PackageError {
PackageError::ReadSettings(error)
}
}

impl From<IoError> for PackageError {
Expand All @@ -49,12 +39,6 @@ impl From<IoError> for PackageError {
}
}

impl From<ParseThemeError> for PackageError {
fn from(error: ParseThemeError) -> PackageError {
PackageError::ParseTheme(error)
}
}

impl From<ParseSyntaxError> for PackageError {
fn from(error: ParseSyntaxError) -> PackageError {
PackageError::ParseSyntax(error)
Expand Down Expand Up @@ -90,18 +74,6 @@ impl PackageSet {
Ok(ps)
}

/// Returns all the themes found in a folder, good for enumerating before loading one with get_theme
pub fn discover_themes<P: AsRef<Path>>(folder: P) -> Result<Vec<PathBuf>, PackageError> {
let mut themes = Vec::new();
for entry in WalkDir::new(folder) {
let entry = try!(entry.map_err(|e| PackageError::WalkDir(e)));
if entry.path().extension().map(|e| e == "tmTheme").unwrap_or(false) {
themes.push(entry.path().to_owned());
}
}
Ok(themes)
}

/// Loads all the .sublime-syntax files in a folder into this package set.
/// It does not link the syntaxes, in case you want to serialize this package set.
///
Expand Down Expand Up @@ -223,20 +195,6 @@ impl PackageSet {
self.link_context(syntax, mut_ref.deref_mut());
}
}

fn read_file(path: &Path) -> Result<BufReader<File>, PackageError> {
let reader = try!(File::open(path));
Ok(BufReader::new(reader))
}

fn read_plist(path: &Path) -> Result<Settings, PackageError> {
Ok(try!(read_plist(try!(Self::read_file(path)))))
}

/// Loads a theme given a path to a .tmTheme file
pub fn get_theme<P: AsRef<Path>>(path: P) -> Result<Theme, PackageError> {
Ok(try!(Theme::parse_settings(try!(Self::read_plist(path.as_ref())))))
}
}

#[cfg(test)]
Expand All @@ -258,32 +216,4 @@ mod tests {
let count = context_iter(main_context.clone()).count();
assert_eq!(count, 91);
}
#[test]
fn can_parse_common_themes() {
use package_set::PackageSet;
use theme::style::Color;
let theme_paths = PackageSet::discover_themes("testdata/themes.tmbundle").unwrap();
for theme_path in theme_paths.iter() {
println!("{:?}", theme_path);
PackageSet::get_theme(theme_path).unwrap();
}

let theme = PackageSet::get_theme("testdata/themes.tmbundle/Themes/Amy.tmTheme").unwrap();
assert_eq!(theme.name.unwrap(), "Amy");
assert_eq!(theme.settings.selection.unwrap(),
Color {
r: 0x80,
g: 0x00,
b: 0x00,
a: 0x80,
});
assert_eq!(theme.scopes[0].style.foreground.unwrap(),
Color {
r: 0x40,
g: 0x40,
b: 0x80,
a: 0xFF,
});
// assert!(false);
}
}
2 changes: 1 addition & 1 deletion src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct ScopeRepository {
atom_index_map: HashMap<String, usize>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, RustcEncodable, RustcDecodable)]
pub struct ScopeStack {
scopes: Vec<Scope>,
}
Expand Down
Loading

0 comments on commit 776e739

Please sign in to comment.