开发者

How to create named code blocks in Intellij?

开发者 https://www.devze.com 2023-02-26 05:55 出处:网络
When Intellij generates code for UI forms you can collapse the cod开发者_如何学JAVAe block and it is named: \"UI Designer generated code\". It is possible to collapse a code selection but its collapse

When Intellij generates code for UI forms you can collapse the cod开发者_如何学JAVAe 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#.


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.


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

How to create named code blocks in Intellij?

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>


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

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


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

//region Description

 ... Your Code ...

//endregion
0

精彩评论

暂无评论...
验证码 换一张
取 消