mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[jd formatter] minor refactoring
This commit is contained in:
+1
-1
@@ -37,7 +37,7 @@ public class JDClassComment extends JDParamListOwnerComment {
|
||||
@Override
|
||||
protected void generateSpecial(@NotNull String prefix, @NotNull StringBuilder sb) {
|
||||
super.generateSpecial(prefix, sb);
|
||||
String continuationPrefix = getContinuationPrefix(prefix);
|
||||
String continuationPrefix = prefix + javadocContinuationIndent();
|
||||
if (!isNull(myAuthorsList)) {
|
||||
JDTag tag = JDTag.AUTHOR;
|
||||
for (String author : myAuthorsList) {
|
||||
|
||||
@@ -56,20 +56,19 @@ public class JDComment {
|
||||
myMultiLineComment = value;
|
||||
}
|
||||
|
||||
protected String getContinuationIndent() {
|
||||
@NotNull
|
||||
protected String javadocContinuationIndent() {
|
||||
if (!myFormatter.getSettings().JD_INDENT_ON_CONTINUATION) return "";
|
||||
return continuationIndent();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String continuationIndent() {
|
||||
CodeStyleSettings settings = myFormatter.getSettings();
|
||||
CommonCodeStyleSettings.IndentOptions indentOptions = settings.getIndentOptions(JavaFileType.INSTANCE);
|
||||
return new IndentInfo(0, indentOptions.CONTINUATION_INDENT_SIZE, 0).generateNewWhiteSpace(indentOptions);
|
||||
}
|
||||
|
||||
protected String getContinuationPrefix(String prefix) {
|
||||
if (myFormatter.getSettings().JD_INDENT_ON_CONTINUATION) {
|
||||
return prefix + getContinuationIndent();
|
||||
} else {
|
||||
return prefix;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String generate(@NotNull String indent) {
|
||||
final String prefix;
|
||||
@@ -94,7 +93,7 @@ public class JDComment {
|
||||
|
||||
generateSpecial(prefix, sb);
|
||||
|
||||
final String continuationPrefix = getContinuationPrefix(prefix);
|
||||
final String continuationPrefix = prefix + javadocContinuationIndent();
|
||||
|
||||
if (!isNull(myUnknownList) && myFormatter.getSettings().JD_KEEP_INVALID_TAGS) {
|
||||
for (String aUnknownList : myUnknownList) {
|
||||
|
||||
+4
-3
@@ -28,7 +28,7 @@ import java.util.List;
|
||||
*/
|
||||
public class JDMethodComment extends JDParamListOwnerComment {
|
||||
private String myReturnTag;
|
||||
private List<NameDesc> myThrowsList;
|
||||
private List<TagDescription> myThrowsList;
|
||||
|
||||
public JDMethodComment(@NotNull CommentFormatter formatter) {
|
||||
super(formatter);
|
||||
@@ -43,7 +43,8 @@ public class JDMethodComment extends JDParamListOwnerComment {
|
||||
JDTag tag = JDTag.RETURN;
|
||||
sb.append(myFormatter.getParser().formatJDTagDescription(myReturnTag,
|
||||
prefix + tag.getWithEndWhitespace(),
|
||||
getContinuationPrefix(prefix)));
|
||||
prefix + javadocContinuationIndent()));
|
||||
|
||||
if (myFormatter.getSettings().JD_ADD_BLANK_AFTER_RETURN) {
|
||||
sb.append(prefix);
|
||||
sb.append('\n');
|
||||
@@ -69,6 +70,6 @@ public class JDMethodComment extends JDParamListOwnerComment {
|
||||
if (myThrowsList == null) {
|
||||
myThrowsList = ContainerUtilRt.newArrayList();
|
||||
}
|
||||
myThrowsList.add(new NameDesc(className, description));
|
||||
myThrowsList.add(new TagDescription(className, description));
|
||||
}
|
||||
}
|
||||
|
||||
+58
-39
@@ -24,7 +24,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.List;
|
||||
|
||||
public class JDParamListOwnerComment extends JDComment {
|
||||
protected List<NameDesc> myParamsList;
|
||||
protected List<TagDescription> myParamsList;
|
||||
|
||||
public JDParamListOwnerComment(@NotNull CommentFormatter formatter) {
|
||||
super(formatter);
|
||||
@@ -49,7 +49,7 @@ public class JDParamListOwnerComment extends JDComment {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public NameDesc getParameter(@Nullable String name) {
|
||||
public TagDescription getParameter(@Nullable String name) {
|
||||
return getNameDesc(name, myParamsList);
|
||||
}
|
||||
|
||||
@@ -57,13 +57,13 @@ public class JDParamListOwnerComment extends JDComment {
|
||||
if (myParamsList == null) {
|
||||
myParamsList = ContainerUtilRt.newArrayList();
|
||||
}
|
||||
myParamsList.add(new NameDesc(name, description));
|
||||
myParamsList.add(new TagDescription(name, description));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static NameDesc getNameDesc(@Nullable String name, @Nullable List<NameDesc> list) {
|
||||
private static TagDescription getNameDesc(@Nullable String name, @Nullable List<TagDescription> list) {
|
||||
if (list == null) return null;
|
||||
for (NameDesc aList : list) {
|
||||
for (TagDescription aList : list) {
|
||||
if (aList.name.equals(name)) {
|
||||
return aList;
|
||||
}
|
||||
@@ -75,50 +75,69 @@ public class JDParamListOwnerComment extends JDComment {
|
||||
* Generates parameters or exceptions
|
||||
*
|
||||
*/
|
||||
protected void generateList(@NotNull String prefix,
|
||||
protected void generateList(@NotNull final String prefix,
|
||||
@NotNull StringBuilder sb,
|
||||
@NotNull List<NameDesc> list,
|
||||
@NotNull List<TagDescription> tagBlocks,
|
||||
@NotNull String tag,
|
||||
boolean align_comments,
|
||||
boolean generate_empty_tags,
|
||||
boolean wrapDescription)
|
||||
boolean descriptionOnNewLine)
|
||||
{
|
||||
int maxNameLength = maxTagDescriptionNameLength(tagBlocks, align_comments, generate_empty_tags, descriptionOnNewLine);
|
||||
|
||||
StringBuilder fill = new StringBuilder(prefix.length() + tag.length() + maxNameLength + 1);
|
||||
fill.append(prefix);
|
||||
StringUtil.repeatSymbol(fill, ' ', maxNameLength + 1 + tag.length());
|
||||
|
||||
for (TagDescription nd : tagBlocks) {
|
||||
if (isNull(nd.desc) && !generate_empty_tags) continue;
|
||||
|
||||
if (descriptionOnNewLine && !isNull(nd.desc)) {
|
||||
sb.append(prefix).append(tag).append(nd.name).append("\n");
|
||||
sb.append(formatJDTagDescription(nd.desc, prefix + continuationIndent()));
|
||||
}
|
||||
else if (align_comments) {
|
||||
int spacesNumber = maxNameLength + 1 - nd.name.length();
|
||||
String spaces = StringUtil.repeatSymbol(' ', Math.max(0, spacesNumber));
|
||||
String firstLinePrefix = prefix + tag + nd.name + spaces;
|
||||
sb.append(formatJDTagDescription(nd.desc, firstLinePrefix, fill));
|
||||
}
|
||||
else {
|
||||
String description = (nd.desc == null) ? "" : nd.desc;
|
||||
StringBuilder tagDescription = formatJDTagDescription(tag + nd.name + " " + description, prefix, prefix + javadocContinuationIndent());
|
||||
sb.append(tagDescription);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int maxTagDescriptionNameLength(@NotNull List<TagDescription> tagBlocks,
|
||||
boolean align_comments,
|
||||
boolean generate_empty_tags,
|
||||
boolean descriptionOnNewLine)
|
||||
{
|
||||
int max = 0;
|
||||
|
||||
if (align_comments && !wrapDescription) {
|
||||
for (NameDesc nd: list) {
|
||||
int currentLength = nd.name.length();
|
||||
if (isNull(nd.desc) && !generate_empty_tags) continue;
|
||||
//finding longest parameter length
|
||||
if (currentLength > max) {
|
||||
max = currentLength;
|
||||
if (align_comments && !descriptionOnNewLine) {
|
||||
for (TagDescription tagDescription: tagBlocks) {
|
||||
int current = tagDescription.name.length();
|
||||
if (isNull(tagDescription.desc) && !generate_empty_tags) continue;
|
||||
if (current > max) {
|
||||
max = current;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder fill = new StringBuilder(prefix.length() + tag.length() + max + 1);
|
||||
fill.append(prefix);
|
||||
StringUtil.repeatSymbol(fill, ' ', max + 1 + tag.length());
|
||||
|
||||
String wrapParametersPrefix = prefix + getContinuationIndent();
|
||||
for (NameDesc nd : list) {
|
||||
if (isNull(nd.desc) && !generate_empty_tags) continue;
|
||||
if (wrapDescription && !isNull(nd.desc)) {
|
||||
sb.append(prefix).append(tag).append(nd.name).append("\n");
|
||||
sb.append(myFormatter.getParser().formatJDTagDescription(nd.desc, wrapParametersPrefix));
|
||||
}
|
||||
else if (align_comments) {
|
||||
int spacesNumber = max + 1 - nd.name.length();
|
||||
String spaces = StringUtil.repeatSymbol(' ', Math.max(0, spacesNumber));
|
||||
String firstLinePrefix = prefix + tag + nd.name + spaces;
|
||||
sb.append(myFormatter.getParser().formatJDTagDescription(nd.desc, firstLinePrefix, fill));
|
||||
}
|
||||
else {
|
||||
String description = (nd.desc == null) ? "" : nd.desc;
|
||||
sb.append(myFormatter.getParser().formatJDTagDescription(tag + nd.name + " " + description,
|
||||
prefix,
|
||||
getContinuationPrefix(prefix)));
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
private StringBuilder formatJDTagDescription(@Nullable String description,
|
||||
@NotNull CharSequence firstLinePrefix,
|
||||
@NotNull CharSequence continuationPrefix) {
|
||||
return myFormatter.getParser().formatJDTagDescription(description, firstLinePrefix, continuationPrefix);
|
||||
}
|
||||
|
||||
private StringBuilder formatJDTagDescription(@Nullable String description, @NotNull CharSequence prefix) {
|
||||
return formatJDTagDescription(description, prefix, prefix);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -22,11 +22,11 @@ import org.jetbrains.annotations.Nullable;
|
||||
*
|
||||
* @author Dmitry Skavish
|
||||
*/
|
||||
public class NameDesc {
|
||||
public class TagDescription {
|
||||
@NotNull public final String name;
|
||||
@Nullable public final String desc;
|
||||
|
||||
public NameDesc(@NotNull String name, @Nullable String desc) {
|
||||
public TagDescription(@NotNull String name, @Nullable String desc) {
|
||||
this.name = name;
|
||||
this.desc = desc;
|
||||
}
|
||||
Reference in New Issue
Block a user