This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Added a TextMate theme parser to the vscode extension.
ClosedPublic

Authored by jvikstrom on Aug 5 2019, 4:32 AM.

Details

Diff Detail

Repository
rL LLVM

Event Timeline

jvikstrom created this revision.Aug 5 2019, 4:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 5 2019, 4:32 AM
hokein added a comment.Aug 5 2019, 8:08 AM

Haven't looked at the patch in details.

Looks like the patch is doing different things, and the test just covers a small set of the code.

  1. find and parse the theme definition files json ;
  2. define related data structures to save the TM scopes and do the transformation;
  3. handle changes of the theme;

could we narrow it further? I think we could just implement 1) for this patch.

clang-tools-extra/clangd/clients/clangd-vscode/src/TextMate.ts
11 ↗(On Diff #213319)

The color word here is ambiguous -- we have different colors in DecorationRenderOptions, e.g. color, backgroundColor, borderColor, I believe you meant color, maybe we could just a more descriptive name, like highlightColor, or TextColor?

I think we may want to use a structure here (color is one of the field), so that the code is extensible to support more options in DecorationRenderOptions.

12 ↗(On Diff #213319)

what does the index mean here?

119 ↗(On Diff #213319)

I think we may define a type/interface (like TokenColorRule) for the entries (tokenColors) in the theme file, and have a function that parsing the content and returning an array of TokenColorRule?

148 ↗(On Diff #213319)

note that json is one of the theme format files, vscode also supports .tmTheme. We probably don't support tmTheme for now (just add a fixme).

nridge added a subscriber: nridge.Aug 5 2019, 10:38 AM
jvikstrom updated this revision to Diff 213528.Aug 6 2019, 12:29 AM
jvikstrom marked 2 inline comments as done.

Narrowed CL down to loading/parsing a text mate theme.

Haven't looked at the patch in details.

Looks like the patch is doing different things, and the test just covers a small set of the code.

  1. find and parse the theme definition files json ;
  2. define related data structures to save the TM scopes and do the transformation;
  3. handle changes of the theme;

could we narrow it further? I think we could just implement 1) for this patch.

Removed everything that wasn't loading a TM theme. Also added an interface for a TM scope that we parse into (which I also added a test for).
The getFullNamedTheme function is not used (or exported) anywhere for now though.

hokein added inline comments.Aug 6 2019, 1:50 AM
clang-tools-extra/clangd/clients/clangd-vscode/src/TextMate.ts
6 ↗(On Diff #213528)

I think we should not use the namespace in typescript. The namespace in TS refers to “Internal modules”.

Each TS file is a separate module, so semantically namespace your code, use separate files. For semantic highlighting feature, I think we put all into one file semantic-highlighting.ts.

7 ↗(On Diff #213528)

TokenColorRule seems a bit clearer to me.

24 ↗(On Diff #213528)

we are introducing 2~3 helper functions here, I think we could simplify it like below.

function loadTheme(themeName) : Promise<ScopeColorRule[]> {
   // iterate all extensions and find the file path of the theme
   // call parseThemeFile().
}

function parseThemeFile(themeFilePath) : Promise<ScopeColorRule[]> {
  // read the file and parse the file content (recusively)
}

And for the test, we could save a small theme file in the test directory, and pass the file path to the parseThemeFile function.

75 ↗(On Diff #213528)

nit: we should filter the .tmTheme out in the code (just checking the extension of the fullpath).

93 ↗(On Diff #213528)

nit: use the full name for the parameters, resolve, reject.

96 ↗(On Diff #213528)

if we have err, we will run both rej and res.

jvikstrom updated this revision to Diff 213567.Aug 6 2019, 3:35 AM
jvikstrom marked 6 inline comments as done.

Renamed file to semantic-highlighting.ts. Added test for parsing theme files. Inlined the parsing into the loading function.

jvikstrom updated this revision to Diff 213568.Aug 6 2019, 3:40 AM

Clarified comments.

hokein added a comment.Aug 6 2019, 6:45 AM

thanks, looks simpler now, just a few more nits.

clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
7 ↗(On Diff #213568)

nit: we need documentation for the fields.

11 ↗(On Diff #213568)

do we need async here? since we don't use any await in the function body.

25 ↗(On Diff #213568)

nit: name it themeData or themeInfo?

31 ↗(On Diff #213568)

nit: textmate grammar is another different thing, the file is the textmate theme.

35 ↗(On Diff #213568)

nit: maybe SeenScopes?

42 ↗(On Diff #213568)

nit: th => tm

45 ↗(On Diff #213568)

I think it would be more suitable to move this comment in the catch block below, we don't throw this error.

94 ↗(On Diff #213568)

nit: return resolve(data).

clang-tools-extra/clangd/clients/clangd-vscode/test/assets/firstIncludedTheme.jsonc
3 ↗(On Diff #213568)

nit: any reason use jsonc not json?

clang-tools-extra/clangd/clients/clangd-vscode/test/assets/secondIncludedTheme.jsonc
2 ↗(On Diff #213568)

since we don't support tmTheme now, I'd prefer just dropping it from the test.

clang-tools-extra/clangd/clients/clangd-vscode/test/assets/testTheme.jsonc
3 ↗(On Diff #213568)

I think using one-level test is sufficient, how about the tests below?

  • simpleTheme.json (a simple non-include theme)
  • includeTheme.json (one-level include them)
clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
1 ↗(On Diff #213568)

this comment doesn't seem to provide much information, I'd remove it.

jvikstrom updated this revision to Diff 213801.Aug 6 2019, 11:51 PM
jvikstrom marked 12 inline comments as done.

Address comments.

clang-tools-extra/clangd/clients/clangd-vscode/test/assets/firstIncludedTheme.jsonc
3 ↗(On Diff #213568)

By default vscode will bind .json files to normal json files which don't allow comments. So if you'd try to run the tests without having set .json to bind to json with comments than it will be a "compile error" because of vscode not allowing comments in .json.

hokein accepted this revision.Aug 7 2019, 1:17 AM

mostly good, please update the patch description.

clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
6 ↗(On Diff #213801)

the interface also needs a documentation.

7 ↗(On Diff #213801)

maybe: // A TextMate scope that specifies the context of the token, e.g. "entity.name.function.cpp"

9 ↗(On Diff #213801)

I know the current name was my suggestion, but rethinking the name here, I think it'd be better to align with the name used by vscode (to avoid confusion), just use foreground.

13 ↗(On Diff #213801)

just // Get all token color rules provided by the theme

34 ↗(On Diff #213801)

nit: I think there is no need to explicitly mention the Recursively.

we are using TextMate and TM to refer the same thing in the source file, could we use a consistent way (just use TextMate)?

clang-tools-extra/clangd/clients/clangd-vscode/test/assets/firstIncludedTheme.jsonc
3 ↗(On Diff #213568)

thanks for the explanations, fair enough.

This revision is now accepted and ready to land.Aug 7 2019, 1:18 AM
jvikstrom retitled this revision from [clangd] Added a TextMate theme parser that updates when the current theme changes. to [clangd] Added a TextMate theme parser to the vscode extension..Aug 7 2019, 1:45 AM
jvikstrom edited the summary of this revision. (Show Details)
This revision was automatically updated to reflect the committed changes.
jvikstrom marked 5 inline comments as done.
Herald added a project: Restricted Project. · View Herald TranscriptAug 7 2019, 1:49 AM