Code Clarity from Theme Improvement via Token/Scope awareness

I like the new Arduino IDE. I had never used VS Code before, but now that I have used it in order to create my Code Clarity via color theme for the Arduino IDE, I think it is a good platform.

In the Arduino 1.x IDE, I used the theme capability to make the code easier to read by adjusting the colors, and easier on the eyes by using the dark theme as a base.

The different parts of code stand out, and are more easily perceived because of colors. IDE 1.8 was color coded with:

  • Integer Numbers in Dark Blue
  • Built-In C primitives are Light Blue
  • Parentheses and Brackets are Dark Tan
  • Variables are White
  • Comments are Light Purple
  • Mathematical Functions, Colon, and Commas are FF0000 Red in order to make them very easily seen.
  • C Keywords are Dark Purple/Magenta.
  • The Cursor is FF00FF Pink, and the only item this color in order to make it easily found.
  • Floating point numbers were FF0000 Red, in order to make them obviously not integer.

Color Coding the different parts of the C language, definitely made coding less fatiguing. Clarity via colors is something I highly recommend for beginners, but it helps me too, a part time programmer who is pretty good with the basics.

When I came back to do some coding recently, I saw that the IDE 2 was released. I wondered if the old theme system would still work on it, but I found out that the underlying VS Code was quite different. After figuring out how to make my own theme is VS Code for use in the Arduino IDE, it turns out that VS Code recognizes, and allows, color differences with more distinctions than the Arduino 1.8 IDE, allowing for more clarity via color.

So I made a theme in VS Code for the purpose of bringing it to the Arduino IDE 2. Starting with the theme "Neon Shimmer Alpha", I created a theme which was easier on the eyes, more beautiful, and provided greater programming clarity than I had in the 1.8 IDE


As you can see in the top picture, many of the different parts of C are now identified via color.

  • Integer Numbers are Yellow, including floating point numbers, but the Decimal Point is FF0000 red for clarity.
  • Built-In C primitives are Turquoise
  • Parentheses and Brackets alternate between 4 different colors as randomly determined by VS Code. This helps you be more sure of which set of Parentheses or Brackets belong with each other.
  • Variables are Green if they are Global, Blue if they are Local. This one color distinction I find very helpful, especially when looking at code that I haven't seen for a while, or came from somebody else.
  • Comments are Light Purple when bracketed with /* */, Orange when following //. This helps me easily see if there are sections of code I have block commented out for debugging purposes as I scroll, which probably need to be revisited.
  • Mathematical Functions and Statement Terminators are FF0000 Red in order to make them very easily seen.
  • Logical Operators || and && are Light Orange.
  • C Keywords are Dark Purple/Magenta.
  • The Cursor is FF00FF Pink, and the only item this color in order to make it easily found.
  • Meta Functions are Yellow Green
  • C Entity Names and Functions are Purple.
  • There is also color ff487c italicized for Variable Parameter, as was already included in the Neon Shimmer Alpha theme.

So I applied the theme to the Arduino IDE 2, and it was close, but there is much more Green Yellow because Parenthesis, Global Variables, and Local Variables, are all seen as Meta Function Definitions. The distinction between Global and Local variables, which I consider this to be one of the most helpful color distinctions, is gone.

Also, the Parentheses and Brackets do not alternate between 4 different colors so as to help see which 2 belong together.

It still looks pretty good, and I will happily use it, but I would really like it if the Global and Local Variables could be seen as distinct by the IDE so that they could have distinct colors. The code in my theme which is already there for these distinctions, and recognized by VS Code, is:

{
"name": "Variables",
"scope": "variable.other.local",
"settings": {
"foreground": "#008cff"
}
},
{
"name": "Variables",
"scope": "variable.other.global",
"settings": {
"foreground": "#0fda0f"
}
},

It would also be very nice if the Parentheses and Brackets would randomly alternate between 4 different colors so as to help see which belong together.

Other than these 3 items, the theme works pretty much as it did in VS Code, which makes it workable to use VS Code to create themes for the Arduino IDE. Good job.

My themes for the 1.8 and 2 IDEs are not published anywhere. If anyone would like to get them, let me know. Maybe I could put them up on github.

Hi @erikintp. Nice theme!

Do you have a C++ language extension installed in VS Code (like this)? That could be the reason why the theme system has more code awareness in VS Code than it does in Arduino IDE 2.x (which does not use a C++ language extension).

Maybe you would be interested in the "indent-rainbow" VS Code extension. A community member shared that they had used it with Arduino IDE 2.x:

It looks like you have properly identified how VS Code sees so many distinctions, especially the Global and Local variable status. I tried to put the current version of the C/C++ extension in the plugins folder,

ms-vscode.cpptools-1.13.9@win32-x64.vsix

but I got this error:
versionError

I found an older version of the C/C++ extension which was the newest version made for VS Code version 1.53. It was the tool's version 15.1.

cpptools-win32.vsix

This did make the IDE 2 more aware, but did not enable the Global verses Local distinctions.

Can the Arduino IDE be made to be seen by the VS Code Extensions as version 1.67, which the extension is looking for?

Maybe the version 15.1 could be made to give that distinction, say by making a version 15.2?

Could the Arduino IDE 2 be made to give the distinction between Global and Local variables without adding an extension?

Thank you for your time.

Great working on finding a way to install the C++ language extension. I have thought about giving it a try, but never found the time.

If you add a tab to the sketch with a .cpp file extension, does it identify the difference in scope of the variables in that file? I'm wondering if it is related to the sketch files having the .ino file extension.

There is a --vscode-api-version command line flag, which is described in the command line help as:

C:\Program Files\Arduino IDE>& ".\Arduino IDE" --help

[...]

  --vscode-api-version               Overrides the version returned by VSCode
                                     API 'vscode.version'. Example:
                                     --vscode-api-version=<Wanted Version>.
                                     Default [1.53.2]                   [string]

[...]

So in theory you could start Arduino IDE with that flag to cause it to indicate to the extension that it uses VS Code API version 1.67.0:

"Arduino IDE.exe" --vscode-api-version=1.67.0

But that isn't guaranteed to work. The VS Code API is versioned for a reason, so if the new version of the extension relies on some changes made since the actual API version used by Arduino IDE, then you won't get away with lying about the API version used by the IDE. But it's worth a try at least.

This VS Code API version specification comes from the Eclipse Theia IDE framework Arduino IDE is built on:

https://github.com/eclipse-theia/theia/blob/v1.31.1/dev-packages/application-package/src/api.ts#L21

Unfortunately I don't have an answer for that. Arduino IDE 2.x does have a language server that provides awareness of the code in order to allow some of the "intellisense" type features such as code completion and "go to definition". This is a custom language server due to the need to work with the Arduino sketch framework, but it is essentially only a translator between the sketch framework and the standard clangd C++ language server. There is a actually a different C++ language extension that uses clangd:

I think it would be more likely that one of the existing language extensions would be pre-installed in the Arduino IDE 2.x installation rather than the Arduino IDE developers reinventing the wheel. One of the huge benefits of the new IDE's use of the Theia framework is that it is possible to leverage existing valuable resources that are created and maintained by Microsoft and the VS Code community.

Thank you for the excellent suggestions/tips. With some effort I have been able to narrow down the situation. I think I know what needs to be done to accomplish getting Global and Local variable distinction and color differentiation in the Arduino IDE 2.

I uninstalled the Microsoft C/C++ Tools: IntelliSense, debugging, and code browsing from VS Code.
ms-vscode.cpptools-1.13.9@win32-x64.vsix

This gave me the same look (colors) with VS Code as I am getting in the Arduino IDE 2. Then I installed the older version of the Microsoft C/C++ Tools into VS Code, which will work with the Arduino VS Code version 1.53 identification. Microsoft C/C++ Tools version 15.1:
cpptools-win32.vsix

This gave me the color scheme I am looking for in VS Code. Then I tried the command line for version masking with the Arduino IDE 2 as you suggested. It worked, at least enough that it implemented the newer Microsoft C/C++ Tools, but I got the same colors which I got with the version 15.1 of Microsoft C/C++ Tools.

So the older Microsoft C/C++ Tools which works with VS Code version 1.53, performs the same as the newer Microsoft C/C++ Tools which works with VS Code version 1.67, in both VS Code and the Arduino IDE 2.

It appears that with just a bit of code refinement in the Arduino IDE 2, which will allow the Microsoft C/C++ Tools to determine the Token/Scope of variables as to whether they are Global or Local, the VS Code themes should pretty much work the same for both programming environments.

To narrow it down more, I can tell you that the built in Ctrl-Shift-P tool in VS Code called "Developer: Inspector Editor Tokens and Scopes" and the comparable tool in the Arduino IDE 2 "Developer: Inspector Tokens", will tell you what the IDE sees, -> what the theme can key on.

Below is an image of the tool in VS Code (top) and the Arduino IDE 2 (bottom). In both IDEs, the cursor is in front of a Local Variable on line 754. In both IDEs, the variable is seen as a "meta.function.definition", but in the VS Code IDE the Microsoft C/C++ Tools seems to allow the "Developer: Inspector Editor Tokens and Scopes" tool to identify the variable as "semantic token type - variable" with "modifier - local".

The theme seems to act the same as this tool reports. If the Arduino IDE programmers can get the "Developer: Inspector Tokens" tool to report a Local Variable as a variable and local, with the Microsoft C/C++ Tool version 15.1 (cpptools-win32.vsix) installed, then the themes for VS Code should perform very much the same on the Arduino IDE 2, providing a very educational distinction for new programmers, and helpful for experienced programmers.

Thank you for the consideration.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.