stsewd / tree-sitter-comment

Tree-sitter grammar for comment tags like TODO, FIXME(user).
https://stsewd.dev/tree-sitter-comment/
MIT License
126 stars 8 forks source link

[Rust]: Highlight "safety comments" #7

Closed BastiDood closed 3 years ago

BastiDood commented 3 years ago

Hello there! It was mentioned in the README that this project is open to suggestions regarding popular comment conventions in other languages. I would like to propose adding highlighting support for "safety comments", a convention that is particularly prevalent among Rust programmers. Safety comments communicate why certain usage of unsafe code indeed does not violate invariants relied upon by the compiler and depended libraries. An example of such usage is given below:

let mut text: String = get_string_from_somwhere();
if text.is_ascii() {
    // SAFETY: We have verified that all characters are within the ASCII range.
    // Thus, the following byte-wise operations preserve UTF-8-validity.
    let bytes = unsafe { text.as_bytes_mut() };
    // Do aforementioned byte-wise operations here...
}

As illustrated above, the keyword I am proposing to add is SAFETY. It would be really neat for safety comments to pop out considering how critical they are to enforcing correctness in code. Thanks!

stsewd commented 3 years ago

Hi, this case is already covered by the current syntax https://github.com/stsewd/tree-sitter-comment#syntax

For example, using nvim-treesitter you get this

Screenshot from 2021-07-06 10-55-15

BastiDood commented 3 years ago

That's strange... Mine doesn't get highlighted. I'm currently using the latest stable Neovim (v0.5.0). Running :checkhealth nvim-treesitter and :checkhealth treesitter don't indicate any errors as well. As far as I know, I have enabled the highlighting module. Is there some configuration option I may have missed?

require'nvim-treesitter.configs'.setup {
    highlight = { enable = true }
}

Also, thanks for the quick response!

stsewd commented 3 years ago

@Some-Dood do you have the comment parser installed? That's :TSInstall comment, it could also have been overridden by the colorsheme you are using.

BastiDood commented 3 years ago

Yup, :TSInstall comment compiles just fine. And yes, you are correct about the color scheme. During the upgrade to the stable v0.5.0, I have forgotten to migrate to the modern color schemes for the One Dark theme. Indeed, as soon as I switched over, everything worked as expected. Thank you very much for your help!