Netbeans generated code settings

前提是你 提交于 2019-12-13 02:46:32

问题


I was wondering where can I change the way Netbeans generates code. For example, I want the opening bracket to be on a new line.

I've been looking in the [Tools] -> [Options] menu but I didn't find anything.


回答1:


Solution 1 should solve your problem. For any auto-generated code formatting that Solution 1 won't solve, use Solution 2. Beware the "Code Templates" tab though, it has been relatively buggy for me in the past.

Important Note: Attempting to modify the "Code Templates" for the placement of braces won't work correctly because it's overridden by the preferences in Solution 1. Any changes to formatting wanted should first be attempted with Solution 1, as the "Formatting" tab in Netbeans takes precedence over the "Code Templates" tab.

Solution 1:

Go to [Tools] -> [Options] -> [Editor] -> [Formatting]. Choose the language you are using, then, for the Category drop down box, select Braces. Directly under that you'll see Braces Placement. You can modify the placement of the braces for a class declaration, method declaration, or "other". Options are:

  • Same Line - places bracket on same line as class, method, or "other".
  • New Line - places bracket on a new line under the class, method, or "other".
  • New Line Half Indented - same as "New Line" with a half indention added.
  • New Line Indented - same as "New Line" with a full indention added.

Solution 2:

Go to [Tools] -> [Options] -> [Editor] -> [Code Templates]. Choose the language you are using, then modify the templates according to what formatting you'd prefer.

For example, if Java is selected, I can scroll to the "pm" abbreviation and see how Netbeans generates private methods. Here is a incomplete but helpful abbreviation-to-meaning list for a few common Java abbreviations you might want to change.

  • m - Method
  • p - Private
  • f - Final
  • s - Static

Therefore, if you want to change the formatting for an auto-generated method that is listed as "private", you can change the formatting for the abbreviation "pm" in the "Expanded Text" area, which shows the auto-generation rules/formatting for a private method.

Note: Combinations of these letters can sometimes mean something different, for example "pr" is expanded to "private", but "prm" is listed as a protected method, not a private method.



来源:https://stackoverflow.com/questions/12038930/netbeans-generated-code-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!