Eugen
Eugen

Reputation: 2370

How to create named code blocks in Intellij?

When Intellij generates code for UI forms you can collapse the code block and it is named: "UI Designer generated code". It is possible to collapse a code selection but its collapsed representation is displayed as "...". Is it possible to give it a custom name? It would look similar to a collapsed #region code block in C#.

Upvotes: 42

Views: 24617

Answers (4)

Hasan Ammori
Hasan Ammori

Reputation: 403

There is a shortcut for that:

Open Surround with menu, by pressing

Linux/Windows: Ctrl +Alt+T

Mac: Option ⌥+Command ⌘+T

And than choose <editor-fold...> Comments option

enter image description here

Or you can do it manually.

For Java/Scala it is:

//<editor-fold desc="DESCRIPTION">
   ___YOUR_CODE___
//</editor-fold>

For Python it's almost the same

# <editor-fold desc="DESCRIPTION">
   ___YOUR_CODE___
# </editor-fold>

Example:

//<editor-fold desc="Main">
public static void main(String[] args) {
    System.out.println("Hello World");
}
//</editor-fold>

Upvotes: 15

Sahil Chhabra
Sahil Chhabra

Reputation: 11686

You just need to replace # by // like below :

//region Description

 ... Your Code ...

//endregion

Upvotes: 5

CrazyCoder
CrazyCoder

Reputation: 402055

IDEA doesn't have such feature yet, please watch/vote the linked issue.

UPDATE: this feature is available starting from IDEA 11.1 release.

Upvotes: 6

Cel
Cel

Reputation: 6659

They have implemented region-support now!

From http://youtrack.jetbrains.com/issue/IDEA-80636

Currently Intellij IDEA supports two basic types of custom folding comments:

NetBeans-like:

//<editor-fold desc="...">
... code ...
//</editor-fold>

And VisualStudio-like:

//region <...>
... code ...
//endregion

Note that line commenting symbols for "region...endregion" can be changed from "//" to "#" if the '#' character is supported by a language. But by default custom folding comments use the same comment characters as normally used for language line comments. Instead of typing the comments manually, in many cases you can use Ctrl+Alt+T (surround with). Please submit separate issues if you find that something doesn't work as expected.

Upvotes: 77

Related Questions