Skip to content

Commit

Permalink
fix: Intellj Quick Documentation broken after error from pluggin (red…
Browse files Browse the repository at this point in the history
…hat-developer#938)

Fixes redhat-developer#938

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jun 9, 2023
1 parent ef75697 commit 67cc355
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 103 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

import com.intellij.lang.documentation.DocumentationProviderEx;
import com.intellij.lang.documentation.ExternalDocumentationHandler;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
Expand All @@ -36,11 +35,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.*;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
Expand All @@ -58,8 +53,10 @@ public class LSPTextHover extends DocumentationProviderEx implements ExternalDoc
private static final Parser PARSER = Parser.builder().build();
private static final HtmlRenderer RENDERER = HtmlRenderer.builder().build();

private static final Key<Integer> OFFSET_KEY = new Key<>(LSPTextHover.class.getName());

private PsiElement lastElement;
private int lastOffset = -1;
private int lastOffset = -1;
private CompletableFuture<List<Hover>> lspRequest;

public LSPTextHover() {
Expand Down Expand Up @@ -99,7 +96,7 @@ private static String toHTMLrgb(Color rgb) {
}

private static void appendAsHexString(StringBuilder buffer, int intValue) {
String hexValue= Integer.toHexString(intValue);
String hexValue = Integer.toHexString(intValue);
if (hexValue.length() == 1) {
buffer.append('0');
}
Expand All @@ -120,32 +117,34 @@ public List<String> getUrlFor(PsiElement element, PsiElement originalElement) {

@Override
public @Nullable PsiElement getCustomDocumentationElement(@NotNull Editor editor, @NotNull PsiFile file, @Nullable PsiElement contextElement, int targetOffset) {
return new LSPPsiElementForHover(editor, file, targetOffset);
contextElement.putUserData(OFFSET_KEY, targetOffset);
return null;
}

@Nullable
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
if (element instanceof LSPPsiElementForHover) {
LSPPsiElementForHover data = (LSPPsiElementForHover) element;
Editor editor = data.getEditor();
initiateHoverRequest(element, data.getTargetOffset());
try {
String result = lspRequest.get(500, TimeUnit.MILLISECONDS).stream()
.filter(Objects::nonNull)
.map(LSPTextHover::getHoverString)
.filter(Objects::nonNull)
.collect(Collectors.joining("\n\n")) //$NON-NLS-1$
.trim();
if (!result.isEmpty()) {
return styleHtml(editor, RENDERER.render(PARSER.parse(result)));
}
} catch (ExecutionException | TimeoutException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
} catch (InterruptedException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
Thread.currentThread().interrupt();
Integer targetOffset = originalElement.getUserData(OFFSET_KEY);
if (targetOffset == null) {
return null;
}
Editor editor = LSPIJUtils.editorForElement(element);
initiateHoverRequest(element, targetOffset);
try {
String result = lspRequest.get(500, TimeUnit.MILLISECONDS).stream()
.filter(Objects::nonNull)
.map(LSPTextHover::getHoverString)
.filter(Objects::nonNull)
.collect(Collectors.joining("\n\n")) //$NON-NLS-1$
.trim();
if (!result.isEmpty()) {
return styleHtml(editor, RENDERER.render(PARSER.parse(result)));
}
} catch (ExecutionException | TimeoutException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
} catch (InterruptedException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
Thread.currentThread().interrupt();
}
return null;
}
Expand Down Expand Up @@ -185,10 +184,8 @@ public String generateDoc(PsiElement element, @Nullable PsiElement originalEleme
* Initialize hover requests with hover (if available) and codelens (if
* available).
*
* @param element
* the PSI element.
* @param offset
* the target offset.
* @param element the PSI element.
* @param offset the target offset.
*/
private void initiateHoverRequest(PsiElement element, int offset) {
PsiDocumentManager manager = PsiDocumentManager.getInstance(element.getProject());
Expand Down

0 comments on commit 67cc355

Please sign in to comment.