[IntelliJ] Create custom code snippet

To standardize code style among different project and teams, snippet is one of the tools to implement coding standard.

However, snippet is the two-side blade for developer. It will introduce duplicate code and result difficult to maintain if abuse. The recommend place to use snippets are:

  1. Unit test for high level format;
  2. DTO / DAO with specific handling in getter / setter method;
  3. Predefined markup (e.g. XML / HTML, JSON, YAML);

In this demo, it will use JUnit parameter test as sample to create snippet.Steps:

  1. Create Live Template
    Highlight code would like to add into snippet, press Control + Shift + A, and input save as live template, then click Enter.
  2. Add snippet abbreviation and variables.
    In Save as Live Template dialog, in Abbreviation, input name ptest, and variables are format like $variableName$.
  3. Configure snippet variables.
    Click Edit Variables, in expression, input default value. You can use method provided to customize value (e.g. upper case, camelCase, etc) to fit your need.
  4. Test snippet.
    In project, type snippet abbreviation, expected it will show in intelli-sense, after press enter, snippet will be render.
About C.H. Ling 262 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.