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

Support targeting multiple scopes in a theme with tree sitter #241703

Merged
merged 6 commits into from
Mar 5, 2025

Conversation

alexr00
Copy link
Member

@alexr00 alexr00 commented Feb 24, 2025

No description provided.

@alexr00 alexr00 self-assigned this Feb 24, 2025
@alexr00 alexr00 added this to the March 2025 milestone Feb 25, 2025
for (let i = lastIndex; i < scopes.length; i++) {
if (scopesAreMatching(scopes[i], identifier)) {
score = (identifierIndex + 1) * 0x10000 + identifier.length;
score = (i + 1) * 0x10000 + identifier.length;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The identifiers are the scopes in a them rule. In this example, they are the scope property here:

{
			"name": "Function declarations",
			"scope": [
				"entity.name.function",
				"support.function",
				"support.constant.handlebars",
				"source.powershell variable.other.member",
				"entity.name.operator.custom-literal"
			],
			"settings": {
				"foreground": "#DCDCAA"
			}
		},

The scopes are the scopes that the token we're trying to find the theme for has. In this code snippet:

new URL('./../../renderer/dist/index.html', import.meta.url);

URL has the following scopes from tree sitter:

new.expr
variable
entity.name.function

By using the identifierIndex the score was using the position of the scope in the theme rule as to calculate the score, and I'm unclear on the intent behind this. Why would it's position in the theme rule influence the score?

This change instead uses the position of the scope in the token's scope list as part of the score, with scopes that are more specific getting greater weight.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks right. The higher up we find the identifier list in the scope, the more relevant rule is.
I think there's another issue: We should start looking from top to bottom
If the stack repeats, we would find the first occurrence, not the last. But the last one would have a higher score, which might beat other rules.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@alexr00 alexr00 Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought https://github.com/microsoft/vscode/pull/229951/files could have regressed this, but I couldn't find a change in there that would have caused this.

Copy link
Member Author

@alexr00 alexr00 Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are already going from top to bottom, since the for loop starts at the last index of the the scopes.
Edit, no we're not.

@alexr00 alexr00 requested a review from aeschli February 25, 2025 14:07
@alexr00 alexr00 marked this pull request as ready for review February 25, 2025 14:07
@alexr00 alexr00 enabled auto-merge (squash) March 4, 2025 15:50
@alexr00 alexr00 merged commit 5ff5853 into main Mar 5, 2025
8 checks passed
@alexr00 alexr00 deleted the alexr00/nasty-lobster branch March 5, 2025 11:07
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

Successfully merging this pull request may close these issues.

3 participants