Search in sources :

Example 6 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class DomElementProblemDescriptorImpl method getPsiElement.

@Nullable
private PsiElement getPsiElement() {
    if (myDomElement instanceof DomFileElement) {
        return ((DomFileElement) myDomElement).getFile();
    }
    if (myDomElement instanceof GenericAttributeValue) {
        final GenericAttributeValue attributeValue = (GenericAttributeValue) myDomElement;
        final XmlAttributeValue value = attributeValue.getXmlAttributeValue();
        return value != null && StringUtil.isNotEmpty(value.getText()) ? value : attributeValue.getXmlElement();
    }
    final XmlTag tag = myDomElement.getXmlTag();
    if (myDomElement instanceof GenericValue && tag != null) {
        final XmlText[] textElements = tag.getValue().getTextElements();
        if (textElements.length > 0) {
            return textElements[0];
        }
    }
    return tag;
}
Also used : XmlText(com.intellij.psi.xml.XmlText) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class DomTarget method getTarget.

@Nullable
public static DomTarget getTarget(DomElement element, GenericDomValue nameElement) {
    if (nameElement instanceof GenericAttributeValue) {
        final GenericAttributeValue genericAttributeValue = (GenericAttributeValue) nameElement;
        final XmlAttributeValue attributeValue = genericAttributeValue.getXmlAttributeValue();
        if (attributeValue == null) {
            return null;
        }
        final int length = attributeValue.getTextLength();
        if (length >= 2) {
            return new DomTarget(element, attributeValue, new TextRange(1, length - 1), nameElement);
        }
    }
    final XmlTag tag = nameElement.getXmlTag();
    if (tag == null) {
        return null;
    }
    XmlTagValue tagValue = tag.getValue();
    if (StringUtil.isEmpty(tagValue.getTrimmedText())) {
        return null;
    }
    return new DomTarget(element, tag, XmlTagUtil.getTrimmedValueRange(tag), nameElement);
}
Also used : XmlTagValue(com.intellij.psi.xml.XmlTagValue) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class DomHardCoreTestCase method getReference.

protected PsiReference getReference(final GenericAttributeValue value) {
    final XmlAttributeValue attributeValue = value.getXmlAttributeValue();
    assertNotNull(attributeValue);
    final PsiReference reference = attributeValue.getContainingFile().findReferenceAt(attributeValue.getTextRange().getStartOffset() + 1);
    assertNotNull(reference);
    assertEquals(attributeValue, reference.resolve());
    return reference;
}
Also used : PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue)

Example 9 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class XmlAttributeValueImpl method updateText.

@Override
public PsiLanguageInjectionHost updateText(@NotNull String text) {
    try {
        final String quoteChar = getTextLength() > 0 ? getText().substring(0, 1) : "";
        String contents = StringUtil.containsAnyChar(quoteChar, "'\"") ? StringUtil.trimEnd(StringUtil.trimStart(text, quoteChar), quoteChar) : text;
        XmlAttribute newAttribute = XmlElementFactory.getInstance(getProject()).createAttribute("q", contents, this);
        XmlAttributeValue newValue = newAttribute.getValueElement();
        CheckUtil.checkWritable(this);
        replaceAllChildrenToChildrenOf(newValue.getNode());
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return this;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue)

Example 10 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class JavaFxEventHandlerInspection 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 visitXmlAttributeValue(XmlAttributeValue xmlAttributeValue) {
            super.visitXmlAttributeValue(xmlAttributeValue);
            final PsiElement valueParent = xmlAttributeValue.getParent();
            if (!(valueParent instanceof XmlAttribute))
                return;
            final XmlAttribute attribute = (XmlAttribute) valueParent;
            final List<PsiMethod> eventHandlerMethods = getEventHandlerMethods(attribute);
            if (eventHandlerMethods.size() == 0)
                return;
            if (eventHandlerMethods.size() != 1) {
                holder.registerProblem(xmlAttributeValue, "Ambiguous event handler name: more than one matching method found");
            }
            if (myDetectNonVoidReturnType) {
                eventHandlerMethods.stream().map(PsiMethod::getReturnType).filter(returnType -> !PsiType.VOID.equals(returnType)).findAny().ifPresent(ignored -> holder.registerProblem(xmlAttributeValue, "Return type of event handler should be void"));
            }
            final PsiClassType declaredType = JavaFxPsiUtil.getDeclaredEventType(attribute);
            if (declaredType == null)
                return;
            for (PsiMethod method : eventHandlerMethods) {
                final PsiParameter[] parameters = method.getParameterList().getParameters();
                if (parameters.length == 1) {
                    final PsiType actualType = parameters[0].getType();
                    if (actualType instanceof PsiClassType) {
                        if (!actualType.isAssignableFrom(declaredType)) {
                            final LocalQuickFix parameterTypeFix = new ChangeParameterTypeQuickFix(attribute, method, declaredType);
                            final PsiClassType actualRawType = ((PsiClassType) actualType).rawType();
                            final PsiClassType expectedRawType = declaredType.rawType();
                            if (actualRawType.isAssignableFrom(expectedRawType)) {
                                final List<LocalQuickFix> quickFixes = new ArrayList<>();
                                quickFixes.add(parameterTypeFix);
                                collectFieldTypeFixes(attribute, (PsiClassType) actualType, quickFixes);
                                holder.registerProblem(xmlAttributeValue, "Incompatible generic parameter of event handler argument: " + actualType.getCanonicalText() + " is not assignable from " + declaredType.getCanonicalText(), quickFixes.toArray(LocalQuickFix.EMPTY_ARRAY));
                            } else {
                                holder.registerProblem(xmlAttributeValue, "Incompatible event handler argument: " + actualRawType.getCanonicalText() + " is not assignable from " + expectedRawType.getCanonicalText(), parameterTypeFix);
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)127 XmlAttribute (com.intellij.psi.xml.XmlAttribute)57 XmlTag (com.intellij.psi.xml.XmlTag)50 NotNull (org.jetbrains.annotations.NotNull)38 PsiElement (com.intellij.psi.PsiElement)31 Nullable (org.jetbrains.annotations.Nullable)24 PsiReference (com.intellij.psi.PsiReference)20 XmlFile (com.intellij.psi.xml.XmlFile)19 TextRange (com.intellij.openapi.util.TextRange)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 ArrayList (java.util.ArrayList)13 Project (com.intellij.openapi.project.Project)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 DomElement (com.intellij.util.xml.DomElement)9 PsiFile (com.intellij.psi.PsiFile)8 LazyValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper)7 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Module (com.intellij.openapi.module.Module)5 ValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)5