From 9687dbc25f495debb572c5bc6ecc601266da1828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20C=C3=A9bron?= Date: Wed, 18 Nov 2020 17:27:55 +0100 Subject: [PATCH] simple language: improve SimpleAnnotator --- .../sdk/language/SimpleAnnotator.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/simple_language_plugin/src/main/java/org/intellij/sdk/language/SimpleAnnotator.java b/simple_language_plugin/src/main/java/org/intellij/sdk/language/SimpleAnnotator.java index 5ae2b0edd..2a1999e2e 100644 --- a/simple_language_plugin/src/main/java/org/intellij/sdk/language/SimpleAnnotator.java +++ b/simple_language_plugin/src/main/java/org/intellij/sdk/language/SimpleAnnotator.java @@ -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 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 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)