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

Manual overriding for theme precedence #12534

Open
llakala opened this issue Jan 14, 2025 · 0 comments
Open

Manual overriding for theme precedence #12534

llakala opened this issue Jan 14, 2025 · 0 comments
Labels
C-enhancement Category: Improvements

Comments

@llakala
Copy link

llakala commented Jan 14, 2025

As stated in the documentation, the rule for which theme key takes precedence is:

When determining styling for a highlight, the longest matching theme key will be used. For example, if the highlight is function.builtin.static, the key function.builtin will be used instead of function.

However, what if you have two keys of equal precedence?

My specific example

I'm attempting to write a theme that works well for the Nix language. My goal is for all strings to be given the same color, even those in an attribute's key. So, in this example:

{
  foo = "bar";
  one."two" = 3;
}

I want to ensure "bar" and "two" are both highlighted the same color. Setting string.fg to some color accomplishes this.
However, what if I want to highlight non-string keys? In this case, foo and bar should receive a certain styling, while the"two" in quotes should not receive this color. I can change the color of these keys by setting variable.fg.

But, now we reach the problem. variable.fg seems to be overriding string.fg, meaning it's setting all variables, even those that should be highlighted as a string. I'm guessing this is because the test for "longest matching theme key" just matches the character length of the key, and variable is longer than string. There doesn't exist a longer string scope that will touch Nix strings, so I'm stuck.

How to fix this

Of course, in my specific case, adding a longer key for strings would fix this. In the documentation on strings, it mentions a TODO list: TODO: string.quoted.{single, double}, string.raw/.unquoted)?. Adding string.quoted would be longer than variable, so it'd override it. However, this is only a fix for my specific scenario. I think a better fix would be the ability to specify precedence, so you didn't have to rely on the length of the key.

A relatively simple implementation of this to implement would be a boolean key alongside fg, bg,underline, and modifiers, where you could specify whether a key should be set above other keys, even those of the same length. If this key was ever set to true, it would be weighted to the top when choosing which style is applied, even over keys of a longer length. If multiple different keys had this value applied, it would then revert to choosing the longest one.

As an alternative, slightly more in-depth approach, I think using a numerical value might work better than a simple boolean. The number would specify how much precedence that key would receive, with a higher number corresponding to a higher precedence. This would work better with multiple keys specifying a high precedence.

I'm not that picky about the actual method for specifying precedence - I just think there should be some way to do it. Simply choosing precedence based on string length works in a hypothetical world where you can always choose a longer key, but if you've already gone as deep as you can, I don't see a clear workaround other than adding manual precedence.

@llakala llakala added the C-enhancement Category: Improvements label Jan 14, 2025
@llakala llakala changed the title Manual overriding for theme precendence Manual overriding for theme precedence Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Improvements
Projects
None yet
Development

No branches or pull requests

1 participant