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

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() {
return 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; }

Related

How to format comments with one space between // and words?

How to automatically format comments with one space between // and words? For example, selecting format in my editors should convert
//login to // login
public interface LoginService {
//login // -> // login
SysAdminDO login(LoginForm form);
}
You are probably looking for the setting
Editor ~> Code Style ~> Java(language) ~> Code Generation
Or else you can alternatively use Comment with line comment shortcut
Just type a line and on that line use the shortcut. e.g.
String var;
I would make this a comment [Press the shortcut]
// I would make this a comment

How change default method name in intellij idea?

When shortkey for extracting new method is pressed Idea show a dialog with method name like that - get + SomeName. Is there a to change this template to create + SomeName?
Note
I have already seen that there is a setting Editor/File and Code templates but it contains only method bodies, not names.
The way you can change the template for the getters and setters is through the Generate menu.
Invoke the menu (Alt+insert) and choose Getter and Setter (or one of Getter and Setter). If you click the ... button next to the template selection you can create your own template that does what you want.
In your case you could simply replace the word "get" with "create" like below to get what you want while retaining everything else from the Intellij default template.
public ##
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
#if ($StringUtil.startsWithIgnoreCase($name, 'is'))
#set($name = $StringUtil.decapitalize($name))
#else
is##
#end
#else
create##
#end
${name}() {
return $field.name;
}

Can I automatically pass an existing text into the method parameter?

For example I have the following block of code:
public String getDbSchema() {
return DB_SCHEMA;
}
Is there a shortcut to quickly turn this code into
public String getDbSchema() {
return properties.getProperty(DB_SCHEMA);
}
Currently I have to do properties.getproperty then take out right bracket and re-insert it into the end of the statement
When you select getProperty from the code completion, instead of pressing Enter, press the shortcut of Edit | Complete Current Statement (e.g. Ctrl+Shift+Enter), and DB_SCHEMA will be wrapped into call parentheses.
Sure, you can use a structural find and replace that is a little bit smart.
First, let's presume that this code has the form return XYZ; where XYZ is a constant identifier (CAPS or _)
Then you can go into search and replace in files (ctrl+shift+R), tick Case Sensitive and Regular Expression and enter:
Text to find: return ([A-Z_]*);
Replace with: return properties.getProperty($1);

How can I get IntelliJ to automatically insert tabs/indents?

In a code block like this ( '|' representing the caret position):
public void myMethod(){
|
}
The caret position is at the start. I want the line to automatically insert the tabs/indents to the line so it looks like this:
public void myMethod(){
|
}
I know it's just me being lazy but Eclipse had this feature and I'd like to know how to get IntelliJ to do this too.
Thanks.
You can ctrl + alt + l and refactor & reformat your code. You can do this before or after selecting the specific code block to be refactored.

Eclipse formatter: can it ignore annotations?

Sometimes I want annotations on fields be in a single line and sometimes in a line each. Is there any way to make Eclipse formatter just ignore these annotations and leave the lines breaks just as I did?
Not quite sure what you mean, but you break up lines for field this way:
String text =
"cake" +
"more cake" +
"alot more cake";
This is also an option:
You can go to Properties -> Java code style -> Formatter -> Edit: Then there should be some tags to on/off.
Also include this line in your code:
/* #formatter:on */
I think so. In Preferences go to Java - Code Style - Wrapper. Then edit your active profile, click "Line Wrapping" and then "Annotations - Element-value pairs". Then set the Line wrapping policy to "Do not wrap".

Categories

Resources