Search in sources :

Example 1 with XmlResourceElement

use of org.jetbrains.android.dom.xml.XmlResourceElement in project android by JetBrains.

the class AndroidDomUtil method getSpecificConverter.

@Nullable
public static Converter getSpecificConverter(@NotNull XmlName attrName, DomElement context) {
    if (context == null) {
        return null;
    }
    if (!SdkConstants.NS_RESOURCES.equals(attrName.getNamespaceKey())) {
        return null;
    }
    final XmlTag xmlTag = context.getXmlTag();
    if (xmlTag == null) {
        return null;
    }
    final String localName = attrName.getLocalName();
    final String tagName = xmlTag.getName();
    if (context instanceof XmlResourceElement) {
        if ("configure".equals(localName) && "appwidget-provider".equals(tagName)) {
            return ACTIVITY_CONVERTER;
        } else if (VIEW_FRAGMENT.equals(localName)) {
            return FRAGMENT_CLASS_CONVERTER;
        }
    } else if (context instanceof LayoutViewElement || context instanceof MenuItem) {
        if (ATTR_ON_CLICK.equals(localName)) {
            return context instanceof LayoutViewElement ? OnClickConverter.CONVERTER_FOR_LAYOUT : OnClickConverter.CONVERTER_FOR_MENU;
        }
    }
    return null;
}
Also used : LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlResourceElement(org.jetbrains.android.dom.xml.XmlResourceElement) MenuItem(org.jetbrains.android.dom.menu.MenuItem) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with XmlResourceElement

use of org.jetbrains.android.dom.xml.XmlResourceElement in project android by JetBrains.

the class AttributeProcessingUtil method processAttributes.

/**
   * Enumerate attributes that are available for the given XML tag, represented by {@link AndroidDomElement},
   * and "return" them via {@link AttributeProcessor}.
   *
   * Primary user is {@link AndroidDomExtender}, which uses it to provide code completion facilities when
   * editing XML files in text editor.
   *
   * Implementation of the method implements {@link Styleable} annotation handling and dispatches on tag type
   * using instanceof checks for adding attributes that don't come from styleable definitions with statically
   * known names.
   *
   * @param processAllExistingAttrsFirst whether already existing attributes should be returned first
   */
public static void processAttributes(@NotNull AndroidDomElement element, @NotNull AndroidFacet facet, boolean processAllExistingAttrsFirst, @NotNull AttributeProcessor callback) {
    XmlTag tag = element.getXmlTag();
    final Set<XmlName> skippedAttributes = processAllExistingAttrsFirst ? registerExistingAttributes(facet, tag, element, callback) : new HashSet<>();
    if (element instanceof ManifestElement) {
        processManifestAttributes(tag, element, callback);
    } else if (element instanceof LayoutElement) {
        processLayoutAttributes(facet, tag, (LayoutElement) element, skippedAttributes, callback);
    } else if (element instanceof XmlResourceElement) {
        processXmlAttributes(facet, tag, (XmlResourceElement) element, skippedAttributes, callback);
    } else if (element instanceof XmlRawResourceElement) {
        processRawAttributes(tag, callback);
    }
    // If DOM element is annotated with @Styleable annotation, load a styleable definition
    // from Android framework with the name provided in annotation and register all attributes
    // from it for code highlighting and completion.
    final Styleable styleableAnnotation = element.getAnnotation(Styleable.class);
    if (styleableAnnotation == null) {
        return;
    }
    final SystemResourceManager manager = facet.getSystemResourceManager();
    if (manager == null) {
        return;
    }
    final AttributeDefinitions definitions = manager.getAttributeDefinitions();
    if (definitions == null) {
        return;
    }
    if (element instanceof MenuItem) {
        processMenuItemAttributes(facet, element, skippedAttributes, callback);
        return;
    }
    for (String styleableName : styleableAnnotation.value()) {
        final StyleableDefinition styleable = definitions.getStyleableByName(styleableName);
        if (styleable == null) {
            // DOM element is annotated with @Styleable annotation, but styleable definition with
            // provided name is not there in Android framework. This is a bug, so logging it as a warning.
            getLog().warn(String.format("@Styleable(%s) annotation doesn't point to existing styleable", styleableName));
        } else {
            registerStyleableAttributes(element, styleable, ANDROID_URI, callback, skippedAttributes);
        }
    }
    // TODO: figure it out how to make it DRY without introducing new method with lots of arguments
    if (element instanceof InterpolatorElement) {
        final String styleableName = InterpolatorDomFileDescription.getInterpolatorStyleableByTagName(tag.getName());
        if (styleableName != null) {
            final StyleableDefinition styleable = definitions.getStyleableByName(styleableName);
            if (styleable == null) {
                getLog().warn(String.format("%s doesn't point to existing styleable for interpolator", styleableName));
            } else {
                registerStyleableAttributes(element, styleable, ANDROID_URI, callback, skippedAttributes);
            }
        }
    }
}
Also used : ManifestElement(org.jetbrains.android.dom.manifest.ManifestElement) XmlResourceElement(org.jetbrains.android.dom.xml.XmlResourceElement) MenuItem(org.jetbrains.android.dom.menu.MenuItem) XmlRawResourceElement(org.jetbrains.android.dom.raw.XmlRawResourceElement) InterpolatorElement(org.jetbrains.android.dom.animation.InterpolatorElement) XmlName(com.intellij.util.xml.XmlName) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with XmlResourceElement

use of org.jetbrains.android.dom.xml.XmlResourceElement in project android by JetBrains.

the class AndroidDomElementDescriptorProvider method getDomElementAndBaseClassQName.

@Nullable
public static Pair<AndroidDomElement, String> getDomElementAndBaseClassQName(@NotNull XmlTag tag) {
    final PsiFile file = tag.getContainingFile();
    if (!(file instanceof XmlFile))
        return null;
    Project project = file.getProject();
    if (project.isDefault())
        return null;
    final DomManager domManager = DomManager.getDomManager(project);
    if (domManager.getFileElement((XmlFile) file, AndroidDomElement.class) == null)
        return null;
    final DomElement domElement = domManager.getDomElement(tag);
    if (!(domElement instanceof AndroidDomElement)) {
        return null;
    }
    String className = null;
    if (domElement instanceof LayoutViewElement) {
        className = AndroidUtils.VIEW_CLASS_NAME;
    } else if (domElement instanceof XmlResourceElement) {
        className = SdkConstants.CLASS_PREFERENCE;
    }
    return Pair.create((AndroidDomElement) domElement, className);
}
Also used : Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlFile(com.intellij.psi.xml.XmlFile) DomManager(com.intellij.util.xml.DomManager) XmlResourceElement(org.jetbrains.android.dom.xml.XmlResourceElement) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlResourceElement (org.jetbrains.android.dom.xml.XmlResourceElement)3 XmlTag (com.intellij.psi.xml.XmlTag)2 LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)2 MenuItem (org.jetbrains.android.dom.menu.MenuItem)2 Nullable (org.jetbrains.annotations.Nullable)2 Project (com.intellij.openapi.project.Project)1 PsiFile (com.intellij.psi.PsiFile)1 XmlFile (com.intellij.psi.xml.XmlFile)1 DomElement (com.intellij.util.xml.DomElement)1 DomManager (com.intellij.util.xml.DomManager)1 XmlName (com.intellij.util.xml.XmlName)1 InterpolatorElement (org.jetbrains.android.dom.animation.InterpolatorElement)1 ManifestElement (org.jetbrains.android.dom.manifest.ManifestElement)1 XmlRawResourceElement (org.jetbrains.android.dom.raw.XmlRawResourceElement)1 SystemResourceManager (org.jetbrains.android.resourceManagers.SystemResourceManager)1