devkit: fix highlighting of extension elements with default Tag and Attribute annotation values (IDEA-208354)

This commit is contained in:
nik
2019-03-05 10:21:50 +03:00
parent 56963c59a8
commit ca51d4dce4
6 changed files with 46 additions and 11 deletions

View File

@@ -150,7 +150,7 @@ public class ExtensionDomExtender extends DomExtender<Extensions> {
final PsiConstantEvaluationHelper evalHelper = JavaPsiFacade.getInstance(field.getProject()).getConstantEvaluationHelper();
final PsiAnnotation attrAnno = findAnnotation(Attribute.class, field, getter, setter);
if (attrAnno != null) {
final String attrName = getStringAttribute(attrAnno, "value", evalHelper);
final String attrName = getStringAttribute(attrAnno, "value", evalHelper, fieldName);
if (attrName != null) {
Class clazz = String.class;
if (withElement != null && isClassField(fieldName)) {
@@ -171,7 +171,7 @@ public class ExtensionDomExtender extends DomExtender<Extensions> {
final PsiAnnotation propAnno = findAnnotation(Property.class, field, getter, setter);
final PsiAnnotation collectionAnnotation = findAnnotation(XCollection.class, field, getter, setter);
//final PsiAnnotation colAnno = modifierList.findAnnotation(Collection.class.getName()); // todo
final String tagName = tagAnno != null? getStringAttribute(tagAnno, "value", evalHelper) :
final String tagName = tagAnno != null ? getStringAttribute(tagAnno, "value", evalHelper, fieldName) :
propAnno != null && getBooleanAttribute(propAnno, "surroundWithTag", evalHelper)? Constants.OPTION : null;
if (tagName != null) {
if (collectionAnnotation == null) {
@@ -244,8 +244,8 @@ public class ExtensionDomExtender extends DomExtender<Extensions> {
PsiConstantEvaluationHelper evalHelper) {
final boolean surroundWithTag = getBooleanAttribute(anno, "surroundWithTag", evalHelper);
if (surroundWithTag) return; // todo Set, List, Array
final String tagName = getStringAttribute(anno, "elementTag", evalHelper);
final String attrName = getStringAttribute(anno, "elementValueAttribute", evalHelper);
final String tagName = getStringAttribute(anno, "elementTag", evalHelper, null);
final String attrName = getStringAttribute(anno, "elementValueAttribute", evalHelper, null);
final PsiType elementType = getElementType(type);
if (elementType == null || TypeConversionUtil.isPrimitiveAndNotNullOrWrapper(elementType)
|| CommonClassNames.JAVA_LANG_STRING.equals(elementType.getCanonicalText())
@@ -267,7 +267,7 @@ public class ExtensionDomExtender extends DomExtender<Extensions> {
if (psiClass != null) {
final PsiModifierList modifierList = psiClass.getModifierList();
final PsiAnnotation tagAnno = modifierList == null? null : modifierList.findAnnotation(Tag.class.getName());
final String classTagName = tagAnno == null? psiClass.getName() : getStringAttribute(tagAnno, "value", evalHelper);
final String classTagName = tagAnno == null? psiClass.getName() : getStringAttribute(tagAnno, "value", evalHelper, null);
if (classTagName != null) {
registrar.registerCollectionChildrenExtension(new XmlName(classTagName), DomElement.class).addExtender(new DomExtender() {
@Override
@@ -283,11 +283,14 @@ public class ExtensionDomExtender extends DomExtender<Extensions> {
@Nullable
static String getStringAttribute(final PsiAnnotation annotation,
final String name,
final PsiConstantEvaluationHelper evalHelper) {
final PsiConstantEvaluationHelper evalHelper, String defaultValueIfEmpty) {
String value = getAttributeValue(annotation, name);
if (value != null) return value;
final Object o = evalHelper.computeConstantExpression(annotation.findAttributeValue(name), false);
return o instanceof String && StringUtil.isNotEmpty((String)o)? (String)o : null;
if (value != null) {
return value.isEmpty() ? defaultValueIfEmpty : value;
}
Object o = evalHelper.computeConstantExpression(annotation.findAttributeValue(name), false);
if (!(o instanceof String)) return null;
return StringUtil.isNotEmpty((String)o) ? (String)o : defaultValueIfEmpty;
}
private static boolean getBooleanAttribute(final PsiAnnotation annotation,

View File

@@ -16,7 +16,6 @@
package org.jetbrains.idea.devkit.dom.impl;
import com.intellij.psi.*;
import com.intellij.psi.util.PropertyUtil;
import com.intellij.psi.util.PropertyUtilBase;
import com.intellij.util.xml.ConvertContext;
import com.intellij.util.xml.ResolvingConverter;
@@ -87,7 +86,7 @@ public class PluginFieldNameConverter extends ResolvingConverter<PsiField> {
final PsiMethod setter = PropertyUtilBase.findSetterForField(psiField);
final PsiAnnotation attrAnno = ExtensionDomExtender.findAnnotation(annotationClass, psiField, getter, setter);
if (attrAnno != null) {
return ExtensionDomExtender.getStringAttribute(attrAnno, "value", evalHelper);
return ExtensionDomExtender.getStringAttribute(attrAnno, "value", evalHelper, null);
}
return null;
}

View File

@@ -0,0 +1,9 @@
import java.lang.String;
public class ExtBeanWithDefaultValuesInAnnotations {
@com.intellij.util.xmlb.annotations.Tag
public String subTag;
@com.intellij.util.xmlb.annotations.Attribute
public String attribute;
}

View File

@@ -0,0 +1,4 @@
<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
</extensions>
</idea-plugin>

View File

@@ -0,0 +1,16 @@
<idea-plugin>
<id>com.intellij.myPlugin</id>
<vendor>JetBrains</vendor>
<version>1.0</version>
<extensionPoints>
<extensionPoint name="myBean" beanClass="ExtBeanWithDefaultValuesInAnnotations"/>
</extensionPoints>
<extensions defaultExtensionNs="com.intellij.myPlugin">
<myBean attribute="123">
<subTag>value</subTag>
</myBean>
</extensions>
</idea-plugin>

View File

@@ -270,6 +270,10 @@ class PluginXmlFunctionalTest extends JavaCodeInsightFixtureTestCase {
doHighlightingTest("extensionWithInnerTags.xml", "ExtBeanWithInnerTags.java")
}
void testExtensionBeanWithDefaultValuesInAnnotations() {
doHighlightingTest("extensionWithDefaultValuesInAnnotations.xml", "ExtBeanWithDefaultValuesInAnnotations.java")
}
void testLanguageAttributeHighlighting() {
configureLanguageAttributeTest()
doHighlightingTest("languageAttribute.xml", "MyLanguageAttributeEPBean.java")