Skip to content

Commit

Permalink
Don't inline annotations (#102)
Browse files Browse the repository at this point in the history
Never put field annotations on the same line as the field declaration.
  • Loading branch information
dansanduleac authored and bulldozer-bot[bot] committed Dec 6, 2019
1 parent 4e8670d commit 615b7a3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 24 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-102.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Never put field annotations on the same line as the field declaration.
links:
- https://github.com/palantir/palantir-java-format/pull/102
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ public Void visitExpressionStatement(ExpressionStatementTree node, Void unused)
@Override
public Void visitVariable(VariableTree node, Void unused) {
sync(node);
visitVariables(ImmutableList.of(node), DeclarationKind.NONE, fieldAnnotationDirection(node.getModifiers()));
visitVariables(ImmutableList.of(node), DeclarationKind.NONE, inlineAnnotationDirection(node.getModifiers()));
return null;
}

Expand Down Expand Up @@ -1863,7 +1863,7 @@ public Void visitTry(TryTree node, Void unused) {
VariableTree variableTree = (VariableTree) resource;
declareOne(
DeclarationKind.PARAMETER,
fieldAnnotationDirection(variableTree.getModifiers()),
inlineAnnotationDirection(variableTree.getModifiers()),
Optional.of(variableTree.getModifiers()),
variableTree.getType(),
/* name= */ variableTree.getName(),
Expand Down Expand Up @@ -3497,7 +3497,8 @@ void addBodyDeclarations(List<? extends Tree> bodyDeclarations, BracesOrNot brac
visitVariables(
variableFragments(it, bodyDeclaration),
DeclarationKind.FIELD,
fieldAnnotationDirection(((VariableTree) bodyDeclaration).getModifiers()));
// We always want field annotations to be vertical
Direction.VERTICAL);
} else {
scan(bodyDeclaration, null);
}
Expand Down Expand Up @@ -3584,10 +3585,10 @@ private Direction canLocalHaveHorizontalAnnotations(ModifiersTree modifiers) {
}

/**
* Should a field with a set of modifiers be declared with horizontal annotations? This is currently true if all
* annotations are marker annotations.
* Should a declaration with a set of modifiers be declared with horizontal annotations? This is currently true if
* all annotations are marker annotations.
*/
private Direction fieldAnnotationDirection(ModifiersTree modifiers) {
private Direction inlineAnnotationDirection(ModifiersTree modifiers) {
for (AnnotationTree annotation : modifiers.getAnnotations()) {
if (!annotation.getArguments().isEmpty()) {
return Direction.VERTICAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ public void importsFixedIfRequested() throws FormatterException {
+ "import java.util.List;\n"
+ "import javax.annotations.Nullable;\n\n"
+ "public class ExampleTest {\n"
+ " @Nullable List<?> xs;\n"
+ " @Nullable\n"
+ " List<?> xs;\n"
+ "}\n";
assertThat(output).isEqualTo(expect);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ class Test {
@Baz
void f() {}

@Foo @Bar @Baz static Object field;
@Foo
@Bar
@Baz
static Object field;

static @Foo @Bar @Baz Object field;

@Foo @Bar @Baz Object field;
@Foo
@Bar
@Baz
Object field;

@Foo(xs = 42)
@Bar
Expand Down Expand Up @@ -87,7 +93,12 @@ class Test {
@Deprecated
Object var;

@Deprecated @Deprecated @Deprecated @Deprecated @Deprecated Object var;
@Deprecated
@Deprecated
@Deprecated
@Deprecated
@Deprecated
Object var;

@Deprecated(x = 42)
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class B20577626 {
private @Mock GsaConfigFlags mGsaConfig;

@Foo @Bar private @Mock GsaConfigFlags mGsaConfig;
@Foo
@Bar
private @Mock GsaConfigFlags mGsaConfig;

@Foo
abstract @Bar void m() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
class B21465477 {

@Nullable private final String simpleFieldName;
@Nullable private final String shortFlagName;
@Nullable
private final String simpleFieldName;

@Nullable
private final String shortFlagName;

private final String containerClassName;
private final String type;
private final String doc;
private final DocLevel docLevel;
@Nullable private final String altName;

@Nullable
private final String altName;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
class B24702438 {

@Inject int x;
@Inject
int x;

@Inject
int y;

@Inject int y;
@Inject
int z;

@Inject int z;
@Inject
int x;

@Inject
int y;

@Inject int x;
@Inject int y;
@Inject int z;
@Inject
int z;

// this is a comment

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ class Test {

class Test {
final CreationMechanism creationMechanism;
@Nullable final String creationUserAgent;

@Nullable
final String creationUserAgent;

final ClientId clientId;
@Nullable final String creationUserAgentXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;

@Nullable
final String creationUserAgentXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;

final Token externalId;

{
Expand All @@ -32,7 +38,8 @@ class Test {

class Test {

@Nullable final String creationUserAgent;
@Nullable
final String creationUserAgent;

{
@Nullable final String creationUserAgent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class I13 {

@Nullable public int f;
@Nullable
public int f;

@Override
public void m() {}
Expand Down

0 comments on commit 615b7a3

Please sign in to comment.