Skip to content

Commit aafe342

Browse files
committed
store original path when syntax is loaded, and use that in syntest
1 parent df23792 commit aafe342

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

assets/default_newlines.packdump

1.38 KB
Binary file not shown.

assets/default_nonewlines.packdump

1.5 KB
Binary file not shown.

examples/syntest.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,9 @@ fn test_file(ss: &SyntaxSet, path: &Path, parse_test_lines: bool) -> Result<Synt
132132
let testtoken_end = captures.name("testtoken_end").map_or(None, |c|Some(c.as_str()));
133133
let syntax_file = captures.name("syntax_file").unwrap().as_str();
134134

135-
// find the relevant syntax definition to parse the file with
136-
// TODO: use the syntax definition file specified in the test file header, rather than relying on file extension
135+
// find the relevant syntax definition to parse the file with - case is important!
137136
println!("The test file references syntax definition file: {}", syntax_file);
138-
let syntax = match ss.find_syntax_for_file(path).unwrap_or(None) {
137+
let syntax = match ss.find_syntax_by_path(syntax_file) {
139138
Some(syntax) => syntax,
140139
None => return Err(SyntaxTestHeaderError::SyntaxDefinitionNotFound)
141140
};

src/parsing/syntax_set.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::mem;
1111
use std::rc::Rc;
1212
use std::ascii::AsciiExt;
1313
use std::sync::Mutex;
14+
use std::collections::HashMap;
1415
use onig::Regex;
1516
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
1617

@@ -26,6 +27,7 @@ pub struct SyntaxSet {
2627
syntaxes: Vec<SyntaxDefinition>,
2728
pub is_linked: bool,
2829
first_line_cache: Mutex<FirstLineCache>,
30+
path_scope_map: HashMap<String, Scope>,
2931
}
3032

3133
fn load_syntax_file(p: &Path,
@@ -44,6 +46,7 @@ impl Default for SyntaxSet {
4446
syntaxes: Vec::new(),
4547
is_linked: true,
4648
first_line_cache: Mutex::new(FirstLineCache::new()),
49+
path_scope_map: HashMap::new(),
4750
}
4851
}
4952
}
@@ -85,7 +88,9 @@ impl SyntaxSet {
8588
let entry = try!(entry.map_err(LoadingError::WalkDir));
8689
if entry.path().extension().map_or(false, |e| e == "sublime-syntax") {
8790
// println!("{}", entry.path().display());
88-
self.syntaxes.push(try!(load_syntax_file(entry.path(), lines_include_newline)));
91+
let syntax = try!(load_syntax_file(entry.path(), lines_include_newline));
92+
self.path_scope_map.insert(entry.path().to_str().unwrap().to_string(), syntax.scope);
93+
self.syntaxes.push(syntax);
8994
}
9095
}
9196
Ok(())
@@ -156,6 +161,11 @@ impl SyntaxSet {
156161
None
157162
}
158163

164+
/// Searches for a syntax by it's original file path when it was first loaded from disk
165+
pub fn find_syntax_by_path<'a>(&'a self, path: &str) -> Option<&'a SyntaxDefinition> {
166+
return self.path_scope_map.keys().find(|p| p.ends_with(path)).and_then(|p| self.syntaxes.iter().find(|&s| &s.scope == self.path_scope_map.get(p).unwrap()));
167+
}
168+
159169
/// Convenience method that tries to find the syntax for a file path,
160170
/// first by extension and then by first line of the file if that doesn't work.
161171
/// May IO Error because it sometimes tries to read the first line of the file.
@@ -372,21 +382,23 @@ impl FirstLineCache {
372382

373383
impl Encodable for SyntaxSet {
374384
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
375-
s.emit_struct("SyntaxSet", 2, |s| {
385+
s.emit_struct("SyntaxSet", 3, |s| {
376386
try!(s.emit_struct_field("syntaxes", 0, |s| self.syntaxes.encode(s)));
377387
try!(s.emit_struct_field("is_linked", 1, |s| self.is_linked.encode(s)));
388+
try!(s.emit_struct_field("path_scope_map", 2, |s| self.path_scope_map.encode(s)));
378389
Ok(())
379390
})
380391
}
381392
}
382393

383394
impl Decodable for SyntaxSet {
384395
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
385-
d.read_struct("SyntaxSet", 2, |d| {
396+
d.read_struct("SyntaxSet", 3, |d| {
386397
let ss = SyntaxSet {
387398
syntaxes: try!(d.read_struct_field("syntaxes", 0, Decodable::decode)),
388399
is_linked: try!(d.read_struct_field("is_linked", 1, Decodable::decode)),
389400
first_line_cache: Mutex::new(FirstLineCache::new()),
401+
path_scope_map: try!(d.read_struct_field("path_scope_map", 2, Decodable::decode)),
390402
};
391403

392404
Ok(ss)

0 commit comments

Comments
 (0)