Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ public State computeBreaks(
CommentsHelper commentsHelper, int maxWidth, State state, Obs.ExplorationNode observationNode) {
String text = commentsHelper.rewrite(tok, maxWidth, state.column());
@SuppressWarnings("for-rollout:NullAway")
int firstLineLength = text.length() - Iterators.getLast(Newlines.lineOffsetIterator(text));
return state.withColumn(state.column() + firstLineLength)
int lastLineStart = Iterators.getLast(Newlines.lineOffsetIterator(text));
int lastLineLength = text.length() - lastLineStart;
// rewrite() indents every line after the first to state.column(), so after a comment that spans lines the
// column is the length of its last line. Adding state.column() to it as well doubled the column for each
// further comment on that line.
int column = lastLineStart == 0 ? state.column() + lastLineLength : lastLineLength;
return state.withColumn(column)
.addNewLines(Iterators.size(Newlines.lineOffsetIterator(text)))
.withTokState(this, ImmutableTokState.of(text));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,21 @@ void formatsDeeplyNestedCallsQuickly() {

assertTimeoutPreemptively(Duration.ofSeconds(10), () -> formatter.formatSource(input));
}

@Test
void indentsCommentsThatShareALineLinearly() throws FormatterException {
// Commented-out code that had javadoc in it puts many multi-line comments on one line: "*//** javadoc *//*".
// Each comment's continuation lines are indented to the column where the comment starts, and the column after
// a comment used to be counted from where it started plus its last line, which already holds that indent. So
// every comment started twice as far right as the one before, and these sixteen came out as 4.3 MB.
StringBuilder input = new StringBuilder("/*\nclass X {\n");
for (int i = 0; i < 16; i++) {
input.append("\t*//** javadoc *//*\n\tpublic void foo(Bar bar) {}\n\n");
}
input.append("}*/\n");
Formatter formatter = Formatter.createFormatter(
JavaFormatterOptions.builder().style(Style.OJF).build());

assertThat(formatter.formatSource(input.toString()).length()).isLessThan(10_000);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
class X {
*//** javadoc *//*
public void foo(Bar bar) {}

*//** javadoc *//*
public void foo(Bar bar) {}

*//** javadoc *//*
public void foo(Bar bar) {}

*//** javadoc *//*
public void foo(Bar bar) {}
}*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
class X {
*//** javadoc *//*
public void foo(Bar bar) {}

*//** javadoc *//*
public void foo(Bar bar) {}

*//** javadoc *//*
public void foo(Bar bar) {}

*//** javadoc *//*
public void foo(Bar bar) {}
}*/
Loading