|
19 | 19 | import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull; |
20 | 20 |
|
21 | 21 | import java.util.List; |
| 22 | +import java.util.NoSuchElementException; |
22 | 23 | import java.util.Objects; |
23 | 24 | import java.util.concurrent.CompletableFuture; |
24 | 25 | import java.util.concurrent.ExecutionException; |
|
51 | 52 | import org.eclipse.lsp4j.HoverParams; |
52 | 53 | import org.eclipse.lsp4j.MarkedString; |
53 | 54 | import org.eclipse.lsp4j.MarkupContent; |
| 55 | +import org.eclipse.lsp4j.Range; |
54 | 56 | import org.eclipse.lsp4j.ServerCapabilities; |
55 | 57 | import org.eclipse.lsp4j.jsonrpc.messages.Either; |
56 | 58 | import org.eclipse.swt.widgets.Shell; |
@@ -156,42 +158,27 @@ public class LSPTextHover implements ITextHover, ITextHoverExtension, ITextHover |
156 | 158 | } |
157 | 159 |
|
158 | 160 | try { |
159 | | - final var oneHoverAtLeast = new boolean[] { false }; |
160 | | - final var regionStartOffset = new int[] { 0 }; |
161 | | - final var regionEndOffset = new int[] { document.getLength() }; |
162 | 161 | // 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() // |
164 | 163 | .filter(Objects::nonNull) // |
165 | 164 | .map(Hover::getRange) // |
166 | 165 | .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$ |
184 | 175 | } catch (InterruptedException e) { |
185 | 176 | Thread.currentThread().interrupt(); |
186 | 177 | } 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. |
189 | 179 | } |
190 | 180 |
|
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); |
195 | 182 | } |
196 | 183 |
|
197 | 184 | private static Region computeHeuristicRegion(final IDocument document, final int offset) { |
|
0 commit comments