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

How can you tell markid to ignore some identifiers? #5

Open
Jacob-Flasheye opened this issue Oct 6, 2022 · 17 comments
Open

How can you tell markid to ignore some identifiers? #5

Jacob-Flasheye opened this issue Oct 6, 2022 · 17 comments

Comments

@Jacob-Flasheye
Copy link

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?

@David-Kunz
Copy link
Owner

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

@Jacob-Flasheye
Copy link
Author

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!

@David-Kunz
Copy link
Owner

Here are some tips for writing queries.

@Jacob-Flasheye
Copy link
Author

Thank you, that's exactly what I needed!

@ziontee113
Copy link

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
Copy link

ziontee113 commented Oct 24, 2022

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

@David-Kunz
Copy link
Owner

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)

@ziontee113
Copy link

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 🤔

Also negated fields negates fields only, not an entire capture group or query 🤔 It's quite unfortunate.

@David-Kunz
Copy link
Owner

David-Kunz commented Oct 24, 2022

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

@ziontee113
Copy link

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 ❤️

@David-Kunz
Copy link
Owner

Thank you, @ziontee113 !

@igorgue
Copy link

igorgue commented Nov 3, 2022

@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

@David-Kunz
Copy link
Owner

David-Kunz commented Nov 3, 2022

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.

@ziontee113
Copy link

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

@igorgue
Copy link

igorgue commented Nov 4, 2022

@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
Copy link

igorgue commented Nov 4, 2022

@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

@David-Kunz
Copy link
Owner

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.

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

4 participants