Saturday, 10 December 2016

IntelliJ: Live Template for generating a single parameter constructor

Here's an IntelliJ / Android Studio Live Template for generating a constructor that takes in a single parameter and sets that parameter into a class field:

@android.support.annotation.NonNull
private final $dependencyClass$ $dependency$;

private $constructorClass$(@NonNull $dependencyClass$ $dependency$){
    this.$dependency$ = $dependency$;
}

Set the "Applicability" of the Live Template to be "Java declarations" and set the "Expression" associated to each of the variables as follows:
  • $dependencyClass$ -> completeSmart()
  • $dependency$ -> suggestVariableName()
  • $constructorClass$ -> className()
Tick the "Skip if defined" option for the $constructorClass$ variable and set the "Abbreviation" and "Description" for the Live Template as you wish and that's it... you're good to go!

No comments: