HighlightingAssets: Turn get_syntax_for_path() into public API

This commit is contained in:
Martin Nordholts 2021-09-25 11:42:19 +02:00
parent ad98d35a48
commit 405a80f3ee
3 changed files with 26 additions and 20 deletions

View File

@ -33,7 +33,7 @@
## `bat` as a library ## `bat` as a library
- Deprecate `HighlightingAssets::syntaxes()` and `HighlightingAssets::syntax_for_file_name()`. Use `HighlightingAssets::get_syntaxes()` and `HighlightingAssets::get_syntax_for_file_name()` instead. They return a `Result` which is needed for upcoming lazy-loading work to improve startup performance. They also return what `SyntaxSet` the returned `SyntaxReference` belongs to. See #1747, #1755 and #1776 (@Enselic) - Deprecate `HighlightingAssets::syntaxes()` and `HighlightingAssets::syntax_for_file_name()`. Use `HighlightingAssets::get_syntaxes()` and `HighlightingAssets::get_syntax_for_path()` instead. They return a `Result` which is needed for upcoming lazy-loading work to improve startup performance. They also return which `SyntaxSet` the returned `SyntaxReference` belongs to. See #1747, #1755, #1776, #1862 (@Enselic)
- Remove `HighlightingAssets::from_files` and `HighlightingAssets::save_to_cache`. Instead of calling the former and then the latter you now make a single call to `bat::assets::build`. See #1802 (@Enselic) - Remove `HighlightingAssets::from_files` and `HighlightingAssets::save_to_cache`. Instead of calling the former and then the latter you now make a single call to `bat::assets::build`. See #1802 (@Enselic)
- Replace the `error::Error(error::ErrorKind, _)` struct and enum with an `error::Error` enum. `Error(ErrorKind::UnknownSyntax, _)` becomes `Error::UnknownSyntax`, etc. Also remove the `error::ResultExt` trait. These changes stem from replacing `error-chain` with `thiserror`. See #1820 (@Enselic) - Replace the `error::Error(error::ErrorKind, _)` struct and enum with an `error::Error` enum. `Error(ErrorKind::UnknownSyntax, _)` becomes `Error::UnknownSyntax`, etc. Also remove the `error::ResultExt` trait. These changes stem from replacing `error-chain` with `thiserror`. See #1820 (@Enselic)

View File

@ -140,29 +140,38 @@ impl HighlightingAssets {
} }
} }
/// Use [Self::get_syntax_for_file_name] instead /// Use [Self::get_syntax_for_path] instead
#[deprecated] #[deprecated]
pub fn syntax_for_file_name( pub fn syntax_for_file_name(
&self, &self,
file_name: impl AsRef<Path>, file_name: impl AsRef<Path>,
mapping: &SyntaxMapping, mapping: &SyntaxMapping,
) -> Option<&SyntaxReference> { ) -> Option<&SyntaxReference> {
self.get_syntax_for_file_name(file_name, mapping) self.get_syntax_for_path(file_name, mapping)
.expect( .ok()
".syntax_for_file_name() is deprecated, use .get_syntax_for_file_name() instead",
)
.map(|syntax_in_set| syntax_in_set.syntax) .map(|syntax_in_set| syntax_in_set.syntax)
} }
pub fn get_syntax_for_file_name( /// Detect the syntax based on, in order:
&self, /// 1. Syntax mappings (e.g. `/etc/profile` -> `Bourne Again Shell (bash)`)
file_name: impl AsRef<Path>, /// 2. The file name (e.g. `Dockerfile`)
mapping: &SyntaxMapping, /// 3. The file name extension (e.g. `.rs`)
) -> Result<Option<SyntaxReferenceInSet>> { ///
Ok(self.get_syntax_for_path(file_name.as_ref(), mapping).ok()) /// When detecting syntax based on syntax mappings, the full path is taken
} /// into account. When detecting syntax based on file name, no regard is
/// taken to the path of the file. Only the file name itself matters. When
fn get_syntax_for_path( /// detecting syntax based on file name extension, only the file name
/// extension itself matters.
///
/// Returns [Error::UndetectedSyntax] if it was not possible detect syntax
/// based on path/file name/extension (or if the path was mapped to
/// [MappingTarget::MapToUnknown]). In this case it is appropriate to fall
/// back to other methods to detect syntax. Such as using the contents of
/// the first line of the file.
///
/// Returns [Error::UnknownSyntax] if a syntax mapping exist, but the mapped
/// syntax does not exist.
pub fn get_syntax_for_path(
&self, &self,
path: impl AsRef<Path>, path: impl AsRef<Path>,
mapping: &SyntaxMapping, mapping: &SyntaxMapping,
@ -218,7 +227,6 @@ impl HighlightingAssets {
let path = input.path(); let path = input.path();
let path_syntax = if let Some(path) = path { let path_syntax = if let Some(path) = path {
// If a path was provided, we try and detect the syntax based on extension mappings.
self.get_syntax_for_path( self.get_syntax_for_path(
PathAbs::new(path).map_or_else(|_| path.to_owned(), |p| p.as_path().to_path_buf()), PathAbs::new(path).map_or_else(|_| path.to_owned(), |p| p.as_path().to_path_buf()),
mapping, mapping,

View File

@ -104,10 +104,8 @@ pub fn get_languages(config: &Config) -> Result<String> {
} }
let test_file = Path::new("test").with_extension(extension); let test_file = Path::new("test").with_extension(extension);
let syntax_in_set = assets let syntax_in_set = assets.get_syntax_for_path(test_file, &config.syntax_mapping);
.get_syntax_for_file_name(test_file, &config.syntax_mapping) matches!(syntax_in_set, Ok(syntax_in_set) if syntax_in_set.syntax.name == lang_name)
.unwrap(); // safe since .get_syntaxes() above worked
matches!(syntax_in_set, Some(syntax_in_set) if syntax_in_set.syntax.name == lang_name)
}); });
} }