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; 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.AnnotationHolder;
import com.intellij.lang.annotation.Annotator; import com.intellij.lang.annotation.Annotator;
import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors; import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLiteralExpression; 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 separatorRange = TextRange.from(prefixRange.getEndOffset(), SIMPLE_SEPARATOR_STR.length());
TextRange keyRange = new TextRange(separatorRange.getEndOffset(), element.getTextRange().getEndOffset() - 1); TextRange keyRange = new TextRange(separatorRange.getEndOffset(), element.getTextRange().getEndOffset() - 1);
// Get the list of properties from the Project // highlight "simple" prefix and ":" separator
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.
holder.newSilentAnnotation(HighlightSeverity.INFORMATION) holder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(prefixRange).textAttributes(DefaultLanguageHighlighterColors.KEYWORD).create(); .range(prefixRange).textAttributes(DefaultLanguageHighlighterColors.KEYWORD).create();
holder.newSilentAnnotation(HighlightSeverity.INFORMATION) holder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(separatorRange).textAttributes(SimpleSyntaxHighlighter.SEPARATOR).create(); .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()) { if (properties.isEmpty()) {
// No well-formed property found following the key-separator holder.newAnnotation(HighlightSeverity.ERROR, "Unresolved property")
AnnotationBuilder builder = holder.newAnnotation(HighlightSeverity.ERROR, "Unresolved property").range(keyRange); .range(keyRange)
// Force the text attributes to Simple syntax bad character .highlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL)
builder.textAttributes(SimpleSyntaxHighlighter.BAD_CHARACTER); // ** Tutorial step 18.3 - Add a quick fix for the string containing possible properties
// ** Tutorial step 18.3 - Add a quick fix for the string containing possible properties .withFix(new SimpleCreatePropertyQuickFix(key))
builder.withFix(new SimpleCreatePropertyQuickFix(possibleProperties)); .create();
// Finish creating new annotation
builder.create();
} else { } else {
// Found at least one property, force the text attributes to Simple syntax value character // Found at least one property, force the text attributes to Simple syntax value character
holder.newSilentAnnotation(HighlightSeverity.INFORMATION) holder.newSilentAnnotation(HighlightSeverity.INFORMATION)