mirror of
https://github.com/sharkdp/bat
synced 2024-11-23 04:13:11 +00:00
Make bat::PrettyPrinter::syntaxes()
iterate over new bat::Syntax
struct (#2222)
We can't keep `syntect::parsing::SyntaxReference` as part of the public API, because that might prevent us from bumping to syntect 6.0.0 without also bumping bat to v2.0.0, once we reach v1.0.0. So introduce a new stripped down struct `Syntax` and return that instead. Let it be fully owned to make the API simple. It is not going to be in a hot code path anyway. I have looked at all code of our 27 dependents but I can't find a single instance of this method being used, so this change should be safe for v1.0.0.
This commit is contained in:
parent
8b8ff96c32
commit
49875d6ce7
4 changed files with 35 additions and 4 deletions
|
@ -28,6 +28,8 @@
|
||||||
|
|
||||||
## `bat` as a library
|
## `bat` as a library
|
||||||
|
|
||||||
|
- Make `bat::PrettyPrinter::syntaxes()` iterate over new `bat::Syntax` struct instead of `&syntect::parsing::SyntaxReference`. See #2222 (@Enselic)
|
||||||
|
|
||||||
|
|
||||||
# v0.21.0
|
# v0.21.0
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ pub(crate) mod syntax_mapping;
|
||||||
mod terminal;
|
mod terminal;
|
||||||
pub(crate) mod wrapping;
|
pub(crate) mod wrapping;
|
||||||
|
|
||||||
pub use pretty_printer::{Input, PrettyPrinter};
|
pub use pretty_printer::{Input, PrettyPrinter, Syntax};
|
||||||
pub use syntax_mapping::{MappingTarget, SyntaxMapping};
|
pub use syntax_mapping::{MappingTarget, SyntaxMapping};
|
||||||
pub use wrapping::WrappingMode;
|
pub use wrapping::WrappingMode;
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ use std::io::Read;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use console::Term;
|
use console::Term;
|
||||||
use syntect::parsing::SyntaxReference;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
assets::HighlightingAssets,
|
assets::HighlightingAssets,
|
||||||
|
@ -28,6 +27,12 @@ struct ActiveStyleComponents {
|
||||||
snip: bool,
|
snip: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub struct Syntax {
|
||||||
|
pub name: String,
|
||||||
|
pub file_extensions: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct PrettyPrinter<'a> {
|
pub struct PrettyPrinter<'a> {
|
||||||
inputs: Vec<Input<'a>>,
|
inputs: Vec<Input<'a>>,
|
||||||
config: Config<'a>,
|
config: Config<'a>,
|
||||||
|
@ -240,10 +245,18 @@ impl<'a> PrettyPrinter<'a> {
|
||||||
self.assets.themes()
|
self.assets.themes()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn syntaxes(&self) -> impl Iterator<Item = &SyntaxReference> {
|
pub fn syntaxes(&self) -> impl Iterator<Item = Syntax> + '_ {
|
||||||
// We always use assets from the binary, which are guaranteed to always
|
// We always use assets from the binary, which are guaranteed to always
|
||||||
// be valid, so get_syntaxes() can never fail here
|
// be valid, so get_syntaxes() can never fail here
|
||||||
self.assets.get_syntaxes().unwrap().iter()
|
self.assets
|
||||||
|
.get_syntaxes()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.filter(|s| !s.hidden)
|
||||||
|
.map(|s| Syntax {
|
||||||
|
name: s.name.clone(),
|
||||||
|
file_extensions: s.file_extensions.clone(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pretty-print all specified inputs. This method will "use" all stored inputs.
|
/// Pretty-print all specified inputs. This method will "use" all stored inputs.
|
||||||
|
|
16
tests/test_pretty_printer.rs
Normal file
16
tests/test_pretty_printer.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
use bat::PrettyPrinter;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn syntaxes() {
|
||||||
|
let printer = PrettyPrinter::new();
|
||||||
|
let syntaxes: Vec<String> = printer.syntaxes().map(|s| s.name).collect();
|
||||||
|
|
||||||
|
// Just do some sanity checking
|
||||||
|
assert!(syntaxes.contains(&"Rust".to_string()));
|
||||||
|
assert!(syntaxes.contains(&"Java".to_string()));
|
||||||
|
assert!(!syntaxes.contains(&"this-language-does-not-exist".to_string()));
|
||||||
|
|
||||||
|
// This language exists but is hidden, so we should not see it; it shall
|
||||||
|
// have been filtered out before getting to us
|
||||||
|
assert!(!syntaxes.contains(&"Git Common".to_string()));
|
||||||
|
}
|
Loading…
Reference in a new issue