0

Given a simple mistake like

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("\");     // note: " is escaped, so this won't compile
}

This is rendered in Arduino IDE 2.0.3 as

Syntax highlighting in Arduino IDE 2.0

and the problem is hard to spot.

The Jetbrains IDEs have special highlighting for escaped characters like

Special highlighting of escape characters

which makes the problem more obvious because the two quotation marks have different color.

Any chance that this can be configured in the new Arduino IDE 2.0.3 as well?

Because it was asked in the comments: no, the syntax highlighting does not highlight the escaped character when the string is complete. The backslash and quotation mark are both still green.

Complete string

Thomas Weller
  • 851
  • 6
  • 17

1 Answers1

1

Any chance that this can be configured in the new Arduino IDE 2.0.3 as well?

Yes. You'll need to enable the Arduino language features provided by the language server. They're disabled by default.

  • Press F1,
  • Type Preferences: Open Settings (JSON),
  • Press Enter,
  • The settings.json file opens.

Add the following to the JSON and save it if you do not have auto-save enabled:

{
  "arduino.language.realTimeDiagnostics": true
}

enter image description here

My settings.json with the Arduino language features enabled.

enter image description here

Select a board. You can ignore the port selection if you like. The language feature should start, and you will see the error in the editor.

enter image description here

More details on the Advanced Settings are here.

dankeboy36
  • 80
  • 4
  • Thanks. Undoubtedly, this is very helpful. The color of the escaped character is still the same, though. – Thomas Weller Dec 30 '22 at 11:43
  • In the JetBrains IDE, you wrote `Serial.println("\");` and in the Arduino IDE `Serial.println("\"); // note: " is escaped, so this won't compile`. Can you please update your question about the syntax coloring in JetBrains with the longer example? Thanks! I compared the built-in C++ extension in VS Code with Arduino IDE, and they behave the same. The compiler error seems valid: _Expected expression_ `clang(expected_expression)`. I do not think you will be able to tweak the [syntax highlighting](https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide) in Arduino IDE. – dankeboy36 Dec 30 '22 at 12:24
  • My VSCode has syntax highlighting in strings: https://i.stack.imgur.com/Tq9iS.png Haven't used it for a long time, but IMHO I only use it for Typescript, not C++, so I don't think I have any C++ specific extensions installed and it even asks me whether I want to install some.. – Thomas Weller Dec 30 '22 at 12:48
  • I am sorry I could not help you with this. This conversation only leads if we compare the same snippets in different IDEs and editors. I did not say VS Code, Arduino IDE, or other tools cannot apply different syntax coloring on a regex token, but your initial use case is different; it's a pure compiler error. Even the built-in StackOverflow snippet highlighting shows the same result as the Arduino IDE; please look at your original question and check the snippet. – dankeboy36 Dec 30 '22 at 13:07
  • 1
    I did another check and can confirm a bug in the Arduino IDE that does not highlight the regex matches. If there are no compiler errors, VS Code correctly highlights `\d` in the `"\d+"` string. Arduino IDE 2.0.3 fails to do it. – dankeboy36 Dec 30 '22 at 13:27
  • 1
    One more update. The regex syntax coloring works in the Arduino IDE with both the `Light (Theia)` and `Dark (Theia)` themes, not with the Arduino ones. Steps to reproduce. Your sketch: `void setup() {} void loop() { Serial.println("\d"); }`. `\d` is not highlighted. Open settings with `Ctrl`/`⌘`+`,`. Select one of the Theia themes from the dropdown, and close the dialog with `OK`. `\d` is highlighted. If I select any of the Arduino themes, it does not work. This is a theme bug. – dankeboy36 Dec 30 '22 at 17:33
  • If you want to give that an an additional answer, I'm happy to upvote and accept it. – Thomas Weller Dec 30 '22 at 18:08