IntelliJ Live Template: modified setters template

▼魔方 西西 提交于 2019-12-01 21:41:52

问题


Is anyone know how to set up a live template in intellij for doing specialized setters - i'm using v5, but I accept with pleasure knowledge for a more recent release) -

My first need is a firePropertyChange setter:

public final static String $PROPERTY$ = "$property$"
public void set$Property$($TYPE$ $property$) {
    Object oldValue = this.$property$;
    this.$property$ = $property$;
    firePropertyChange($PROPERTY$, oldValue, $property$);
}

I have a semi-working version that generate the implementation with variables defined like this: $property$ --> completeSmart() $PROPERTY$ --> completeSmart()

My second need is a builder style setter that call the regular setter and then return this after the set:

public $THIS_TYPE$ with$Property$($TYPE$ $property$) {
    set$Property$($property$); 
    return this;
}

For this one I have nothing really good: I still have to type a lot !

Any suggestion ?


回答1:


Something like this

private $TYPE$ $NAME$;
public $THIS$ set$BNAME$($TYPE$ $NAME$) {
    this.$NAME$ = $NAME$;
    return this;
}

where

Type = complete()
NAME = suggestVariableName()
BNAME = capitalize(NAME)
THIS = className()

The only Problem ist that className will not work in nested classes as it will return "Outer$Inner" but it should work good enough.



来源:https://stackoverflow.com/questions/2147743/intellij-live-template-modified-setters-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!