google / google/google-java-format

Trouble with character ranges

Đang mở
#433 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Java
Star
6.2k
Fork
937
Merge trung bình
6 phút
Pull request đã merge (30 ngày)
3

Mô tả

Please have a look at the following two unit tests which should both work, but only the second does:

@Test
public void testCharaterRangesFormatWithAnnotation() throws Exception {
final String text = "package foo; public class Bar {\n" +
" @SuppressWarnings(\"unchecked\")\n" +
" // some comment \n" +
" public void doSomething(String arg) { System.out.println(\"Hello World\"); } public boolean doSomethingElse(Object obj) { return false; } }";

final String expected = "package foo;\n" +
"\n" +
"public class Bar {\n" +
// START -- this should not be formatted
" @SuppressWarnings(\"unchecked\")\n" +
" // some comment \n" +
" public void doSomething(String arg) { System.out.println(\"Hello World\"); }\n" +
// END -- this should not be formatted
"\n" +
" public boolean doSomethingElse(Object obj) {\n" +
" return false;\n" +
" }\n" +
"}\n" +
"";

Collection> characterRanges = new ArrayList<>();
characterRanges.add(Range.open(0, 66));
characterRanges.add(Range.open(171, 234));

Formatter formatter = new Formatter(JavaFormatterOptions.builder().style(JavaFormatterOptions.Style.GOOGLE).build());

String actual = formatter.formatSource(text, characterRanges);
Assert.assertEquals("Formatting should change the code", expected, actual);
}

@Test
public void testCharaterRangesFormatWithoutAnnotation() throws Exception {
final String text = "package foo; public class Bar {\n" +
" @SuppressWarnings(\"unchecked\")\n" +
" // some comment \n" +
" public void doSomething(String arg) { System.out.println(\"Hello World\"); } public boolean doSomethingElse(Object obj) { return false; } }";

final String expected = "package foo;\n" +
"\n" +
"public class Bar {\n" +
" @SuppressWarnings(\"unchecked\")\n" +
// START -- this should not be formatted
" // some comment \n" +
" public void doSomething(String arg) { System.out.println(\"Hello World\"); }\n" +
// END -- this should not be formatted
"\n" +
" public boolean doSomethingElse(Object obj) {\n" +
" return false;\n" +
" }\n" +
"}\n" +
"";

Collection> characterRanges = new ArrayList<>();
characterRanges.add(Range.open(0, 31));
characterRanges.add(Range.open(171, 234));

Formatter formatter = new Formatter(JavaFormatterOptions.builder().style(JavaFormatterOptions.Style.GOOGLE).build());

String actual = formatter.formatSource(text, characterRanges);
Assert.assertEquals("Formatting should change the code", expected, actual);
}

When I run those tests I get this as an result:

Failures:
FormatterTest.testCharaterRangesFormatWithAnnotation:58 Formatting should change the code expected:<...ings("unchecked")
[ // some comment
public void doSomething(String arg) {] System.out.println(...> but was:<...ings("unchecked")
[// some comment
public void doSomething(String arg) {
] System.out.println(...>

Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

It seems there is an issue when there is an annotation before a comment or a method, it seems the formatter is formatting code outside of the given character range.

Regards,
Fabian

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.