How to add a tooltip to a Highlighted text?

 This will highlight the text but won't set the tooltip. I want to use RangeHighlighter for this. Not annotator.

int lineStartOffset = document.getLineStartOffset(Math.max(0, (Integer) line.get(i)-1)) + (Integer) column.get(i) - 1;
int lineEndOffset = document.getLineStartOffset(Math.max(0, (Integer) line.get(i) -1)) + (Integer) end.get(i);
editor.getMarkupModel().addRangeHighlighter(
lineStartOffset, lineEndOffset,3333, x, HighlighterTargetArea.EXACT_RANGE
).setErrorStripeTooltip(tooltip);
0
2 comments

RangeHighlighter-s don't support easy addition of tooltips, you'll need to implement showing them yourselves, by registering an editor mouse motion listener (see com.intellij.codeInsight.daemon.impl.DaemonListeners.MyEditorMouseMotionListener as an example).

0
Avatar
Permanently deleted user

I tried using Annotator like below and registered it in plugin.xml. But method annotate does not get called.

<extensions defaultExtensionNs="com.intellij">
<annotator language="JAVA" implementationClass="Tools.SimpleAnnotator"/>
</extensions>

 

package Tools;

import com.intellij.lang.annotation.*;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;

public class SimpleAnnotator implements Annotator {

@Override
public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder annotationHolder) {
SyntaxHighlighter syntaxHighlighter= new SyntaxHighlighter();

if (!syntaxHighlighter.annotateoffsets.isEmpty()) {
for (int i = 1; i < syntaxHighlighter.annotateoffsets.size() + 1; i++) {
int startOffset = syntaxHighlighter.annotateoffsets.get(i).get(0);
int endOffset = syntaxHighlighter.annotateoffsets.get(i).get(1);
String tooltip = syntaxHighlighter.tooltips.get(i-1);
System.out.println(tooltip);
System.out.println(startOffset);
System.out.println(endOffset);
System.out.println("ffffffffffffffffffff");
TextRange range = new TextRange(startOffset, endOffset);
Annotation annotation = annotationHolder.createErrorAnnotation(range, tooltip);
annotation.setTooltip(tooltip);
}
}
}
}
0

Please sign in to leave a comment.