bat: Current master does not properly highlight Rust source file
What version of bat are you using?
bat --version
bat 0.15.0
Describe the bug you encountered:
I’m trying to display/highlight the following Rust source file const-generics.rs:
#![feature(const_generics)]
mod state {
pub struct State<const S: &'static str> {
total: u32,
}
impl State<"init"> {
pub fn new() -> Self {
Self { total: 0 }
}
pub fn accumulate(self) -> State<"accumulate"> {
State { total: self.total }
}
}
impl State<"accumulate"> {
pub fn add(&mut self, add: u32) {
self.total += add
}
pub fn freeze(self) -> State<"freeze"> {
State { total: self.total }
}
}
impl State<"freeze"> {
pub fn unwrap(self) -> u32 {
self.total
}
}
}
use state::State;
fn main() {
let init = State::new();
// init.add(120); // nope
let mut acc = init.accumulate();
acc.add(10);
acc.add(20);
let frozen = acc.freeze();
// frozen.add(120); // nope
let val = frozen.unwrap();
println!("{}", val);
}
What is happening - it stops syntax-highlighting after the first impl<> is processed:

Describe what you expected to happen? The entire source file highlighted appropriately. Same way, as, e.g., the above source is highlighted by this web page.
How did you install bat?
$ cargo install bat
system
------
**$ uname -srm**
Darwin 19.4.0 x86_64
**$ sw_vers**
ProductName: Mac OS X
ProductVersion: 10.15.4
BuildVersion: 19E266
bat
---
**$ bat --version**
bat 0.13.0
**$ env**
bat_config
----------
bat_wrapper
-----------
No wrapper script.
bat_wrapper_function
--------------------
No wrapper function.
tool
----
**$ less --version**
less 487 (POSIX regular expressions)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (5 by maintainers)
I’m not here to argue, but I think that both tools (syntax validators and syntax highlighters) have a lot in common. You cannot expect a completely invalid syntax to be highlighted properly (whatever that would mean). Some syntaxes in
bateven have a “invalid syntax” scope/color, further demonstrating that syntax validation and highlighting (ideally) is pretty much the same.We’re not going to add syntaxes for every possible flavor of every possible programming language out there. That is not feasible (from a maintenance and performance point of view).
“Rust nightly” is not (yet) the official version of the language. Its syntax is prone to frequent changes. There is no way we could keep up with that.
Adding new syntaxes adds a maintenance cost and a performance cost (#951). The current Rust syntax has served us well for years. Using a new syntax could lead to all kinds of surprises (e.g. #677 #914 #924 #750).
Note that the Rust syntax is part of sublimehq/Packages, which we either include completely or not at all. But that’s not the main point. Maintenance cost (see above) is.
Also, we have invested a lot of time in the customization features of
bat. If users prefer another syntax, it’s easy to replace it.You may be right. Anyway, this thing is supposed to syntax-highlight the source, not to play
lintorclippyon it. IMHO, at least.Regardless, the only thing I care about here is that the sources are properly and comprehensively highlighted. The workaround you suggested works perfectly for me, with a minimal inconvenience.
Therefore, IMHO it’s OK to patch (or switch from Sublime to RustEnhanced) whenever you think the right time is, if at all. Since my machines all now incorporate the workaround - timeline doesn’t matter (for me).
Thank you for reporting this upstream (https://github.com/sublimehq/Packages/issues/2323).
Given that you are trying to highlight Rust source code with a language feature (const generics) that is not stable and hasn’t even finished the design phase (https://github.com/rust-lang/rust/issues/44580), I think that this doesn’t really constitute a bug.
In this case, I think we can just wait for https://github.com/sublimehq/Packages/pull/2305 to be merged and close this?
This is not a problem with
bat(orsyntect), but theRust.sublime-syntaxsyntax definition being used - it currently doesn’t correctly handle strings inside angle brackets. It may be worth logging it at https://github.com/sublimehq/Packages/issues/