Annotating custom language via Actions triggered by users (on demand)

I'm trying to accomplish the following:

I have a custom language that I'm writing and I would like to give users the option to click an "analyze" action when they want to ensure the code they've typed in plays nice with the underlying compiler, the action implementation runs the jar based compiler for my tool and gives back a list of issue objects indicating whether they are warnings, errors, etc.

Without resorting to an "ExternalAnnotator" (I don't want this thing running over and over in the background) I would like to somehow annotate any found issues onto the PSI..

While I did mess with an ExternalAnnotator and manage to get it working---I would like to do things using an Action as mentioned so I can avoid all the continuous processing going on in the background. In other words, I want annotation on demand via a triggered user action...

It seems external annotators work by adding found issues to an AnnotationHolder... I don't know how to get an instance of this outside of implementing another externalAnnotator...

Say users run my action and it finds some errors and underlines them in the editor. If they change the document, Its OK for the underlines to disappear until they run the action again (when they do so is up to them). I know how to erase document highlights via a document listener already.

My only stumbling point is getting the actual annotations on the tree. AnnotationHolder seems the way to go, but it seems like I HAVE to go through an external annotator --- which, for reasons described above--- I don't want to do.

Is what I'm trying to accomplish possible?

 

 

0
4 comments

Have you tried adding the highlights directly to the markup model (e.g. using com.intellij.openapi.editor.impl.DocumentMarkupModel#forDocument)?

0
Avatar
Permanently deleted user

No I haven't. Right now I just have a list of Issue objects that I retrieved from running my tool:

public static class Issue {
String annotation;
List<Token> offendingTokens = new ArrayList<>();
RESOLVEMessage msg;

public Issue(RESOLVEMessage msg) {
this.msg = msg;
}
}

and in the apply method (of the old ExternalAnnotator I was messing around with) I would just do this: (which essentially just adds all the issues to the corresponding AnnotationHolder.

/** Called 3rd */
@Override
public void apply(@NotNull PsiFile file,
@NotNull List<RESOLVEExternalAnnotator.Issue> issues,
@NotNull AnnotationHolder holder) {
for (Issue issue : issues) {
for (int j = 0; j < issue.offendingTokens.size(); j++) {
Token t = issue.offendingTokens.get(j);
if (t instanceof CommonToken) {
CommonToken ct = (CommonToken) t;
int startIndex = ct.getStartIndex();
int stopIndex = ct.getStopIndex();
TextRange range = new TextRange(startIndex, stopIndex + 1);
ErrorSeverity severity = ErrorSeverity.INFO;
if (issue.msg.getErrorType() != null) {
severity = issue.msg.getErrorType().severity;
}
switch (severity) {
case ERROR:
case ERROR_ONE_OFF:
case FATAL:
holder.createErrorAnnotation(range, issue.annotation);
break;

case WARNING:
holder.createWarningAnnotation(range, issue.annotation);
break;

case WARNING_ONE_OFF:
case INFO:
holder.createWeakWarningAnnotation(range, issue.annotation);
default:
break;
}
}
}
}
super.apply(file, issues, holder);
}

But that supposes I have access to the AnnotationHolder in my Action (which I don't as far as I can tell)... If I were to use just DocumentMarkupModel#forDocument I don't see how I can use the methods predefined specially for creating analysis (warning, error, etc) annotations such as:

AnnotationHolder#createWarningAnnotation(..)

AnnotationHolder#createErrrorAnnotation(..)

Would I just have to remake all of those markup styles myself?

0

It's a lower-level API that doesn't know anything about errors/warnings, it only knows about offsets and text attributes (colors, fonts). You'd probably need com.intellij.openapi.editor.markup.MarkupModel#addRangeHighlighter. To retrieve text attributes, you can use com.intellij.codeInsight.daemon.impl.SeverityRegistrar#getTextAttributesBySeverity.

0
Avatar
Permanently deleted user

Thanks Peter!

I'll investigate this soon (probably in a week or so) and update this thread with my findings. 

0

Please sign in to leave a comment.