6
6
import com .intellij .lang .annotation .Annotator ;
7
7
import com .intellij .openapi .util .IconLoader ;
8
8
import com .intellij .openapi .util .NotNullLazyValue ;
9
+ import com .intellij .psi .PsiClass ;
9
10
import com .intellij .psi .PsiElement ;
10
11
import com .intellij .psi .PsiIdentifier ;
11
12
import com .intellij .psi .PsiMethodCallExpression ;
13
+ import com .intellij .psi .util .PsiTypesUtil ;
12
14
import org .axonframework .intellij .ide .plugin .handler .AnnotationTypes ;
13
15
import org .axonframework .intellij .ide .plugin .handler .EventHandler ;
14
16
import org .axonframework .intellij .ide .plugin .handler .HandlerProviderManager ;
@@ -33,53 +35,96 @@ public class AxonGutterAnnotator implements Annotator {
33
35
34
36
@ Override
35
37
public void annotate (@ NotNull final PsiElement element , @ NotNull AnnotationHolder holder ) {
36
- if (!(element instanceof PsiMethodCallExpression || element instanceof PsiIdentifier )) {
37
- return ;
38
- }
39
-
40
38
final PublisherProviderManager publisherManager = PublisherProviderManager .getInstance (element .getProject ());
41
39
final HandlerProviderManager handlerManager = HandlerProviderManager .getInstance (element .getProject ());
42
- final EventPublisher publisher = publisherManager .resolveEventPublisher (element );
43
- final EventHandler handler = handlerManager .resolveEventHandler (element .getContext ());
44
- if (publisher != null ) {
45
- NotNullLazyValue <Collection <? extends PsiElement >> targetResolver = new NotNullLazyValue <Collection <? extends PsiElement >>() {
46
- @ NotNull
47
- @ Override
48
- protected Collection <? extends PsiElement > compute () {
49
- Set <EventHandler > handlers = handlerManager .getRepository ().findHandlers (publisher .getPublishedType ());
50
- Set <PsiEventHandlerWrapper > destinations = new TreeSet <PsiEventHandlerWrapper >();
51
- for (EventHandler eventHandler : handlers ) {
52
- PsiElement elementForAnnotation = eventHandler .getElementForAnnotation ();
53
- if (elementForAnnotation .isValid ()) {
54
- destinations .add (new PsiEventHandlerWrapper (elementForAnnotation , eventHandler ));
55
- }
56
- }
57
- return destinations ;
40
+ if (element instanceof PsiClass ) {
41
+ tryAnnotateEventClass (holder , (PsiClass ) element , publisherManager , handlerManager );
42
+ tryAnnotateCommandClass (holder , (PsiClass ) element , handlerManager );
43
+ } else if ((element instanceof PsiMethodCallExpression || element instanceof PsiIdentifier )) {
44
+ final EventPublisher publisher = publisherManager .resolveEventPublisher (element );
45
+ final EventHandler handler = handlerManager .resolveEventHandler (element .getContext ());
46
+ if (publisher != null ) {
47
+ NotNullLazyValue <Collection <? extends PsiElement >> targetResolver =
48
+ createNotNullLazyValueForHandlers (handlerManager .getRepository ().findHandlers (publisher .getPublishedType ()));
49
+ Annotation gutterIconForPublisher = createGutterIconForEventHandlers (element , holder , targetResolver , handlerManager );
50
+ if (targetResolver .getValue ().isEmpty ()) {
51
+ addCreateEventHandlerQuickFixes (publisher , gutterIconForPublisher );
58
52
}
59
- };
60
- Annotation gutterIconForPublisher = createGutterIconForPublisher (element , holder , targetResolver , handlerManager );
61
- if (targetResolver .getValue ().isEmpty ()) {
62
- addCreateEventHandlerQuickFixes (publisher , gutterIconForPublisher );
53
+ }
54
+
55
+ if (handler != null ) {
56
+ Collection <EventPublisher > publishers = publisherManager .getRepository ()
57
+ .getPublishersFor (handler .getHandledType ());
58
+ createGutterIconForEventPublishers (
59
+ handler .getElementForAnnotation (),
60
+ holder ,
61
+ createNotNullLazyValueForPublishers (publishers ));
63
62
}
64
63
}
64
+ }
65
65
66
- if (handler != null ) {
67
- createGutterIconForHandler (handler .getElementForAnnotation (), holder , new NotNullLazyValue <Collection <? extends PsiElement >>() {
68
- @ NotNull
69
- @ Override
70
- protected Collection <? extends PsiElement > compute () {
71
- Collection <EventPublisher > publishers = publisherManager .getRepository ()
72
- .getPublishersFor (handler .getHandledType ());
73
- Collection <PsiElement > publishLocations = new ArrayList <PsiElement >();
74
- for (EventPublisher eventPublisher : publishers ) {
75
- PsiElement psiElement = eventPublisher .getPsiElement ();
76
- if (psiElement .isValid ()) {
77
- publishLocations .add (psiElement );
78
- }
66
+ private NotNullLazyValue <Collection <? extends PsiElement >> createNotNullLazyValueForPublishers (final Collection <EventPublisher > publishers ) {
67
+ return new NotNullLazyValue <Collection <? extends PsiElement >>() {
68
+ @ NotNull
69
+ @ Override
70
+ protected Collection <? extends PsiElement > compute () {
71
+ Collection <PsiElement > publishLocations = new ArrayList <PsiElement >();
72
+ for (EventPublisher eventPublisher : publishers ) {
73
+ PsiElement psiElement = eventPublisher .getPsiElement ();
74
+ if (psiElement .isValid ()) {
75
+ publishLocations .add (psiElement );
79
76
}
80
- return publishLocations ;
81
77
}
82
- });
78
+ return publishLocations ;
79
+ }
80
+ };
81
+ }
82
+
83
+ private NotNullLazyValue <Collection <? extends PsiElement >> createNotNullLazyValueForHandlers (final Set <EventHandler > handlers ) {
84
+ return new NotNullLazyValue <Collection <? extends PsiElement >>() {
85
+ @ NotNull
86
+ @ Override
87
+ protected Collection <? extends PsiElement > compute () {
88
+ Set <PsiEventHandlerWrapper > destinations = new TreeSet <PsiEventHandlerWrapper >();
89
+ for (EventHandler eventHandler : handlers ) {
90
+ PsiElement elementForAnnotation = eventHandler .getElementForAnnotation ();
91
+ if (elementForAnnotation .isValid ()) {
92
+ destinations .add (new PsiEventHandlerWrapper (elementForAnnotation , eventHandler ));
93
+ }
94
+ }
95
+ return destinations ;
96
+ }
97
+ };
98
+ }
99
+
100
+ private void tryAnnotateCommandClass (AnnotationHolder holder , PsiClass classElement , HandlerProviderManager handlerManager ) {
101
+ Set <EventHandler > handlers =
102
+ handlerManager .getRepository ().findCommandHandlers (PsiTypesUtil .getClassType (classElement ));
103
+ if (!handlers .isEmpty ()) {
104
+ createGutterIconForCommandHandlers (
105
+ classElement .getNameIdentifier (),
106
+ holder ,
107
+ createNotNullLazyValueForHandlers (handlers ));
108
+ }
109
+ }
110
+
111
+ private void tryAnnotateEventClass (AnnotationHolder holder , PsiClass classElement ,
112
+ PublisherProviderManager publisherManager , HandlerProviderManager handlerManager ) {
113
+ Set <EventPublisher > publishers = publisherManager .getRepository ().getPublishersFor (PsiTypesUtil .getClassType (classElement ));
114
+ if (!publishers .isEmpty ()) {
115
+ createGutterIconForEventPublishers (
116
+ classElement .getNameIdentifier (),
117
+ holder ,
118
+ createNotNullLazyValueForPublishers (publishers ));
119
+ }
120
+ Set <EventHandler > handlers =
121
+ handlerManager .getRepository ().findEventHandlers (PsiTypesUtil .getClassType (classElement ));
122
+ if (!handlers .isEmpty ()) {
123
+ createGutterIconForEventHandlers (
124
+ classElement .getNameIdentifier (),
125
+ holder ,
126
+ createNotNullLazyValueForHandlers (handlers ),
127
+ handlerManager );
83
128
}
84
129
}
85
130
@@ -89,8 +134,8 @@ private void addCreateEventHandlerQuickFixes(EventPublisher publisher, Annotatio
89
134
gutterIconForPublisher .registerFix (new CreateEventHandlerQuickfix (publisher .getPublishedType (), AnnotationTypes .SAGA_EVENT_HANDLER ));
90
135
}
91
136
92
- private static Annotation createGutterIconForHandler (PsiElement psiElement , AnnotationHolder holder ,
93
- NotNullLazyValue <Collection <? extends PsiElement >> targetResolver ) {
137
+ private static Annotation createGutterIconForEventPublishers (PsiElement psiElement , AnnotationHolder holder ,
138
+ NotNullLazyValue <Collection <? extends PsiElement >> targetResolver ) {
94
139
return NavigationGutterIconBuilder .create (AxonIconIn )
95
140
.setEmptyPopupText ("No publishers found for this event" )
96
141
.setTargets (targetResolver )
@@ -100,8 +145,20 @@ private static Annotation createGutterIconForHandler(PsiElement psiElement, Anno
100
145
.install (holder , psiElement );
101
146
}
102
147
103
- private static Annotation createGutterIconForPublisher (PsiElement psiElement , AnnotationHolder holder ,
104
- NotNullLazyValue <Collection <? extends PsiElement >> targetResolver , HandlerProviderManager handlerManager ) {
148
+ private static Annotation createGutterIconForCommandHandlers (PsiElement psiElement , AnnotationHolder holder ,
149
+ NotNullLazyValue <Collection <? extends PsiElement >> targetResolver ) {
150
+ return NavigationGutterIconBuilder .create (AxonIconIn )
151
+ .setEmptyPopupText ("No handlers found for this command" )
152
+ .setTargets (targetResolver )
153
+ .setPopupTitle ("Command handlers" )
154
+ .setCellRenderer (new ContainingMethodCellRenderer ())
155
+ .setTooltipText ("Navigate to the handlers" )
156
+ .install (holder , psiElement );
157
+ }
158
+
159
+ private static Annotation createGutterIconForEventHandlers (PsiElement psiElement , AnnotationHolder holder ,
160
+ NotNullLazyValue <Collection <? extends PsiElement >> targetResolver ,
161
+ HandlerProviderManager handlerManager ) {
105
162
return NavigationGutterIconBuilder .create (AxonIconOut )
106
163
.setEmptyPopupText ("No handlers found for this event" )
107
164
.setTargets (targetResolver )
0 commit comments