Andy Konsor
Andy Konsor

Reputation: 1

Change Netbeans Autocomplete

Is there a way to change the suggestions that NetBeans autocomplete suggests. For example when I type pu public static main is the first suggestion and I want to change it to suggest public.

Upvotes: 0

Views: 699

Answers (1)

skomisa
skomisa

Reputation: 17343

First, to clarify what is happening, NetBeans provides code completion for the following by default:

  • Typing pu[tab] will generate public
  • Typing psvm[tab] will generate public static void main(String[] args) { ${cursor} }

However, when you type pu Auto Popup finds two potential matches which start with pu:

  • public
  • public static void main(String[] args) { ${cursor} }

For reasons I don't understand NetBeans employs a sort order which determines that public static void... precedes public, so you are being offered the unwanted option as the default in the Auto Popup.

There are (at least) three ways to address this, and you may not like any of them, but here goes anyway:

  • You could simply turn off Auto Popup, then pu[tab] would automatically generate public. To do this, select Tools -> Options -> Code Completion tab -> Uncheck Auto Popup on Typing Any Java Identifier Part, then click OK. This fixes the problem, but you lose autocompletion.

  • You could remove the mapping of psvm to public static void.... To do that, select Tools -> Options -> Code Templates tab, select the line containing the Abbreviation of psvm and click Remove, then click OK. With this approach you can turn code completion back on, but you will no longer be able to generate the main() method by typing psvm[tab].

  • You could tweak the text generated by typing typing psvm[tab] so that it does not start with public. For example you could insert a leading space (" public static void...."), or change the declaration to "static public void...", which is perfectly valid for the main() method (though a bit unconventional). With this approach you can leave Auto Popup on, but you will not get public static void... as an option.

To change the expanded text generated by psvm, select Tools -> Options -> select the Code Templates tab, select the line containing the Abbreviation of psvm and then edit the text in the Expanded Text window. See the screen shot below where the Expanded Text has been subtly changed from "public static void main(..." to " public static void main(..." with a leading space.

codeCompletion

None of these are perfect solutions, but you may find that one of them is preferable to what you have now.


Update:

A fourth option, and perhaps the best one of all, is to simply change the abbreviation of public from pu to pb. To do that, select the existing pu abbreviation, copy its Expanded Text to the clipboard, click Remove, then click New to create the abbreviation pb and paste the clipboard into the Expanded Text field.

Upvotes: 3

Related Questions