Search in sources :

Example 1 with SystemResourceManager

use of org.jetbrains.android.resourceManagers.SystemResourceManager in project android by JetBrains.

the class AndroidXmlDocumentationProvider method generateDoc.

@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    if (element instanceof ProvidedDocumentationPsiElement) {
        return ((ProvidedDocumentationPsiElement) element).getDocumentation();
    }
    if (element instanceof LazyValueResourceElementWrapper) {
        LazyValueResourceElementWrapper wrapper = (LazyValueResourceElementWrapper) element;
        ValueResourceInfo resourceInfo = wrapper.getResourceInfo();
        ResourceType type = resourceInfo.getType();
        String name = resourceInfo.getName();
        Module module = ModuleUtilCore.findModuleForPsiElement(element);
        if (module == null) {
            return null;
        }
        AndroidFacet facet = AndroidFacet.getInstance(element);
        if (facet == null) {
            return null;
        }
        ResourceUrl url;
        ResourceUrl originalUrl = originalElement != null ? ResourceUrl.parse(originalElement.getText()) : null;
        if (originalUrl != null && name.equals(originalUrl.name)) {
            url = originalUrl;
        } else {
            boolean isFramework = false;
            if (originalUrl != null) {
                isFramework = originalUrl.framework;
            } else {
                // Figure out if this resource is a framework file.
                // We really should store that info in the ValueResourceInfo instances themselves.
                // For now, attempt to figure it out
                SystemResourceManager systemResourceManager = facet.getSystemResourceManager();
                VirtualFile containingFile = resourceInfo.getContainingFile();
                if (systemResourceManager != null) {
                    VirtualFile parent = containingFile.getParent();
                    if (parent != null) {
                        VirtualFile resDir = parent.getParent();
                        if (resDir != null) {
                            isFramework = systemResourceManager.isResourceDir(resDir);
                        }
                    }
                }
            }
            url = ResourceUrl.create(type, name, isFramework, false);
        }
        return generateDoc(element, url);
    } else if (element instanceof MyResourceElement) {
        return getResourceDocumentation(element, ((MyResourceElement) element).myResource);
    } else if (element instanceof XmlAttributeValue) {
        return getResourceDocumentation(element, ((XmlAttributeValue) element).getValue());
    }
    if (originalElement instanceof XmlToken) {
        XmlToken token = (XmlToken) originalElement;
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_START_DELIMITER) {
            PsiElement next = token.getNextSibling();
            if (next instanceof XmlToken) {
                token = (XmlToken) next;
            }
        } else if (token.getTokenType() == XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            PsiElement prev = token.getPrevSibling();
            if (prev instanceof XmlToken) {
                token = (XmlToken) prev;
            }
        }
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_TOKEN) {
            String documentation = getResourceDocumentation(originalElement, token.getText());
            if (documentation != null) {
                return documentation;
            }
        } else if (token.getTokenType() == XML_DATA_CHARACTERS) {
            String text = token.getText().trim();
            String documentation = getResourceDocumentation(originalElement, text);
            if (documentation != null) {
                return documentation;
            }
        }
    }
    if (element instanceof PomTargetPsiElement && originalElement != null) {
        final PomTarget target = ((PomTargetPsiElement) element).getTarget();
        if (target instanceof DomAttributeChildDescription) {
            synchronized (ANDROID_ATTRIBUTE_DOCUMENTATION_CACHE_KEY) {
                return generateDocForXmlAttribute((DomAttributeChildDescription) target, originalElement);
            }
        }
    }
    if (element instanceof MyDocElement) {
        return ((MyDocElement) element).myDocumentation;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) DomAttributeChildDescription(com.intellij.util.xml.reflect.DomAttributeChildDescription) XmlToken(com.intellij.psi.xml.XmlToken) PomTarget(com.intellij.pom.PomTarget) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Module(com.intellij.openapi.module.Module) ResourceUrl(com.android.ide.common.resources.ResourceUrl) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) FakePsiElement(com.intellij.psi.impl.FakePsiElement) PsiElement(com.intellij.psi.PsiElement) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) ValueResourceInfo(org.jetbrains.android.resourceManagers.ValueResourceInfo)

Example 2 with SystemResourceManager

use of org.jetbrains.android.resourceManagers.SystemResourceManager in project android by JetBrains.

the class SetAttributeQuickFix method askForAttributeValue.

@Nullable
private String askForAttributeValue(@NotNull PsiElement context) {
    final AndroidFacet facet = AndroidFacet.getInstance(context);
    final String message = "Specify value of attribute '" + myAttributeName + "'";
    final String title = "Set Attribute Value";
    if (facet != null) {
        final SystemResourceManager srm = facet.getSystemResourceManager();
        if (srm != null) {
            final AttributeDefinitions attrDefs = srm.getAttributeDefinitions();
            if (attrDefs != null) {
                final AttributeDefinition def = attrDefs.getAttrDefByName(myAttributeName);
                if (def != null) {
                    final String[] variants = def.getValues();
                    if (variants.length > 0) {
                        return Messages.showEditableChooseDialog(message, title, Messages.getQuestionIcon(), variants, variants[0], null);
                    }
                }
            }
        }
    }
    return Messages.showInputDialog(context.getProject(), message, title, Messages.getQuestionIcon());
}
Also used : AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with SystemResourceManager

use of org.jetbrains.android.resourceManagers.SystemResourceManager 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)

Aggregations

SystemResourceManager (org.jetbrains.android.resourceManagers.SystemResourceManager)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 ResourceUrl (com.android.ide.common.resources.ResourceUrl)1 ResourceType (com.android.resources.ResourceType)1 Module (com.intellij.openapi.module.Module)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PomTarget (com.intellij.pom.PomTarget)1 PomTargetPsiElement (com.intellij.pom.PomTargetPsiElement)1 PsiElement (com.intellij.psi.PsiElement)1 FakePsiElement (com.intellij.psi.impl.FakePsiElement)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 XmlTag (com.intellij.psi.xml.XmlTag)1 XmlToken (com.intellij.psi.xml.XmlToken)1 XmlName (com.intellij.util.xml.XmlName)1 DomAttributeChildDescription (com.intellij.util.xml.reflect.DomAttributeChildDescription)1 InterpolatorElement (org.jetbrains.android.dom.animation.InterpolatorElement)1 AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)1 AttributeDefinitions (org.jetbrains.android.dom.attrs.AttributeDefinitions)1 ManifestElement (org.jetbrains.android.dom.manifest.ManifestElement)1 MenuItem (org.jetbrains.android.dom.menu.MenuItem)1