Skip to content

Commit 823d7c9

Browse files
java-team-github-botgoogle-java-format Team
authored and
google-java-format Team
committed
Print all known values when an invalid range is given.
For easier root cause analysis when an invalid request is passed. PiperOrigin-RevId: 613110312
1 parent f20d393 commit 823d7c9

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaInput.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,11 @@ Range<Integer> characterRangeToTokenRange(Range<Integer> characterRange)
573573
if (characterRange.upperEndpoint() > text.length()) {
574574
throw new FormatterException(
575575
String.format(
576-
"error: invalid length %d, offset + length (%d) is outside the file",
576+
"error: invalid offset (%d) or length (%d); offset + length (%d) > file length (%d)",
577+
characterRange.lowerEndpoint(),
577578
characterRange.upperEndpoint() - characterRange.lowerEndpoint(),
578-
characterRange.upperEndpoint()));
579+
characterRange.upperEndpoint(),
580+
text.length()));
579581
}
580582
// empty range stands for "format the line under the cursor"
581583
Range<Integer> nonEmptyRange =

core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void testFormatAosp() throws Exception {
6464

6565
Path tmpdir = testFolder.newFolder().toPath();
6666
Path path = tmpdir.resolve("A.java");
67-
Files.write(path, input.getBytes(UTF_8));
67+
Files.writeString(path, input);
6868

6969
StringWriter out = new StringWriter();
7070
StringWriter err = new StringWriter();
@@ -116,7 +116,7 @@ public void testFormatLengthUpToEOF() throws Exception {
116116

117117
Path tmpdir = testFolder.newFolder().toPath();
118118
Path path = tmpdir.resolve("Foo.java");
119-
Files.write(path, input.getBytes(UTF_8));
119+
Files.writeString(path, input);
120120

121121
StringWriter out = new StringWriter();
122122
StringWriter err = new StringWriter();
@@ -133,7 +133,7 @@ public void testFormatLengthOutOfRange() throws Exception {
133133

134134
Path tmpdir = testFolder.newFolder().toPath();
135135
Path path = tmpdir.resolve("Foo.java");
136-
Files.write(path, input.getBytes(UTF_8));
136+
Files.writeString(path, input);
137137

138138
StringWriter out = new StringWriter();
139139
StringWriter err = new StringWriter();
@@ -142,7 +142,25 @@ public void testFormatLengthOutOfRange() throws Exception {
142142
String[] args = {"--offset", "0", "--length", "9999", path.toString()};
143143
assertThat(main.format(args)).isEqualTo(1);
144144
assertThat(err.toString())
145-
.contains("error: invalid length 9999, offset + length (9999) is outside the file");
145+
.contains("error: invalid offset (0) or length (9999); offset + length (9999)");
146+
}
147+
148+
@Test
149+
public void testFormatOffsetOutOfRange() throws Exception {
150+
String input = "class Foo{}\n";
151+
152+
Path tmpdir = testFolder.newFolder().toPath();
153+
Path path = tmpdir.resolve("Foo.java");
154+
Files.writeString(path, input);
155+
156+
StringWriter out = new StringWriter();
157+
StringWriter err = new StringWriter();
158+
159+
Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in);
160+
String[] args = {"--offset", "9998", "--length", "1", path.toString()};
161+
assertThat(main.format(args)).isEqualTo(1);
162+
assertThat(err.toString())
163+
.contains("error: invalid offset (9998) or length (1); offset + length (9999)");
146164
}
147165

148166
@Test
@@ -303,7 +321,7 @@ private void importOrdering(String sortArg, String outputResourceName)
303321
String inputResourceName = "com/google/googlejavaformat/java/testimports/A.input";
304322
String input = getResource(inputResourceName);
305323
String expectedOutput = getResource(outputResourceName);
306-
Files.write(path, input.getBytes(UTF_8));
324+
Files.writeString(path, input);
307325

308326
StringWriter out = new StringWriter();
309327
StringWriter err = new StringWriter();

0 commit comments

Comments
 (0)