Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format type annotation as part of the type, not part of the modifiers list #805

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public PartialInlineability partialInlineability() {
* But you can still get this (see test B20128760):
*
* <pre>
* Stream<ItemKey> itemIdsStream = stream(members).flatMap(m -> m.getFieldValues().entrySet().stream()
* {@code Stream<ItemKey> itemIdsStream = stream(members).flatMap(m -> m.getFieldValues().entrySet().stream()}
* .filter(...)
* .map(...));
* </pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -323,6 +324,28 @@ public Optional<String> peekToken(int skip) {
return idx < tokens.size() ? Optional.of(tokens.get(idx).getTok().getOriginalText()) : Optional.empty();
}

/**
* Returns the {@link Input.Tok}s starting at the current source position, which are satisfied by
* the given predicate.
*/
public ImmutableList<Tok> peekTokens(int startPosition, Predicate<Input.Tok> predicate) {
ImmutableList<? extends Input.Token> tokens = input.getTokens();
Preconditions.checkState(
tokens.get(tokenI).getTok().getPosition() == startPosition,
"Expected the current token to be at position %s, found: %s",
startPosition,
tokens.get(tokenI));
ImmutableList.Builder<Tok> result = ImmutableList.builder();
for (int idx = tokenI; idx < tokens.size(); idx++) {
Tok tok = tokens.get(idx).getTok();
if (!predicate.apply(tok)) {
break;
}
result.add(tok);
}
return result.build();
}

/**
* Emit an optional token iff it exists on the input. This is used to emit tokens whose existence has been lost in
* the AST.
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.palantir.javaformat.Op;
import com.palantir.javaformat.OpsBuilder;
import com.palantir.javaformat.OpsBuilder.BlankLineWanted;
import com.palantir.javaformat.java.JavaInputAstVisitor;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.BindingPatternTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.CaseTree;
Expand Down Expand Up @@ -114,7 +114,8 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {

private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) {
if (modifiers != null) {
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
List<AnnotationTree> annotations = visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty());
visitAnnotations(annotations, BreakOrNot.NO, BreakOrNot.YES);
}
scan(type, null);
builder.breakOp(" ");
Expand Down Expand Up @@ -162,11 +163,9 @@ public Void visitClass(ClassTree tree, Void unused) {

public void visitRecordDeclaration(ClassTree node) {
sync(node);
List<Op> breaks = visitModifiers(
node.getModifiers(), Direction.VERTICAL, /* declarationAnnotationBreak= */ Optional.empty());
typeDeclarationModifiers(node.getModifiers());
Verify.verify(node.getExtendsClause() == null);
boolean hasSuperInterfaceTypes = !node.getImplementsClause().isEmpty();
builder.addAll(breaks);
token("record");
builder.space();
visit(node.getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ public void importsFixedIfRequested() throws FormatterException {
+ "import java.util.List;\n"
+ "import javax.annotations.Nullable;\n\n"
+ "public class ExampleTest {\n"
+ " @Nullable\n"
+ " List<?> xs;\n"
+ " @Nullable List<?> xs;\n"
+ "}\n";
assertThat(output).isEqualTo(expect);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ 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 @@ -93,12 +87,7 @@ 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,9 +1,7 @@
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,16 +1,10 @@
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
Expand Up @@ -44,11 +44,11 @@ class B24702438 {

void f(
int a,
@Nullable @Deprecated ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Nullable @Deprecated ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Nullable @Deprecated ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Deprecated @Nullable ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Deprecated @Nullable ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Deprecated @Nullable ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
int c) {}
void g(
@Nullable @Deprecated ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Nullable @Deprecated ImmutableList<String> veryVeryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooong) {}
@Deprecated @Nullable ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Deprecated @Nullable ImmutableList<String> veryVeryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooong) {}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
class B24702438 {

@Inject
int x;

@Inject
int y;
@Inject int x;

@Inject
int z;
@Inject int y;

@Inject
int x;

@Inject
int y;
@Inject int z;

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

// this is a comment

Expand Down Expand Up @@ -47,17 +39,17 @@ class B24702438 {

void f(
int a,
@Nullable @Deprecated
@Deprecated @Nullable
ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Nullable @Deprecated
@Deprecated @Nullable
ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Nullable @Deprecated
@Deprecated @Nullable
ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
int c) {}

void g(
@Nullable @Deprecated
@Deprecated @Nullable
ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Nullable @Deprecated
@Deprecated @Nullable
ImmutableList<String> veryVeryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooong) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@ 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 @@ -38,8 +32,7 @@ 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,7 +1,6 @@
class I13 {

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

@Override
public void m() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import org.checkerframework.checker.nullness.qual.Nullable;

class TypeAnnotations {

@Deprecated
public @Nullable Object foo() {}

public @Deprecated Object foo() {}

@Nullable Foo handle() {
@Nullable Bar bar = bar();
try (@Nullable Baz baz = baz()) {}
}

Foo(
@Nullable Bar //
param1, //
Baz //
param2) {}

void g(
@Deprecated @Nullable ImmutableList<String> veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Deprecated @Nullable ImmutableList<String> veryVeryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooong) {}

@Deprecated @Nullable TypeAnnotations() {}

enum Foo {
@Nullable
BAR;
}

@Nullable @Nullable Object doubleTrouble() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import org.checkerframework.checker.nullness.qual.Nullable;

class TypeAnnotations {

@Deprecated
public @Nullable Object foo() {}

public @Deprecated Object foo() {}

@Nullable Foo handle() {
@Nullable Bar bar = bar();
try (@Nullable Baz baz = baz()) {}
}

Foo(
@Nullable Bar //
param1, //
Baz //
param2) {}

void g(
@Deprecated
@Nullable ImmutableList<String>
veryVeryLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
@Deprecated
@Nullable ImmutableList<String>
veryVeryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooong) {}

@Deprecated
@Nullable TypeAnnotations() {}

enum Foo {
@Nullable BAR;
}

@Nullable @Nullable Object doubleTrouble() {}
}