markid
markid copied to clipboard
How can you tell markid to ignore some identifiers?
Hi!
I really like the idea behind this plugin, but there are some identifiers that I do not want it to highlight, for example the identifier in between the from
and import
in this python code:
from foo.bar import baz
How would I instruct markid to ignore identifiers depending on their place in the tree?
Hi @Jacob-Flasheye ,
Thanks a lot!
In principle, you can create a custom Tree-Sitter query for identifier selection and set it in queries.default
(or for your specific language).
Hint: You can use the tool Tree-Sitter Playground to play around with queries.
Best regards, David
Thanks for the answer.
I've tried to use the playground, but the problem is I don't understand the language used to perform queries, and I've struggled to find resources explaining it. If you know a good resource, please share it, otherwise I'll keep looking!
Here are some tips for writing queries.
Thank you, that's exactly what I needed!
I use this query to ignore any require
or ipairs
identifiers in Lua
markit.queries = {
default = '((identifier) @markid (#not-any-of? @markid "require" "ipairs"))',
}
@David-Kunz Hi sir. I wonder if it's possible for Markid to match any identifier
that doesn't match this query:
((function_declaration name:(identifier) @ignoreMe))
Or is there a way for me to write a query that ignores any identifier
that's under function_declaration
from the beginning? Basically ignore an entire query with Markid. Thank you very much!
Hi @ziontee113 ,
Unfortunately I haven't played around with exclusion queries, but you can try it with treesitter-playground.
Maybe with negated fields?
Negated Fields You can also constrain a pattern so that it only matches nodes that lack a certain field. To do this, add a field name prefixed by a ! within the parent pattern. For example, this pattern would match a class declaration with no type parameters:
(class_declaration
name: (identifier) @class_name
!type_parameters)
Hi @David-Kunz , I did try around with exclusion queries with no success. I feel that it's up to Markid to handle exclusion queries logic if there's not a way to write exclusion queries like so :thinking:
Also negated fields
negates fields only, not an entire capture group or query :thinking: It's quite unfortunate.
@ziontee113 , I see.
I'd rather not include that in markid as this would mean to not only retrieve all matches for the first query, but also for the exclusion query and then comparing both. This could cripple the performance.
I'd rather not include that in markid as this would mean to not only retrieve all matches for the first query, but also for the exclusion query and then comparing both. This could cripple the performance.
I agree. Thank you very much for the amazing plugin :heart:
Thank you, @ziontee113 !
@David-Kunz I was trying to ignore the params highlight and I found of a useful [not]-has_parent
predicate:
queries = {
default = '((identifier) @markid (#not-has-parent? @markid parameters typed_parameter typed_default_parameter))',
},
But doesn't really follow
the identifier and it keeps getting colored, look:
Thanks for the info @igorgue . What happens if you run this query in TSPlayground, are the right identifiers highlighted? I would imagine that only the identifiers in the function signature are excluded, not the ones un the function body.
Hi @David-Kunz , @igorgue , I can confirm this query works on Treesitter Playground and with Markid:
( (identifier) @markid (#not-has-ancestor? @markid jsx_expression jsx_opening_element jsx_closing_element) )
or
( (identifier) @markid (#not-has-parent? @markid jsx_expression jsx_opening_element jsx_closing_element) )
@David-Kunz It's okay I just wanted to help, but no, the query would get the identifier in the params but it'd still get selected since it's an identifier in the block of the function... I'm still thinking about the query
@David-Kunz maybe like Formatter
the plugin [1], you can have a way to have default queries
by language in the repo, since I think most people I show this they say it's too much to color every identifier, I think is a great default, but I've seen the opinion, I just don't know which those defaults are, on the top of my mind probably do not color constants?
[1] https://github.com/mhartington/formatter.nvim/tree/master/lua/formatter/filetypes
That's true @igorgue , for many it's too colourful. It's just hard to come up with better defaults without introducing many language-specific queries. I haven't yet come to a solution.