自动将注释附加到 Netbeans 中自动插入的大括号

发布于 2024-12-06 14:50:43 字数 300 浏览 0 评论 0原文

我真的很欣赏 NetBeans 如何自动在我正在处理的代码中输入右括号。它是否有可能进一步简化如下代码的开发?:

public class Test
{
    public static void main(String[] Args)
    {
        //Some code here

        while(true)
        {
            //Some more code here

        }//end while

    }//end main
}//end class

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

        while(true)
        {
            //Some more code here

        }//end while

    }//end main
}//end class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-12-13 14:50:43

不幸的是,似乎没有办法自定义 NetBeans 添加右括号时生成的内容:在“工具”->“选项”->“编辑器”->“代码完成”中只有一个复选框用于指示是否执行此操作。

这是下一个最佳解决方案:

NetBeans 具有“代码模板”功能,它是预定义代码片段,当您键入一些预定义缩写然后按 Tab 键时会自动生成。例如,如果您键入 if 并点击 Tab,您的编辑器应生成以下代码:

if (true) {

}

您可以根据需要修改这些代码模板或创建自己的新代码模板。转到工具->选项->编辑器->代码模板。您将看到一个广泛的模板列表,每个模板都匹配一个缩写,以及一个用于更改它们的小编辑框。如果向下滚动到“if”,您将看到这个定义:

if (${EXP instanceof="boolean" default="true"}) {
   ${selection}${cursor}
}

您所需要做的就是修改它以生成您想要的内容:

if (${EXP instanceof="boolean" default="true"}) {
   ${selection}${cursor}
}//end if

您可以对其他语句(例如 while)执行类似的操作。您会注意到,每个语句通常有多个基于不同模式的模板,因此这可能会变得乏味,具体取决于您想要的彻底程度。

该解决方案也适用于 main 方法,因为为其定义了一个模板 - 输入 psvm 并按 Tab 键生成它。对于其他任意方法,有一个带有缩写 m 的模板。

对于顶级班级来说,达到这种效果会有所不同。转到工具->模板。在这里您将看到另一组可以探索和修改的模板。不同之处在于这些是针对文件而不是代码片段。展开 Java 并打开 Java 类模板文件。您将看到以下内容:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

<#if package?? && package != "">
package ${package};

</#if>
/**
 *
 * @author ${user}
 */
public class ${name} {

}

您可以再次根据需要修改此模板:

public class ${name} {

}//end class

甚至:

public class ${name} {

}//end {$name}

这将为名为 Foo 的类添加 //end Foo

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文