Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Current master does not properly highlight Rust source file #906

Closed
mouse07410 opened this issue Apr 6, 2020 · 15 comments
Closed

Current master does not properly highlight Rust source file #906

mouse07410 opened this issue Apr 6, 2020 · 15 comments
Labels
question Further information is requested syntax-highlighting

Comments

@mouse07410
Copy link

mouse07410 commented Apr 6, 2020

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:

Screen Shot 2020-04-05 at 22 06 31

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)  
@mouse07410 mouse07410 added the bug Something isn't working label Apr 6, 2020
@keith-hall
Copy link
Collaborator

This is not a problem with bat (or syntect), but the Rust.sublime-syntax syntax 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/

@thomcc
Copy link

thomcc commented Apr 6, 2020

https://github.com/rust-lang/rust-enhanced might be better to use (although I don't know if it does any better here)

@mouse07410
Copy link
Author

https://github.com/rust-lang/rust-enhanced might be better to use (although I don't know if it does any better here)

Probably it would. How can I try it with the Cargo-installed bat (not build from local repo)?

Also, whatever's highlighting the code on this web page, appears to do a better job. Perhaps it could be used...

@mouse07410
Copy link
Author

@sharkdp can you switch to https://github.com/rust-lang/rust-enhanced ? Based on the opinion of the 'netizens, it is likely to give better results.

If you do switch (and I hope you do) - to get your updates, I should just reinstall bat via $ cargo install bat?

@sharkdp
Copy link
Owner

sharkdp commented Apr 6, 2020

There is no need to reinstall/recompile bat if you want to try a new syntax. Simply follow the steps at https://github.com/sharkdp/bat#adding-new-syntaxes--language-definitions

@mouse07410
Copy link
Author

Following your instructions above and installing the syntaxes - I can confirm that both work fine:

So, either of the two proposed fixes would work (tested).

@sharkdp
Copy link
Owner

sharkdp commented Apr 7, 2020

Thank you for reporting this upstream (sublimehq/Packages#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 (rust-lang/rust#44580), I think that this doesn't really constitute a bug.

In this case, I think we can just wait for sublimehq/Packages#2305 to be merged and close this?

@sharkdp sharkdp added question Further information is requested and removed bug Something isn't working upstream-error A bug in an upstream component labels Apr 7, 2020
@mouse07410
Copy link
Author

mouse07410 commented Apr 7, 2020

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 (rust-lang/rust#44580), I think that this doesn't really constitute a bug

I disagree - the problem seems unrelated to the unstable feature. The fact that you can remove several lines of code (reduce the sample) and still observe the problem seems to confirm it. And it is remedied by the current https://github.com/rust-lang/rust-enhanced

I think we can just wait for sublimehq/Packages#2305 to be merged and close this?

Since you gave me a good workaround (copying the relevant ...-syntax file and bat cache --build), I have no problem with this approach.

@sharkdp
Copy link
Owner

sharkdp commented Apr 7, 2020

I disagree - the problem seems unrelated to the unstable feature. The fact that you can remove several lines of code (reduce the sample) and still observe the problem seems to confirm it.

How would the smaller example look like? The problem is apparently caused by the string within angle brackets. Without const generics, strings inside angle brackets wouldn't be allowed, right?

@mouse07410
Copy link
Author

The problem is apparently caused by the string within angle brackets. Without const generics, strings inside angle brackets wouldn't be allowed, right?

You may be right. Anyway, this thing is supposed to syntax-highlight the source, not to play lint or clippy on 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).

@mouse07410
Copy link
Author

Current release 0.15 still fails:
Screen Shot 2020-04-26 at 10 51 36

Using syntax file from ExtendedRust still succeeds (with 0.15 release as well):
Screen Shot 2020-04-26 at 10 52 52

I repeat my suggestion: ditch Sublime syntax module for Rust, and pull one from ExtendedRust instead.

@mouse07410 mouse07410 reopened this Apr 26, 2020
@sharkdp
Copy link
Owner

sharkdp commented Apr 26, 2020

I repeat my suggestion: ditch Sublime syntax module for Rust, and pull one from ExtendedRust instead.

I deliberately decided against this for the reasons given above. If there is a Rust source code file that compiles on stable Rust, but is highlighted incorrectly by bat, I'm happy to reconsider.

If someone tells me that it is extremely likely that this unstable syntax will make it into stable Rust, I'm also OK with patching the current Rust syntax. I'd rather not have to maintain two Rust syntaxes, if possible.

@mouse07410
Copy link
Author

mouse07410 commented Apr 26, 2020

I deliberately decided against this for the reasons given above...

Respectfully disagree. bat is not a syntax validator - that's what clippy is for (which, might I remind, exists in the "nightly" toolchain also).

bat is supposed to be usable, e.g., for the source written for "nightly" toolchain - and there is no good reason that I'm aware of why it shouldn't be so. Especially since it won't cost you a single extra line of code.

I'd rather not have to maintain two Rust syntaxes, if possible.

Of course! Nobody's suggesting that. What I recommend is that you ditch the current
Sublime-based syntax and replace it with ExtendedRust, which, as shown above, works perfectly well for both "stable" and "nightly".

@sharkdp
Copy link
Owner

sharkdp commented Apr 29, 2020

Respectfully disagree. bat is not a syntax validator

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 bat even have a "invalid syntax" scope/color, further demonstrating that syntax validation and highlighting (ideally) is pretty much the same.

bat is supposed to be usable, e.g., for the source written for "nightly" toolchain - and there is no good reason that I'm aware of why it shouldn't be so.

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.

Especially since it won't cost you a single extra line of code.

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).

What I recommend is that you ditch the current Sublime-based syntax and replace it with ExtendedRust

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.

@mouse07410
Copy link
Author

mouse07410 commented Apr 29, 2020

both tools (syntax validators and syntax highlighters) have a lot in common.

So? Highlighters are expected to, well, highlight the code even with errors in it.

You cannot expect a completely invalid syntax to be highlighted properly (whatever that would mean)

Let's not go down this rabbit hole. "Completely" invalid and such...

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.

I'm not familiar enough with bat internals, but I thought that the whole idea of using somebody else's syntax packages was that somebody else maintains them (and incurs the cost), while bat merely pulls them in as-is.

Your point about "either include completely or not at all" is well-taken. That explains why my proposal may not be feasible.

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.

Yes, you're right. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested syntax-highlighting
Projects
None yet
Development

No branches or pull requests

4 participants