helix icon indicating copy to clipboard operation
helix copied to clipboard

Rust: incorrect highlights of `pub macro` and `///` (doc-comment)

Open Rudxain opened this issue 5 months ago • 5 comments

Summary

I've done a git bisect and both bugs exist long before the Tree-House migration, so I suspect the problem lies in TS-Rs grammar

Reproduction Steps

I tried this:

  1. hx tmp.rs
  2. Paste this into the buffer:
// this is needed for repro
fn f(){}

// prepending `pub` fixes the HL of the next `pub`
macro _const_assert {
    ($condition: expr, $const_msg:literal, $runtime_msg:literal, $($arg:tt)*) => {{
        if !$crate::intrinsics::likely($condition) {
            $crate::panic::const_panic!($const_msg, $runtime_msg, $($arg)*)
        }
    }}
} //`;` // fixes the HL of the lone `///`
// only if the previous `macro` has `pub` as well

/// A version of `assert` that prints a non-formatting message in const contexts.
///
/// See [`const_panic!`].
#[doc(hidden)] // attributes are needed to repro `pub` bug
pub macro const_assert {
    ($condition: expr, $const_msg:literal, $runtime_msg:literal, $($arg:tt)*) => {{
        if !$crate::intrinsics::likely($condition) {
            $crate::panic::const_panic!($const_msg, $runtime_msg, $($arg)*)
        }
    }}
}

I expected this to happen: pub is keyword; /// is comment.

Instead, this happened: pub is variable; /// is operator.

Helix log

~/.cache/helix/helix.log

Platform

Linux

Terminal Emulator

irrelevant

Installation Method

source

Helix Version

unknown

Rudxain avatar Jun 10 '25 00:06 Rudxain

Even GitHub's highlighter has the same bugs, lol. Should I close this?

Rudxain avatar Jun 10 '25 00:06 Rudxain

Looking at subtrees, it seems that there's some problem with parsing the macro which breaks how tree-sitter reads the rest of it?

CalebLarsen avatar Jun 10 '25 01:06 CalebLarsen

Yeah this is something that will need to be handled in tree-sitter-rust. I think the macro keyword is relatively new addition to Rust so it probably hasn't been added in tree-sitter-rust yet. Also I believe GitHub uses tree-sitter-rust for syntax highlighting these days.

the-mikedavis avatar Jun 10 '25 14:06 the-mikedavis

Yeah this is something that will need to be handled in tree-sitter-rust. I think the macro keyword is relatively new addition to Rust so it probably hasn't been added in tree-sitter-rust yet. Also I believe GitHub uses tree-sitter-rust for syntax highlighting these days.

How about switching to the tree-sitter-rust-orchad fork of tree-sitter-rust? It would fix this issue as well as support other features, like impl const

It seems to be more actively maintained:

Notable improvements

nik-rev avatar Jun 11 '25 10:06 nik-rev

If I recall correctly, imp const, or I guess impl ~const, was removed in an effort with a new RFC? Not sure that should be a thing in a grammar yet. But if it fixes other things I guess parsing other (random?) things could be fine?

RoloEdits avatar Jun 11 '25 11:06 RoloEdits