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

Divide capture groups more precisely, for better control over highlighting. #177

Open
flosker opened this issue Aug 11, 2021 · 7 comments
Open
Assignees

Comments

@flosker
Copy link

flosker commented Aug 11, 2021

I think current capture / highlighting groups are somewhat vague & need to be divided more precisely.

E.g. currently :

  1. variable declaration & variable used in code share the same group @variable. They could be separate - @variable & @declaration or something like that.

  2. All types of brackets, braces & parens share the same group @punctuation.bracket. They could be separate - @punctuation.paren, @punctuation.brace, etc.

  3. Object key (when defining) & property (when using) have the same group @property. One more could be added like @object.key.

  4. try & catch keyword share the same group @exception. They could be separate - @exception.try & @exception.catch

  5. import & from keyword share the same group @include. They could be separates @include.import, @include.from. There are many more vague keywords.

  6. Regular strings & backtick (template strings) share the same group @string. One more could be added - @string.template.

  7. ....

Why

Doing this will allow user to create more precise highlighting of the code. For e.g. one could make try block in green color, & catch block in red. As catch only executes when try block produces errors. Different uses have different taste, so this will allow users to highlight each part of code exactly as they want.

Lot of traditional syntax highlighting file (e.g. vim-javascript) provide these kinds of precisions & allows better control over code highlighting. The mentioned syntax file even provides separate groups for braces of objects, function, conditionals, etc.

@flosker flosker changed the title Divide highlight groups more precisely, for better control over highlighting. Divide capture groups more precisely, for better control over highlighting. Aug 11, 2021
@flosker
Copy link
Author

flosker commented Aug 11, 2021

Right now, it is possible for user to create custom groups themselves : nvim-treesitter/nvim-treesitter#167

Example: Custom group for variable declarations @variable.declaration.

(variable_declarator
    name : (identifier)
@variable.declaration)

Although, It would better if they were more specific by default.

@mjambon
Copy link
Contributor

mjambon commented Aug 11, 2021

Hey all! This seems like a popular request (so many thumbs-up!). This isn't my expertise as we don't use the tree-sitter parsers for this at all. I'll defer to @maxbrunsfeld and to the GitHub folks who know about these choices.

Meanwhile:

Different constructs are given the same name via the alias() construct in the grammar files. The typescript grammar extends the javascript grammar, so there are two main files to inspect. I suggest looking at all the alias() constructs (of the form alias(original_name, visible_name)) and making pull requests or just a list of the proposed renaming. Here are the grammar files in question:

@flosker
Copy link
Author

flosker commented Aug 12, 2021

I did some groups overriding as per above method & here are the results. Things are easier to distinguish & are made more logical. READ comments for elements which are changed.

Screenshot 1

Screenshot 2

  1. Test file in screenshot

  2. queries/typescript/highlights.scm

  3. Treesitter plugin config for adding highlight groups - Neovim

export keyword rule is directly added in main source file, because overriding was not working due to priority issues.

@Iron-E
Copy link

Iron-E commented Aug 12, 2021

This would greatly help me resolve Iron-E/nvim-highlite#15 in my colorscheme. Rather than writing new queries I could simply link highlight types to look as expected.

I too have noticed traditional regex plugins provide finer control, and haven't had time to write custom queries to bridge the gap.

Edit: to be specific, I would be in favor of making all the treesitter groups more precise in this way, not just typescript.

@flosker
Copy link
Author

flosker commented Sep 18, 2021

Does anybody know how to give user customized rules priority than builtin ones? Like export keyword in above example.

@Iron-E
Copy link

Iron-E commented May 3, 2023

Does anybody know how to give user customized rules priority than builtin ones? Like export keyword in above example.

((super_important_node) @superimportant (#set! "priority" 105))

You may have to add nocombine to your highlights as well.

@diegodario88
Copy link

I did some groups overriding as per above method & here are the results. Things are easier to distinguish & are made more logical. READ comments for elements which are changed.

Screenshot 1

Screenshot 2

  1. Test file in screenshot
  2. queries/typescript/highlights.scm
  3. Treesitter plugin config for adding highlight groups - Neovim

export keyword rule is directly added in main source file, because overriding was not working due to priority issues.

How can I use these files to enhance the highlights in my colorscheme? Currently, both the 'await' and 'export' keywords are treated the same, but I want them to have different colors.

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

7 participants