9
9
package org .eclipse .lsp4e .operations .inlayhint ;
10
10
11
11
import java .util .List ;
12
+ import java .util .Objects ;
12
13
import java .util .Optional ;
13
14
import java .util .concurrent .CompletableFuture ;
14
15
import java .util .function .Consumer ;
48
49
public class LSPLineContentCodeMining extends LineContentCodeMining {
49
50
50
51
private InlayHint inlayHint ;
51
- private final LanguageServerWrapper wrapper ;
52
- private IDocument document ;
52
+ private final @ NonNull LanguageServerWrapper wrapper ;
53
+ private final @ NonNull IDocument document ;
53
54
54
55
private Point location ;
55
56
private FontData [] fontData ;
56
57
57
- public LSPLineContentCodeMining (InlayHint inlayHint , IDocument document , LanguageServerWrapper languageServerWrapper ,
58
+ public LSPLineContentCodeMining (@ NonNull InlayHint inlayHint , @ NonNull IDocument document , @ NonNull LanguageServerWrapper languageServerWrapper ,
58
59
InlayHintProvider provider ) throws BadLocationException {
59
60
super (toPosition (inlayHint .getPosition (), document ), provider );
60
61
this .inlayHint = inlayHint ;
@@ -122,15 +123,19 @@ private static org.eclipse.jface.text.Position toPosition(Position position, IDo
122
123
123
124
@ Override
124
125
public final Consumer <MouseEvent > getAction () {
125
- return me -> {
126
- String title = getLabel ();
127
- if (title != null && !title .isEmpty ()) {
128
- findLabelPart (me ).map (InlayHintLabelPart ::getCommand ).ifPresent (command -> {
126
+ return inlayHint .getLabel ().map (l -> null , r -> labelPartAction (r ));
127
+ }
128
+
129
+ private Consumer <MouseEvent > labelPartAction (List <InlayHintLabelPart > labelParts ) {
130
+ String title = getLabel ();
131
+ if (title != null && !title .isEmpty () && labelParts .stream ().map (InlayHintLabelPart ::getCommand ).anyMatch (Objects ::nonNull )) {
132
+ return me -> {
133
+ findLabelPart (me , labelParts ).map (InlayHintLabelPart ::getCommand ).filter (Objects ::nonNull ).ifPresent (command -> {
129
134
ExecuteCommandOptions provider = wrapper .getServerCapabilities ().getExecuteCommandProvider ();
130
135
String commandId = command .getCommand ();
131
136
if (provider != null && provider .getCommands ().contains (commandId )) {
132
137
LanguageServers .forDocument (document ).computeAll ((w , ls ) -> {
133
- if (w == this . wrapper ) {
138
+ if (w == wrapper ) {
134
139
return ls .getWorkspaceService ()
135
140
.executeCommand (new ExecuteCommandParams (commandId , command .getArguments ()));
136
141
}
@@ -140,48 +145,46 @@ public final Consumer<MouseEvent> getAction() {
140
145
CommandExecutor .executeCommandClientSide (command , document );
141
146
}
142
147
});
143
- }
144
- };
148
+ };
149
+ }
150
+ return null ;
145
151
}
146
152
147
- private Optional <InlayHintLabelPart > findLabelPart (MouseEvent me ) {
148
- if (inlayHint .getLabel ().isRight ()) {
149
- List <InlayHintLabelPart > labelParts = inlayHint .getLabel ().getRight ();
150
- if (labelParts .size () == 1 ) {
151
- return Optional .of (labelParts .get (0 ));
152
- }
153
- if (location != null && fontData != null ) {
154
- Point relativeLocation = new Point (me .x - location .x , me .y - location .y );
155
- Display display = Display .getCurrent ();
156
- Image image = null ;
157
- GC gc = null ;
158
- Font font = null ;
159
- try {
160
- image = new Image (display , 1 , 1 );
161
- gc = new GC (image );
162
- font = new Font (display , fontData );
163
- gc .setFont (font );
164
- Point origin = new Point (0 , 0 );
165
- for (InlayHintLabelPart labelPart : labelParts ) {
166
- Point size = gc .stringExtent (labelPart .getValue ());
167
- Rectangle bounds = new Rectangle (origin .x , origin .y , size .x , size .y );
168
- if (bounds .contains (relativeLocation )) {
169
- return Optional .of (labelPart );
170
- } else {
171
- origin .x += size .x ;
172
- }
173
- }
174
- } finally {
175
- if (font != null && !font .isDisposed ()) {
176
- font .dispose ();
177
- }
178
- if (gc != null && !gc .isDisposed ()) {
179
- gc .dispose ();
180
- }
181
- if (image != null && !image .isDisposed ()) {
182
- image .dispose ();
153
+ private Optional <InlayHintLabelPart > findLabelPart (MouseEvent me , List <InlayHintLabelPart > labelParts ) {
154
+ if (labelParts .size () == 1 ) {
155
+ return Optional .of (labelParts .get (0 ));
156
+ }
157
+ if (location != null && fontData != null ) {
158
+ Point relativeLocation = new Point (me .x - location .x , me .y - location .y );
159
+ Display display = Display .getCurrent ();
160
+ Image image = null ;
161
+ GC gc = null ;
162
+ Font font = null ;
163
+ try {
164
+ image = new Image (display , 1 , 1 );
165
+ gc = new GC (image );
166
+ font = new Font (display , fontData );
167
+ gc .setFont (font );
168
+ Point origin = new Point (0 , 0 );
169
+ for (InlayHintLabelPart labelPart : labelParts ) {
170
+ Point size = gc .stringExtent (labelPart .getValue ());
171
+ Rectangle bounds = new Rectangle (origin .x , origin .y , size .x , size .y );
172
+ if (bounds .contains (relativeLocation )) {
173
+ return Optional .of (labelPart );
174
+ } else {
175
+ origin .x += size .x ;
183
176
}
184
177
}
178
+ } finally {
179
+ if (font != null && !font .isDisposed ()) {
180
+ font .dispose ();
181
+ }
182
+ if (gc != null && !gc .isDisposed ()) {
183
+ gc .dispose ();
184
+ }
185
+ if (image != null && !image .isDisposed ()) {
186
+ image .dispose ();
187
+ }
185
188
}
186
189
}
187
190
return Optional .empty ();
0 commit comments