545

I want to use regions for code folding in Eclipse; how can that be done in Java?

An example usage in C#:

#region name
//code
#endregion
Cœur
  • 37,241
  • 25
  • 195
  • 267
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • 2
    http://programmers.stackexchange.com/questions/53086/are-regions-an-antipattern-or-code-smell/ seems similar in spirit. – Adam Parkin Oct 30 '14 at 18:46
  • I joined the party late. I agree with IKashef that there are other options that the current selected answer. The answer of Yaqub Ahmad saved my life. I didn't need to download the Intellig IDE. I downloaded the mentioned CoffeeBytes plugin from the comment of cking24343, I applied the configuration of 'null n void' and worked well in Luna. You need to restart after applying the configuration – Maximus Decimus Aug 28 '16 at 04:09
  • 3
    start with `//region [Description]` and end with `//endregion` – Amir Ziarati Jun 08 '17 at 03:21
  • later and not specifically Java, but related for Eclipse folding: https://stackoverflow.com/q/13505413/7224691 – Ben Jan 22 '22 at 10:31

23 Answers23

542

Jet Brains IDEA has this feature. You can use hotkey surround with for that (ctrl + alt + T). It's just IDEA feature.

Regions there look like this:

//region Description

Some code

//endregion
Alexander Bezrodniy
  • 8,474
  • 7
  • 22
  • 24
228

There's no such standard equivalent. Some IDEs - Intellij, for instance, or Eclipse - can fold depending on the code types involved (constructors, imports etc.), but there's nothing quite like #region.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 32
    Actually this can be achieved. If you scroll down on this post "Yaqub Ahmad" explains how to get this functionality. Also here is a link (http://kosiara87.blogspot.com/2011/12/how-to-install-coffee-bytes-plugin-in.html) that shows you how to add it to your existing eclipse environment...extremely useful! – cking24343 Jun 17 '13 at 12:54
  • 14
    Sure. But there's nothing *standard*. It's performed in IDEs rather than having a language (or text) construct – Brian Agnew May 14 '14 at 16:08
  • @BrianAgnew, This answer is unfortunately so incorrect, please check tons of options below and update your answer! – Mazen Elkashef Mar 01 '16 at 14:02
  • 2
    @lKashef - so what's the *standard* equivalent then ? Everything noted below relates to a particular IDE, doesn't it ? – Brian Agnew Mar 01 '16 at 16:54
  • 6
    @BrianAgnew, The OP didn't mention anything about standards instead he just want to be able to have regions-like blocks in his code. For instance you said "but there's nothing quite like #region." in IDEA for example //region .. //endregion works exactly like #region. There's no standard way of doing this at all in the first place since it depends on the IDE itself or a plugin. Developers with microsoft backgrounds will always see MS Visual Studio as "The" IDE but in fact it's just one of the options and another IDE might or might not implement such functionality. – Mazen Elkashef Mar 01 '16 at 17:00
  • 1
    I'll let @Pentium10 decide if it's answered his question then – Brian Agnew Mar 01 '16 at 17:02
  • 5
    @BrianAgnew, I almost gave up until I saw other people's answers which helped more than yours. Seriously no hard feeling here, We are all here to learn and share our knowledge and am stating the fact that I was mislead by your answer, maybe it was a good one back in 2010 but not today. I hope you get my point. – Mazen Elkashef Mar 01 '16 at 17:02
  • There is //region my region {code} //endregion available in Android Studio and by using this plugin in eclipse : http://sites.google.com/site/bkosarzyckistorage1/eclipse-folding-plugin.tar.gz – NET3 Feb 05 '18 at 15:39
  • For those like me, using Android Studio "Cyril Jacquart"'s answer is exactly what you're looking for! – Kalin Krastev Dec 07 '18 at 12:33
  • Well... you say it like it's part of C# language. I guess it's not. Visual Studio just adds this feature, the same can do other IDEs for Java (Intellij Idea) – user25 Dec 16 '18 at 20:19
  • #booJava Another place where the well-thought-out-ness of c# noticed. I can live with this one. Type Erasure on the other hand. – granadaCoder Sep 30 '19 at 13:23
  • //region Worked in Intellij – Vijai Sep 16 '20 at 18:50
216

With Android Studio, try this:

//region VARIABLES
private String _sMyVar1;
private String _sMyVar2;
//endregion

Careful : no blank line after //region ...

And you will get:

bummi
  • 27,123
  • 14
  • 62
  • 101
Cyril Jacquart
  • 2,632
  • 3
  • 25
  • 24
  • was going through all answers and figured since Android Studio is based on IntelliJ then this must be supported. good thing to point it out though! – serine Jan 20 '14 at 11:22
  • 5
    bummi, I did try it in 0.4.2 with a blank line and still works – serine Jan 20 '14 at 20:21
  • 2
    Just a comment/advice (and by no means an answer to your question). It is a widespread opinion that regions should be avoided, since they let you hide class complexity instead of working on your coding style. Instead of huge classes, where each class is responsible for a lot of functionality, work on breaking down your code into smaller classes instead. It will make your code easier to manage over time. Read more here: http://blog.codinghorror.com/the-problem-with-code-folding/ – Daniel Saidi Feb 05 '15 at 17:51
  • Question is asking for Eclipse solutions. – Duncan Jones Apr 02 '15 at 11:31
  • Sigh, it works but not *inside* switch statements in Android Stduio 1.2.2. – Attacktive Jul 08 '15 at 02:09
  • 6
    @DanielSaidi oh, the horror! :) the guy in the article "_I can't see anything! I have to manually expand those sections to browse any of the code in this class._" ... so because he's too lazy to press a key (that woulld expand all sections in the file), the feature must not be used - it's a super wonderful feature. Right now I'm using it to one by one hide the methods I've verified are thread safe in the class I'm working one, it really helps to keep organized.Sure once in a while I've wondered where something is that I thought was there, but that is a small price to pay... – ycomp Oct 18 '15 at 16:23
  • Well, if you have to expand "all sections in the file", I would still argue that you have too large of a file/class :) But Duncan is right, the question was HOW to do it, not asking for if it should be done. – Daniel Saidi Oct 19 '15 at 06:45
  • 4
    @DanielSaidi It's not about hiding complexity but hiding mundane code such as variable declarations, getters, initialization code and so on. Also note that you DO NOT require #regions to collapse the "complex code" found in functions and classes. The beef with programmers using a tool incorrectly does not amount to "the tool is bad". – user1567453 Feb 27 '16 at 09:30
100

No equivalent in the language... Based on IDEs...

For example in netbeans:

NetBeans/Creator supports this syntax:

// <editor-fold defaultstate="collapsed" desc="Your Fold Comment">
...
// </editor-fold>

http://forums.java.net/jive/thread.jspa?threadID=1311

Kevin LaBranche
  • 20,908
  • 5
  • 52
  • 76
  • 1
    Even though it's not a language specific folding but still helped me in NetBeans. Exactly what I wanted to achieve. Just want my code to be more handy and organized for me. – Indigo Aug 28 '13 at 10:11
  • Is there any shortcut that can auto fill this line? – Pini Cheyni Mar 14 '16 at 13:44
  • 1
    @pini-cheyni - https://ui.netbeans.org/docs/ui/code_folding/cf_uispec.html has shortcuts available for folding.... – Kevin LaBranche Mar 14 '16 at 20:13
  • @JadChahine: The OP asked about Eclipse, but agree this works well for NetBeans. Syntax is a slightly clumsy but copy-paste makes it easy enough. – AlainD Nov 11 '16 at 15:17
53

Custom code folding feature can be added to eclipse using CoffeeScript code folding plugin.

This is tested to work with eclipse Luna and Juno. Here are the steps

  1. Download the plugin from here

  2. Extract the contents of archive

  3. Copy paste the contents of plugin and features folder to the same named folder inside eclipse installation directory
  4. Restart the eclipse
  5. Navigate Window >Preferences >Java >Editor >Folding >Select folding to use: Coffee Bytes Java >General tab >Tick checkboxes in front of User Defined Fold

    enter image description here

  6. Create new region as shown:

    enter image description here

  7. Restart the Eclipse.

  8. Try out if folding works with comments prefixed with specified starting and ending identifiers

    enter image description here

    enter image description here

You can download archive and find steps at this Blog also.

Michieru
  • 174
  • 2
  • 10
DeltaCap019
  • 6,532
  • 3
  • 48
  • 70
  • 2
    not working for me on eclipse neon: copy and paste did nothing: the only thing that changed was under marketplace >> installed plugins >>update. I updated the coffeeScript plugin, but I didn't get the option "Select folding to use" under java>> editor >> folding – Angelo Oparah Jan 01 '17 at 12:48
  • It worked in the Eclipse Oxygen, however you need to restart the eclipse again. The icon displayed to me was a red icon so I changed in the configuration, **Window → Preference → Java → Editor → Folding** click at **Advanced** tab and change the Icon theme set to **Modern** – Michieru Jan 04 '19 at 12:05
51

For Eclipse IDE the Coffee-Bytes plugin can do it, download link is here.

EDIT:

Latest information about Coffee-Bytes is here.

Community
  • 1
  • 1
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
  • 7
    +1 thanks for this info. Since the original website of the developper of the plugin is no longer available in 2012 - i found [a short desctription for installation and usage](http://kosiara87.blogspot.com/2011/12/how-to-install-coffee-bytes-plugin-in.html). – k3b Feb 10 '12 at 13:48
  • anyone found an intelliJ alternative? – philx_x Apr 04 '17 at 18:04
  • The download link does no longer exist. – derHugo Jul 03 '19 at 06:53
38

This is more of an IDE feature than a language feature. Netbeans allows you to define your own folding definitions using the following definition:

// <editor-fold defaultstate="collapsed" desc="user-description">
  ...any code...
// </editor-fold>

As noted in the article, this may be supported by other editors too, but there are no guarantees.

heavyd
  • 17,303
  • 5
  • 56
  • 74
  • In Android studio, at least, you cannot fold part of a block. If the fold spec starts outside of a block, and ends inside a block, it will not activate. – Prof Von Lemongargle Aug 31 '14 at 05:15
32

the fastest way in Android Studio (or IntelliJ IDEA)

  1. highlight the code you want to surround it
  2. press ctrl + alt + t
  3. press c ==> then enter the description
  4. enjoy
ycomp
  • 8,316
  • 19
  • 57
  • 95
Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
30

AndroidStudio region
Create region

First, find (and define short cut if need) for Surround With menu enter image description here

Then, select the code, press Ctrl+Alt+Semicolon -> choose region..endregion...
enter image description here

Go to region

First, find Custom Folding short cut
enter image description here Second, from anywhere in your code, press Ctrl+Alt+Period('>' on keyboard) enter image description here

Linh
  • 57,942
  • 23
  • 262
  • 279
19

Contrary to what most are posting, this is NOT an IDE thing. It is a language thing. The #region is a C# statement.

Johann
  • 1,977
  • 3
  • 19
  • 18
  • 55
    What most are posting is that IN JAVA this is an IDE feature. – WhyNotHugo Sep 16 '11 at 14:04
  • 1
    I do see the 2 most popular answers saying there is no such language feature. -1. – Mohayemin Aug 30 '12 at 03:36
  • 23
    In C# it is partly IDE, partly language. The language supports the preprocessor directives, but the preprocessor simply ignores them (the same way processor ignores comments). The IDE (Visual Studio) uses them to do code folding. – ADTC Sep 07 '12 at 08:20
  • Java doesn't has specified code folding only the IDE (Netbeans) has, Netbeans has more special comments like "TODO" and more. They are very useful to track your progress through SW project development process – Pini Cheyni Mar 17 '16 at 13:22
18

I were coming from C# to java and had the same problem and the best and exact alternative for region is something like below (working in Android Studio, dont know about intelliJ):

 //region [Description]
 int a;
 int b;
 int c;
//endregion

the shortcut is like below:

1- select the code

2- press ctrl + alt + t

3- press c and write your description

Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
  • In AndroidStudio version 2.3.1 it folds. I don't know when it started to fold. – MiguelSlv Apr 10 '17 at 15:38
  • Thanks. Works perfectly in Android Studio 3.0 beta 2. By curiosity I tried in Eclipse Oxygen, it doesn't work by default but didn't search further. – Laurent Sep 06 '17 at 08:36
16

The best way

//region DESCRIPTION_REGION
int x = 22;
// Comments
String s = "SomeString";
//endregion;

Tip: Put ";" at the end of the "endregion"

Taddeo
  • 161
  • 1
  • 2
12

If anyone is interested, in Eclipse you can collapse all your methods etc in one go, just right click when you'd normally insert a break point, click 'Folding' > 'Collapse all'. It know it's not an answer to the question, but just providing an alternative to quick code folding.

Ricky
  • 7,785
  • 2
  • 34
  • 46
  • 1
    You can also have Eclipse auto-collapse everything when opening a source code file. Preferences > Java > Editor > Folding > "Initially fold these elements" (check all in the list). I find it convenient to focus on only what I need especially in long source code files. – ADTC Sep 07 '12 at 08:46
  • @ADTC - Thanks - so many hundreds of times I've done it manually every time I open a file - never again!! – ToolmakerSteve Jun 29 '15 at 21:10
  • @ToolmakerSteve If you can do anything repetitive manually on a computer, there just *has* to be a way to automate it. ***=D*** – ADTC Jul 01 '15 at 04:48
12

here is an example:

//region regionName
//code
//endregion

100% works in Android studio

Coder123
  • 784
  • 2
  • 8
  • 29
  • 2
    I don't know why you have been downvoted, this actually works in Android Studio without any configuration needed! – maxdelia Mar 20 '20 at 10:13
9
#region

// code

#endregion

Really only gets you any benefit in the IDE. With Java, there's no set standard in IDE, so there's really no standard parallel to #region.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 3
    Well if there's a standard to `region`, then we can be sure that java IDE's *will* support it isn't it? – Pacerier Nov 19 '11 at 18:01
  • @Pacerier if they support that standard, yes. I think #region is more an IDE feature than a standard. In theory the java IDE's could implement this behavior and make it a de-facto standard – El Mac Sep 07 '14 at 08:43
3

vscode

I use vscode for java and it works pretty much the same as visual studio except you use comments:

//#region name

//code

//#endregion

enter image description here

Solarcloud
  • 555
  • 1
  • 7
  • 15
2

I usually need this for commented code so I use curly brackets at start and end of that.

{
// Code
// Code
// Code
// Code
}

It could be used for code snippets but can create problems in some code because it changes the scope of variable.

Yawar
  • 1,924
  • 3
  • 29
  • 39
2

Meet custom folding regions ⌥⌘T

demo

George
  • 3,384
  • 5
  • 40
  • 64
2

In Visual Studio Code, try this:

//region Variables
// Code you need
//endregion
1

On Mac and Android Studio follow this sequence:

  1. Highlight the source code to fold
  2. Press Alt+Command+t
  3. Select <editor-fold>

Also you can select other options:

enter image description here

Victor Ruiz.
  • 1,706
  • 22
  • 22
1

Actually johann, the # indicates that it's a preprocessor directive, which basically means it tells the IDE what to do.

In the case of using #region and #endregion in your code, it makes NO difference in the final code whether it's there or not. Can you really call it a language element if using it changes nothing?

Apart from that, java doesn't have preprocessor directives, which means the option of code folding is defined on a per-ide basis, in netbeans for example with a //< code-fold> statement

Cosmin
  • 21,216
  • 5
  • 45
  • 60
  • 1
    can you really call it a language element if using it changes nothing? - how about comments ?! – epeleg Sep 21 '11 at 07:04
  • 2
    I've hear the argument before that it isn't a function of the language. And this question "Can you really call it a language element if using it changes nothing?" is ridiculous. A lot of time refactoring is making the code easier to read for humans and does nothing (hopefully) to impact the function of the code. – Menefee Mar 28 '12 at 18:35
-1

In Eclipse you can collapse the brackets wrapping variable region block. The closest is to do something like this:

public class counter_class 
{ 

    { // Region

        int variable = 0;

    }
}
j0k
  • 22,600
  • 28
  • 79
  • 90
Dennis
  • 15
  • 1
  • 8
    This is useless. It doesn't work inside a method, plus it BREAKS CODE! (Private variables will complain they want to be final. Variable with no scope definition will not be accessible outside the inner brackets!) – ADTC Sep 07 '12 at 08:16
  • 1
    I don't like this method, adds too many curly braces. This can lead to confusing code :( – Broken_Window Dec 03 '14 at 23:07
-3

Just intall and enable Coffee-Bytes plugin (Eclipse)

acmoune
  • 2,981
  • 3
  • 24
  • 41
  • Not only is this mentioned in [an earlier answer](http://stackoverflow.com/a/8533767/2030691), but you didn't even provide any useful information about it. – Xynariz Sep 04 '15 at 19:28