File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11#[ cfg( feature = "auto-detect" ) ]
22use hyperpolyglot_fork:: detectors:: classify;
3+ use std:: path:: Path ;
34use syntect:: parsing:: { SyntaxReference , SyntaxSet } ;
45
56use crate :: components:: interface:: render_error:: RenderError ;
@@ -17,13 +18,20 @@ impl SyntaxProvider {
1718 ) -> Result < SyntaxReference , RenderError > {
1819 let syntax = match & language {
1920 Some ( language) => self . syntax_set . find_syntax_by_token ( & language) ,
20- None => match & code_file_path {
21- Some ( file_path) => self
22- . syntax_set
23- . find_syntax_for_file ( & file_path)
24- . map_err ( |_| RenderError :: NoSuchFile ( file_path. clone ( ) ) ) ?,
25- None => self . syntax_set . find_syntax_by_first_line ( code) ,
26- } ,
21+ None => {
22+ let by_path = code_file_path. as_deref ( ) . and_then ( |p| {
23+ let path = Path :: new ( p) ;
24+ path. file_name ( )
25+ . and_then ( |n| n. to_str ( ) )
26+ . and_then ( |n| self . syntax_set . find_syntax_by_extension ( n) )
27+ . or_else ( || {
28+ path. extension ( )
29+ . and_then ( |e| e. to_str ( ) )
30+ . and_then ( |e| self . syntax_set . find_syntax_by_extension ( e) )
31+ } )
32+ } ) ;
33+ by_path. or_else ( || self . syntax_set . find_syntax_by_first_line ( code) )
34+ }
2735 } ;
2836
2937 #[ cfg( feature = "auto-detect" ) ]
You can’t perform that action at this time.
0 commit comments