Reputation: 208022
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
Upvotes: 556
Views: 294559
Reputation: 8814
JetBrains IDEA has this feature. You can use hotkey "surround with" for that (ctrl + alt + T). It's just an IDEA feature.
Regions there look like this:
//region Description
Some code
//endregion
Upvotes: 554
Reputation: 139
In Visual Studio Code, try this:
//region Variables
// Code you need
//endregion
Upvotes: 2
Reputation: 605
vscode
I use vscode for java and it works pretty much the same as visual studio except you use comments:
//#region name
//code
//#endregion
Upvotes: 3
Reputation: 854
here is an example:
//region regionName
//code
//endregion
100% works in Android studio
Upvotes: 12
Reputation: 6635
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
Download the plugin from here
Extract the contents of archive
Navigate Window >Preferences >Java >Editor >Folding >Select folding to use: Coffee Bytes Java >General tab >Tick checkboxes in front of User Defined Fold
Create new region as shown:
Restart the Eclipse.
Try out if folding works with comments prefixed with specified starting and ending identifiers
You can download archive and find steps at this Blog also.
Upvotes: 53
Reputation: 61009
AndroidStudio
region
Create region
First, find (and define short cut if need) for Surround With
menu
Then, select the code, press Ctrl+Alt+Semicolon
-> choose region..endregion...
Go to region
First, find Custom Folding
short cut
Second, from anywhere in your code, press
Ctrl+Alt+Period('>' on keyboard)
Upvotes: 30
Reputation: 15097
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
Upvotes: 19
Reputation: 1732
On Mac and Android Studio follow this sequence:
<editor-fold>
Also you can select other options:
Upvotes: 1
Reputation: 15357
the fastest way in
Android Studio
(orIntelliJ IDEA
)
highlight the code
you want to surround itctrl
+ alt
+ t
c
==> then enter the description Upvotes: 32
Reputation: 1943
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.
Upvotes: 2
Reputation: 171
The best way
//region DESCRIPTION_REGION
int x = 22;
// Comments
String s = "SomeString";
//endregion;
Tip: Put ";" at the end of the "endregion"
Upvotes: 17
Reputation: 272397
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
.
Upvotes: 232
Reputation: 2762
With Android Studio, try this:
//region VARIABLES
private String _sMyVar1;
private String _sMyVar2;
//endregion
Careful : no blank line after //region ...
And you will get:
Upvotes: 218
Reputation: 15
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;
}
}
Upvotes: -1
Reputation: 27659
For Eclipse IDE the Coffee-Bytes plugin can do it, download link is here.
EDIT:
Latest information about Coffee-Bytes is here.
Upvotes: 51
Reputation: 19
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
Upvotes: 1
Reputation: 2017
Contrary to what most are posting, this is NOT an IDE thing. It is a language thing. The #region is a C# statement.
Upvotes: 19
Reputation: 7899
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.
Upvotes: 12
Reputation: 21088
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
Upvotes: 101
Reputation: 17751
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.
Upvotes: 38
Reputation: 245479
#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
.
Upvotes: 10