Automatically Appending Comments to Auto-Inserted Curly Braces in Netbeans
I really appreciate how NetBeans automatically enters closing brackets to the code I'm working on. Would it happen to be possible for it to further streamline the development of code that looks like this?:
public class Test
{
public static void main(String[] Args)
{
//Some code here
开发者_如何学C while(true)
{
//Some more code here
}//end while
}//end main
}//end class
Unfortunately there doesn't seem to be a way to customize what is generated when NetBeans adds the closing bracket: there is just a checkbox for whether to do this or not in Tools->Options->Editor->Code Completion.
Here is the next best solution:
NetBeans features "code templates", which are snippets of predefined code that are automatically generated when you type some predefined abbreviation and then hit tab. For example, if you type if
and hit tab, your editor should generate the following code:
if (true) {
}
You can modify these code templates to your needs or create new ones of your own. Go to Tools->Options->Editor->Code Templates. You will see an extensive list of templates, each matching an abbreviation, and a small edit box to change them. If you scroll down to "if" you'll see this definition:
if (${EXP instanceof="boolean" default="true"}) {
${selection}${cursor}
}
All you need to do is modify it to generate what you desire:
if (${EXP instanceof="boolean" default="true"}) {
${selection}${cursor}
}//end if
You can do something similar for other statements such as while
. You'll notice there often are multiple templates per statement based on different patterns, so this could get tedious depending on how thorough you want to be.
This solution also works well for the main
method, since there is a template defined for it - typing psvm
and tabbing generates it. For other arbitrary methods, there is a template with the abbreviation m
.
Achieving this effect for a top-level class will be different. Go to Tools->Templates. Here you'll see another set of templates which you can explore and modify. The difference is that these are for files rather than code snippets. Expand Java and open the Java Class template file. You'll see the following:
<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">
<#if package?? && package != "">
package ${package};
</#if>
/**
*
* @author ${user}
*/
public class ${name} {
}
Once again, you can modify this template to your needs:
public class ${name} {
}//end class
Or even:
public class ${name} {
}//end {$name}
which will put //end Foo
for a class named Foo
精彩评论