Bat: bin/rails script highlighted as HTML (Rails) instead of Ruby

Created on 19 May 2020  路  2Comments  路  Source: sharkdp/bat

What version of bat are you using?
bat 0.15.1

Describe the bug you encountered:
bin/rails in a Ruby on Rails project has a #!/usr/bin/env ruby shebang, but the Sublime syntax definition for "HTML (Rails)" includes the rails extension, which matches first and prevents any useful highlighting from occurring when running bat bin/rails.

Describe what you expected to happen?
bat bin/rails uses the #!/usr/bin/env ruby shebang to detect a Ruby filetype and use the Ruby syntax definition for highlighting.

How did you install bat?
brew install bat


system
------

**$ uname -srm**
Darwin 19.4.0 x86_64

**$ sw_vers**
ProductName:    Mac OS X
ProductVersion: 10.15.4
BuildVersion:   19E2269

bat
---

**$ bat --version**
bat 0.15.1

**$ env**

bat_config
----------

bat_wrapper
-----------

No wrapper script for 'bat'.

bat_wrapper_function
--------------------

No wrapper function for 'bat'.

No wrapper function for 'cat'.

tool
----

**$ less --version**
less 487 (POSIX regular expressions)
bug

Most helpful comment

Thank you for reporting this!

We recently discussed whether first-line syntax detection should take precedence over the filename syntax detection: see #685 and https://github.com/sharkdp/bat/issues/891#issuecomment-604617183

Unfortunately, there are some cases where it's not favorable to rely on first-line detection: https://github.com/sharkdp/bat/issues/685#issuecomment-564023303.

the Sublime syntax definition for "HTML (Rails)" includes the rails extension, which matches first and prevents any useful highlighting from occurring when running bat bin/rails

This is an unfortunate limitation of Sublime syntaxes. A pattern like rails will match against files with the rails extension but also against files with the name rails. This is to support things like Makefile or Dockerfile. Unfortunately, Sublime syntax does not distinguish between the two. In bat, we actually built a new mechanism to override/fix this behavior: the builtin syntax mappings and the --map-syntax command line option. We can use this to map glob patterns to specific syntaxes or to "unknown" (in which case we would look at the first line pattern).

To fix this bug, we only need to add a "map to unknown" entry:

diff --git a/assets/syntaxes.bin b/assets/syntaxes.bin
index a5a81ce..8c1932f 100644
Binary files a/assets/syntaxes.bin and b/assets/syntaxes.bin differ
diff --git a/assets/themes.bin b/assets/themes.bin
index 9de9839..f1510b0 100644
Binary files a/assets/themes.bin and b/assets/themes.bin differ
diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs
index 58a7154..b892c90 100644
--- a/src/syntax_mapping.rs
+++ b/src/syntax_mapping.rs
@@ -35,6 +35,12 @@ impl<'a> SyntaxMapping<'a> {
                 MappingTarget::MapTo("Bourne Again Shell (bash)"),
             )
             .unwrap();
+        mapping
+            .insert(
+                "rails",
+                MappingTarget::MapToUnknown,
+            )
+            .unwrap();
         mapping
             .insert(
                 "/etc/profile",

Note that this pattern only applies to files named rails (not to files named anything.rails, which would require a *.rails pattern).

Unfortunately, we do not have a way to do the same via --map-syntax yet. Otherwise, you could fix this locally by adding a line to bats config file. Maybe we should support something like --map-syntax=rails:- or --map-syntax=rails:unknown.

What you could do would be to add --map-syntax=rails:Ruby to your config file, which would be a workaround for this particular instance of the bug :smile:

All 2 comments

Thank you for reporting this!

We recently discussed whether first-line syntax detection should take precedence over the filename syntax detection: see #685 and https://github.com/sharkdp/bat/issues/891#issuecomment-604617183

Unfortunately, there are some cases where it's not favorable to rely on first-line detection: https://github.com/sharkdp/bat/issues/685#issuecomment-564023303.

the Sublime syntax definition for "HTML (Rails)" includes the rails extension, which matches first and prevents any useful highlighting from occurring when running bat bin/rails

This is an unfortunate limitation of Sublime syntaxes. A pattern like rails will match against files with the rails extension but also against files with the name rails. This is to support things like Makefile or Dockerfile. Unfortunately, Sublime syntax does not distinguish between the two. In bat, we actually built a new mechanism to override/fix this behavior: the builtin syntax mappings and the --map-syntax command line option. We can use this to map glob patterns to specific syntaxes or to "unknown" (in which case we would look at the first line pattern).

To fix this bug, we only need to add a "map to unknown" entry:

diff --git a/assets/syntaxes.bin b/assets/syntaxes.bin
index a5a81ce..8c1932f 100644
Binary files a/assets/syntaxes.bin and b/assets/syntaxes.bin differ
diff --git a/assets/themes.bin b/assets/themes.bin
index 9de9839..f1510b0 100644
Binary files a/assets/themes.bin and b/assets/themes.bin differ
diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs
index 58a7154..b892c90 100644
--- a/src/syntax_mapping.rs
+++ b/src/syntax_mapping.rs
@@ -35,6 +35,12 @@ impl<'a> SyntaxMapping<'a> {
                 MappingTarget::MapTo("Bourne Again Shell (bash)"),
             )
             .unwrap();
+        mapping
+            .insert(
+                "rails",
+                MappingTarget::MapToUnknown,
+            )
+            .unwrap();
         mapping
             .insert(
                 "/etc/profile",

Note that this pattern only applies to files named rails (not to files named anything.rails, which would require a *.rails pattern).

Unfortunately, we do not have a way to do the same via --map-syntax yet. Otherwise, you could fix this locally by adding a line to bats config file. Maybe we should support something like --map-syntax=rails:- or --map-syntax=rails:unknown.

What you could do would be to add --map-syntax=rails:Ruby to your config file, which would be a workaround for this particular instance of the bug :smile:

This issue should be fixed in bat v0.15.2. Feedback would be appreciated.

Was this page helpful?
0 / 5 - 0 ratings