Skip to content

Commit 010cef1

Browse files
cushonabashev
authored andcommitted
Treat a single upper case letter as a type name in a dotted chain
A dotted name is broken after its type-name prefix, and the prefix is found from the case of each segment: "com.example.Foo.bar" is a package, a class and a member. A single upper case letter such as Android's R counted as an all-caps constant, so in "com.some.extremely.verbose.pkg.name.R.string.some_resource" no prefix was found and the name broke at every dot, one segment per line. This ports google/google-java-format#731 by Liam Miller-Cushon: a single upper case letter is UpperCamelCase, so the chain keeps "com.some.extremely.verbose.pkg.name.R.string" together and breaks before the last segment. The golden b26306390 and the two TypeNameClassifierTest assertions come from upstream, with the expected output in this project's style. The 15,747 files of the JDK 21 sources format exactly as before.
1 parent 746fb60 commit 010cef1

4 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/TypeNameClassifier.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ static JavaCaseFormat from(String name) {
164164
hasLowercase |= Character.isLowerCase(c);
165165
}
166166
if (firstUppercase) {
167-
return hasLowercase ? UPPER_CAMEL : UPPERCASE;
167+
// A single upper case letter is a type name: Android's R, and generic type parameters such as T.
168+
return (hasLowercase || name.length() == 1) ? UPPER_CAMEL : UPPERCASE;
168169
} else {
169170
return hasUppercase ? LOWER_CAMEL : LOWERCASE;
170171
}

‎open-java-format/src/test/java/com/palantir/javaformat/java/TypeNameClassifierTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void caseFormat() throws Exception {
4141
assertThat(JavaCaseFormat.from("a_$")).isEqualTo(JavaCaseFormat.LOWERCASE);
4242
assertThat(JavaCaseFormat.from("_")).isEqualTo(JavaCaseFormat.LOWERCASE);
4343
assertThat(JavaCaseFormat.from("_A")).isEqualTo(JavaCaseFormat.UPPERCASE);
44+
assertThat(JavaCaseFormat.from("A")).isEqualTo(JavaCaseFormat.UPPER_CAMEL);
4445
}
4546

4647
private static Optional<Integer> getPrefix(String qualifiedName) {
@@ -60,6 +61,7 @@ public void typePrefixLength() {
6061
assertThat(getPrefix("ClassName.CONST")).hasValue(1);
6162
assertThat(getPrefix("ClassName.varName")).hasValue(1);
6263
assertThat(getPrefix("ClassName.Inner.varName")).hasValue(2);
64+
assertThat(getPrefix("com.R.foo")).hasValue(2);
6365
}
6466

6567
@Test
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class B26306390 {
2+
int resourceId = com.some.extremely.verbose.pkg.name.R.string.some_extremely_long_resource_identifier_that_exceeds_the_column_limit;
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class B26306390 {
2+
int resourceId = com.some.extremely.verbose.pkg.name.R.string
3+
.some_extremely_long_resource_identifier_that_exceeds_the_column_limit;
4+
}

0 commit comments

Comments
 (0)