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

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

Open
chrisgrieser opened this issue Feb 22, 2024 · 3 comments
Open

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

chrisgrieser opened this issue Feb 22, 2024 · 3 comments

Comments

@chrisgrieser
Copy link

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?

@stsewd
Copy link
Owner

stsewd commented Feb 23, 2024

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.

@chrisgrieser
Copy link
Author

chrisgrieser commented Feb 23, 2024

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?

@stsewd
Copy link
Owner

stsewd commented Feb 24, 2024

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 "^`"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants