|
22 | 22 | import com.google.common.collect.ImmutableSortedSet;
|
23 | 23 | import com.google.googlejavaformat.Newlines;
|
24 | 24 | import com.google.googlejavaformat.java.JavaInput.Tok;
|
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
25 | 27 | import org.openjdk.tools.javac.parser.Tokens.TokenKind;
|
26 | 28 |
|
27 | 29 | /** Orders imports in Java source code. */
|
@@ -115,27 +117,26 @@ private String reorderImports() throws FormatterException {
|
115 | 117 | throw new FormatterException("Imports not contiguous (perhaps a comment separates them?)");
|
116 | 118 | }
|
117 | 119 |
|
118 |
| - // Add back the text from after the point where we stopped tokenizing. |
119 |
| - String tail; |
120 |
| - if (toks.isEmpty()) { |
121 |
| - tail = ""; |
122 |
| - } else { |
123 |
| - Tok lastTok = getLast(toks); |
124 |
| - int tailStart = lastTok.getPosition() + lastTok.length(); |
125 |
| - tail = text.substring(tailStart); |
126 |
| - } |
127 |
| - |
128 | 120 | StringBuilder result = new StringBuilder();
|
129 | 121 | result.append(
|
130 | 122 | CharMatcher.whitespace().trimTrailingFrom(tokString(0, unindentedFirstImportStart)));
|
131 | 123 | if (result.length() > 0) {
|
132 | 124 | result.append(lineSeparator).append(lineSeparator);
|
133 | 125 | }
|
134 | 126 | result.append(reorderedImportsString(imports.imports));
|
135 |
| - result.append(lineSeparator); |
136 |
| - result.append( |
137 |
| - CharMatcher.whitespace().trimLeadingFrom(tokString(afterLastImport, toks.size()))); |
138 |
| - result.append(tail); |
| 127 | + |
| 128 | + List<String> tail = new ArrayList<>(); |
| 129 | + tail.add(CharMatcher.whitespace().trimLeadingFrom(tokString(afterLastImport, toks.size()))); |
| 130 | + if (!toks.isEmpty()) { |
| 131 | + Tok lastTok = getLast(toks); |
| 132 | + int tailStart = lastTok.getPosition() + lastTok.length(); |
| 133 | + tail.add(text.substring(tailStart)); |
| 134 | + } |
| 135 | + if (tail.stream().anyMatch(s -> !s.isEmpty())) { |
| 136 | + result.append(lineSeparator); |
| 137 | + tail.forEach(result::append); |
| 138 | + } |
| 139 | + |
139 | 140 | return result.toString();
|
140 | 141 | }
|
141 | 142 |
|
|
0 commit comments