Skip to content

Commit ff5efb6

Browse files
committed
refactor: simplify LSPTextHover#getHoverRegion
Also avoid an warning for a timeout getting the hover region, as the code handles this by creating an UI element with the corresponding feedback to the user.
1 parent 8784548 commit ff5efb6

1 file changed

Lines changed: 14 additions & 27 deletions

File tree

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/hover/LSPTextHover.java

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull;
2020

2121
import java.util.List;
22+
import java.util.NoSuchElementException;
2223
import java.util.Objects;
2324
import java.util.concurrent.CompletableFuture;
2425
import java.util.concurrent.ExecutionException;
@@ -51,6 +52,7 @@
5152
import org.eclipse.lsp4j.HoverParams;
5253
import org.eclipse.lsp4j.MarkedString;
5354
import org.eclipse.lsp4j.MarkupContent;
55+
import org.eclipse.lsp4j.Range;
5456
import org.eclipse.lsp4j.ServerCapabilities;
5557
import org.eclipse.lsp4j.jsonrpc.messages.Either;
5658
import org.eclipse.swt.widgets.Shell;
@@ -156,42 +158,27 @@ public class LSPTextHover implements ITextHover, ITextHoverExtension, ITextHover
156158
}
157159

158160
try {
159-
final var oneHoverAtLeast = new boolean[] { false };
160-
final var regionStartOffset = new int[] { 0 };
161-
final var regionEndOffset = new int[] { document.getLength() };
162161
// Wait shortly for hover region result, fallback to heuristics if LS is laggy
163-
castNonNull(this.request).get(GET_HOVER_REGION_TIMEOUT_MS, TimeUnit.MILLISECONDS).stream() //
162+
Range range = castNonNull(this.request).get(GET_HOVER_REGION_TIMEOUT_MS, TimeUnit.MILLISECONDS).stream() //
164163
.filter(Objects::nonNull) //
165164
.map(Hover::getRange) //
166165
.filter(Objects::nonNull) //
167-
.forEach(range -> {
168-
try {
169-
regionStartOffset[0] = Math.max(regionStartOffset[0],
170-
LSPEclipseUtils.toOffset(range.getStart(), document));
171-
regionEndOffset[0] = Math.min(regionEndOffset[0],
172-
LSPEclipseUtils.toOffset(range.getEnd(), document));
173-
oneHoverAtLeast[0] = true;
174-
} catch (BadLocationException e) {
175-
LanguageServerPlugin.logError(e);
176-
}
177-
});
178-
if (oneHoverAtLeast[0]) {
179-
this.lastRegion = new Region(regionStartOffset[0], regionEndOffset[0] - regionStartOffset[0]);
180-
return this.lastRegion;
181-
}
182-
} catch (ExecutionException e) {
183-
LanguageServerPlugin.logError(e);
166+
.reduce((first, second) -> second) //
167+
.get();
168+
int regionStartOffset = Math.max(0,
169+
LSPEclipseUtils.toOffset(range.getStart(), document));
170+
int regionEndOffset = Math.min(document.getLength(),
171+
LSPEclipseUtils.toOffset(range.getEnd(), document));
172+
return this.lastRegion = new Region(regionStartOffset, regionEndOffset - regionStartOffset);
173+
} catch (ExecutionException | NoSuchElementException | BadLocationException e) {
174+
LanguageServerPlugin.logError("Cannot get hover region for offset " + offset, e); //$NON-NLS-1$
184175
} catch (InterruptedException e) {
185176
Thread.currentThread().interrupt();
186177
} catch (TimeoutException e) {
187-
LanguageServerPlugin.logWarning(
188-
"Could not get hover region due to timeout after " + GET_HOVER_REGION_TIMEOUT_MS + " milliseconds"); //$NON-NLS-1$ //$NON-NLS-2$
178+
// Fallback to heuristic region without blocking.
189179
}
190180

191-
// Fallback to heuristic region without blocking.
192-
final Region heuristic = computeHeuristicRegion(document, offset);
193-
this.lastRegion = heuristic;
194-
return heuristic;
181+
return this.lastRegion = computeHeuristicRegion(document, offset);
195182
}
196183

197184
private static Region computeHeuristicRegion(final IDocument document, final int offset) {

0 commit comments

Comments
 (0)