问题
When you create a new java class in Intellij a file header is automatically inserted (based on "File Templates" options). Is there a way to insert that same template into an existing java source file? I was hoping there was a way to use my template definition on existing code without doing a cut/paste all over the place. The template makes use of macro expansion and I hope to be able to keep that. There is a similar function in eclipse called "Generate Element Comment" that would handle this case. Does this exist within Intellij IDEA?
Any help would be much appreciated.
回答1:
Maybe the best solution is to use a "Live Template" but you can´t reuse the File Template, so, you will need at last one copy/paste operation
回答2:
Use Copyright header instead. Configure your Copyright profile as stated at jetbrains.com. Press Alt-Insert or Right mouse click -> select "Generate" -> "Copyright".
You can check what variables you can use in Copyright statement here.
回答3:
go to Settings > File and Code Templates then include tab and select header and define you
/**
* @author : Name
* @since : ${DATE}, ${DAY_NAME_SHORT}
**/
回答4:
You can reach your aim using the Replacing all dialog.
Call the Replacing all dialog, in a top filed set a package
word and in a bottom field set \/\*\nYour file header\n\*\/\n\npackage
and press a Replace All button.
You can specify a set of files which should be upgraded by using a In Project / Module / Directory / Scope settings.
回答5:
File -> Settings -> Editor -> File and Code Templates -> Includes -> File Header
Write This:
/**
* @author : ${USER}
* @created : ${DATE}, ${DAY_NAME_FULL}
**/
To see an header like this:
/**
* @author : BUY
* @created : 21/08/2020, Friday
**/
public class Foo{}
回答6:
I do this by Alfred Snippets
Add following config in alfred snippet, and when I insert !header
, it automaticlly insert wished template.
来源:https://stackoverflow.com/questions/4778543/intellij-insert-file-template-header