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.
请先登录再写评论。
Please enable Settings (Preferences on macOS) | Editor | Code Style | Java | Wrapping and Braces | Binary expressions | Operation sign on next line and invoke Reformat Code.
I already had that setting enabled. Is it possible to have it properly formatted when the code is generated, without invoking Reformat Code afterwards?
Please see https://youtrack.jetbrains.com/issue/IDEA-142763#focus=Comments-27-2692180.0-0 .
How do I modify the To String template?
See https://www.jetbrains.com/help/idea/generate-tostring-settings-dialog.html .
Thank you. When I try to edit that template, it says "this view is read only".
Make a copy of the template using the corresponding toolbar button, then edit it.
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.
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:
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.
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