on master help sign in
HighlightingAssets: Turn get_syntax_for_path() into public API
commit405a80f3eeab25870249943ceb4f209e6599eca1 authorMartin Nordholts <enselic@gmail.com> dateSat 25 Sep 2021, 11:42 (5 years ago) parentad98d35
3 files changed+26−20
CHANGELOG.md+1−1 src/assets.rs+23−15 src/bin/bat/main.rs+2−4
CHANGELOG.md+1−1
@@ -33,7 +33,7 @@
3333
3434## `bat` as a library
3535
36−- 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)
36+- 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)
3737- 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)
3838- 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)
3939
src/assets.rs+23−15
@@ -140,29 +140,38 @@ impl HighlightingAssets {
140140 }
141141 }
142142
143− /// Use [Self::get_syntax_for_file_name] instead
143+ /// Use [Self::get_syntax_for_path] instead
144144 #[deprecated]
145145 pub fn syntax_for_file_name(
146146 &self,
147147 file_name: impl AsRef<Path>,
148148 mapping: &SyntaxMapping,
149149 ) -> Option<&SyntaxReference> {
150− self.get_syntax_for_file_name(file_name, mapping)
151− .expect(
152− ".syntax_for_file_name() is deprecated, use .get_syntax_for_file_name() instead",
153− )
150+ self.get_syntax_for_path(file_name, mapping)
151+ .ok()
154152 .map(|syntax_in_set| syntax_in_set.syntax)
155153 }
156154
157− pub fn get_syntax_for_file_name(
158− &self,
159− file_name: impl AsRef<Path>,
160− mapping: &SyntaxMapping,
161− ) -> Result<Option<SyntaxReferenceInSet>> {
162− Ok(self.get_syntax_for_path(file_name.as_ref(), mapping).ok())
163− }
164−
165− fn get_syntax_for_path(
155+ /// Detect the syntax based on, in order:
156+ /// 1. Syntax mappings (e.g. `/etc/profile` -> `Bourne Again Shell (bash)`)
157+ /// 2. The file name (e.g. `Dockerfile`)
158+ /// 3. The file name extension (e.g. `.rs`)
159+ ///
160+ /// When detecting syntax based on syntax mappings, the full path is taken
161+ /// into account. When detecting syntax based on file name, no regard is
162+ /// taken to the path of the file. Only the file name itself matters. When
163+ /// detecting syntax based on file name extension, only the file name
164+ /// extension itself matters.
165+ ///
166+ /// Returns [Error::UndetectedSyntax] if it was not possible detect syntax
167+ /// based on path/file name/extension (or if the path was mapped to
168+ /// [MappingTarget::MapToUnknown]). In this case it is appropriate to fall
169+ /// back to other methods to detect syntax. Such as using the contents of
170+ /// the first line of the file.
171+ ///
172+ /// Returns [Error::UnknownSyntax] if a syntax mapping exist, but the mapped
173+ /// syntax does not exist.
174+ pub fn get_syntax_for_path(
166175 &self,
167176 path: impl AsRef<Path>,
168177 mapping: &SyntaxMapping,
@@ -218,7 +227,6 @@ impl HighlightingAssets {
218227
219228 let path = input.path();
220229 let path_syntax = if let Some(path) = path {
221− // If a path was provided, we try and detect the syntax based on extension mappings.
222230 self.get_syntax_for_path(
223231 PathAbs::new(path).map_or_else(|_| path.to_owned(), |p| p.as_path().to_path_buf()),
224232 mapping,
src/bin/bat/main.rs+2−4
@@ -104,10 +104,8 @@ pub fn get_languages(config: &Config) -> Result<String> {
104104 }
105105
106106 let test_file = Path::new("test").with_extension(extension);
107− let syntax_in_set = assets
108− .get_syntax_for_file_name(test_file, &config.syntax_mapping)
109− .unwrap(); // safe since .get_syntaxes() above worked
110− matches!(syntax_in_set, Some(syntax_in_set) if syntax_in_set.syntax.name == lang_name)
107+ let syntax_in_set = assets.get_syntax_for_path(test_file, &config.syntax_mapping);
108+ matches!(syntax_in_set, Ok(syntax_in_set) if syntax_in_set.syntax.name == lang_name)
111109 });
112110 }
113111