@@ -11,6 +11,7 @@ use std::mem;
1111use std:: rc:: Rc ;
1212use std:: ascii:: AsciiExt ;
1313use std:: sync:: Mutex ;
14+ use std:: collections:: HashMap ;
1415use onig:: Regex ;
1516use 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
3133fn 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
373383impl 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
383394impl 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