How to annotate Java files by line number? I'm using PsiElement & Annotator

Hello,

I'm creating a static analysis tool for Java that displays errors in the gutter for a given Java file. I have most of this working, but I'm not clear on how to annotate by line number.

For example, in my annotator class below (which I inject into the Java language object), let's say I want to create an error annotation at line 10. I tried using FileViewProvider, but that doesn't map to line numbers. If it helps, I can easily obtain a VirtualFile object for my Java file.

public void init() {
Language j = StdLanguages.JAVA;
j.injectExternalAnnotator(new MyAnnotator());
}

private class MyAnnotator implements ExternalAnnotator {
public void annotate(PsiFile psiFile, AnnotationHolder holder) {
if (fIsEnabled) {
System.out.println("Annotate: " + psiFile);
FileViewProvider fvp = psiFile.getViewProvider();
PsiElement elt = fvp.findElementAt(10); // character offset, not line offset

// How do I create a PsiElement at line 10 of psiFile?

Annotation a = holder.createErrorAnnotation(elt, "Foo bar foo foo foo");
// ... etc
}
}
}

3 comments
Comment actions Permalink

I didn't test it, but IMHO you should find the PsiElement on a given line by something like this:

@Nullable
private PsiElement getElementAtLine(PsiFile file, int line) {
Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
int offset = document.getLineStartOffset(line);
PsiElement element = file.getViewProvider().findElementAt(offset);
if (document.getLineNumber(element.getTextOffset()) != line) {
element = element.getNextSibling();
}
return element;
}

0
Comment actions Permalink

Hello JoeNearBoston,

Looks like the highlighting you want to perform actually has nothing to do
with any specific PSI elements. Thus, you shouldn't try to add annotations
through the PSI, and should work with the editor markup model directly.

I'm creating a static analysis tool for Java that displays errors in
the gutter for a given Java file. I have most of this working, but I'm
not clear on how to annotate by line number.

For example, in my annotator class below (which I inject into the Java
language object), let's say I want to create an error annotation at
line 10. I tried using FileViewProvider, but that doesn't map to line
numbers. If it helps, I can easily obtain a VirtualFile object for my
Java file.

public void init() {
Language j = StdLanguages.JAVA;
j.injectExternalAnnotator(new MyAnnotator());
}
private class MyAnnotator implements ExternalAnnotator {
public void annotate(PsiFile psiFile, AnnotationHolder holder)
{
if (fIsEnabled) {
System.out.println("Annotate: " + psiFile);
FileViewProvider fvp = psiFile.getViewProvider();
PsiElement elt = fvp.findElementAt(10); // character
offset, not line offset
// How do I create a PsiElement at line 10 of psiFile?

Annotation a = holder.createErrorAnnotation(elt, "Foo
bar foo foo foo");
// ... etc
}
}
}

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thanks! That worked perfectly!

0

Please sign in to leave a comment.