diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java index 7b5eb841f..b5290ab10 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInput.java @@ -573,9 +573,11 @@ Range characterRangeToTokenRange(Range characterRange) if (characterRange.upperEndpoint() > text.length()) { throw new FormatterException( String.format( - "error: invalid length %d, offset + length (%d) is outside the file", + "error: invalid offset (%d) or length (%d); offset + length (%d) > file length (%d)", + characterRange.lowerEndpoint(), characterRange.upperEndpoint() - characterRange.lowerEndpoint(), - characterRange.upperEndpoint())); + characterRange.upperEndpoint(), + text.length())); } // empty range stands for "format the line under the cursor" Range nonEmptyRange = diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java index 9bbca496a..b0d7b4030 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java @@ -64,7 +64,7 @@ public void testFormatAosp() throws Exception { Path tmpdir = testFolder.newFolder().toPath(); Path path = tmpdir.resolve("A.java"); - Files.write(path, input.getBytes(UTF_8)); + Files.writeString(path, input); StringWriter out = new StringWriter(); StringWriter err = new StringWriter(); @@ -116,7 +116,7 @@ public void testFormatLengthUpToEOF() throws Exception { Path tmpdir = testFolder.newFolder().toPath(); Path path = tmpdir.resolve("Foo.java"); - Files.write(path, input.getBytes(UTF_8)); + Files.writeString(path, input); StringWriter out = new StringWriter(); StringWriter err = new StringWriter(); @@ -133,7 +133,7 @@ public void testFormatLengthOutOfRange() throws Exception { Path tmpdir = testFolder.newFolder().toPath(); Path path = tmpdir.resolve("Foo.java"); - Files.write(path, input.getBytes(UTF_8)); + Files.writeString(path, input); StringWriter out = new StringWriter(); StringWriter err = new StringWriter(); @@ -142,7 +142,25 @@ public void testFormatLengthOutOfRange() throws Exception { String[] args = {"--offset", "0", "--length", "9999", path.toString()}; assertThat(main.format(args)).isEqualTo(1); assertThat(err.toString()) - .contains("error: invalid length 9999, offset + length (9999) is outside the file"); + .contains("error: invalid offset (0) or length (9999); offset + length (9999)"); + } + + @Test + public void testFormatOffsetOutOfRange() throws Exception { + String input = "class Foo{}\n"; + + Path tmpdir = testFolder.newFolder().toPath(); + Path path = tmpdir.resolve("Foo.java"); + Files.writeString(path, input); + + StringWriter out = new StringWriter(); + StringWriter err = new StringWriter(); + + Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in); + String[] args = {"--offset", "9998", "--length", "1", path.toString()}; + assertThat(main.format(args)).isEqualTo(1); + assertThat(err.toString()) + .contains("error: invalid offset (9998) or length (1); offset + length (9999)"); } @Test @@ -303,7 +321,7 @@ private void importOrdering(String sortArg, String outputResourceName) String inputResourceName = "com/google/googlejavaformat/java/testimports/A.input"; String input = getResource(inputResourceName); String expectedOutput = getResource(outputResourceName); - Files.write(path, input.getBytes(UTF_8)); + Files.writeString(path, input); StringWriter out = new StringWriter(); StringWriter err = new StringWriter();