stsewd / tree-sitter-comment

Tree-sitter grammar for comment tags like TODO, FIXME(user).

Home Page:https://stsewd.dev/tree-sitter-comment/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] How to match inline-code in comments?

chrisgrieser opened this issue · comments

I am trying to match inline code in comments, e.g. the "foobar" in here:

// the function `foobar` should not be called to do this and that
function foobar () {
}

Following the example in the readme, I tried this:

("text" @markup.raw.markdown_inline
 (#match? @markup.raw.markdown_inline "`.+`"))

Which correctly highlights `foobar` but not `foobar()` or `foo.bar`. I also tried variations like "`[a-zA-Z\.]+`" for the regex, but those also did not work. I assume there are some limitations to the regex syntax, but in the docs I could find, I could not find clues?

Hi, exposing the text nodes from the parser was more like a hack in order to support uppercase only annotations (that don't end with :), issues, PRs, and usernames references (!, #, @). I've been considering stop exposing text nodes and just expose nodes for uppercase tags without :, and maybe nodes for #!@.

If you are trying to highlight Markdown in your comments, what you probably want to do is just inject the Markdowm parser in the comments (instead of or in addition to this parser).

Anyway, if you still want to manually match that, the problem is that `foobar()` produces several text tokens, `foobar, (, ), `. The reason is that those are basically mark a stop word, so things like (TODO: I'm a tag) work.

If you are trying to highlight Markdown in your comments, what you probably want to do is just inject the Markdowm parser in the comments (instead of or in addition to this parser).

Oh, that's a good idea! Is it possible to make "global injections", meaning for every filetype? Like, for instance, I wrote this to inject markdown into lua files, but it feels a bit tedious to add such injections for every single filetype.

; queries/lua/injections.scm
(comment
  (comment_content) @injection.content (#set! injection.language "markdown"))

Also, it seems to have a performance impact to inject markdown into every comment 😕


Anyway, if you still want to manually match that, the problem is that foobar() produces several text tokens, foobar, (, ), . The reason is that those are basically mark a stop word, so things like (TODO: I'm a tag) work.

This could be the better way I guess. So I see the problem with the tokens—but how do I deal with that problem though?

Oh, that's a good idea! Is it possible to make "global injections", meaning for every filetype? Like, for instance, I wrote this to inject markdown into lua files, but it feels a bit tedious to add such injections for every single filetype.

That's the way of doing it. If you are using Neovim, you may be able to override some internal lua function, maybe.

but how do I deal with that problem though?

You need to write several queries for each case. Maybe something like this

(("text"  @_start "text"* @content "text" @_end)
 (#match? @_start "^`")
 (#match? @_end "^`"))