Skip to content

Commit

Permalink
Merge pull request #129 from forkeith/fallback_name
Browse files Browse the repository at this point in the history
pass fallback name based on syntax filename to the YAML loader
  • Loading branch information
trishume authored Jan 2, 2018
2 parents af810cd + 2a39a2f commit 5934b0c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/parsing/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,11 @@ contexts:
fn expect_scope_stacks(line_without_newline: &str, expect: &[&str], syntax: &str) {
println!("Parsing with newlines");
let line_with_newline = format!("{}\n", line_without_newline);
let syntax_newlines = SyntaxDefinition::load_from_str(&syntax, true).unwrap();
let syntax_newlines = SyntaxDefinition::load_from_str(&syntax, true, None).unwrap();
expect_scope_stacks_with_syntax(&line_with_newline, expect, syntax_newlines);

println!("Parsing without newlines");
let syntax_nonewlines = SyntaxDefinition::load_from_str(&syntax, false).unwrap();
let syntax_nonewlines = SyntaxDefinition::load_from_str(&syntax, false, None).unwrap();
expect_scope_stacks_with_syntax(&line_without_newline, expect, syntax_nonewlines);
}

Expand Down
4 changes: 2 additions & 2 deletions src/parsing/syntax_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn load_syntax_file(p: &Path,
let mut s = String::new();
f.read_to_string(&mut s)?;

Ok(SyntaxDefinition::load_from_str(&s, lines_include_newline)?)
Ok(SyntaxDefinition::load_from_str(&s, lines_include_newline, p.file_stem().and_then(|x| x.to_str()))?)
}

impl Default for SyntaxSet {
Expand Down Expand Up @@ -124,7 +124,7 @@ impl SyntaxSet {
pub fn load_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).unwrap();
let syn = SyntaxDefinition::load_from_str(s, false, None).unwrap();
self.syntaxes.push(syn);
}

Expand Down
33 changes: 23 additions & 10 deletions src/parsing/yaml_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ impl SyntaxDefinition {
/// In case you want to create your own SyntaxDefinition's in memory from strings.
/// Generally you should use a `SyntaxSet`
pub fn load_from_str(s: &str,
lines_include_newline: bool)
lines_include_newline: bool,
fallback_name: Option<&str>)
-> Result<SyntaxDefinition, ParseSyntaxError> {
let docs = match YamlLoader::load_from_str(s) {
Ok(x) => x,
Expand All @@ -77,12 +78,13 @@ impl SyntaxDefinition {
}
let doc = &docs[0];
let mut scope_repo = SCOPE_REPO.lock().unwrap();
SyntaxDefinition::parse_top_level(doc, scope_repo.deref_mut(), lines_include_newline)
SyntaxDefinition::parse_top_level(doc, scope_repo.deref_mut(), lines_include_newline, fallback_name)
}

fn parse_top_level(doc: &Yaml,
scope_repo: &mut ScopeRepository,
lines_include_newline: bool)
lines_include_newline: bool,
fallback_name: Option<&str>)
-> Result<SyntaxDefinition, ParseSyntaxError> {
let h = doc.as_hash().ok_or(ParseSyntaxError::TypeMismatch)?;

Expand Down Expand Up @@ -113,7 +115,7 @@ impl SyntaxDefinition {
SyntaxDefinition::add_initial_contexts(&mut contexts, &mut state, top_level_scope);

let defn = SyntaxDefinition {
name: get_key(h, "name", |x| x.as_str()).unwrap_or("Unnamed").to_owned(),
name: get_key(h, "name", |x| x.as_str()).unwrap_or(fallback_name.unwrap_or("Unnamed")).to_owned(),
scope: top_level_scope,
file_extensions: {
get_key(h, "file_extensions", |x| x.as_vec())
Expand Down Expand Up @@ -563,7 +565,7 @@ mod tests {
fn can_parse() {
let defn: SyntaxDefinition =
SyntaxDefinition::load_from_str("name: C\nscope: source.c\ncontexts: {main: []}",
false)
false, None)
.unwrap();
assert_eq!(defn.name, "C");
assert_eq!(defn.scope, Scope::new("source.c").unwrap());
Expand Down Expand Up @@ -603,7 +605,7 @@ mod tests {
- match: '\"'
pop: true
",
false)
false, None)
.unwrap();
assert_eq!(defn2.name, "C");
let top_level_scope = Scope::new("source.c").unwrap();
Expand Down Expand Up @@ -673,7 +675,7 @@ mod tests {
with_prototype:
- match: (?=(?i)(?=</style))
pop: true
"#,false).unwrap();
"#,false, None).unwrap();

let def_with_embed = SyntaxDefinition::load_from_str(r#"
name: C
Expand All @@ -689,7 +691,7 @@ mod tests {
embed: scope:source.css
embed_scope: source.css.embedded.html
escape: (?i)(?=</style)
"#,false).unwrap();
"#,false, None).unwrap();

assert_eq!(old_def.contexts["main"], def_with_embed.contexts["main"]);
}
Expand All @@ -709,7 +711,7 @@ mod tests {
1: meta.tag.style.begin.html punctuation.definition.tag.end.html
embed: scope:source.css
embed_scope: source.css.embedded.html
"#,false);
"#,false, None);
assert!(def.is_err());
match def.unwrap_err() {
ParseSyntaxError::MissingMandatoryKey(key) => assert_eq!(key, "escape"),
Expand Down Expand Up @@ -742,7 +744,7 @@ mod tests {
- match: '(?=\\S)'
pop: true
",
false)
false, None)
.unwrap();
assert_eq!(defn.name, "LaTeX");
let top_level_scope = Scope::new("text.tex.latex").unwrap();
Expand All @@ -765,6 +767,17 @@ mod tests {
}
}

#[test]
fn can_use_fallback_name() {
let def = SyntaxDefinition::load_from_str(r#"
scope: source.c
contexts:
main:
- match: ''
"#,false, Some("C"));
assert_eq!(def.unwrap().name, "C");
}

#[test]
fn can_rewrite_regex() {
fn rewrite(s: &str) -> String {
Expand Down

0 comments on commit 5934b0c

Please sign in to comment.