Skip to content

Commit 6ff3d03

Browse files
authored
Do not return a consumer<MouseEvent> if the code mining has no action. (#1026)
According to ICodeMining.getAction(), null should be returned if there is no action to be executed when the mining is clicked. When several InlayHintLabelParts are used, we cannot be sure if there is an action or not without knowing the actual MouseEvent. However, if no InlayHint has an action, we can be sure there is none even without knowing the MouseEvent. For these cases, the code returns null now.
1 parent 51e467d commit 6ff3d03

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/inlayhint/InlayHintProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ private CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(@NonNu
6363
}
6464
}
6565

66-
private LSPLineContentCodeMining toCodeMining(IDocument document, LanguageServerWrapper languageServerWrapper,
67-
InlayHint inlayHint) {
66+
private LSPLineContentCodeMining toCodeMining(@NonNull IDocument document, @NonNull LanguageServerWrapper languageServerWrapper,
67+
@NonNull InlayHint inlayHint) {
6868
try {
6969
return new LSPLineContentCodeMining(inlayHint, document, languageServerWrapper, InlayHintProvider.this);
7070
} catch (BadLocationException e) {

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/inlayhint/LSPLineContentCodeMining.java

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package org.eclipse.lsp4e.operations.inlayhint;
1010

1111
import java.util.List;
12+
import java.util.Objects;
1213
import java.util.Optional;
1314
import java.util.concurrent.CompletableFuture;
1415
import java.util.function.Consumer;
@@ -48,13 +49,13 @@
4849
public class LSPLineContentCodeMining extends LineContentCodeMining {
4950

5051
private InlayHint inlayHint;
51-
private final LanguageServerWrapper wrapper;
52-
private IDocument document;
52+
private final @NonNull LanguageServerWrapper wrapper;
53+
private final @NonNull IDocument document;
5354

5455
private Point location;
5556
private FontData[] fontData;
5657

57-
public LSPLineContentCodeMining(InlayHint inlayHint, IDocument document, LanguageServerWrapper languageServerWrapper,
58+
public LSPLineContentCodeMining(@NonNull InlayHint inlayHint, @NonNull IDocument document, @NonNull LanguageServerWrapper languageServerWrapper,
5859
InlayHintProvider provider) throws BadLocationException {
5960
super(toPosition(inlayHint.getPosition(), document), provider);
6061
this.inlayHint = inlayHint;
@@ -122,15 +123,19 @@ private static org.eclipse.jface.text.Position toPosition(Position position, IDo
122123

123124
@Override
124125
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 -> {
129134
ExecuteCommandOptions provider = wrapper.getServerCapabilities().getExecuteCommandProvider();
130135
String commandId = command.getCommand();
131136
if (provider != null && provider.getCommands().contains(commandId)) {
132137
LanguageServers.forDocument(document).computeAll((w, ls) -> {
133-
if (w == this.wrapper) {
138+
if (w == wrapper) {
134139
return ls.getWorkspaceService()
135140
.executeCommand(new ExecuteCommandParams(commandId, command.getArguments()));
136141
}
@@ -140,48 +145,46 @@ public final Consumer<MouseEvent> getAction() {
140145
CommandExecutor.executeCommandClientSide(command, document);
141146
}
142147
});
143-
}
144-
};
148+
};
149+
}
150+
return null;
145151
}
146152

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;
183176
}
184177
}
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+
}
185188
}
186189
}
187190
return Optional.empty();

0 commit comments

Comments
 (0)