问题很简单。
我使用 Intellij IDEA 进行编码。 我希望我的代码在简单的条件下着色。
我附上一张图片以便更好地参考。
假设我有以下代码:
我希望应用以下条件突出显示该字符串文字。
我怎样才能实现这个目标? 我想我需要一些有关 IDEA 插件或其他方式的建议?
这是链接中的示例代码。
final class SimpleAnnotator implements Annotator {
// Define strings for the Simple language prefix - used for annotations, line markers, etc.
public static final String SIMPLE_PREFIX_STR = "simple";
public static final String SIMPLE_SEPARATOR_STR = ":";
@Override
public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
// Ensure the PSI Element is an expression
if (!(element instanceof PsiLiteralExpression literalExpression)) {
return;
}
// Ensure the Psi element contains a string that starts with the prefix and separator
String value = literalExpression.getValue() instanceof String ? (String) literalExpression.getValue() : null;
if (value == null || !value.startsWith(SIMPLE_PREFIX_STR + SIMPLE_SEPARATOR_STR)) {
return;
}
// Define the text ranges (start is inclusive, end is exclusive)
// "simple:key"
// 01234567890
TextRange prefixRange = TextRange.from(element.getTextRange().getStartOffset(), SIMPLE_PREFIX_STR.length() + 1);
TextRange separatorRange = TextRange.from(prefixRange.getEndOffset(), SIMPLE_SEPARATOR_STR.length());
TextRange keyRange = new TextRange(separatorRange.getEndOffset(), element.getTextRange().getEndOffset() - 1);
// 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()) {
holder.newAnnotation(HighlightSeverity.ERROR, "Unresolved property")
.range(keyRange)
.highlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL)
// ** Tutorial step 19. - 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)
.range(keyRange).textAttributes(SimpleSyntaxHighlighter.VALUE).create();
}
}
}
您还可以使用LivePlugin或Flora而无需创建新插件。