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

Implement bracket coloring by depth #21875

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

marcospb19
Copy link
Contributor

@marcospb19 marcospb19 commented Dec 11, 2024

Implement bracket coloring by depth.

Closes #5259.

bracket coloring preview


Hi! 👋 This is my first PR.

I just hit a wall and need some feedback.

(I'm willing to pair!)

Approach taken

In the Tree Sitter highlighting rules file highlights.scm, add two captures:

  • punctuation.bracket.open
  • punctuation.bracket.close

So we can track depth with the existing highlighting pass (no additional tree traversals).

This is backward-compatible with extensions, as punctuation.bracket is only used for highlighting, and punctuation.bracket.[open,close] are both highlighted by the punctuation.bracket style as a fallback.

The PR code is language-agnostic and bracket coloring doesn't work till the language's highlights.scm file is updated with both open and close definitions, this way, extension authors can choose what symbols should be colored by depth for the respective language.

Note about brackets.scm (click me).

It's worth noting that these definitions are different from what can be found in brackets.scm, which is "all symbols that delimit a region", and include || for closures, and "" for strings, those are used in different parts of the editor like region selection and innermost region highlight, but, they shouldn't be colored by depth (IMO).

Problems

I'm checking Tree Sitter captures in the iteration done in BufferChunks, but it seems like the highlights query doesn't walk the syntax from the very start.

As a result, when I scroll down into nested scopes, I can't keep track of the context in the lines above, which makes calculating the depth impossible.

Am I missing something?

TODO:

  • Fix implementation (problems described above)
  • Add docs on punctuation.bracket.[open,close] for extensions
  • Edit highlights.scm of remaining languages in the repo
    • For testing purposes, I only updated Rust's for now
  • Add tests
  • Add setting to toggle bracket coloring
    • Update release notes line in Pull Request
  • Make it look better?
    • I'm using theme accent colors
    • Maybe blend rainbow colors with punctuation.bracket or text color?

Open questions:

  • Should it match colors with indentation coloring?
  • How to error-recover from unmatched/unclosed brackets?
    • Should a ( match colors with the next } or ]?

Release Notes:

  • Added bracket coloring by depth (TODO: update this with setting name).

brackets can only be colored in languages that further specify
punctuation.bracket.[open,close], otherwise, it can't be parsed
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rainbow brackets
1 participant