markid icon indicating copy to clipboard operation
markid copied to clipboard

How can you tell markid to ignore some identifiers?

Open Jacob-Flasheye opened this issue 2 years ago • 17 comments

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?

Jacob-Flasheye avatar Oct 06 '22 09:10 Jacob-Flasheye

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

David-Kunz avatar Oct 06 '22 11:10 David-Kunz

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!

Jacob-Flasheye avatar Oct 06 '22 11:10 Jacob-Flasheye

Here are some tips for writing queries.

David-Kunz avatar Oct 06 '22 11:10 David-Kunz

Thank you, that's exactly what I needed!

Jacob-Flasheye avatar Oct 06 '22 11:10 Jacob-Flasheye

I use this query to ignore any require or ipairs identifiers in Lua

markit.queries = {
	default = '((identifier) @markid (#not-any-of? @markid "require" "ipairs"))',
}

ziontee113 avatar Oct 24 '22 13:10 ziontee113

@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!

ziontee113 avatar Oct 24 '22 13:10 ziontee113

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)

David-Kunz avatar Oct 24 '22 14:10 David-Kunz

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 avatar Oct 24 '22 14:10 ziontee113

@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.

David-Kunz avatar Oct 24 '22 15:10 David-Kunz

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:

ziontee113 avatar Oct 24 '22 15:10 ziontee113

Thank you, @ziontee113 !

David-Kunz avatar Oct 24 '22 15:10 David-Kunz

@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:

image

igorgue avatar Nov 03 '22 19:11 igorgue

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.

David-Kunz avatar Nov 03 '22 21:11 David-Kunz

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) )

ziontee113 avatar Nov 03 '22 21:11 ziontee113

@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

igorgue avatar Nov 04 '22 16:11 igorgue

@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

igorgue avatar Nov 04 '22 16:11 igorgue

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.

David-Kunz avatar Nov 04 '22 19:11 David-Kunz