check for mismatched brakets, can IDE do this?

Hi, I'm afraid I added one (or more) closing brackets on my sketch, the compiler doesn't report any error but I could suffer from a compilig bug.. I mean, I did have experience in the past that if I don't properly uses brackets the sketch may be compiled but after some time of running it may come unstability.

I've a strange behaviour wich can be hardware related, but I want to ask and understand deeper:

  1. Am I sure that if I add an extra closing bracket the compiler will always warn me about it?
  2. Why IDE doesn't have automatically a bracket control like for example Node Red does? Any other chance to implement a plugin for example that could control syntax?

Thank you

GiovanniG11:
Hi, I'm afraid I added one (or more) closing brackets on my sketch, the compiler doesn't report any error

If your code has mismatched brackets it will always fail to compile.

GiovanniG11:
the sketch may be compiled but after some time of running it may come unstability.

Nope. It just won't compile.

GiovanniG11:
2) Why IDE doesn't have automatically a bracket control like for example Node Red does?

It does have a couple of features that can help. If you do a Tools > Auto Format and then check the resulting indentation of your code, that will clearly show where there is an extra or missing brace.

If you put the cursor at one bracket, the Arduino IDE will put a box around the matching bracket. If you put the cursor on a closing bracket and the opening bracket is off the screen, the Arduino IDE will show it as a tooltip after a short delay.

GiovanniG11:
Any other chance to implement a plugin for example that could control syntax?

I'm not sure. The Arduino IDE does have a plugin system, but I don't know whether it is powerful enough to permit that sort of functionality. The existing plugins are Arduino's WiFi101/WiFiNINA Firmware Updater, the ESP Exception Decoder, and ESP8266 Sketch Data Uploader. None of those do anything like what you're looking for. I'd like to more 3rd party IDE plugins. I think a lot of people are not even aware that this is possible in the Arduino IDE and there is no official documentation of it, other than the source code for an example "Mangler" plugin included with the IDE.

any compiler will detect if a bracket is never closed or if inappropriately started or closed in an inappropriate way.
but no compiler can detect your thought process so you can still accidently move brackets to places you didnt want them to be

There is also the code-folding option which you can enable in file -> preferences. It will result in a '-' sign next to an opening {; clicking on it will collapse all code till the matching closing }.

In some cases the user has mixup of { ] ("open" does not match "close"),
the autoformat will not show anything strange.
The compiler will see the mismatch but will not show the exact line, it will "pinpoint" to first line of next "scope" and message "...not declared..."

Anyway, it won't compile

When I am writing code I always (well, most of the time) insert the closing bracket immediately after I insert the opening bracket. Then I fill in the code between the brackets.

...R

When I am writing code I always (well, most of the time) insert the closing bracket immediately after I insert the opening bracket. Then I fill in the code between the brackets.

Me too, but the IDE editor helpfully automatically adds the closing bracket so if I am not careful I end up with too many.

UKHeliBob:
Me too, but the IDE editor helpfully automatically adds the closing bracket so if I am not careful I end up with too many.

I find that behavior not so helpful. Luckily there is a way to disable it. Just add this line to your preferences.txt:

editor.auto_close_braces=false

You can find the location of preferences.txt by clicking the link at the line following File > Preferences > More preferences can be edited directly in the file.

I find that behavior not so helpful. Luckily there is a way to disable it

I have tried it both ways but I am ambivalent about it. When I turn the option on I sometimes wish that it was disabled. When I turn the option off I sometimes wish that it was enabled.

What I really want is an option like

editor.auto_close_braces=only_when_useful

:slight_smile:

I think that I will go back to turning it off again

UKHeliBob:
Me too, but the IDE editor helpfully automatically adds the closing bracket so if I am not careful I end up with too many.

I never use the IDE editor - only Geany

...R

thank you mates a lor for your kind answers!!
Maybe I confuse with Node Red, where you may add extra brakets and the script doesn not report them, and also compiler.. so as result there may be unexpected behaviour, sometimes difficult to figure out.

Anyway I got here that the compiler in Arduino is really attentive, and won't compile if there is any mismatch. I think it's enough to consider the problem under control. Thank you

I find it easier to make sure that brackets match by always putting each { and each } on their own lines and using autoformat often. Mismatched brackets usually show right up.

if (something)
{
   do this;
}
else
{
   do that;
}

find it easier to make sure that brackets match by always putting each { and each } on their own lines and using autoformat often.

Good advice. If I type a short program directly into the forum editor I often do Ctrl/T out of habit only to have a new tab open in the browser !

I have Auto format in the IDE configured to put { and } on their own lines which is helpful in tracking down some types of problems in code copied from the forum

I often do Ctrl/T out of habit only to have a new tab open in the browser !

I did not know that as I do not use the online editor. I will remember that so that when I advise using autoformat I can point out that it does not work the same in the online editor.

NOTE : in case there is any confusion I was referring to the forum message editor opening a new tab when Ctrl/T is entered. I do not know what happens if you do it in the online IDE editor but I assume that it allows Auto format in some way

I did, indeed, misunderstand. Got it now.

The keyboard shortcut for auto format in Arduino Web Editor is Ctrl + B.

That's a funny story UKHeliBob shared about hitting Ctrl + T on the forum. I compulsively Ctrl + T in the Arduino IDE, but surprisingly I have never done so on the forum.