GAMS-dev / studio

GAMS Studio
https://www.gams.com/latest/docs/T_STUDIO.html
GNU General Public License v3.0
36 stars 5 forks source link

Code folding for sections #212

Open christophe-gouel opened 2 months ago

christophe-gouel commented 2 months ago

It would be great to extend the code folding feature, which is limited to a very small set of code blocks for now, to work with whole sections.

This would allow quick navigation in big models by jumping to the proper section after folding everything.

One question is how to define code sections. One reasonable approach would be to follow RStudio approach, which would translate as defining a section by a line starting with a comment character and that contains at least four dashes:

* First level section ----

** Second level section ----

*** Third level section ----

This is compatible with the language and is easy to type and parse.

Here is an example of what it gives in emacs, where I have just implemented it: section-folding

The icing on the cake would be to add a different syntax highlighting for section headings to distinguish them from simple comments, as I did in the emacs example.

I don't need this feature in GAMS Studio since I program in emacs, but having this in GAMS Studio would create a standard for code section and prevent me from fighting with co-authors who would not see the point of writing comments like this and would break my flow.

LutzWestermann commented 2 months ago

Thank you for your suggestion! I agree that it can be very helpful to collapse defined sections. We actually introduced the $onFold / $offFold statements for this. They are just comments for GAMS but are understood by GAMS Studio. They can be nested and the first one can get an argument, that is still visible, when the section is collapsed. This could look like this with a modified version of the trnsport model:

image

More about the topic can be found here.

Can this work for you?

christophe-gouel commented 2 months ago

Thanks for your quick answer.

Cool, I didn't know the $onFold/$offFold$ feature.

However, I don't think it really compares to what I suggest. The point is that GAMS models can lead to very big files that can be difficult to navigate. Basically, if you don't remember exactly the name of the equation you are looking for, it takes endless scrolling. So, the folding structure should be properly nested. OK, you can nest $onfold statements, but they have not a really hierarchy (they are not sections).

You cannot replicate the quick navigation I showed in my gif with your approach. Plus it takes two lines instead of one and visually when unfolded it does not look like separated sections.

In RStudio, code sections lead to a nice outline (see https://posit.co/blog/rstudio-v1-4-preview-little-things/). The outline is both in the text, when everything is folded and in an outline pane for quick navigation (cool but a bit redundant). When looking at the text, it is pretty clear the section level you are in. In RStudio, you can fold some code (like the body of a function) as in GAMS Studio, but also blocks of code through the sectioning system. The fact that the sectioning system is based on comments makes it transparent to the user (much more than $onFold) and allows quick navigation.

LutzWestermann commented 2 months ago

OK, I see your point and we'll discuss how to proceed here.