Maps an array of TM scopes to the most specific scope that is added. Needed to have fast access to a rule when doing the actual coloring.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
- Build Status
Buildable 36667 Build 36666: arc lint + arc unit
Event Timeline
I think we could make the layering clearer:
- we now have a list of theme color rules, and scope names provided by clangd; and we we want to find the best match theme rule for a particular clangd scope;
- we could define a function like getBestThemeRule(clangd_scope_name: string) - when we render a token, we get the index of the lookup table, find the exact clangd scope name, and call the function to get the corresponding theme rule;
- we need to cache the result to avoid re-computing, we could make the function as a class method;
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts | ||
---|---|---|
65 | maybe just get<string>, and get rid of the following check. | |
113 | I'd name it ThemeRuleMatcher? | |
125 | hmm, here comes the question, we need algorithm to find the best match rule. not doing it in this patch is fine, please add a FIXME. could you also document what's the strategy using here? | |
140 | nit: no need to change this method, you could construct the ThemeRules from the returned results. |
Removed stray edits from loadTheme.
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts | ||
---|---|---|
125 | But isn't the best match for a scope just the rule that is the longest prefix of the scope (or a perfect match if one exists)? (as that should be the most specific rule) |
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts | ||
---|---|---|
51 | the variable name should be updated as well. | |
119 | The best rule for a scope is the rule ... sounds like implementation details, should be moved to the function body. | |
125 | that depends, given the fact that the theme scope maybe not exactly the same with the scope provided by clangd. The longest-prefix match may not work well on cases like a.b.<theme-specific-field>.c.d and a.b.c.d. But we don't know yet without further experiment, I'd add a FIXME for revisiting the strategy here. | |
128 | The algorithm doesn't seems correct to me, if scope.length > rule.scope.length, then we drop it. I think we should
| |
clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts | ||
47 | I'd use assert.deepEqual(tm.getBestThemeRule('c.b').scope, 'c.b'); to make the code more readable. |
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts | ||
---|---|---|
128 | If the scope's length is less than the rule's length the rule can not be a prefix. If we check common prefixes we run into this weird case (this is taken from the Light+ theme): variable.css variable.scss variable.other.less variable With that kind of matching we have now this means that variable.other.less will match variable.other and variable.other.less and the variables would be colored as less variables while they should be variable.other. Same goes for variable.other.field. And even if variable.other.less did not exist variable.other would still match variable.css and now be highlighted as css variables. | |
clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts | ||
47 | For the a case we are interested in the foreground color as well. Should I change the others and keep assert.deepEqual(tm.getBestThemeRule('a'), rules[1]); as is or be consistent? |
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts | ||
---|---|---|
115 | I'd name it themeRules. | |
128 | I thought that we are finding longest common prefix, but actually you are using a different one (I checked that "vscode-cpptools" is also using the same matching algorithm). That sounds fair enough, could you add this context to the comment? | |
133 | I think we could simplify the code like if (scope.startsWith(rule.scope) && rule.scope.length > bestRule.scope.length) { ... } | |
clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts | ||
40 | Even for the test, I'd use the real scopes, e.g. variable.css, variable.other.less. | |
47 | a case is interesting here - we have duplicates, but actually we won't have duplicates in the theme rules (as we check the scope is being seen when parsing theme file). I think for a, just use assert.deepEqual(tm.getBestThemeRule('a'), rules[1]); // we match the first element if there are duplicates; for others, use the scope. |
the variable name should be updated as well.