Skip to content

Commit 16e72a1

Browse files
cushongoogle-java-format Team
authored andcommitted
Treat single-character upper case identifiers as UpperCamelCase
as a concession to Android `R` classes. PiperOrigin-RevId: 424878859
1 parent 0198230 commit 16e72a1

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static JavaCaseFormat from(String name) {
164164
hasLowercase |= Character.isLowerCase(c);
165165
}
166166
if (firstUppercase) {
167-
return hasLowercase ? UPPER_CAMEL : UPPERCASE;
167+
return (hasLowercase || name.length() == 1) ? UPPER_CAMEL : UPPERCASE;
168168
} else {
169169
return hasUppercase ? LOWER_CAMEL : LOWERCASE;
170170
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public void caseFormat() throws Exception {
4343
assertThat(JavaCaseFormat.from("a_$")).isEqualTo(JavaCaseFormat.LOWERCASE);
4444
assertThat(JavaCaseFormat.from("_")).isEqualTo(JavaCaseFormat.LOWERCASE);
4545
assertThat(JavaCaseFormat.from("_A")).isEqualTo(JavaCaseFormat.UPPERCASE);
46+
assertThat(JavaCaseFormat.from("A")).isEqualTo(JavaCaseFormat.UPPER_CAMEL);
4647
}
4748

4849
private static Optional<Integer> getPrefix(String qualifiedName) {
@@ -62,6 +63,7 @@ public void typePrefixLength() {
6263
assertThat(getPrefix("ClassName.CONST")).hasValue(1);
6364
assertThat(getPrefix("ClassName.varName")).hasValue(1);
6465
assertThat(getPrefix("ClassName.Inner.varName")).hasValue(2);
66+
assertThat(getPrefix("com.R.foo")).hasValue(2);
6567
}
6668

6769
@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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class B26306390 {
2+
int resourceId =
3+
com.some.extremely.verbose.pkg.name.R.string
4+
.some_extremely_long_resource_identifier_that_exceeds_the_column_limit;
5+
}

0 commit comments

Comments
 (0)