clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
644 stars 113 forks source link

delete[] and new not colored #685

Closed ChenCuiPlasma closed 2 months ago

ChenCuiPlasma commented 2 months ago

Please describe the problem. I recently found that after starting the clangd in the vscode, the "new" and "delete" in words "new double []" and "delete []" will be changed to white rather than the purple highlight it originally had. I have attached two screenshots here information. Before start any add-on of the vs code:

image

After only starting the clangd:

image

System information Clangd version (from the log, or clangd --version): Homebrew 18.1.8 clangd extension version: v0.1.29 VS Code Information: Version: 1.93.1 (Universal) Commit: 38c31bc77e0dd6ae88a4e9cc93428cc27a56ba40 Date: 2024-09-11T17:20:05.685Z Electron: 30.4.0 ElectronBuildId: 10073054 Chromium: 124.0.6367.243 Node.js: 20.15.1 V8: 12.4.254.20-electron.0 OS: Darwin arm64 23.6.0

HighCommander4 commented 2 months ago

clangd assigns the new and delete tokens a semantic token of kind operator (https://clangd.llvm.org/features#kinds).

If you prefer it to be colored differently, two options are:

ChenCuiPlasma commented 2 months ago

Hi, thanks for the reply and guidance. I noticed that, if I write something like "new variable_name" or "delete variable_name", the clangd gives the "new" or "delete" a correct highlight. However, if I try to use the "new" or "delete" to dynamically allocate an array or release the allocated array, like doing "new double [somenumber]" or "delete [] array_name", the clangd will make the new and delete highlighted as white, which seems to have something wrong.

HighCommander4 commented 2 months ago

I noticed that, if I write something like "new variable_name" or "delete variable_name", the clangd gives the "new" or "delete" a correct highlight. However, if I try to use the "new" or "delete" to dynamically allocate an array or release the allocated array, like doing "new double [somenumber]" or "delete [] array_name", the clangd will make the new and delete highlighted as white, which seems to have something wrong.

I can't reproduce this, for me it looks the same (white) in all four cases.

Can you try with extensions other than clangd disabled, and your vscode user / workspace settings (temporarily) blanked?

ChenCuiPlasma commented 2 months ago

Thank you for your help and guidance.

I have double checked and found you are correct. If I only use clangd, then no matter what I do with new or delete, it all shows white.

Just one follow up question: As you mentioned in the previous threads, the "new" and "delete" are included in the "operator" category in the clangd. Can I inquire if all of the "operator" category are highlighted into white by the clangd?

Thank you.

HighCommander4 commented 2 months ago

Can I inquire if all of the "operator" category are highlighted into white by the clangd?

Clangd doesn't specify any colors, it just specifies a token type from the list in the Language Server Protocol. The decision to assign a color like white for a token type like operator is made by the client (vscode), based on the theme and any customizations in user settings.

ChenCuiPlasma commented 2 months ago

Thank you for the help and I really appreciate it. So based on my understanding, if no extensions is activated, the VS code, by using its default dark scheme, will assign all of the operators with "white" color. And VS code does not put "new" and "delete" into the category of operator, so it highlights these two into "purple" by TextMate.

clangd, on the other hand, puts the "new" and "delete" into the category of the operator, and then sends back its category to the VS code, and the vs code makes the color to the tokens. Now, vs code receives the instructions from the clangd, and it also put the "new" and "delete" into the category "operator", so it no longer assign the purple color to the new and delete.

Can I further inquire what is identified by the clangd as an operator? I just want to make sure before I use the second way you mentioned in the previous threads to change the highlight of the "new" and "delete".

Thanks so much for your time and efforts!

HighCommander4 commented 2 months ago

So based on my understanding, if no extensions is activated, the VS code, by using its default dark scheme, will assign all of the operators with "white" color. And VS code does not put "new" and "delete" into the category of operator, so it highlights these two into "purple" by TextMate.

To be a bit more precise, I believe what happens is that the "white" color is associated with the "keyword.operator" TextMate scope here, but the new and delete keywords have more specific scopes, "keyword.operator.new" and "keyword.operator.delete", which are associated with the "purple" color here.

Whereas, with the semantic tokens sent by the server, it's all one token kind, operator, which gets mapped to the TextMate scope "keyword.operator" (and therefore the "white" color) here.

Can I further inquire what is identified by the clangd as an operator?

It includes the punctuation tokens of unary operators (e.g. !), binary operators (+), and the ternary operator (? and :), as new as new and delete.


For what it's worth, when the operator semantic highlighting was added to clangd in https://reviews.llvm.org/D136594, I did bring up the concern that this is mostly useful for highlighting overloaded operators, and that the highlighting of built-in operators may be better left to the client; but ultimately, the patch did add the highlighting for built-in operators as well.

ChenCuiPlasma commented 2 months ago

Thanks so much for your help! I think this fully resolve my confusions and I really appreciate it!