Search in sources :

Example 21 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class AntDomMacrodefAttributeReference method getParentMacrodef.

@Nullable
private AntDomMacroDef getParentMacrodef() {
    final PsiElement element = getElement();
    if (element == null) {
        return null;
    }
    final DomElement domElement = DomUtil.getDomElement(element);
    if (domElement == null) {
        return null;
    }
    return domElement.getParentOfType(AntDomMacroDef.class, false);
}
Also used : DomElement(com.intellij.util.xml.DomElement) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class InspectionMappingConsistencyInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
    return new XmlElementVisitor() {

        @Override
        public void visitXmlTag(XmlTag tag) {
            DomElement element = DomUtil.getDomElement(tag);
            if (element instanceof Extension) {
                ExtensionPoint extensionPoint = ((Extension) element).getExtensionPoint();
                if (extensionPoint != null && InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), "com.intellij.codeInspection.InspectionEP")) {
                    boolean key = tag.getAttribute("key") != null;
                    boolean groupKey = tag.getAttribute("groupKey") != null;
                    if (key) {
                        if (tag.getAttribute("bundle") == null) {
                            checkDefaultBundle(element, holder);
                        }
                    } else if (tag.getAttribute("displayName") == null) {
                        registerProblem(element, holder, "displayName or key should be specified", "displayName", "key");
                    }
                    if (groupKey) {
                        if (tag.getAttribute("bundle") == null && tag.getAttribute("groupBundle") == null) {
                            checkDefaultBundle(element, holder);
                        }
                    } else if (tag.getAttribute("groupName") == null) {
                        registerProblem(element, holder, "groupName or groupKey should be specified", "groupName", "groupKey");
                    }
                }
            }
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) Extension(org.jetbrains.idea.devkit.dom.Extension) DomElement(com.intellij.util.xml.DomElement) ExtensionPoint(org.jetbrains.idea.devkit.dom.ExtensionPoint) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class AntReferenceInjector method addMacrodefParameterRefs.

public static void addMacrodefParameterRefs(@NotNull XmlAttributeValue element, final Collection<PsiReference> refs) {
    final DomElement domElement = DomUtil.getDomElement(element);
    if (domElement == null) {
        return;
    }
    final AntDomMacroDef macrodef = domElement.getParentOfType(AntDomMacroDef.class, true);
    if (macrodef == null) {
        return;
    }
    final String text = ElementManipulators.getValueText(element);
    final int valueBeginingOffset = Math.abs(element.getTextRange().getStartOffset() - element.getValueTextRange().getStartOffset());
    int startIndex;
    int endIndex = -1;
    while ((startIndex = text.indexOf("@{", endIndex + 1)) > endIndex) {
        startIndex += 2;
        endIndex = startIndex;
        int nestedBrackets = 0;
        while (text.length() > endIndex) {
            final char ch = text.charAt(endIndex);
            if (ch == '}') {
                if (nestedBrackets == 0) {
                    break;
                }
                --nestedBrackets;
            } else if (ch == '{') {
                ++nestedBrackets;
            }
            ++endIndex;
        }
        if (nestedBrackets > 0 || endIndex == text.length())
            return;
        if (endIndex >= startIndex) {
            //final String name = text.substring(startIndex, endIndex);
            refs.add(new AntDomMacrodefAttributeReference(element, new TextRange(valueBeginingOffset + startIndex, valueBeginingOffset + endIndex)));
        }
        endIndex = startIndex;
    }
}
Also used : DomElement(com.intellij.util.xml.DomElement) TextRange(com.intellij.openapi.util.TextRange)

Example 24 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class AntCreatePropertyFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    final PsiFile containingFile = psiElement.getContainingFile();
    final FileModificationService modificationService = FileModificationService.getInstance();
    Navigatable result = null;
    if (myPropFile != null) {
        final VirtualFile vFile = myPropFile.getVirtualFile();
        boolean canModify = true;
        if (myPropFile instanceof PsiFile) {
            canModify = modificationService.prepareFileForWrite((PsiFile) myPropFile);
        } else if (vFile != null) {
            canModify = modificationService.prepareVirtualFilesForWrite(project, Collections.singleton(vFile));
        }
        if (canModify) {
            final IProperty generatedProperty = myPropFile.addProperty(myCanonicalText, "");
            result = vFile != null ? new OpenFileDescriptor(project, vFile, generatedProperty.getPsiElement().getTextRange().getEndOffset()) : generatedProperty;
        }
    } else {
        if (containingFile instanceof XmlFile) {
            final XmlFile xmlFile = (XmlFile) containingFile;
            final XmlTag rootTag = xmlFile.getRootTag();
            if (rootTag != null && modificationService.prepareFileForWrite(xmlFile)) {
                final XmlTag propTag = rootTag.createChildTag(PROPERTY, rootTag.getNamespace(), null, false);
                propTag.setAttribute(NAME_ATTR, myCanonicalText);
                propTag.setAttribute(VALUE_ATTR, "");
                final DomElement contextElement = DomUtil.getDomElement(descriptor.getPsiElement());
                PsiElement generated;
                if (contextElement == null) {
                    generated = rootTag.addSubTag(propTag, true);
                } else {
                    final AntDomTarget containingTarget = contextElement.getParentOfType(AntDomTarget.class, false);
                    final DomElement anchor = containingTarget != null ? containingTarget : contextElement;
                    final XmlTag tag = anchor.getXmlTag();
                    if (!rootTag.equals(tag)) {
                        generated = tag.getParent().addBefore(propTag, tag);
                    } else {
                        generated = rootTag.addSubTag(propTag, true);
                    }
                }
                if (generated instanceof XmlTag) {
                    final XmlAttribute valueAttrib = ((XmlTag) generated).getAttribute(VALUE_ATTR);
                    if (valueAttrib != null) {
                        final XmlAttributeValue valueElement = valueAttrib.getValueElement();
                        if (valueElement instanceof Navigatable) {
                            result = (Navigatable) valueElement;
                        }
                    }
                }
                if (result == null && generated instanceof Navigatable) {
                    result = (Navigatable) generated;
                }
            }
        }
    }
    if (result != null) {
        result.navigate(true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) Navigatable(com.intellij.pom.Navigatable) DomElement(com.intellij.util.xml.DomElement) IProperty(com.intellij.lang.properties.IProperty) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiElement(com.intellij.psi.PsiElement) FileModificationService(com.intellij.codeInsight.FileModificationService) XmlTag(com.intellij.psi.xml.XmlTag)

Example 25 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class AntResolveTest method testPropertyInMacrodefParam.

public void testPropertyInMacrodefParam() throws Exception {
    PsiReference ref = configure();
    final PsiElement result = ref.resolve();
    assertTrue(result instanceof PomTargetPsiElement);
    final PsiElement naviElem = ((DomTarget) ((PomTargetPsiElement) result).getTarget()).getNavigationElement();
    final DomElement domElement = DomUtil.getDomElement(naviElem);
    assertNotNull(domElement);
    assertNotNull(domElement.getParentOfType(AntDomMacrodefAttribute.class, false));
}
Also used : DomElement(com.intellij.util.xml.DomElement) AntDomMacrodefAttribute(com.intellij.lang.ant.dom.AntDomMacrodefAttribute) PsiReference(com.intellij.psi.PsiReference) DomTarget(com.intellij.util.xml.DomTarget) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

DomElement (com.intellij.util.xml.DomElement)97 XmlTag (com.intellij.psi.xml.XmlTag)34 PsiElement (com.intellij.psi.PsiElement)19 Nullable (org.jetbrains.annotations.Nullable)19 NotNull (org.jetbrains.annotations.NotNull)15 Project (com.intellij.openapi.project.Project)11 PsiFile (com.intellij.psi.PsiFile)11 XmlFile (com.intellij.psi.xml.XmlFile)11 XmlAttribute (com.intellij.psi.xml.XmlAttribute)10 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)9 ArrayList (java.util.ArrayList)8 LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)7 ResourceElement (org.jetbrains.android.dom.resources.ResourceElement)6 DomManager (com.intellij.util.xml.DomManager)5 Style (org.jetbrains.android.dom.resources.Style)5 AntDomTarget (com.intellij.lang.ant.dom.AntDomTarget)4 XmlElement (com.intellij.psi.xml.XmlElement)4 DomElementProblemDescriptor (com.intellij.util.xml.highlighting.DomElementProblemDescriptor)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 TextRange (com.intellij.openapi.util.TextRange)3