Skip to content

Commit

Permalink
Treat single-character upper case identifiers as UpperCamelCase
Browse files Browse the repository at this point in the history
as a concession to Android `R` classes.

PiperOrigin-RevId: 424878859
  • Loading branch information
cushon authored and google-java-format Team committed Jan 28, 2022
1 parent 0198230 commit 16e72a1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static JavaCaseFormat from(String name) {
hasLowercase |= Character.isLowerCase(c);
}
if (firstUppercase) {
return hasLowercase ? UPPER_CAMEL : UPPERCASE;
return (hasLowercase || name.length() == 1) ? UPPER_CAMEL : UPPERCASE;
} else {
return hasUppercase ? LOWER_CAMEL : LOWERCASE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void caseFormat() throws Exception {
assertThat(JavaCaseFormat.from("a_$")).isEqualTo(JavaCaseFormat.LOWERCASE);
assertThat(JavaCaseFormat.from("_")).isEqualTo(JavaCaseFormat.LOWERCASE);
assertThat(JavaCaseFormat.from("_A")).isEqualTo(JavaCaseFormat.UPPERCASE);
assertThat(JavaCaseFormat.from("A")).isEqualTo(JavaCaseFormat.UPPER_CAMEL);
}

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

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class B26306390 {
int resourceId = com.some.extremely.verbose.pkg.name.R.string.some_extremely_long_resource_identifier_that_exceeds_the_column_limit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class B26306390 {
int resourceId =
com.some.extremely.verbose.pkg.name.R.string
.some_extremely_long_resource_identifier_that_exceeds_the_column_limit;
}

0 comments on commit 16e72a1

Please sign in to comment.