ICommentContainerInformation in = commentContext.create(Math.max(0, pos));
CommentProcessor cpr = new CommentProcessor();
CommentText comment = cpr.separateCommentFromContainer(textOfNodes(nodes), in, lineSeparator);
// format in position 0 to measure it
TextFlow formatted = cpr.formatComment(comment, pos0Context, new CommentFormattingOptions(
advice, Integer.MAX_VALUE, trailing, maxTrailing), layoutContext);
int w = formatted.getWidth();
if(w <= available) {
// yay, it will fit as a hanging comment, reformat for this position if the position is not
// at an even indent multiple.
int use = current - pos_effectiveIndent;
if(use > 0) {
// out = commentContext.create(use);
formatted = cpr.formatComment(comment, commentContext.create(use), new CommentFormattingOptions(
advice, Integer.MAX_VALUE, trailing, maxTrailing), layoutContext);
}
output.appendText(CharSequences.trimLeft(formatted.getText())); // , true);
}
else {
// Did not fit un-wrapped
// if output ends with a break, then current is at the leftmost position already, and moving it
// is not an option. The position is also at an indent multiple, so no need to reposition the comment.
//
if(output.endsWithBreak() || output.isEmpty()) {
// re-format for effective indent
formatted = cpr.formatComment(
//
comment, //
pos0Context, //
new CommentFormattingOptions(advice, maxWidth - pos_effectiveIndent, trailing, maxTrailing),
layoutContext);
}
else {
// re-format for the available space (hanging at current position)
formatted = cpr.formatComment(//
comment, //
commentContext.create(current - pos_effectiveIndent), //
new CommentFormattingOptions(advice, available, trailing, maxTrailing), layoutContext);
// if comment formatted for hanging at current does not fit the width (because it
// has non-breakable content, or starts at an awkward position)
// then reformat and move the comment to the effective indent.
if(formatted.getWidth() > maxWidth - pos_effectiveIndent) {
// ouch, not possible to format it as hanging at current position, must move it to the next line.
output.appendBreak();
// re-format for the effective indent position space
formatted = cpr.formatComment(
//
comment, //
pos0Context, //
new CommentFormattingOptions(advice, maxWidth - pos_effectiveIndent, trailing, maxTrailing),
layoutContext);
}
}
output.appendText(CharSequences.trimLeft(formatted.getText())); // , true);
}