14

I dislike Eclipse's code folding feature, because the folding state isn't saved when you close the file, like Visual Studio does with its #region directives.

Does anyone know of a plugin which saves the status of the code folding per file, so that when I open it, the code stays folded?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Axonn
  • 10,076
  • 6
  • 31
  • 43
  • not sure but i think thats not supported yet. – fmucar Jul 29 '11 at 12:30
  • Am I the only one who thinks this is *completely* silly? I mean, the feature's been there for more than 2 years! I cannot, for the life of me, fathom how they could imagine anybody would like to fold code every time he/she opens a file (well, at least those who like code folding. I can live without it, but sometimes its helpful, especially in big resource files). Their "implementation" of this feature is revolting me ::- D. It's like those applications which don't remember their window position when you closed them and reopen them. Oh well...... – Axonn Jul 29 '11 at 12:32
  • 2
    and honestly I have never needed that feature at all. Perhaps it's the same with most people, and so it's underdeveloped – Bozho Jul 29 '11 at 12:33
  • 1
    I would say if the file is really big, there is probably sth wrong :) – fmucar Jul 29 '11 at 13:26
  • but still think it is a good to have feature anyway – fmucar Jul 29 '11 at 13:26
  • 3
    Hey guess what!? Eclipse is an open source project!! Stop crying and contribute to it if you really want such feature! – greydet Jul 29 '11 at 13:28
  • And where would you like them to save it? Only MS languages define '#regions'. – bmargulies Jul 29 '11 at 13:57
  • @bmargulies: in the project file, you could save folded line numbers when the file is closed. – Axonn Jul 29 '11 at 14:18
  • [Coffee-Bytes for Eclipse 3.6 & higher][1]. [1]: http://stackoverflow.com/questions/8534542/what-code-folding-plugins-work-on-eclipse-3-6 – Yaqub Ahmad Jan 03 '12 at 07:47
  • I would like to see this feature implemented in Eclipse, and I don't think there's anything wrong with writing large classes. Its all about balance and organisation, and I find that code folding really helps to speed up organisation – WonderWorker Apr 26 '13 at 12:25
  • I vote to have options to either let Eclipse save the folding records internally, by a single file, or in a file for each individual file. I think it should be saved in xml by expected linenumber, with an optional ExpectedFoldedLineContent parameter, which would store a string representation of the line folded in case the file is edited outside of Eclipse (perish the thought). Then it would look for the closest line to the recorded linenumber with the nearest match to the ExpectedFoldedLineContent, and FO-O-O-OLD! Sound good? – WonderWorker Apr 26 '13 at 12:36
  • Why do you even want to fold code? Maybe there is a deeper problem. I suggest three solutions: 1. For your own classes: refactor, if you can not find your way arround. Hiding implementation details is one main goal of object oriented design. 2. For foreign classes: get used to one of the other navigation tools eclipse provides (e.g. Ctrl+O for quick outline) 3. If you really want to limit your sight: Give the [task-focused view of Mylyn](http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.mylyn.help.ui/userguide/Task-Focused-Interface.html) a try. – Jens Piegsa May 20 '13 at 13:09
  • @jeppi: Oh, interesting. "Some editors such as the Java editor support focusing. Clicking the Focus button in the toolbar will fold all declarations that are not part of the active task context." That seems perfect for maintaining code folding for relevant methods, except that I don't see any way of creating a simple, lightweight task that doesn't require you to go through the process of using the New Task wizard so you can simply maintain relevancy for a short period of time, such as when you're working on a small/personal project that doesn't have a strictly defined task list. – JAB May 30 '13 at 18:53
  • (Though it does seem odd that Eclipse doesn't support persistent folding between editing sessions that do not involve closing files, only the environment itself; Notepad++ allows preservation of line folding between program executions, so given that both programs appear to save editing sessions between executions, it should not be any more difficult for Eclipse to preserve the folding. If it can preserve current file position, it can preserve folding state. I know N++ uses XML to represent session state; how does Eclipse do so?) – JAB May 30 '13 at 19:01
  • [for additional reading](http://www.codinghorror.com/blog/2008/07/the-problem-with-code-folding.html) – Jens Piegsa May 31 '13 at 20:22

2 Answers2

10

Try to use: http://code.google.com/p/coffee-bytes/

You have to download the source code, import project into Eclipse and build it. After that Export code as a deployable plug-in for Eclipse. You will have a package for installation.

  1. Install it into Eclipse.
  2. Go To Eclipse Preferences...
  3. Type in search "Folding"
  4. Select Java->Editor->Folding
  5. Select in ComboBox "Coffee Bytes Java Folding"
  6. On General Tab set checkbox - "User Defined Regions"
  7. In User Defined Regions tab - enter #region as a start tag and #endregion as an end tag

In code folding will be available after setting special comments into it:

// #region
... some code ...
// #endregion
dan1st
  • 12,568
  • 8
  • 34
  • 67
Oleksandr Kucherenko
  • 1,921
  • 1
  • 17
  • 21
  • Interesting. Thank you. But isn't this provided as an already compiled Eclipse plugin somewhere? – Axonn Nov 18 '11 at 22:09
  • 1
    I followed the instructions in Bartosz Kosarzycki's blog (http://kosiara87.blogspot.com/2011/12/how-to-install-coffee-bytes-plugin-in.html), and it seems to be working fine in my Juno 4.2 Eclipse. BTW, does anyone know what the "Type Settings" section in the "User Defined Regions" tab in the Folding preference is all about? – vanmelle Jul 03 '13 at 01:04
  • 1
    I created an update site that works with Eclipse Neon: https://github.com/stefaneidelloth/EclipseFolding/raw/master/com.cb.platsupp.site – Stefan Feb 12 '17 at 13:38
  • @Stefan: can you please describe, how to use your plugin? I need this feature. Thanks. – sarwar026 Apr 29 '20 at 13:45
  • Help => Install new Software => Work with https://github.com/stefaneidelloth/EclipseFolding/raw/master/com.cb.platsupp.site After Installation of the plugin follow the instructions from the above answer about the Eclipse Preferences. Does that help? – Stefan Apr 29 '20 at 15:19
0

If you are using CDT plugin for eclipse (which means you use C/C++ as your programming language), you may find this is very helpful:

http://syncor.blogspot.com/2011/02/folding-c-code-regions-in-eclipse.html

don't know if Java has a similar feature

RainSia
  • 113
  • 1
  • 7
  • 1
    I didn't install your suggestion, but I do use Java. What happens is is that Eclipse allows code to be folded at every label level, but when a file is closed, it does not keep a record or which methods were folded and which were expanded. In the method that you are suggesting, does it remember which methods are folded and unfolded? – WonderWorker Apr 26 '13 at 12:23