Skip to content

Commit

Permalink
extracted methods to improve readability
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jun 27, 2023
1 parent 0828002 commit 3835cac
Showing 1 changed file with 63 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,43 +63,59 @@ public InlayHintsCollector getCollectorFor(@NotNull PsiFile psiFile,
return new FactoryInlayHintsCollector(editor) {
@Override
public boolean collect(@NotNull PsiElement psiElement, @NotNull Editor editor, @NotNull InlayHintsSink inlayHintsSink) {
Document document = editor.getDocument();
try {
URI docURI = LSPIJUtils.toUri(editor.getDocument());
if (docURI != null) {
Range viewPortRange = getViewPortRange(editor);
InlayHintParams param = new InlayHintParams(new TextDocumentIdentifier(docURI.toString()), viewPortRange);
BlockingDeque<Pair<InlayHint, LanguageServer>> pairs = new LinkedBlockingDeque<>();
List<Pair<Integer, Pair<InlayHint, LanguageServer>>> inlayhints = new ArrayList<>();
CompletableFuture<Void> future = LanguageServiceAccessor.getInstance(psiElement.getProject())
.getLanguageServers(editor.getDocument(), capabilities -> capabilities.getInlayHintProvider() != null)
.thenComposeAsync(languageServers -> CompletableFuture.allOf(languageServers.stream()
.map(languageServer -> languageServer.getSecond().getTextDocumentService().inlayHint(param)
.thenAcceptAsync(inlayHints -> {
// textDocument/codeLens may return null
if (inlayHints != null) {
inlayHints.stream().filter(Objects::nonNull)
.forEach(inlayHint -> pairs.add(new Pair(inlayHint, languageServer.getSecond())));
}
}))
.toArray(CompletableFuture[]::new)));
while (!future.isDone() || !pairs.isEmpty()) {
ProgressManager.checkCanceled();
Pair<InlayHint, LanguageServer> pair = pairs.poll(25, TimeUnit.MILLISECONDS);
if (pair != null) {
int offset = LSPIJUtils.toOffset(pair.getFirst().getPosition(), editor.getDocument());
inlayhints.add(Pair.create(offset, pair));
}
}
Map<Integer, List<Pair<Integer, Pair<InlayHint, LanguageServer>>>> elements = inlayhints.stream().collect(Collectors.groupingBy(p -> p.first));
elements.forEach((offset, list) -> inlayHintsSink.addInlineElement(offset, false,
toPresentation(editor, offset, list, getFactory()), false));
URI docURI = LSPIJUtils.toUri(document);
if (docURI == null) {
return false;
}
Range viewPortRange = getViewPortRange(editor);
InlayHintParams param = new InlayHintParams(new TextDocumentIdentifier(docURI.toString()), viewPortRange);
BlockingDeque<Pair<InlayHint, LanguageServer>> pairs = new LinkedBlockingDeque<>();
CompletableFuture<Void> future = collect(psiElement.getProject(), document, param, pairs);
List<Pair<Integer, Pair<InlayHint, LanguageServer>>> inlayHints = createInlayHints(document, pairs, future);
Map<Integer, List<Pair<Integer, Pair<InlayHint, LanguageServer>>>> elements = inlayHints.stream().collect(Collectors.groupingBy(p -> p.first));
elements.forEach((offset, list) ->
inlayHintsSink.addInlineElement(offset, false, toPresentation(editor, list, getFactory()), false));
} catch (InterruptedException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
Thread.currentThread().interrupt();
}
return false;
}

@NotNull
private List<Pair<Integer, Pair<InlayHint, LanguageServer>>> createInlayHints(
@NotNull Document document,
BlockingDeque<Pair<InlayHint, LanguageServer>> pairs,
CompletableFuture<Void> future)
throws InterruptedException {
List<Pair<Integer, Pair<InlayHint, LanguageServer>>> inlayHints = new ArrayList<>();
while (!future.isDone() || !pairs.isEmpty()) {
ProgressManager.checkCanceled();
Pair<InlayHint, LanguageServer> pair = pairs.poll(25, TimeUnit.MILLISECONDS);
if (pair != null) {
int offset = LSPIJUtils.toOffset(pair.getFirst().getPosition(), document);
inlayHints.add(Pair.create(offset, pair));
}
}
return inlayHints;
}

private CompletableFuture<Void> collect(@NotNull Project project, @NotNull Document document, InlayHintParams param, BlockingDeque<Pair<InlayHint, LanguageServer>> pairs) {
return LanguageServiceAccessor.getInstance(project)
.getLanguageServers(document, capabilities -> capabilities.getInlayHintProvider() != null)
.thenComposeAsync(languageServers -> CompletableFuture.allOf(languageServers.stream()
.map(languageServer -> languageServer.getSecond().getTextDocumentService().inlayHint(param)
.thenAcceptAsync(inlayHints -> {
// textDocument/codeLens may return null
if (inlayHints != null) {
inlayHints.stream().filter(Objects::nonNull)
.forEach(inlayHint -> pairs.add(new Pair(inlayHint, languageServer.getSecond())));
}
}))
.toArray(CompletableFuture[]::new)));
}
};
}

Expand All @@ -114,7 +130,7 @@ private static Range getViewPortRange(Editor editor) {
return new Range(start, end);
}

private InlayPresentation toPresentation(Editor editor, int offset,
private InlayPresentation toPresentation(Editor editor,
List<Pair<Integer, Pair<InlayHint, LanguageServer>>> elements,
PresentationFactory factory) {
List<InlayPresentation> presentations = new ArrayList<>();
Expand All @@ -125,17 +141,7 @@ private InlayPresentation toPresentation(Editor editor, int offset,
} else {
int index = 0;
for (InlayHintLabelPart part : label.getRight()) {
InlayPresentation text = factory.smallText(part.getValue());
if (!hasCommand(part)) {
// No command, create a simple text inlay hint
presentations.add(text);
} else {
// InlayHintLabelPart defines a Command, create a clickable inlay hint
int finalIndex = index;
text = factory.referenceOnHover(text, (event, translated) -> {
executeClientCommand(p.second.second, p.second.first, finalIndex, (Component) event.getSource(), editor.getProject());
});
}
InlayPresentation text = createInlayPresentation(editor.getProject(), factory, presentations, p, index, part);
if (part.getTooltip() != null && part.getTooltip().isLeft()) {
text = factory.withTooltip(part.getTooltip().getLeft(), text);
}
Expand All @@ -147,6 +153,22 @@ private InlayPresentation toPresentation(Editor editor, int offset,
return factory.roundWithBackground(new SequencePresentation(presentations));
}

@NotNull
private InlayPresentation createInlayPresentation(Project project, PresentationFactory factory, List<InlayPresentation> presentations, Pair<Integer, Pair<InlayHint, LanguageServer>> p, int index, InlayHintLabelPart part) {
InlayPresentation text = factory.smallText(part.getValue());
if (!hasCommand(part)) {
// No command, create a simple text inlay hint
presentations.add(text);
} else {
// InlayHintLabelPart defines a Command, create a clickable inlay hint
int finalIndex = index;
text = factory.referenceOnHover(text, (event, translated) -> {
executeClientCommand(p.second.second, p.second.first, finalIndex, (Component) event.getSource(), project);
});
}
return text;
}

private static boolean hasCommand(InlayHintLabelPart part) {
Command command = part.getCommand();
return (command != null && command.getCommand() != null && !command.getCommand().isEmpty());
Expand Down

0 comments on commit 3835cac

Please sign in to comment.