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.
@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:
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
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
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:
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?