简体   繁体   English

自动向Netbeans中自动插入的花括号添加注释

[英]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. 我非常感谢NetBeans如何自动在我正在处理的代码中输入右括号。 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

        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. 不幸的是,似乎没有一种方法可以自定义NetBeans添加右括号时生成的内容:在“工具”->“选项”->“编辑器”->“代码完成”中只有一个复选框可以选择是否执行此操作。

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. NetBeans具有“代码模板”,这是预定义代码的片段,当您键入一些预定义的缩写然后单击tab时,这些片段将自动生成。 For example, if you type if and hit tab, your editor should generate the following code: 例如,如果您键入if并点击tab,那么编辑器应生成以下代码:

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 . 您可以对其他语句(如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. 该解决方案也适用于main方法,因为为它定义了一个模板-键入psvm并通过制表键生成它。 For other arbitrary methods, there is a template with the abbreviation m . 对于其他任意方法,有一个缩写为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. 展开Java并打开Java Class模板文件。 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 这会将//end Foo放到名为Foo的类中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM