google / google/google-java-format

Handle type annotations

Đang mở
#5 18 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Java
Star
6.2k
Fork
937
Merge trung bình
6 phút
Pull request đã merge (30 ngày)
3

Mô tả

We always want to format type annotations in-line:

```
@Nullable Object foo() {}
```

This is tricky in contexts where type annotations could be mixed together with declaration annotations, such as field, method, and variable declarations. There's no syntactic information to we can use to distinguish between `@NullableType Object foo() {}` and `@Deprecated Object foo() {}`, but we want to output the first annotation in-line, and the second vertically.

Currently, we output ambiguous annotations as declaration annotations. This is fine for Java 7, and has the advantage of never incorrectly formatting a declaration annotation as a type annotation.

Here are some possible heuristics we could use to support this case for Java 8:

1) If the annotation occurs before a regular modifier, it's probably a declaration annotation:

```
@Deprecated
static Object foo() {}
```

2) If the annotation occurs after regular modifiers, and it isn't on a void-returning method, it's probably a type annotation:

```
static @Nullable Object foo() {}
```

3) If the declaration isn't a void-returning method, and it has no regular modifiers, consider the existing formatting: type annotations are probably on the same line as the type, declaration annotations are probably vertical. (The risk here is that we'd preserve things like `@Override Object foo() {}`.)

4) If the declaration is named `@Override`, it's probably a declaration annotation. This would offset the disadvantage of (3), but it could be confusing if `@Override` behaves differently than other annotations.

5) An annotation with parameters is unlikely to be a type annotation, and should probably be formatted vertically.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.