Search in sources :

Example 56 with DomElement

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

the class RunTargetAction method findAntTarget.

@Nullable
private static Pair<AntBuildFileBase, AntDomTarget> findAntTarget(@NotNull AnActionEvent e) {
    final Editor editor = e.getData(CommonDataKeys.EDITOR);
    final Project project = e.getProject();
    if (project == null || editor == null) {
        return null;
    }
    final VirtualFile file = e.getData(CommonDataKeys.VIRTUAL_FILE);
    if (file == null) {
        return null;
    }
    final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
    if (!(psiFile instanceof XmlFile)) {
        return null;
    }
    final XmlFile xmlFile = (XmlFile) psiFile;
    final AntBuildFileBase antFile = AntConfigurationBase.getInstance(project).getAntBuildFile(xmlFile);
    if (antFile == null) {
        return null;
    }
    final PsiElement element = xmlFile.findElementAt(editor.getCaretModel().getOffset());
    if (element == null) {
        return null;
    }
    final XmlTag xmlTag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
    if (xmlTag == null) {
        return null;
    }
    DomElement dom = AntSupport.getAntDomElement(xmlTag);
    while (dom != null && !(dom instanceof AntDomTarget)) {
        dom = dom.getParent();
    }
    final AntDomTarget domTarget = (AntDomTarget) dom;
    if (domTarget == null) {
        return null;
    }
    return Pair.create(antFile, domTarget);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) AntBuildFileBase(com.intellij.lang.ant.config.AntBuildFileBase) DomElement(com.intellij.util.xml.DomElement) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 57 with DomElement

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

the class AntDomPropertyReference method handleElementRename.

public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    final MyResolveResult resolveResult = doResolve();
    if (resolveResult != null) {
        final PsiElement resolve = resolveResult.getElement();
        final PropertiesProvider provider = resolveResult.getProvider();
        final String refText = getCanonicalText();
        if (provider instanceof AntDomProject) {
            final DomElement resolvedDomElem = AntDomReferenceBase.toDomElement(resolve);
            if (provider.equals(resolvedDomElem)) {
                final String oldProjectName = ((AntDomProject) provider).getName().getValue();
                if (oldProjectName != null && refText.endsWith(oldProjectName)) {
                    final String prefix = refText.substring(0, refText.length() - oldProjectName.length());
                    newElementName = prefix + newElementName;
                }
            }
        } else if (provider instanceof AntDomProperty) {
            final AntDomProperty antProperty = (AntDomProperty) provider;
            if (antProperty.equals(AntDomReferenceBase.toDomElement(resolve))) {
                String envPrefix = antProperty.getEnvironment().getValue();
                if (envPrefix != null) {
                    if (!envPrefix.endsWith(".")) {
                        envPrefix = envPrefix + ".";
                    }
                    if (refText.startsWith(envPrefix)) {
                        final String envVariableName = refText.substring(envPrefix.length());
                        final String newPrefix = newElementName.endsWith(".") ? newElementName : newElementName + ".";
                        newElementName = newPrefix + envVariableName;
                    }
                }
            } else {
                final String prefix = antProperty.getPropertyPrefixValue();
                if (prefix != null) {
                    newElementName = prefix + newElementName;
                }
            }
        }
    }
    return super.handleElementRename(newElementName);
}
Also used : DomElement(com.intellij.util.xml.DomElement) PsiElement(com.intellij.psi.PsiElement)

Example 58 with DomElement

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

the class AntDomPropertyReference method isReferenceTo.

public boolean isReferenceTo(PsiElement element) {
    // optimization to exclude obvious variants
    final DomElement domElement = AntDomReferenceBase.toDomElement(element);
    if (domElement instanceof AntDomProperty) {
        final AntDomProperty prop = (AntDomProperty) domElement;
        final String propName = prop.getName().getRawText();
        if (propName != null && prop.getPrefix().getRawText() == null && prop.getEnvironment().getRawText() == null) {
            // if only 'name' attrib is specified  
            if (!propName.equalsIgnoreCase(getCanonicalText())) {
                return false;
            }
        }
    }
    return super.isReferenceTo(element);
}
Also used : DomElement(com.intellij.util.xml.DomElement)

Example 59 with DomElement

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

the class AntDomTargetReference method bindToElement.

public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
    final DomElement targetDomElement = toDomElement(element);
    if (targetDomElement != null) {
        final AntDomTarget pointingToTarget = targetDomElement.getParentOfType(AntDomTarget.class, false);
        if (pointingToTarget != null) {
            // the aim here is to receive all variants available at this particular context
            final TargetResolver.Result result = doResolve(null);
            if (result != null) {
                final Map<String, AntDomTarget> variants = result.getVariants();
                String newName = null;
                if (!variants.isEmpty()) {
                    List<Couple<String>> prefixNamePairs = null;
                    for (Map.Entry<String, AntDomTarget> entry : variants.entrySet()) {
                        final AntDomTarget candidateTarget = entry.getValue();
                        if (pointingToTarget.equals(candidateTarget)) {
                            final String candidateName = entry.getKey();
                            final String candidateTargetName = candidateTarget.getName().getRawText();
                            if (candidateName.endsWith(candidateTargetName)) {
                                final String prefix = candidateName.substring(0, candidateName.length() - candidateTargetName.length());
                                if (prefixNamePairs == null) {
                                    // lazy init
                                    prefixNamePairs = new ArrayList<>();
                                }
                                prefixNamePairs.add(Couple.of(prefix, candidateName));
                            }
                        }
                    }
                    final String currentRefText = getCanonicalText();
                    for (Couple<String> pair : prefixNamePairs) {
                        final String prefix = pair.getFirst();
                        final String effectiveName = pair.getSecond();
                        if (currentRefText.startsWith(prefix)) {
                            if (newName == null || effectiveName.length() > newName.length()) {
                                // this candidate's prefix matches current reference text and this name is longer 
                                // than the previous candidate, then prefer this name
                                newName = effectiveName;
                            }
                        }
                    }
                }
                if (newName != null) {
                    handleElementRename(newName);
                    if (myGroup != null) {
                        myGroup.textChanged(this, newName);
                    }
                }
            }
        }
    }
    return getElement();
}
Also used : DomElement(com.intellij.util.xml.DomElement) Couple(com.intellij.openapi.util.Couple)

Example 60 with DomElement

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

the class AntReferenceInjector method addPropertyReferences.

private static void addPropertyReferences(@NotNull ConvertContext context, final XmlAttributeValue xmlAttributeValue, final Collection<PsiReference> result) {
    final String value = xmlAttributeValue.getValue();
    final DomElement contextElement = context.getInvocationElement();
    final XmlAttribute attrib = PsiTreeUtil.getParentOfType(xmlAttributeValue, XmlAttribute.class);
    if (attrib != null) {
        final String name = attrib.getName();
        if ("if".equals(name) || "unless".equals(name)) {
            // special handling of if/unless attributes
            final AntDomPropertyReference ref = new AntDomPropertyReference(contextElement, xmlAttributeValue, ElementManipulators.getValueTextRange(xmlAttributeValue));
            // in runtime, if execution reaches this task the property is defined since it is used in if-condition
            // so it is would be a mistake to highlight this as unresolved prop
            ref.setShouldBeSkippedByAnnotator(true);
            result.add(ref);
            return;
        }
    }
    if (xmlAttributeValue != null) /*&& value.indexOf("@{") < 0*/
    {
        final int valueBeginingOffset = Math.abs(xmlAttributeValue.getTextRange().getStartOffset() - xmlAttributeValue.getValueTextRange().getStartOffset());
        int startIndex;
        int endIndex = -1;
        while ((startIndex = value.indexOf("${", endIndex + 1)) > endIndex) {
            if (startIndex > 0 && value.charAt(startIndex - 1) == '$') {
                // the '$' is escaped
                endIndex = startIndex + 1;
                continue;
            }
            startIndex += 2;
            endIndex = startIndex;
            int nestedBrackets = 0;
            while (value.length() > endIndex) {
                final char ch = value.charAt(endIndex);
                if (ch == '}') {
                    if (nestedBrackets == 0) {
                        break;
                    }
                    --nestedBrackets;
                } else if (ch == '{') {
                    ++nestedBrackets;
                }
                ++endIndex;
            }
            if (nestedBrackets > 0 || endIndex > value.length())
                return;
            if (endIndex >= startIndex) {
                //final String propName = value.substring(startIndex, endIndex);
                //if (antFile.isEnvironmentProperty(propName) && antFile.getProperty(propName) == null) {
                //  continue;
                //}
                final AntDomPropertyReference ref = new AntDomPropertyReference(contextElement, xmlAttributeValue, new TextRange(valueBeginingOffset + startIndex, valueBeginingOffset + endIndex));
                result.add(ref);
            }
            endIndex = startIndex;
        }
    }
}
Also used : DomElement(com.intellij.util.xml.DomElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) TextRange(com.intellij.openapi.util.TextRange)

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