Is there an elegant way of hiding/collapsing/folding only a portion of a function? If functions could span multiple cells, this would have been straight-forward!
I have been toying with this feature as a part of some set of tools I have been working on (the code formatter in particular). The recent question prompted me to post a small illustration here. This is more to show how things might work than a complete solution, though.
I prefer to work with the code formatter, but you don't have to, if you don't feel like it:
Here are some functions we will need:
In what follows, I will use
So, now, I take some random piece of code from one of my recent posts:
I deliberately did not format the function. Now, the result looks like
When you click on the opener, the result looks like
First, it should be clear that the resulting cell is not supposed to be evaluatable. One can probably make it evaluatable in at least two ways: either by using
Next, the implementation above is just the proof of concept, nothing more. I am working on a more systematic and powerful version of this as a part of some larger set of tools on which I work (as a side project). So, hopefully, some time soon I will be able to say more.
One deeper reason why I connect this with the code formatter is that the pattern-matching based on boxes is fragile, so the proper way of doing this is via matching to the symbolic intermediate representation created by the formatter. Such functionality I call formatting annotations, and I do plan to include a general support for such annotations in the next versions of the formatter.
Finally, it may well be that this way of doing things is fundamentally flawed for reasons known to more knowledgable people than myself, since I am by far not an expert in FrontEnd.
The Mathematica Front End does not have this feature. Wolfram Workbench does though.
However, it is possible to collapse cell groups in notebooks by double clicking their brackets. This is useful.
For navigating functions, the Ctrl+. key combination (extend selection) is extremely useful.
Also, for ease of use when working with sections etc., activate the "Show open/close icon for cell groups" checkbox under Edit->Preferences. This adds a toggle chevron on the left side.
The frontend encourages/enforces you to break long code passages into smaller parts, which is not always a bad idea, especially for testing and debugging (although debugging in Mathematica is something of a sore point when you are used to other systems). Still, a solution for in-cell partitioning would be nice-to-have.
There is also some option to format sections so that a click on their content toggles open/close instead of just the tiny triangle, but I can´t remember at the moment how that worked. Perhaps someone will chip in here.