开发者

IntelliJ getter/setter format (single-line versus multi-line)

How can you get IntelliJ to generate getter/setters accessor methods on one line like this:

public String getAbc() { return abc; }

… instead of multiple lines like this:

public String getAbc() {
   retur开发者_开发问答n abc;
}


I'm using IntelliJ IDEA 14.1.0 and you can customise this behaviour.

Just use the "Generate..." option, or use Alt+Insert shortcut, and select "Getter and Setter".

In the "Select Fields" window that gets opened, you have the "Getter Template" option at the top. Use the "..." button next to the dropdown, to edit the template.

Select "IntelliJ Default" and click the "Copy" button to create a new one named "AlwayStartWithGet", which you can edit.

Just remove the following section:

#if ($field.boolean)
  #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
    #set($name = $StringUtil.decapitalize($name))
  #else
    is##
#end
#else
  get##
#end

And replace it with a simple

get##

You should be left with:

public ##
#if($field.modifierStatic)
  static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}() {
  return $field.name;
}

Now you can use the custom template when generating code, by selecting it in the getter template dropdown.


For Idea 2016.

Getter template

Merge the last 3 lines into a single line:

${name}() { return $field.name; }

Setter template

Add double hash (without a space) at the end of the longest line:

[...] ($field.type, $paramName) {##

Merge the last 2 lines into a single line:

$field.name = $paramName; }

Note: as commented by @mindas, you will probably want instead the visual auto folding that doesn't get versioned.


There are no templates neither for getters nor for equals/hashcode. These are hardcoded in IDEA.

Source

You can see that in this IntelliJ Wishlist


You didn't mention what version of IDEA you are using, so I assume the recent 8 or 9.

Check your Code Style settings, under "Alignment and Braces". You should find a "Simple methods in one line" option there.


I don't know why you want to do this, presumably for saving visual space. If so, just use IntelliJ's feature to fold trivial getters/setters and forget about how lines does it take. Folding feature can be found in

Settings -> IDE Settings -> Editor -> Code Folding -> Show code folding outline -> Simple property accessors


Alex G and laffuste provided answers that helped me, but even after you follow these instructions, IntelliJ may still automatically format your code. To stop this from happening, go to Settings -> Editor -> Code Style -> Java (or other language). Click on the tab titled "Wrapping and Braces". Under the title "Keep when reformatting" click the checkbox next to "Simple methods in one line".

For further brevity, you can eliminate the blank lines between the getter and setter methods. To do this, click on the tab titled "Blank Lines". In the section titled "Minimum Blank Lines", find the text box next to "Around method". Enter 0 in the text box.

Get

public ##
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}(){ return $field.name; }

Set

#set($paramName = $helper.getParamName($field, $project))
#if($field.modifierStatic)
static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) { ##
#if ($field.name == $paramName)
    #if (!$field.modifierStatic)
    this.##
    #else
        $classname.##
    #end
#end
$field.name = $paramName; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜