Skip to content

Commit e22e29a

Browse files
[Fix::syntax] avoid filesystem access when guessing syntax (#125)
1 parent df80869 commit e22e29a

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

core/src/utils/syntax_provider.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[cfg(feature = "auto-detect")]
22
use hyperpolyglot_fork::detectors::classify;
3+
use std::path::Path;
34
use syntect::parsing::{SyntaxReference, SyntaxSet};
45

56
use 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")]

0 commit comments

Comments
 (0)