Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename methods on SyntaxSetBuilder #195

Merged
merged 1 commit into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions benches/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ fn bench_load_theme(b: &mut Bencher) {
});
}

fn bench_load_syntaxes(b: &mut Bencher) {
fn bench_load_from_folder(b: &mut Bencher) {
b.iter(|| {
let mut builder = SyntaxSetBuilder::new();
builder.load_syntaxes("testdata/Packages", false).unwrap()
builder.load_from_folder("testdata/Packages", false).unwrap()
});
}

fn bench_link_syntaxes(b: &mut Bencher) {
let mut builder = SyntaxSetBuilder::new();
builder.load_syntaxes("testdata/Packages", false).unwrap();
builder.load_from_folder("testdata/Packages", false).unwrap();
b.iter(|| {
builder.clone().build();
});
Expand All @@ -44,7 +44,7 @@ fn loading_benchmark(c: &mut Criterion) {
c.bench_function("load_internal_dump", bench_load_internal_dump);
c.bench_function("load_internal_themes", bench_load_internal_themes);
c.bench_function("load_theme", bench_load_theme);
c.bench_function("load_syntaxes", bench_load_syntaxes);
c.bench_function("load_from_folder", bench_load_from_folder);
c.bench_function("link_syntaxes", bench_link_syntaxes);
}

Expand Down
8 changes: 4 additions & 4 deletions examples/gendata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ fn main() {
Some(ref packpath_newlines),
Some(ref packpath_nonewlines)) if cmd == "synpack" => {
let mut builder = SyntaxSetBuilder::new();
builder.load_plain_text_syntax();
builder.load_syntaxes(package_dir, true).unwrap();
builder.add_plain_text_syntax();
builder.load_from_folder(package_dir, true).unwrap();
let ss = builder.build();
dump_to_file(&ss, packpath_newlines).unwrap();

let mut builder_nonewlines = SyntaxSetBuilder::new();
builder_nonewlines.load_plain_text_syntax();
builder_nonewlines.load_syntaxes(package_dir, false).unwrap();
builder_nonewlines.add_plain_text_syntax();
builder_nonewlines.load_from_folder(package_dir, false).unwrap();
let ss_nonewlines = builder_nonewlines.build();
dump_to_file(&ss_nonewlines, packpath_nonewlines).unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions examples/syncat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn main() {
if let Some(folder) = matches.opt_str("extra-syntaxes") {
// TODO: no way to go back to builder anymore :/
let mut builder = ss.into_builder();
builder.load_syntaxes(folder, !no_newlines).unwrap();
builder.load_from_folder(folder, !no_newlines).unwrap();
ss = builder.build();
}

Expand Down Expand Up @@ -100,7 +100,7 @@ fn main() {

// We use read_line instead of `for line in highlighter.reader.lines()` because that
// doesn't return strings with a `\n`, and including the `\n` gets us more robust highlighting.
// See the documentation for `SyntaxSet::load_syntaxes`.
// See the documentation for `SyntaxSetBuilder::load_from_folder`.
// It also allows re-using the line buffer, which should be a tiny bit faster.
let mut line = String::new();
while highlighter.reader.read_line(&mut line).unwrap() > 0 {
Expand Down
2 changes: 1 addition & 1 deletion examples/syntest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ fn main() {
if !syntaxes_path.is_empty() {
println!("loading syntax definitions from {}", syntaxes_path);
let mut builder = SyntaxSetBuilder::new();
builder.load_syntaxes(&syntaxes_path, true).unwrap(); // note that we load the version with newlines
builder.load_from_folder(&syntaxes_path, true).unwrap(); // note that we load the version with newlines
ss = builder.build();
}

Expand Down
8 changes: 4 additions & 4 deletions src/dumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl SyntaxSet {
///
/// This method loads the version for parsing line strings with no `\n` characters at the end.
/// If you're able to efficiently include newlines at the end of strings, use `load_defaults_newlines`
/// since it works better. See `SyntaxSet#load_syntaxes` for more info on this issue.
/// since it works better. See `SyntaxSetBuilder::load_from_folder` for more info on this issue.
///
/// This is the recommended way of creating a syntax set for
/// non-advanced use cases. It is also significantly faster than loading the YAML files.
Expand Down Expand Up @@ -129,7 +129,7 @@ mod tests {
use super::*;
use parsing::SyntaxSetBuilder;
let mut builder = SyntaxSetBuilder::new();
builder.load_syntaxes("testdata/Packages", false).unwrap();
builder.load_from_folder("testdata/Packages", false).unwrap();
let ss = builder.build();

let bin = dump_binary(&ss);
Expand All @@ -145,12 +145,12 @@ mod tests {
use parsing::SyntaxSetBuilder;

let mut builder1 = SyntaxSetBuilder::new();
builder1.load_syntaxes("testdata/Packages", false).unwrap();
builder1.load_from_folder("testdata/Packages", false).unwrap();
let ss1 = builder1.build();
let bin1 = dump_binary(&ss1);

let mut builder2 = SyntaxSetBuilder::new();
builder2.load_syntaxes("testdata/Packages", false).unwrap();
builder2.load_from_folder("testdata/Packages", false).unwrap();
let ss2 = builder2.build();
let bin2 = dump_binary(&ss2);
// This is redundant, but assert_eq! can be really slow on a large
Expand Down
4 changes: 2 additions & 2 deletions src/parsing/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ contexts:
// check that each expected scope stack appears at least once while parsing the given test line

let mut builder = SyntaxSetBuilder::new();
builder.add_syntax(syntax);
builder.add(syntax);
let syntax_set = builder.build();

let mut state = ParseState::new(&syntax_set.syntaxes()[0]);
Expand All @@ -1424,7 +1424,7 @@ contexts:
fn parse(line: &str, syntax: &str) -> Vec<(usize, ScopeStackOp)> {
let syntax = SyntaxDefinition::load_from_str(syntax, true, None).unwrap();
let mut builder = SyntaxSetBuilder::new();
builder.add_syntax(syntax);
builder.add(syntax);
let syntax_set = builder.build();

let mut state = ParseState::new(&syntax_set.syntaxes()[0]);
Expand Down
50 changes: 27 additions & 23 deletions src/parsing/syntax_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ impl SyntaxSet {
SyntaxSet::default()
}

/// Convenience constructor calling `new` and then `load_syntaxes` on the resulting set
/// defaults to lines given not including newline characters, see the
/// `load_syntaxes` method docs for an explanation as to why this might not be the best.
/// Convenience constructor for creating a builder, then loading syntax
/// definitions from a folder and then building the syntax set.
///
/// Note that this uses `lines_include_newline` set to `false`, see the
/// `load_from_folder` method docs on `SyntaxSetBuilder` for an explanation
/// as to why this might not be the best.
#[cfg(feature = "yaml-load")]
pub fn load_from_folder<P: AsRef<Path>>(folder: P) -> Result<SyntaxSet, LoadingError> {
let mut builder = SyntaxSetBuilder::new();
builder.load_syntaxes(folder, false)?;
builder.load_from_folder(folder, false)?;
Ok(builder.build())
}

Expand Down Expand Up @@ -209,13 +212,13 @@ impl SyntaxSet {
/// to use the same highlighting pipeline code.
///
/// This syntax should always be present, if not this method will panic.
/// If the way you load syntaxes doesn't create one, use `load_plain_text_syntax`.
/// If the way you load syntaxes doesn't create one, use `add_plain_text_syntax`.
///
/// # Examples
/// ```
/// use syntect::parsing::SyntaxSetBuilder;
/// let mut builder = SyntaxSetBuilder::new();
/// builder.load_plain_text_syntax();
/// builder.add_plain_text_syntax();
/// let ss = builder.build();
/// let syntax = ss.find_syntax_by_token("rs").unwrap_or_else(|| ss.find_syntax_plain_text());
/// assert_eq!(syntax.name, "Plain Text");
Expand Down Expand Up @@ -292,22 +295,22 @@ impl SyntaxSetBuilder {
}

/// Add a syntax to the set.
pub fn add_syntax(&mut self, syntax: SyntaxDefinition) {
pub fn add(&mut self, syntax: SyntaxDefinition) {
self.syntaxes.push(syntax);
}

/// Rarely useful method that loads in a syntax with no highlighting rules for plain text.
/// Exists mainly for adding the plain text syntax to syntax set dumps, because for some
/// reason the default Sublime plain text syntax is still in `.tmLanguage` format.
#[cfg(feature = "yaml-load")]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that this needs YAML, but we could change it to instead just create the structs directly. Any thoughts?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure if you want to do the translation, submit a separate PR and I'll approve.

pub fn load_plain_text_syntax(&mut self) {
pub fn add_plain_text_syntax(&mut self) {
let s = "---\nname: Plain Text\nfile_extensions: [txt]\nscope: text.plain\ncontexts: \
{main: []}";
let syn = SyntaxDefinition::load_from_str(s, false, None).unwrap();
self.syntaxes.push(syn);
}

/// Loads all the .sublime-syntax files in a folder into this syntax set.
/// Loads all the .sublime-syntax files in a folder into this builder.
///
/// The `lines_include_newline` parameter is used to work around the fact that Sublime Text normally
/// passes line strings including newline characters (`\n`) to its regex engine. This results in many
Expand All @@ -319,10 +322,11 @@ impl SyntaxSetBuilder {
/// In the future I might include a "slow mode" that copies the lines passed in and appends a newline if there isn't one.
/// but in the interest of performance currently this hacky fix will have to do.
#[cfg(feature = "yaml-load")]
pub fn load_syntaxes<P: AsRef<Path>>(&mut self,
folder: P,
lines_include_newline: bool)
-> Result<(), LoadingError> {
pub fn load_from_folder<P: AsRef<Path>>(
&mut self,
folder: P,
lines_include_newline: bool
) -> Result<(), LoadingError> {
for entry in WalkDir::new(folder).sort_by(|a, b| a.file_name().cmp(b.file_name())) {
let entry = entry.map_err(LoadingError::WalkDir)?;
if entry.path().extension().map_or(false, |e| e == "sublime-syntax") {
Expand Down Expand Up @@ -588,7 +592,7 @@ mod tests {
#[test]
fn can_load() {
let mut builder = SyntaxSetBuilder::new();
builder.load_syntaxes("testdata/Packages", false).unwrap();
builder.load_from_folder("testdata/Packages", false).unwrap();

let cmake_dummy_syntax = SyntaxDefinition {
name: "CMake".to_string(),
Expand All @@ -600,8 +604,8 @@ mod tests {
contexts: HashMap::new(),
};

builder.add_syntax(cmake_dummy_syntax);
builder.load_plain_text_syntax();
builder.add(cmake_dummy_syntax);
builder.add_plain_text_syntax();

let ps = builder.build();

Expand Down Expand Up @@ -643,8 +647,8 @@ mod tests {
fn can_clone() {
let cloned_syntax_set = {
let mut builder = SyntaxSetBuilder::new();
builder.add_syntax(syntax_a());
builder.add_syntax(syntax_b());
builder.add(syntax_a());
builder.add(syntax_b());

let syntax_set_original = builder.build();
syntax_set_original.clone()
Expand All @@ -662,8 +666,8 @@ mod tests {
fn can_add_more_syntaxes_with_builder() {
let syntax_set_original = {
let mut builder = SyntaxSetBuilder::new();
builder.add_syntax(syntax_a());
builder.add_syntax(syntax_b());
builder.add(syntax_a());
builder.add(syntax_b());
builder.build()
};

Expand All @@ -681,7 +685,7 @@ mod tests {
push: scope:source.a#main
"#, true, None).unwrap();

builder.add_syntax(syntax_c);
builder.add(syntax_c);

let syntax_set = builder.build();

Expand All @@ -698,8 +702,8 @@ mod tests {

let syntax_set = {
let mut builder = SyntaxSetBuilder::new();
builder.add_syntax(syntax_a());
builder.add_syntax(syntax_b());
builder.add(syntax_a());
builder.add(syntax_b());
builder.build()
};

Expand Down