-
Notifications
You must be signed in to change notification settings - Fork 870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix-#1199/refactor-implementation-smells #1200
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The refactored code looks much better. However, I still get headaches when I see the parameter |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,28 +42,69 @@ public String rewrite(Tok tok, int maxWidth, int column0) { | |
if (!tok.isComment()) { | ||
return tok.getOriginalText(); | ||
} | ||
|
||
String text = formatJavadocIfNeeded(tok, column0); | ||
List<String> lines = parseCommentLines(tok, text); | ||
|
||
return formatComment(tok, lines, column0); | ||
} | ||
|
||
/** | ||
* Format javadoc comment if needed | ||
*/ | ||
private String formatJavadocIfNeeded(Tok tok, int column0) { | ||
String text = tok.getOriginalText(); | ||
if (tok.isJavadocComment() && options.formatJavadoc()) { | ||
text = JavadocFormatter.formatJavadoc(text, column0); | ||
return JavadocFormatter.formatJavadoc(text, column0); | ||
} | ||
return text; | ||
} | ||
|
||
/** | ||
* Parse comment into lines with appropriate trimming | ||
*/ | ||
private List<String> parseCommentLines(Tok tok, String text) { | ||
List<String> lines = new ArrayList<>(); | ||
Iterator<String> it = Newlines.lineIterator(text); | ||
|
||
while (it.hasNext()) { | ||
String line = it.next(); | ||
if (tok.isSlashSlashComment()) { | ||
lines.add(CharMatcher.whitespace().trimFrom(it.next())); | ||
lines.add(CharMatcher.whitespace().trimFrom(line)); | ||
} else { | ||
lines.add(CharMatcher.whitespace().trimTrailingFrom(it.next())); | ||
lines.add(CharMatcher.whitespace().trimTrailingFrom(line)); | ||
} | ||
} | ||
return lines; | ||
} | ||
|
||
/** | ||
* Format the comment based on its type | ||
*/ | ||
private String formatComment(Tok tok, List<String> lines, int column0) { | ||
if (tok.isSlashSlashComment()) { | ||
return indentLineComments(lines, column0); | ||
} | ||
|
||
return getFormattedBlockComment(tok, lines, column0); | ||
} | ||
|
||
/** | ||
* Get formatted block comment, either as parameter comment or regular block comment | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Who is supposed to read this JavaDoc comment? By default, JavaDoc for private methods is not published. You can only see it in the IDE by hovering over the method name and waiting, or by jumping to the method’s implementation. The method signature is already well chosen. |
||
*/ | ||
private String getFormattedBlockComment(Tok tok, List<String> lines, int column0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For my taste, the methods are too fragmented. It’s hard to keep track of all the small subroutines in my head. Isn’t there a middle ground between being too small and too complex? |
||
return CommentsHelper.reformatParameterComment(tok) | ||
.orElseGet( | ||
() -> | ||
javadocShaped(lines) | ||
? indentJavadoc(lines, column0) | ||
: preserveIndentation(lines, column0)); | ||
.orElseGet(() -> formatBlockComment(lines, column0)); | ||
} | ||
|
||
/** | ||
* Format block comment based on whether it is javadoc-shaped or not | ||
*/ | ||
private String formatBlockComment(List<String> lines, int column0) { | ||
if (javadocShaped(lines)) { | ||
return indentJavadoc(lines, column0); | ||
} | ||
return preserveIndentation(lines, column0); | ||
} | ||
|
||
// For non-javadoc-shaped block comments, shift the entire block to the correct | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good!