use of com.intellij.xml.XmlAttributeDescriptor in project intellij-community by JetBrains.
the class JavaFxColorRgbInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
if (!JavaFxFileTypeFactory.isFxml(session.getFile()))
return PsiElementVisitor.EMPTY_VISITOR;
return new XmlElementVisitor() {
@Override
public void visitXmlAttribute(XmlAttribute attribute) {
super.visitXmlAttribute(attribute);
final String attributeValue = attribute.getValue();
if (attributeValue == null)
return;
final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
if (descriptor instanceof JavaFxPropertyAttributeDescriptor) {
final PsiClass psiClass = ((JavaFxPropertyAttributeDescriptor) descriptor).getPsiClass();
if (psiClass != null && JavaFxCommonNames.JAVAFX_SCENE_COLOR.equals(psiClass.getQualifiedName())) {
final XmlAttributeValue valueElement = attribute.getValueElement();
final PsiElement location = valueElement != null ? valueElement : attribute;
validateColorComponent(psiClass, attribute.getName(), attributeValue, location);
}
}
}
@Override
public void visitXmlTag(XmlTag tag) {
super.visitXmlTag(tag);
if (tag.getSubTags().length != 0)
return;
final XmlElementDescriptor descriptor = tag.getDescriptor();
if (descriptor instanceof JavaFxPropertyTagDescriptor) {
final PsiClass psiClass = ((JavaFxPropertyTagDescriptor) descriptor).getPsiClass();
if (psiClass != null && JavaFxCommonNames.JAVAFX_SCENE_COLOR.equals(psiClass.getQualifiedName())) {
final XmlTagValue valueElement = tag.getValue();
final XmlText[] textElements = valueElement.getTextElements();
final PsiElement location = textElements.length == 1 ? textElements[0] : tag;
validateColorComponent(psiClass, tag.getName(), valueElement.getTrimmedText(), location);
}
}
}
private void validateColorComponent(@NotNull PsiClass psiClass, @NotNull String propertyName, @NotNull String propertyValue, @NotNull PsiElement location) {
final PsiMember declaration = JavaFxPsiUtil.getWritableProperties(psiClass).get(propertyName);
final String boxedQName = JavaFxPsiUtil.getBoxedPropertyType(psiClass, declaration);
if (CommonClassNames.JAVA_LANG_FLOAT.equals(boxedQName) || CommonClassNames.JAVA_LANG_DOUBLE.equals(boxedQName)) {
try {
double value = Double.parseDouble(propertyValue);
if (value < 0.0 || value > 1.0) {
holder.registerProblem(location, "Color component has to be a number between 0.0 and 1.0, inclusively");
}
} catch (NumberFormatException ignored) {
}
}
}
};
}
use of com.intellij.xml.XmlAttributeDescriptor in project intellij-community by JetBrains.
the class JavaFxRedundantPropertyValueInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
if (!JavaFxFileTypeFactory.isFxml(session.getFile()))
return PsiElementVisitor.EMPTY_VISITOR;
return new XmlElementVisitor() {
@Override
public void visitXmlAttribute(XmlAttribute attribute) {
super.visitXmlAttribute(attribute);
final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
if (!(descriptor instanceof JavaFxPropertyAttributeDescriptor))
return;
final String attributeName = attribute.getName();
final String attributeValue = attribute.getValue();
if (attributeValue == null || attributeValue.startsWith("$") || attributeValue.startsWith("#") || attributeValue.startsWith("%") || FxmlConstants.FX_ID.equals(attributeName) || FxmlConstants.FX_VALUE.equals(attributeName) || FxmlConstants.FX_CONSTANT.equals(attributeName) || FxmlConstants.FX_CONTROLLER.equals(attributeName)) {
return;
}
final PsiClass tagClass = JavaFxPsiUtil.getTagClass(attribute.getParent());
final String defaultValue = getDefaultValue(attributeName, tagClass);
if (defaultValue == null)
return;
if (isEqualValue(tagClass, attributeValue, defaultValue, descriptor.getDeclaration())) {
holder.registerProblem(attribute, "Attribute is redundant because it contains default value", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new RemoveAttributeIntentionFix(attributeName, attribute));
}
}
@Override
public void visitXmlTag(XmlTag tag) {
super.visitXmlTag(tag);
final XmlElementDescriptor descriptor = tag.getDescriptor();
if (!(descriptor instanceof JavaFxPropertyTagDescriptor)) {
return;
}
if (tag.getSubTags().length != 0)
return;
final String tagText = tag.getValue().getTrimmedText();
if (tagText.startsWith("$") || tagText.startsWith("#") || tagText.startsWith("%")) {
return;
}
final XmlTag parentTag = tag.getParentTag();
if (parentTag == null)
return;
final PsiClass tagClass = JavaFxPsiUtil.getTagClass(parentTag);
final String defaultValue = getDefaultValue(tag.getName(), tagClass);
if (defaultValue == null)
return;
if (isEqualValue(tagClass, tagText, defaultValue, descriptor.getDeclaration())) {
holder.registerProblem(tag, "Tag is redundant because it contains default value", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new RemoveTagIntentionFix(tag.getName(), tag));
}
}
};
}
use of com.intellij.xml.XmlAttributeDescriptor in project intellij-community by JetBrains.
the class JavaFxExpandAttributeIntention method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final XmlAttribute attr = (XmlAttribute) element.getParent();
final String name = attr.getName();
final XmlAttributeDescriptor descriptor = attr.getDescriptor();
LOG.assertTrue(descriptor != null);
String value = attr.getValue();
final PsiElement declaration = descriptor.getDeclaration();
if (declaration instanceof PsiMember) {
final PsiType propertyType = PropertyUtil.getPropertyType((PsiMember) declaration);
final PsiType itemType = JavaGenericsUtil.getCollectionItemType(propertyType, declaration.getResolveScope());
if (itemType != null) {
final String typeNode = itemType.getPresentableText();
JavaFxPsiUtil.insertImportWhenNeeded((XmlFile) attr.getContainingFile(), typeNode, itemType.getCanonicalText());
final String[] vals = value != null ? value.split(",") : ArrayUtil.EMPTY_STRING_ARRAY;
value = StringUtil.join(vals, s -> "<" + typeNode + " " + FxmlConstants.FX_VALUE + "=\"" + s.trim() + "\"/>", "\n");
}
}
final XmlTag childTag = XmlElementFactory.getInstance(project).createTagFromText("<" + name + ">" + value + "</" + name + ">");
attr.getParent().add(childTag);
attr.delete();
}
use of com.intellij.xml.XmlAttributeDescriptor in project intellij-community by JetBrains.
the class JavaFxExpandAttributeIntention method isAvailable.
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_NAME) {
final PsiElement parent = element.getParent();
if (parent instanceof XmlAttribute) {
final XmlAttributeDescriptor descriptor = ((XmlAttribute) parent).getDescriptor();
if (descriptor instanceof JavaFxPropertyAttributeDescriptor && !(descriptor instanceof JavaFxBuiltInAttributeDescriptor)) {
PsiType tagType = null;
final PsiElement declaration = descriptor.getDeclaration();
if (declaration instanceof PsiMember) {
tagType = PropertyUtil.getPropertyType((PsiMember) declaration);
}
PsiClass tagClass = PsiUtil.resolveClassInType(tagType instanceof PsiPrimitiveType ? ((PsiPrimitiveType) tagType).getBoxedType(parent) : tagType);
if ((tagClass != null && JavaFxPsiUtil.isAbleToInstantiate(tagClass)) || descriptor instanceof JavaFxStaticSetterAttributeDescriptor) {
setText("Expand '" + ((XmlAttribute) parent).getName() + "' to tag");
return true;
}
}
}
}
return false;
}
use of com.intellij.xml.XmlAttributeDescriptor in project intellij-community by JetBrains.
the class JavaFxBuiltInTagDescriptor method getAttributesDescriptors.
@Override
public XmlAttributeDescriptor[] getAttributesDescriptors(@Nullable XmlTag context) {
final List<XmlAttributeDescriptor> descriptors = new ArrayList<>();
final List<String> builtInAttributeNames = FxmlConstants.FX_BUILT_IN_TAG_SUPPORTED_ATTRIBUTES.get(getName());
if (builtInAttributeNames != null) {
for (String attrName : builtInAttributeNames) {
descriptors.add(JavaFxBuiltInAttributeDescriptor.create(attrName, getName()));
}
}
JavaFxClassTagDescriptorBase.collectStaticAttributesDescriptors(context, descriptors);
final XmlTag referencedTag = getReferencedTag(myXmlTag);
if (referencedTag != null) {
final XmlElementDescriptor referencedDescriptor = referencedTag.getDescriptor();
if (referencedDescriptor != null) {
final XmlAttributeDescriptor[] attributesDescriptors = referencedDescriptor.getAttributesDescriptors(referencedTag);
if (attributesDescriptors != null) {
Collections.addAll(descriptors, attributesDescriptors);
}
}
}
final XmlTag includedRoot = getIncludedRoot(context);
if (includedRoot != null) {
final XmlElementDescriptor includedRootDescriptor = includedRoot.getDescriptor();
if (includedRootDescriptor instanceof JavaFxClassTagDescriptorBase) {
((JavaFxClassTagDescriptorBase) includedRootDescriptor).collectInstanceProperties(descriptors);
}
}
return descriptors.isEmpty() ? XmlAttributeDescriptor.EMPTY : descriptors.toArray(XmlAttributeDescriptor.EMPTY);
}
Aggregations