Working with PostFix Code Completion

Nowadays we developer are happy about code that we do not have to write ourselves. The common IDEs like Eclipse and IntelliJ support us with code completion and templates to write code faster.
Both IDEs have a lot of built-in templates to generate boilerplate code. They can generate for-loops, null checks, switch statements, instanceof checks and a lot more code snippets. Every project is different and has other requirements. The best thing is that these templates can be extended directly by the user in the IDE.
To get a feeling for the existing templates take a look at the preferences/settings:
Eclipse: Window/Preferences/Java/Editor/Templates
IntelliJ: Settings/Editor/Live templates
Another way
The so called PostFix Code Completion mechanism is another way to generate code within our IDEs. It is supported in both Eclipse and IntelliJ.
As the name “Postfix” suggests, we can append short suffixes to code blocks to invoke a completion. The completion will use the preceding code to fill out a template. This mechanism really helps me to stay in the flow of writing code. Sounds good? Here are some examples:
Generate local variable
1 2 3 4 5 6 7 |
"PostfixCompletion".var --> String string = "PostfixCompletion"; false.var --> boolean false1 = false; arrayList.size().var --> int name = arrayList.size(); |
Generate new field
1 2 3 4 5 6 7 |
"PostfixCompletion".field --> private String string; false.field --> private boolean b; arrayList.size().field --> private int i; |
Generate a private/public constant (Eclipse only)
1 2 3 4 5 |
"PostfixCompletion".constpriv --> private static final String STRING = "PostfixCompletion"; false.constpub --> public static final boolean BOOLEAN = false; |
Generate an enhanced for-loop
1 2 3 |
arrayListOfStrings.for --> for(String arrayListOfStrings2 : arrayListOfStrings) {} |
Generate println statement
1 2 3 4 |
"ECLIPSE: SOME TEXT".sysout --> System.out.println("ECLIPSE: SOME TEXT"); "INTELLIJ: SOME TEXT".sout --> System.out.println("INTELLIJ: SOME TEXT"); |
… a lot more
Both IDEs support a lot more postfix templates. Eclipse provides 22 built-in templates, IntelliJ even 30 java templates and 17 templates for Kotlin. It’s worth to give it a try.
Setup
Eclipse only supports this feature with an additional plugin. Use Install New Software and paste the following in the update site input field:
1 |
https://raw.githubusercontent.com/trylimits/Eclipse-Postfix-Code-Completion/master/org.eclipse.jdt.postfixcompletion.updateSite/target/site/ |
Select the checkbox for JDT Postfix code completion. The JDT Patch is only required for Eclipse Luna or even older versions (I hope nobody has to use such a prehistoric eclipse version).
IntelliJ is already delivered with that feature.
Within Eclipse the new completions can be found in the preferences dialog where the default templates are located. The only difference is that the new ones have the Java postfix context. This is also the place to add new templates.
In IntelliJ the postfix completions are located in Settings/Editor/General/Postfix Completion. However, I haven’t found a way to add my own templates to IntelliJ.
Conclusion
I like this solution to generate small code elements. It allows me to stay in flow and avoid remembering weird keyboard shortcuts.
If you know a way to add new templates to IntelliJ, let me know in the comments. Also feel free to drop me a comment with your own productivity hacks.
Recent posts






Comment article