user1550876
user1550876

Reputation: 109

Netbeans generated code settings

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.

Upvotes: 0

Views: 1803

Answers (1)

CODe
CODe

Reputation: 2301

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.

Upvotes: 3

Related Questions