simple language: improve SimpleAnnotator

This commit is contained in:
Yann Cébron 2020-11-18 17:27:55 +01:00
parent 62264e1eb4
commit 9687dbc25f

View File

@ -2,12 +2,11 @@
package org.intellij.sdk.language;
import com.intellij.lang.annotation.AnnotationBuilder;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLiteralExpression;
@ -43,25 +42,23 @@ public class SimpleAnnotator implements Annotator {
TextRange separatorRange = TextRange.from(prefixRange.getEndOffset(), SIMPLE_SEPARATOR_STR.length());
TextRange keyRange = new TextRange(separatorRange.getEndOffset(), element.getTextRange().getEndOffset() - 1);
// Get the list of properties from the Project
String possibleProperties = value.substring(SIMPLE_PREFIX_STR.length() + SIMPLE_SEPARATOR_STR.length());
Project project = element.getProject();
List<SimpleProperty> properties = SimpleUtil.findProperties(project, possibleProperties);
// Set the annotations using the text ranges - Normally there would be one range, set by the element itself.
// highlight "simple" prefix and ":" separator
holder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(prefixRange).textAttributes(DefaultLanguageHighlighterColors.KEYWORD).create();
holder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(separatorRange).textAttributes(SimpleSyntaxHighlighter.SEPARATOR).create();
// Get the list of properties for given key
String key = value.substring(SIMPLE_PREFIX_STR.length() + SIMPLE_SEPARATOR_STR.length());
List<SimpleProperty> properties = SimpleUtil.findProperties(element.getProject(), key);
if (properties.isEmpty()) {
// No well-formed property found following the key-separator
AnnotationBuilder builder = holder.newAnnotation(HighlightSeverity.ERROR, "Unresolved property").range(keyRange);
// Force the text attributes to Simple syntax bad character
builder.textAttributes(SimpleSyntaxHighlighter.BAD_CHARACTER);
// ** Tutorial step 18.3 - Add a quick fix for the string containing possible properties
builder.withFix(new SimpleCreatePropertyQuickFix(possibleProperties));
// Finish creating new annotation
builder.create();
holder.newAnnotation(HighlightSeverity.ERROR, "Unresolved property")
.range(keyRange)
.highlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL)
// ** Tutorial step 18.3 - Add a quick fix for the string containing possible properties
.withFix(new SimpleCreatePropertyQuickFix(key))
.create();
} else {
// Found at least one property, force the text attributes to Simple syntax value character
holder.newSilentAnnotation(HighlightSeverity.INFORMATION)