Formatter places opening """ of text block on same line as ? / : operators in ternary expressions
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 68/100
Hướng nghiên cứu
Bắt đầu bằng cách định dạng tệp được cung cấp /tmp/TernaryBug.java bằng /tmp/google-java-format.jar và so sánh diff được hiển thị. Theo dõi cách formatter xử lý các biểu thức ba ngôi chứa các khối văn bản, sau đó xác minh rằng mỗi dấu ngoặc kép ba lần mở là nội dung duy nhất không phải khoảng trắng trên dòng đó và rằng đầu ra mong đợi không còn xung đột với kiểm tra TextBlockGoogleStyleFormatting của Checkstyle.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
google-java-format formats ternary expressions containing text blocks such that the opening """ is placed on the same line as the ? and : operators. This appears to violate Google Java Style Guide Section 4.8.9, which states:
The opening """ of a text block is always on a new line. That line may either follow the same indentation rules as other constructs, or it may have no indentation at all (so it starts at the left margin). The closing """ is on a new line with the same indentation as the opening """, and may be followed on the same line by further code. Each line of text in the text block is indented at least as much as the opening and closing """. (If a line is indented further, then the string literal defined by the text block will have space at the start of that line.)
The key requirement is that the """ must be on a new line, but the formatter places an operator (? or :) immediately before it on the same line, meaning the """ is no longer on the new line.
Reproduction
vivek@Viveks-MacBook-Air checkstyle % cat > /tmp/TernaryBug.java << 'EOF'
class TernaryBug {
boolean flag = true;
String a = flag
? """
yes
"""
: """
no
""";
}
EOF
vivek@Viveks-MacBook-Air checkstyle % java -jar /tmp/google-java-format.jar /tmp/TernaryBug.java > /tmp/TernaryBug_formatted.java
diff -u /tmp/TernaryBug.java /tmp/TernaryBug_formatted.java
--- /tmp/TernaryBug.java 2026-04-18 18:43:21
+++ /tmp/TernaryBug_formatted.java 2026-04-18 18:43:26
@@ -1,11 +1,12 @@
class TernaryBug {
- boolean flag = true;
+ boolean flag = true;
- String a = flag
- ? """
+ String a =
+ flag
+ ? """
yes
"""
- : """
+ : """
no
""";
}
vivek@Viveks-MacBook-Air checkstyle % cat > /tmp/TernaryBug.java << 'EOF'
class TernaryBug {
boolean flag = true;
String d = flag ?
"""
yes
"""
:
"""
no
""";
}
EOF
vivek@Viveks-MacBook-Air checkstyle % java -jar /tmp/google-java-format.jar /tmp/TernaryBug.java > /tmp/TernaryBug_formatted.java
diff -u /tmp/TernaryBug.java /tmp/TernaryBug_formatted.java
--- /tmp/TernaryBug.java 2026-04-18 18:45:14
+++ /tmp/TernaryBug_formatted.java 2026-04-18 18:45:19
@@ -1,12 +1,12 @@
class TernaryBug {
- boolean flag = true;
+ boolean flag = true;
- String d = flag ?
- """
- yes
- """
- :
- """
- no
- """;
+ String d =
+ flag
+ ? """
+ yes
+ """
+ : """
+ no
+ """;
}
Expected Behavior
The formatter should place the opening"""on its own line, separate from the ? and : operators. For example:
class TernaryBug {
boolean flag = true;
String a =
flag
?
"""
yes
"""
:
"""
no
""";
}
Or any equivalent layout where """ is the only non-whitespace content on its starting line.
Why This Matters
Checkstyle's TextBlockGoogleStyleFormatting check enforces the Google Java Style Guide rule that the opening """ must be on its own line. The current formatter output directly conflicts with this check, code formatted by google-java-format produces Checkstyle violations. This creates a situation where developers cannot simultaneously satisfy both the formatter and the style checker, which are both intended to enforce the same Google Java Style Guide.
- Ngôn ngữ chính
- Java
- Star
- 6.2k
- Fork
- 936
- Merge trung bình
- 6 phút
- Pull request đã merge (30 ngày)
- 3
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của google/google-java-format
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
google/google-java-format#1094 · 1 bình luận ·
-
Complementary Teamups Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
google/google-java-format#1450 · 1 reaction ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
google/google-java-format#1439 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 62/100
google/google-java-format#1436 · 2 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
google/google-java-format#1428 · 3 bình luận ·
Tất cả issue của google/google-java-format
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
elastic/gradle-plugins#157 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
cryptomator/hub#497 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
johanhaleby/occurrent#1120 ·