Extract common syntax functionality to helper fns

This commit is contained in:
Kyle Criddle 2020-04-19 08:56:53 -06:00 committed by David Peter
parent 8067fd9eda
commit 83d408bab3

View file

@ -184,17 +184,8 @@ impl HighlightingAssets {
(Some(language), _) => self.syntax_set.find_syntax_by_token(language), (Some(language), _) => self.syntax_set.find_syntax_by_token(language),
(None, InputFile::Ordinary(ofile)) => { (None, InputFile::Ordinary(ofile)) => {
let path = Path::new(ofile.filename()); let path = Path::new(ofile.filename());
let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or(""); let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
let extension = path.extension().and_then(|x| x.to_str()).unwrap_or(""); let line_syntax = self.get_first_line_syntax(reader);
let ext_syntax = self
.syntax_set
.find_syntax_by_extension(&file_name)
.or_else(|| self.syntax_set.find_syntax_by_extension(&extension));
let line_syntax = String::from_utf8(reader.first_line.clone())
.ok()
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l));
let absolute_path = path.canonicalize().ok().unwrap_or(path.to_owned()); let absolute_path = path.canonicalize().ok().unwrap_or(path.to_owned());
match mapping.get_syntax_for(absolute_path) { match mapping.get_syntax_for(absolute_path) {
@ -204,31 +195,39 @@ impl HighlightingAssets {
self.syntax_set.find_syntax_by_name(syntax_name) self.syntax_set.find_syntax_by_name(syntax_name)
} }
Some(MappingTarget::MapToUnknown) => line_syntax, Some(MappingTarget::MapToUnknown) => line_syntax,
None => ext_syntax.or(line_syntax), None => self.get_extension_syntax(file_name).or(line_syntax),
} }
} }
(None, InputFile::StdIn(None)) => String::from_utf8(reader.first_line.clone()) (None, InputFile::StdIn(None)) => String::from_utf8(reader.first_line.clone())
.ok() .ok()
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)), .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)),
(None, InputFile::StdIn(Some(file_name))) => self (None, InputFile::StdIn(Some(file_name))) => self
.syntax_set .get_extension_syntax(file_name.to_str().unwrap())
.find_syntax_by_extension(file_name.to_str().unwrap()) .or(self.get_first_line_syntax(reader)),
.or_else(|| {
self.syntax_set.find_syntax_by_extension(
Path::new(file_name)
.extension()
.and_then(|x| x.to_str())
.unwrap_or(""),
)
})
.or(String::from_utf8(reader.first_line.clone())
.ok()
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))),
(_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"), (_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
}; };
syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text()) syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
} }
fn get_extension_syntax(&self, file_name: &str) -> Option<&SyntaxReference> {
self.syntax_set
.find_syntax_by_extension(file_name)
.or_else(|| {
self.syntax_set.find_syntax_by_extension(
Path::new(file_name)
.extension()
.and_then(|x| x.to_str())
.unwrap_or(""),
)
})
}
fn get_first_line_syntax(&self, reader: &mut InputFileReader) -> Option<&SyntaxReference> {
String::from_utf8(reader.first_line.clone())
.ok()
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))
}
} }
#[cfg(test)] #[cfg(test)]