Skip to content

Commit 2c6a47d

Browse files
committed
add implements vs extends differentiating in pretty-printing
1 parent a285b29 commit 2c6a47d

17 files changed

+75
-29
lines changed

lsif-semanticdb/src/main/java/com/sourcegraph/lsif_semanticdb/SignatureFormatter.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ private void formatClassSignature(ClassSignature classSignature) {
4949
classSignature.getParentsList().stream()
5050
.anyMatch(t -> t.getTypeRef().getSymbol().equals(ANNOTATION_SYMBOL));
5151

52+
boolean isEnum = has(Property.ENUM);
53+
5254
printKeyword(formatAccess());
53-
if (!has(Property.ENUM) && !isAnnotation) printKeyword(formatModifiers());
55+
if (!isEnum && !isAnnotation) printKeyword(formatModifiers());
5456

5557
switch (symbolInformation.getKind()) {
5658
case CLASS:
57-
if (has(Property.ENUM)) {
59+
if (isEnum) {
5860
printKeyword("enum");
5961
} else {
6062
printKeyword("class");
@@ -78,19 +80,63 @@ private void formatClassSignature(ClassSignature classSignature) {
7880
.collect(Collectors.joining(", ", "<", ">")));
7981
}
8082

83+
boolean hasSuperClass = !classSignature.getParentsList().contains(OBJECT_TYPE_REF);
84+
8185
List<Type> nonSyntheticParents =
8286
classSignature.getParentsList().stream()
8387
.filter(parent -> !parent.equals(OBJECT_TYPE_REF))
8488
.filter(parent -> !parent.getTypeRef().getSymbol().equals(ENUM_SYMBOL))
8589
.filter(parent -> !parent.getTypeRef().getSymbol().equals(ANNOTATION_SYMBOL))
8690
.collect(Collectors.toList());
87-
// TODO: extends vs implements
88-
if (!nonSyntheticParents.isEmpty()) {
89-
printKeyword(" extends");
9091

91-
String parents =
92-
nonSyntheticParents.stream().map(this::formatType).collect(Collectors.joining(", "));
93-
s.append(parents);
92+
if (nonSyntheticParents.isEmpty()) return;
93+
94+
// Determine which parents from ClassSignature.parents are classes or interfaces so we know to
95+
// use
96+
// 'extends' or 'implements'.
97+
// The logic is as follows:
98+
// 1. If the symbol has type CLASS, there will always be at least 1 parent. For enums, this is
99+
// java/lang/Enum#, otherwise it is java/lang/Object# if no superclass is specified.
100+
// Therefore, if the parents list contains java/lang/Object# type or the symbol is an enum,
101+
// then no superclass was defined and all parents are interfaces and we must print 'implements'
102+
// followed by all superinterfaces.
103+
// Else if it is not an enum and the list of non-synthetic parents is non empty, a superclass
104+
// was specified and we must print it with the 'extends' keyword prefixed, followed by
105+
// 'implements' and all superinterfaces, if any.
106+
// 2. If the symbol has type INTERFACE, then any defined parents must also be interfaces, so if
107+
// the list of non-synthetic parents is not empty, print 'implements' and all superinterfaces.
108+
switch (symbolInformation.getKind()) {
109+
case CLASS:
110+
// if no superclass or is an enum, every non synthetic parent is an interface
111+
if (isEnum || !hasSuperClass) {
112+
printKeyword(" implements");
113+
114+
String superInterfaces =
115+
nonSyntheticParents.stream().map(this::formatType).collect(Collectors.joining(", "));
116+
s.append(superInterfaces);
117+
} else {
118+
// else if has a superclass and is not an enum
119+
printKeyword(" extends");
120+
s.append(formatType(nonSyntheticParents.get(0)));
121+
122+
String superInterfaces =
123+
nonSyntheticParents.stream()
124+
.skip(1)
125+
.map(this::formatType)
126+
.collect(Collectors.joining(", "));
127+
if (!superInterfaces.isEmpty()) {
128+
printKeyword(" implements");
129+
s.append(superInterfaces);
130+
}
131+
}
132+
break;
133+
case INTERFACE:
134+
// can only extend other interfaces
135+
printKeyword(" extends");
136+
137+
String superInterfaces =
138+
nonSyntheticParents.stream().map(this::formatType).collect(Collectors.joining(", "));
139+
s.append(superInterfaces);
94140
}
95141
}
96142

tests/snapshots/src/main/generated/com/airbnb/epoxy/BaseEpoxyAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
// ^^^^^^^^^^^^ reference androidx/recyclerview/widget/RecyclerView#
6161

6262
public abstract class BaseEpoxyAdapter
63-
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BaseEpoxyAdapter# public abstract class BaseEpoxyAdapter extends unresolved_type, unresolved_type
63+
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BaseEpoxyAdapter# public abstract class BaseEpoxyAdapter extends unresolved_type implements unresolved_type
6464
extends RecyclerView.Adapter<EpoxyViewHolder>
6565
// ^^^^^^^^^^^^ reference RecyclerView/
6666
// ^^^^^^^ reference RecyclerView/Adapter#

tests/snapshots/src/main/generated/com/airbnb/epoxy/BoundViewHolders.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
@SuppressWarnings("WeakerAccess")
2424
//^^^^^^^^^^^^^^^ reference java/lang/SuppressWarnings#
2525
public class BoundViewHolders implements Iterable<EpoxyViewHolder> {
26-
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders# public class BoundViewHolders extends Iterable<EpoxyViewHolder>
26+
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders# public class BoundViewHolders implements Iterable<EpoxyViewHolder>
2727
// ^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#`<init>`(). public BoundViewHolders()
2828
// ^^^^^^^^ reference java/lang/Iterable#
2929
// ^^^^^^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyViewHolder#
@@ -104,7 +104,7 @@ public EpoxyViewHolder getHolderForModel(EpoxyModel<?> model) {
104104
}
105105

106106
private class HolderIterator implements Iterator<EpoxyViewHolder> {
107-
// ^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#HolderIterator# private class HolderIterator extends Iterator<EpoxyViewHolder>
107+
// ^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#HolderIterator# private class HolderIterator implements Iterator<EpoxyViewHolder>
108108
// ^^^^^^^^^^^^^^ definition com/airbnb/epoxy/BoundViewHolders#HolderIterator#`<init>`(). private HolderIterator()
109109
// ^^^^^^^^ reference java/util/Iterator#
110110
// ^^^^^^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyViewHolder#

tests/snapshots/src/main/generated/com/airbnb/epoxy/DebugTimer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// ^^^ reference android/util/Log#
77

88
class DebugTimer implements Timer {
9-
// ^^^^^^^^^^ definition com/airbnb/epoxy/DebugTimer# class DebugTimer extends Timer
9+
// ^^^^^^^^^^ definition com/airbnb/epoxy/DebugTimer# class DebugTimer implements Timer
1010
// ^^^^^ reference com/airbnb/epoxy/Timer#
1111

1212
private final String tag;

tests/snapshots/src/main/generated/com/airbnb/epoxy/EpoxyController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
* accurate.
113113
*/
114114
public abstract class EpoxyController implements ModelCollector, StickyHeaderCallbacks {
115-
// ^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyController# public abstract class EpoxyController extends unresolved_type, unresolved_type
115+
// ^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyController# public abstract class EpoxyController implements unresolved_type, unresolved_type
116116
// ^^^^^^^^^^^^^^ reference _root_/
117117
// ^^^^^^^^^^^^^^^^^^^^^ reference _root_/
118118

tests/snapshots/src/main/generated/com/airbnb/epoxy/EpoxyControllerAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
// ^^^^^^^^^^^^ reference androidx/recyclerview/widget/RecyclerView#
5757

5858
public final class EpoxyControllerAdapter extends BaseEpoxyAdapter implements ResultCallback {
59-
// ^^^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyControllerAdapter# public final class EpoxyControllerAdapter extends BaseEpoxyAdapter, ResultCallback
59+
// ^^^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyControllerAdapter# public final class EpoxyControllerAdapter extends BaseEpoxyAdapter implements ResultCallback
6060
// ^^^^^^^^^^^^^^^^ reference com/airbnb/epoxy/BaseEpoxyAdapter#
6161
// ^^^^^^^^^^^^^^ reference com/airbnb/epoxy/AsyncEpoxyDiffer#ResultCallback#
6262
private final NotifyBlocker notifyBlocker = new NotifyBlocker();

tests/snapshots/src/main/generated/com/airbnb/epoxy/EpoxyModelTouchCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* your own {@link ItemTouchHelper} if you need extra flexibility or customization.
4040
*/
4141
public abstract class EpoxyModelTouchCallback<T extends EpoxyModel>
42-
// ^^^^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyModelTouchCallback# public abstract class EpoxyModelTouchCallback<T extends EpoxyModel> extends EpoxyTouchHelperCallback, EpoxyDragCallback<T>, EpoxySwipeCallback<T>
42+
// ^^^^^^^^^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyModelTouchCallback# public abstract class EpoxyModelTouchCallback<T extends EpoxyModel> extends EpoxyTouchHelperCallback implements EpoxyDragCallback<T>, EpoxySwipeCallback<T>
4343
// ^ definition com/airbnb/epoxy/EpoxyModelTouchCallback#[T] T extends EpoxyModel
4444
// ^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyModel#
4545
extends EpoxyTouchHelperCallback implements EpoxyDragCallback<T>, EpoxySwipeCallback<T> {

tests/snapshots/src/main/generated/com/airbnb/epoxy/EpoxyTouchHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ public void clearView(U model, View itemView) {
544544
}
545545

546546
public abstract static class DragCallbacks<T extends EpoxyModel>
547-
// ^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyTouchHelper#DragCallbacks# public abstract static class DragCallbacks<T extends EpoxyModel> extends EpoxyDragCallback<T>
547+
// ^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyTouchHelper#DragCallbacks# public abstract static class DragCallbacks<T extends EpoxyModel> implements EpoxyDragCallback<T>
548548
// ^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyTouchHelper#DragCallbacks#`<init>`(). public DragCallbacks()
549549
// ^ definition com/airbnb/epoxy/EpoxyTouchHelper#DragCallbacks#[T] T extends EpoxyModel
550550
// ^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyModel#
@@ -1043,7 +1043,7 @@ public void clearView(U model, View itemView) {
10431043
}
10441044

10451045
public abstract static class SwipeCallbacks<T extends EpoxyModel>
1046-
// ^^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyTouchHelper#SwipeCallbacks# public abstract static class SwipeCallbacks<T extends EpoxyModel> extends EpoxySwipeCallback<T>
1046+
// ^^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyTouchHelper#SwipeCallbacks# public abstract static class SwipeCallbacks<T extends EpoxyModel> implements EpoxySwipeCallback<T>
10471047
// ^^^^^^^^^^^^^^ definition com/airbnb/epoxy/EpoxyTouchHelper#SwipeCallbacks#`<init>`(). public SwipeCallbacks()
10481048
// ^ definition com/airbnb/epoxy/EpoxyTouchHelper#SwipeCallbacks#[T] T extends EpoxyModel
10491049
// ^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyModel#

tests/snapshots/src/main/generated/com/airbnb/epoxy/HandlerExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* same as the handler's thread.
2828
*/
2929
class HandlerExecutor implements Executor {
30-
// ^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/HandlerExecutor# class HandlerExecutor extends Executor
30+
// ^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/HandlerExecutor# class HandlerExecutor implements Executor
3131
// ^^^^^^^^ reference java/util/concurrent/Executor#
3232
final Handler handler;
3333
// ^^^^^^^ reference _root_/

tests/snapshots/src/main/generated/com/airbnb/epoxy/ModelList.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public Iterator<EpoxyModel<?>> iterator() {
414414
* the parent methods so that the proper notifications are done.
415415
*/
416416
private class Itr implements Iterator<EpoxyModel<?>> {
417-
// ^^^ definition com/airbnb/epoxy/ModelList#Itr# private class Itr extends Iterator<EpoxyModel<?>>
417+
// ^^^ definition com/airbnb/epoxy/ModelList#Itr# private class Itr implements Iterator<EpoxyModel<?>>
418418
// ^^^ definition com/airbnb/epoxy/ModelList#Itr#`<init>`(). private Itr()
419419
// ^^^^^^^^ reference java/util/Iterator#
420420
// ^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyModel#
@@ -537,7 +537,7 @@ public ListIterator<EpoxyModel<?>> listIterator(int index) {
537537
* notifications are done.
538538
*/
539539
private class ListItr extends Itr implements ListIterator<EpoxyModel<?>> {
540-
// ^^^^^^^ definition com/airbnb/epoxy/ModelList#ListItr# private class ListItr extends Itr, ListIterator<EpoxyModel<?>>
540+
// ^^^^^^^ definition com/airbnb/epoxy/ModelList#ListItr# private class ListItr extends Itr implements ListIterator<EpoxyModel<?>>
541541
// ^^^ reference com/airbnb/epoxy/ModelList#Itr#
542542
// ^^^^^^^^^^^^ reference java/util/ListIterator#
543543
// ^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyModel#
@@ -713,7 +713,7 @@ private static class SubList extends AbstractList<EpoxyModel<?>> {
713713
// ^^^^ definition com/airbnb/epoxy/ModelList#SubList#size. private int size
714714

715715
private static final class SubListIterator implements ListIterator<EpoxyModel<?>> {
716-
// ^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/ModelList#SubList#SubListIterator# private static final class SubListIterator extends ListIterator<EpoxyModel<?>>
716+
// ^^^^^^^^^^^^^^^ definition com/airbnb/epoxy/ModelList#SubList#SubListIterator# private static final class SubListIterator implements ListIterator<EpoxyModel<?>>
717717
// ^^^^^^^^^^^^ reference java/util/ListIterator#
718718
// ^^^^^^^^^^ reference com/airbnb/epoxy/EpoxyModel#
719719
private final SubList subList;

0 commit comments

Comments
 (0)