Code format when generating To String.

已回答

When I generate ToString, I want the "+" to appear on the next line.

 

I tried a few different things, but it didn't seem to do it.

0

Please enable Settings (Preferences on macOS) | Editor | Code Style | Java | Wrapping and Braces | Binary expressions | Operation sign on next line and invoke Reformat Code.

0

I already had that setting enabled.  Is it possible to have it properly formatted when the code is generated, without invoking Reformat Code afterwards?

0

How do I modify the To String template?

0

Thank you.  When I try to edit that template, it says "this view is read only".

0

Make a copy of the template using the corresponding toolbar button, then edit it.

0

How can I modify the template to get a format like this?

 

return "HospitalInfoCategory{"
+ "title='" + title + '\''
+ ", content='" + content + '\''
+ ", url='" + url + '\''
+ ", icon='" + icon + '\''
+ '}';

 

If I put that + that's on line 15 of the template, on the next line, then that's the only thing that appears on that line.

 

I tried putting the plus at the beginning of the line, but it didn't do what I wanted either.

 

Also, is it possible to modify the template for equals(Object o)?  There's a few things about the way that code is formatted, that I want to change too.  I couldn't find a template for equals though.  

0

Alakhotia,

Hello, please use the following template that seems to cover your case:

public java.lang.String toString() {
#if ( $members.size() > 0 )
#set ( $i = 0 )
return "$classname{"
+#foreach( $member in $members )
#if ( $i == 0 )
"##
#else
", ##
#end
#if ( $member.objectArray )
#if ($java_version < 5)
$member.name=" + ($member.accessor == null ? null : java.util.Arrays.asList($member.accessor))
+#else $member.name=" + java.util.Arrays.toString($member.accessor)
+#end
#elseif ( $member.primitiveArray && $java_version >= 5)
$member.name=" + java.util.Arrays.toString($member.accessor)
+#elseif ( $member.string )
$member.name='" + $member.accessor+ '\''
+#else
$member.name=" + $member.accessor+ '\''
+#end
#set ( $i = $i + 1 )
#end
'}';
#else
return "$classname{}";
#end
}

While generating the equals() template, you may press the three dots to open the template settings and change them the same way:

0

Thank you.  The toString template works great now.

 

Is there a clean way to format the equals and hashcode template so that it's like this:

 

    if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
HospitalInfoCategory that = (HospitalInfoCategory) o;
return Objects.equals(title, that.title)
&& Objects.equals(content, that.content)
&& Objects.equals(url, that.url)
&& Objects.equals(icon, that.icon);
}

 

1). All if or if else blocks have { } brackets formatted like pasted in that block.  Even if there is a single line within the if block, it should be in brackets.

 

2). All && start on new lines.

 

Thank you.

0

Hello,

Unfortunately it can't be done with the template. Please follow the feature request covering the issue: https://youtrack.jetbrains.com/issue/IDEA-183037

Thank you 

0

请先登录再写评论。