Search in sources :

Example 21 with XmlElement

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

the class AntDomExtender method registerExtensions.

public void registerExtensions(@NotNull final AntDomElement antDomElement, @NotNull DomExtensionsRegistrar registrar) {
    final XmlElement xmlElement = antDomElement.getXmlElement();
    if (xmlElement instanceof XmlTag) {
        final XmlTag xmlTag = (XmlTag) xmlElement;
        final String tagName = xmlTag.getName();
        final AntDomProject antProject = antDomElement.getAntProject();
        if (antProject == null) {
            return;
        }
        final ReflectedProject reflected = ReflectedProject.getProject(antProject.getClassLoader());
        if (reflected.getProject() == null) {
            return;
        }
        final DomGenericInfo genericInfo = antDomElement.getGenericInfo();
        AntIntrospector classBasedIntrospector = null;
        final Hashtable<String, Class> coreTaskDefs = reflected.getTaskDefinitions();
        final Hashtable<String, Class> coreTypeDefs = reflected.getDataTypeDefinitions();
        final boolean isCustom = antDomElement instanceof AntDomCustomElement;
        if ("project".equals(tagName)) {
            classBasedIntrospector = getIntrospector(reflected.getProject().getClass());
        } else if ("target".equals(tagName)) {
            classBasedIntrospector = getIntrospector(reflected.getTargetClass());
        } else {
            if (isCustom) {
                final AntDomCustomElement custom = (AntDomCustomElement) antDomElement;
                final Class definitionClass = custom.getDefinitionClass();
                if (definitionClass != null) {
                    classBasedIntrospector = getIntrospector(definitionClass);
                }
            } else {
                Class elemType = antDomElement.getChildDescription().getUserData(ELEMENT_IMPL_CLASS_KEY);
                if (elemType == null) {
                    if (coreTaskDefs != null) {
                        elemType = coreTaskDefs.get(tagName);
                    }
                }
                if (elemType == null) {
                    if (coreTypeDefs != null) {
                        elemType = coreTypeDefs.get(tagName);
                    }
                }
                if (elemType != null) {
                    classBasedIntrospector = getIntrospector(elemType);
                }
            }
        }
        AbstractIntrospector parentIntrospector = null;
        if (classBasedIntrospector != null) {
            parentIntrospector = new ClassIntrospectorAdapter(classBasedIntrospector, coreTaskDefs, coreTypeDefs);
        } else {
            if (isCustom) {
                final AntDomNamedElement declaringElement = ((AntDomCustomElement) antDomElement).getDeclaringElement();
                if (declaringElement instanceof AntDomMacroDef) {
                    parentIntrospector = new MacrodefIntrospectorAdapter((AntDomMacroDef) declaringElement);
                } else if (declaringElement instanceof AntDomMacrodefElement) {
                    parentIntrospector = new MacrodefElementOccurrenceIntrospectorAdapter((AntDomMacrodefElement) declaringElement);
                } else if (declaringElement instanceof AntDomScriptDef) {
                    parentIntrospector = new ScriptdefIntrospectorAdapter((AntDomScriptDef) declaringElement);
                }
            }
        }
        if (parentIntrospector != null) {
            defineAttributes(xmlTag, registrar, genericInfo, parentIntrospector);
            if ("project".equals(tagName) || parentIntrospector.isContainer()) {
                // can contain any task or/and type definition
                if (coreTaskDefs != null) {
                    for (Map.Entry<String, Class> entry : coreTaskDefs.entrySet()) {
                        final DomExtension extension = registerChild(registrar, genericInfo, entry.getKey());
                        if (extension != null) {
                            final Class type = entry.getValue();
                            if (type != null) {
                                extension.putUserData(ELEMENT_IMPL_CLASS_KEY, type);
                            }
                            extension.putUserData(AntDomElement.ROLE, AntDomElement.Role.TASK);
                        }
                    }
                }
                if (coreTypeDefs != null) {
                    for (Map.Entry<String, Class> entry : coreTypeDefs.entrySet()) {
                        final DomExtension extension = registerChild(registrar, genericInfo, entry.getKey());
                        if (extension != null) {
                            final Class type = entry.getValue();
                            if (type != null) {
                                extension.putUserData(ELEMENT_IMPL_CLASS_KEY, type);
                            }
                            extension.putUserData(AntDomElement.ROLE, AntDomElement.Role.DATA_TYPE);
                        }
                    }
                }
                registrar.registerCustomChildrenExtension(AntDomCustomElement.class, new AntCustomTagNameDescriptor());
            } else {
                final Iterator<String> nested = parentIntrospector.getNestedElementsIterator();
                while (nested.hasNext()) {
                    final String nestedElementName = nested.next();
                    final DomExtension extension = registerChild(registrar, genericInfo, nestedElementName);
                    if (extension != null) {
                        Class type = parentIntrospector.getNestedElementType(nestedElementName);
                        if (type != null && CommonClassNames.JAVA_LANG_OBJECT.equals(type.getName())) {
                            // hack to support badly written tasks 
                            type = null;
                        }
                        if (type == null) {
                            if (coreTypeDefs != null) {
                                type = coreTypeDefs.get(nestedElementName);
                            }
                        }
                        if (type != null) {
                            extension.putUserData(ELEMENT_IMPL_CLASS_KEY, type);
                        }
                        AntDomElement.Role role = AntDomElement.Role.DATA_TYPE;
                        if (coreTaskDefs != null && coreTaskDefs.containsKey(nestedElementName)) {
                            role = AntDomElement.Role.TASK;
                        } else if (type != null && isAssignableFrom(Task.class.getName(), type)) {
                            role = AntDomElement.Role.TASK;
                        }
                        if (role != null) {
                            extension.putUserData(AntDomElement.ROLE, role);
                        }
                    }
                }
                registrar.registerCustomChildrenExtension(AntDomCustomElement.class, new AntCustomTagNameDescriptor());
            }
        }
    }
}
Also used : Task(org.apache.tools.ant.Task) ReflectedProject(com.intellij.lang.ant.ReflectedProject) AntIntrospector(com.intellij.lang.ant.AntIntrospector) XmlElement(com.intellij.psi.xml.XmlElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 22 with XmlElement

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

the class AntDomDocumentationProvider method getQuickNavigateInfo.

@Nullable
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) {
    // todo!
    if (element instanceof PomTargetPsiElement) {
        final PomTarget pomTarget = ((PomTargetPsiElement) element).getTarget();
        if (pomTarget instanceof DomTarget) {
            final DomElement domElement = ((DomTarget) pomTarget).getDomElement();
            if (domElement instanceof AntDomTarget) {
                final AntDomTarget antTarget = (AntDomTarget) domElement;
                final String description = antTarget.getDescription().getRawText();
                if (description != null && description.length() > 0) {
                    final String targetName = antTarget.getName().getRawText();
                    final StringBuilder builder = StringBuilderSpinAllocator.alloc();
                    try {
                        builder.append("Target");
                        if (targetName != null) {
                            builder.append(" \"").append(targetName).append("\"");
                        }
                        final XmlElement xmlElement = antTarget.getXmlElement();
                        if (xmlElement != null) {
                            final PsiFile containingFile = xmlElement.getContainingFile();
                            if (containingFile != null) {
                                final String fileName = containingFile.getName();
                                builder.append(" [").append(fileName).append("]");
                            }
                        }
                        return builder.append(" ").append(description).toString();
                    } finally {
                        StringBuilderSpinAllocator.dispose(builder);
                    }
                }
            }
        } else if (pomTarget instanceof DomChildrenDescription) {
            final DomChildrenDescription description = (DomChildrenDescription) pomTarget;
            Type type = null;
            try {
                type = description.getType();
            } catch (UnsupportedOperationException e) {
                LOG.info(e);
            }
            if (type instanceof Class && AntDomElement.class.isAssignableFrom(((Class) type))) {
                final String elemName = description.getName();
                if (elemName != null) {
                    final AntDomElement.Role role = description.getUserData(AntDomElement.ROLE);
                    final StringBuilder builder = StringBuilderSpinAllocator.alloc();
                    try {
                        if (role == AntDomElement.Role.TASK) {
                            builder.append("Task ");
                        } else if (role == AntDomElement.Role.DATA_TYPE) {
                            builder.append("Data structure ");
                        }
                        builder.append(elemName);
                        return builder.toString();
                    } finally {
                        StringBuilderSpinAllocator.dispose(builder);
                    }
                }
            }
        }
    }
    return null;
}
Also used : AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) DomTarget(com.intellij.util.xml.DomTarget) DomChildrenDescription(com.intellij.util.xml.reflect.DomChildrenDescription) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) PomTarget(com.intellij.pom.PomTarget) Type(java.lang.reflect.Type) DomElement(com.intellij.util.xml.DomElement) AntDomElement(com.intellij.lang.ant.dom.AntDomElement) XmlElement(com.intellij.psi.xml.XmlElement) PsiFile(com.intellij.psi.PsiFile) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with XmlElement

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

the class AntDomProject method getNavigationElement.

@Nullable
public PsiElement getNavigationElement(String propertyName) {
    final DomTarget target = DomTarget.getTarget(this);
    final PsiElement nameElementPsi = target != null ? PomService.convertToPsi(target) : null;
    if (nameElementPsi != null) {
        return nameElementPsi;
    }
    final XmlElement xmlElement = getXmlElement();
    return xmlElement != null ? xmlElement.getNavigationElement() : null;
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with XmlElement

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

the class AntResolveInspection method getPropertyFiles.

@NotNull
private static Collection<PropertiesFile> getPropertyFiles(@Nullable AntDomProject antDomProject, @NotNull XmlElement stopElement) {
    if (antDomProject == null) {
        return Collections.emptyList();
    }
    final Set<PropertiesFile> files = new java.util.HashSet<>();
    final int stopOffset = stopElement.getTextOffset();
    for (Iterator<AntDomElement> iterator = antDomProject.getAntChildrenIterator(); iterator.hasNext(); ) {
        AntDomElement child = iterator.next();
        final XmlElement xmlElement = child.getXmlElement();
        if (xmlElement != null && xmlElement.getTextOffset() >= stopOffset) {
            // no need to offer to add properties to files that are imported after the property reference
            break;
        }
        if (child instanceof AntDomProperty) {
            final AntDomProperty property = (AntDomProperty) child;
            final PropertiesFile file = property.getPropertiesFile();
            if (file != null) {
                files.add(file);
            }
        }
    }
    return files;
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with XmlElement

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

the class AntDomTargetDependsListConverter method createReferences.

@NotNull
public PsiReference[] createReferences(GenericDomValue<TargetResolver.Result> value, PsiElement element, ConvertContext context) {
    final XmlElement xmlElement = value.getXmlElement();
    if (!(xmlElement instanceof XmlAttribute)) {
        return PsiReference.EMPTY_ARRAY;
    }
    final XmlAttributeValue valueElement = ((XmlAttribute) xmlElement).getValueElement();
    if (valueElement == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final String refsString = value.getStringValue();
    if (refsString == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final List<PsiReference> refs = new ArrayList<>();
    final AntDomTargetReference.ReferenceGroup group = new AntDomTargetReference.ReferenceGroup();
    final TextRange wholeStringRange = ElementManipulators.getValueTextRange(valueElement);
    final StringTokenizer tokenizer = new StringTokenizer(refsString, ",", false);
    while (tokenizer.hasMoreTokens()) {
        final String token = tokenizer.nextToken();
        int tokenStartOffset = tokenizer.getCurrentPosition() - token.length();
        final String ref = token.trim();
        if (ref.length() != token.length()) {
            for (int idx = 0; idx < token.length(); idx++) {
                if (Character.isWhitespace(token.charAt(idx))) {
                    tokenStartOffset++;
                } else {
                    break;
                }
            }
        }
        refs.add(new AntDomTargetReference(element, TextRange.from(wholeStringRange.getStartOffset() + tokenStartOffset, ref.length()), group));
    }
    return refs.toArray(new PsiReference[refs.size()]);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) StringTokenizer(com.intellij.util.text.StringTokenizer) XmlElement(com.intellij.psi.xml.XmlElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

XmlElement (com.intellij.psi.xml.XmlElement)69 PsiElement (com.intellij.psi.PsiElement)16 NotNull (org.jetbrains.annotations.NotNull)16 Nullable (org.jetbrains.annotations.Nullable)11 XmlTag (com.intellij.psi.xml.XmlTag)10 PsiFile (com.intellij.psi.PsiFile)7 PsiReference (com.intellij.psi.PsiReference)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)6 XmlFile (com.intellij.psi.xml.XmlFile)6 Manifest (org.jetbrains.android.dom.manifest.Manifest)5 Module (com.intellij.openapi.module.Module)4 Project (com.intellij.openapi.project.Project)4 DomElement (com.intellij.util.xml.DomElement)4 ArrayList (java.util.ArrayList)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 CompositePsiElement (com.intellij.psi.impl.source.tree.CompositePsiElement)3 SmartList (com.intellij.util.SmartList)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 ResourceType (com.android.resources.ResourceType)2 Result (com.intellij.openapi.application.Result)2