Можно ли автоматизировать комментарии для классов, методов, конструкторов и т. Д. В Eclipse?
Скажем, если я создам новый класс в Eclipse под названием "MyClass", код будет сгенерирован следующим образом:
public class MyClass
{
}
По личному соглашению, я всегда заканчиваю последнюю скобку комментарием, таким как это:
public class MyClass
{
} // end of class MyClass
- Итак, можно ли будет вставить такой комментарий для каждого класса, метода, конструктора и т. Д.?
- И можно ли будет "затравить" комментарий с соответствующим названием?
... такие как: // end of class (class name here)
2 ответа
Yes, almost, if you disregard a carriage return.
Go to Window -> Preferences -> Java -> Code Style - Code Templates
This is on Eclipse Indigo - it might be different in other versions.
Now in the "Configure generated code..." panel on the right, open up the "Code" branch and select and edit the "New Java files" entry.
In the Edit dialog, in the Pattern text box, you see the following:
${filecomment}
${package_declaration}
${typecomment}
${type_declaration}
which you need to modify to this:
${filecomment}
${package_declaration}
${typecomment}
${type_declaration}//end of ${type_name}
and then save and close. Try creating a new class and you'll see it comes out like this:
package com.nomadsoft.cortex.application;
public class AdamsClass {
}
//end of AdamsClass
Unfortunately you can't get rid of the carriage return that puts the comment you want on the next line down. It is hard-coded within Eclipse.
Depending on how much effort you want to go to, you have a choice of how to get the comment on the same line as the end-bracket:
(Option 1) you could edit the template files in the JDT jar:
eclipse\plugins\org.eclipse.jdt.ui_*.jar
open up templates/default-codetemplates.xml and find the "newtype" template in the xml, and replace the "${type_declaration}" with
public class ${type_name} \{
\} // end of ${type_name}
(Option 2) write a complete plugin to do this. I'd love to have the time to do that. Maybe you do. See enter link description here
In my opinion this is a bad practice, because the comments can get out of sync.
However , you can get the same visual effect using the Bracketeer plugin (see also this blog post ). The plugin automatically displays closing comments, as you desire:
without saving them to the source file. Thus the "phantom" comments always reflect the current state of the code, giving you the best of both worlds.
The comment style is configurable .