Skip to content

Commit

Permalink
Support multiline tokens in GJF-core
Browse files Browse the repository at this point in the history
Cursory perf measurements show no detectable cost from scanning every token. If one should be discovered later, we can add guards to only scan potentially multiline tokens.

PiperOrigin-RevId: 620285981
  • Loading branch information
nreid260 authored and google-java-format Team committed Apr 1, 2024
1 parent 3167818 commit 5cd320c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions core/src/main/java/com/google/googlejavaformat/Doc.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ boolean isReal() {
private final Indent plusIndentCommentsBefore;
private final Optional<Indent> breakAndIndentTrailingComment;

private Input.Tok tok() {
return token.getTok();
}

private Token(
Input.Token token,
RealOrImaginary realOrImaginary,
Expand Down Expand Up @@ -465,7 +469,8 @@ public void add(DocBuilder builder) {

@Override
int computeWidth() {
return token.getTok().length();
int idx = Newlines.firstBreak(tok().getOriginalText());
return (idx >= 0) ? MAX_LINE_WIDTH : tok().length();
}

@Override
Expand All @@ -480,8 +485,7 @@ Range<Integer> computeRange() {

@Override
public State computeBreaks(CommentsHelper commentsHelper, int maxWidth, State state) {
String text = token.getTok().getOriginalText();
return state.withColumn(state.column + text.length());
return state.withColumn(state.column + computeWidth());
}

@Override
Expand Down

0 comments on commit 5cd320c

Please sign in to comment.