Quicktip: How to display your own Java code-templates in the code completion of NetBeans IDE

After blogged about How to display your own PHP code-templates in the code completion of NetBeans IDE a NetBeans user asked me how it works for Java code.

Yes, it is possible and it is even simpler. Since NetBeans 7.4 (issue #160771) you can define the context, where code-template has to be shown. Don’t be overwhelmed by the available contexts. For completions within a block/method choose BLOCK.

2016-01-09_13h39_35

Advertisements

Quicktip: How to display your own PHP code-templates in the code completion of NetBeans IDE

Here a small tip for all the PHP developers, which want to see their code-templates in the code-completion list of NetBeans IDE.

  1. First create your own code template in code-template options at “Tools|Options|Editor|Code Templates” and save the options.
  2. Then open the code-templates file from your NetBeans user dir. For example: c:\Users\johndoe\AppData\Roaming\NetBeans\8.0.2\config\Editors\text\x-php5\CodeTemplates\org-netbeans-modules-editor-settings-CustomCodeTemplates.xml This XML file contains all your custom template settings for the given mime-type.
  3. Search for your entry and add contexts="php-code" to the <codetemplate>-tag and save it.

This way the template will be displayed in the code-completion list too.

When it looks like this

<codetemplate abbreviation="ech" contexts="php-code">
    <code><![CDATA[echo "${cursor} it works"]]></code>
</codetemplate>

it will work like that.

2016-01-07_23h05_10

If you like to have a option for that in the UI the NetBeans IDE itself, then please file an issue at https://netbeans.org/bugzilla -> Not needed anymore. See the update below.

UPDATE:  In the next version of NetBeans IDE (>8.1) no fiddling in files is needed anymore. Thanks to junichi11, who provided a patch to solve this via issue #246664 . The context can be set in the new context tab within the code-templates options.

2016-01-09_13h25_12