A LiveTemplate is a little code snippet that is generate when a pre-defined abbreviation for the snippet is typed.
For example if you were using the templates in this repo and you wanted to log something at info level in a Java class file type lgi
then press tab
this will expand to LOGGER.info(String.format(""));
.
For more information on LiveTemplates see: Live Templates
- Download and copy the xml file(s) to your templates folder:
- Windows:
<your home directory>\.<product name><version number>\config\templates
- Linux:
~\.<product name><version number>\config\templates
- OS X:
~/Library/Preferences/<product name><version number>/templates
e.g. ~/Library/Preferences/IntelliJIdea12/templates
on OS X for IntelliJ 12.
- Restart IntelliJ.
- To see all templates, go to Preferences->Live Templates and expand the Template Group.
To see templates available for a context type Cmd + J
on Mac OS X or Ctrl + J
in Windows/Linux.
To use a LiveTemplate, you type the abbreviation for a template then press tab.
Alternatively if you do not remember the shortcut for a template press:
- Windows/Linux:
Ctrl + J
- Mac OS X:
Cmd + J
Or for a surround template (a template that uses selected code inside of it) highlight the item you want to insert a template for and press:
- Windows/Linux:
Ctrl + Alt + J
- Mac:
Cmd + alt + J
This will result in a list of applicable templates showing up where you can start typing to filter down to the template you want. Select the item you want and press enter.
###Traceability
lg
: Declare the logger and initialise it in a static block using the current class name.lgd
: Log at the debug level:LOGGER.debug("");
lge
: Log at the error level:LOGGER.error("");
lgi
: Log at the info level:LOGGER.info("");
lgt
: Log at the trace level:LOGGER.trace("");
###Testing
ase
: Assert Equals:Assert.assertEquals(String.format(""),,);
asf
: Assert false:Assert.assertFalse(String.format(""),);
asn
: Assert null:Assert.assertNull(String.format(""),);
asnn
: Assert not null:Assert.assertNotNull(String.format(""),);
ast
: Assert true:Assert.assertTrue(String.format(""),);
eme
: Generates the .reset ; .expect ; .replay and .verify for the selected service being mocked.junit
: Creates the Spring JUnit annotation. Should be run at the top of a test class to ensure it is generated in the correct place.
###Utility
sf
: Generates:String.format("")
. Semicolon intentionally left out so this can be used inside other methods and not just to define a variable.
If you want to create your own LiveTemplates here are some tips and tricks to get you started:
Use the fully qualified class name when referencing a class instance variable for example instead of
Assert.assertTrue("Somebool should be true", someBool);
use
org.junit.Assert.assertTrue("Somebool should be true", someBool);
Doing it this way means when using the template you will not have to keep telling IntelliJ what package the object belongs to (IntelliJ will automatically remove the fully qualified name and include it as an import if there is no other object in the given file that has the same class name).
- Always add new/custom LiveTemplates to their own group as apposed to the default groups IntelliJ provides.
- This allows people to clone your LiveTemplate repo into their templates folder and not overwrite their existing templates (if you use the same group name it will be placed in a file with that name which will overwrite their existing file when they clone your repo).
- Creating new LiveTemplate groups in IntelliJ is not too intuitive. To do this create your new template in any group, then right click on the template and choose
move to group
then selectnew group
and give it a name. Going forward you can create new templates in this new group.
To create a LiveTemplate:
- Select the code fragment you want to turn in to a LiveTemplate
- Go to the
Tools
menu and clickSave as LiveTemplate
- The LiveTemplate creation modal should appear
- Input the abbreviation you want
- Configure any variables (if any) and you're done
This is one of 2 built-in variables (the other will be described below).
This is used to specify where the cursor will be positioned after the template has executed.
For example if you have the template with keyword sf
configured as
String.format("$END$");
then the cursor will appear between the quotes after executing the LiveTemplate.
This built-in variable is used to create a surround template. It basically lets the user select text then choose the surround template they want which gets the template wrapped around it.
For example the EasyMock template described above is defined as follows:
org.easymock.EasyMock.reset($SELECTION$);
org.easymock.EasyMock.expect($SELECTION$.$END$).andReturn(/*TODO*/);
org.easymock.EasyMock.replay($SELECTION$);
org.easymock.EasyMock.verify($SELECTION$); //TODO move this to where it is appropriate
This allows the user for example to highlight myAwesomeService
then choose eme
from the surround templates which results in the below being generated (cursor will be place before ).andReturn(
):
org.easymock.EasyMock.reset(myAwesomeService);
org.easymock.EasyMock.expect(myAwesomeService.).andReturn(/*TODO*/);
org.easymock.EasyMock.replay(myAwesomeService);
org.easymock.EasyMock.verify(myAwesomeService); //TODO move this to where it is appropriate
Fork, update and submit pull requests.