Auto format unfolding code

I just discovered the code folding option which is awesome for productivity.
But then so is auto format, but if I use it, all the folded code gets unfolded.
Is there any way to use these functions simultaneously?

There's a 'collapse all' function, but that collapses absolutely everything in braces which is even more annoying.

Which version of the Arduino IDE are you using @TijuanaKez?

Arduino 1.8.19
Thanks.

EDIT: I'll take that as a hint to try Arduino IDE 2.0 which I have somehow missed :wink:

what is "folding"?

what is "folding"?

You can collapse functions away (or any section of code between curly braces) temporarily to tidy up the workspace.

what does "collapse functions away ... temporarily" mean.

sounds like you're describing #if/#endif

@gcjr nope. It is a convenience UI feature of the Arduino IDE (actually any modern IDE will have this). It doesn't have any functional effect on the code.

You can enable it via the File > Preferences > Enable Code Folding in Arduino IDE 1.x:

image

Do you see the - icon next to the first line of the setup function? If you click on that, then everything in the block is hidden, as you can see I did for the loop function. Then if you click the + icon, it is expanded again.

This works for any block. Not only for functions.

You can also do some bulk folding via the Folding submenu of the context menu. It can be a very useful feature for working in larger sketches.

Arduino IDE 2.x does preserve the folding through an auto format. Unfortunately, the folding in Arduino IDE 2.x is actually not as smart as Arduino IDE 1.x:

But worth giving the new IDE a test drive anyway even if just to get a preview of things to come.

thanks for explaining the IDE editor feature. i use an external editor

why not? seems that the folding feature simply hides code within a matching pair of braces. making sure that those pairs are on lines by themselves which i believe the ctl-T autoformat feature does, only make the "folding" feature better

FYI

You should also investigate using Tabs in the IDE to help keep sketches organized.

why not? seems that the folding feature simply hides code within a matching pair of braces. making sure that those pairs are on lines by themselves which i believe the ctl-T autoformat feature does, only make the "folding" feature better

The problem is when you've just collapsed a heap of functions, then you hit ctrl-t later to auto format, it expands everything again and you have to go back and refold everything manually.

Yes I've been splitting off some larger sections into their own files, but there are caveats with that.
I'm trying to learn the coding practice of moving everything into functions (however small) to make intentions clear and readable without any need for comments. I've seen others do this and I really like it.

Problem is you end up with a lot of functions. It's great being able to hide their contents. Great until they all unfold with auto format :frowning:

Well, I gave it a try and happy to see code folding is always on and seems to work fine for me.

What a shame that auto format does absolutely nothing. I guess back to 1.8.19 till that's fixed.

seems that folding simply hides from view lines of text in the file being edited.

doesn't the auto-format simply unfold things before re-formating? is there reason you can't re-fold things after auto-formating?

if you posted code on this forum, would you want it folded?

Autoformat is a good way to see if and where you're missing braces etc so it's nice to use regularly.
I don't want to have to go through and refold 30 functions each and every time I press Ctr-t. Would you?

i had never heard of folding until today and don't believe it is very common. there are other tools for finding matching braces and identify missing braces

i had a 30 year career working on software, never using "folding", using various techniques to keep large amounts of code manageable, using multiple files and various tools to navigate that code.

Did you select a board from the Tools > Board menu? Auto Format in 2.x is handled by the Arduino Language Server, which can only work when a board is selected. The reason for that is the language server needs to have an understanding of the sketch program for its more advanced features (code completion, problem detection, go to definition), and it can only understand the sketch program once it knows which board it is being built for.

Once you have selected a board the first time, you will pretty much always have one board or another selected from then on, so this limitation is not something you run into regularly, but unfortunately there is no board selected on a fresh install so it is part of the first time experience.

There have also been some recent improvements to the language server, but those came after the 2.0.0-rc3 release, so you only benefit from them when using the nightly build:

https://github.com/arduino/arduino-ide#nightly-builds

You got it.

In Arduino IDE 1.x, yes, but that is just a quirk of that IDE.

Because you will often have selectively folded the code you don't need to look at at the moment. So it's not so simple as just doing a bulk "fold all".

Wut? I guess you didn't get it after all.

Super common. You've been missing out!

The tools weren't so good 30 years ago. But fortunately that is the past and this is 2022.

i didn't stop coding 30 years ago. i never saw folding working at Bell Labs where C/C++ and most of the professional tools used today were invented and later at Qualcomm. i never saw professional coders using IDEs. our IDE, our programming environment was unix/linux.

i'm suggesting your wouldn't (if that's not obvious). if i'm missing something, would you explain?

Aha. That was it! Ok well it takes a a while to process, but.... it works, and no unfolding!
Thanks for the tips!