Search in sources :

Example 76 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.

the class DbLanguageInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces injectionPlacesRegistrar) {
    if (!(host instanceof XmlAttributeValue)) {
        return;
    }
    String valueText = ((XmlAttributeValue) host).getValue();
    if (!DataBindingUtil.isBindingExpression(valueText)) {
        return;
    }
    String prefix = valueText.startsWith(PREFIX_TWOWAY_BINDING_EXPR) ? PREFIX_TWOWAY_BINDING_EXPR : PREFIX_BINDING_EXPR;
    PsiElement parent = host.getParent();
    if (!(parent instanceof XmlAttribute))
        return;
    GenericAttributeValue element = DomManager.getDomManager(host.getProject()).getDomElement((XmlAttribute) parent);
    if (element == null || !(element.getParent() instanceof AndroidDomElement))
        return;
    // Parser only parses the expression, not the prefix '@{' or the suffix '}'. Extract the start/end index of the expression.
    String unescapedValue = host.getText();
    int startIndex = unescapedValue.indexOf(prefix.charAt(0)) + prefix.length();
    int endIndex;
    if (valueText.endsWith("}")) {
        endIndex = unescapedValue.lastIndexOf('}');
    } else {
        if (host.getNode().getLastChildNode().getElementType() == XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            endIndex = host.getLastChild().getStartOffsetInParent();
        } else {
            endIndex = unescapedValue.length();
        }
    }
    if (endIndex == startIndex) {
        // No expression found.
        return;
    }
    injectionPlacesRegistrar.addPlace(DbLanguage.INSTANCE, TextRange.from(startIndex, endIndex - startIndex), null, null);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidDomElement(org.jetbrains.android.dom.AndroidDomElement) PsiElement(com.intellij.psi.PsiElement) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue)

Example 77 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.

the class AndroidFindStyleApplicationsAction method getStyleData.

@Nullable
static MyStyleData getStyleData(@NotNull XmlTag tag) {
    final DomElement element = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
    if (!(element instanceof Style)) {
        return null;
    }
    final Style style = (Style) element;
    final GenericAttributeValue<String> styleNameDomAttr = style.getName();
    final String styleName = styleNameDomAttr.getStringValue();
    final XmlAttributeValue styleNameAttrValue = styleNameDomAttr.getXmlAttributeValue();
    if (styleName == null || styleName.length() == 0 || styleNameAttrValue == null) {
        return null;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(tag);
    if (facet == null) {
        return null;
    }
    return new MyStyleData(style, styleName, facet, styleNameAttrValue);
}
Also used : DomElement(com.intellij.util.xml.DomElement) Style(org.jetbrains.android.dom.resources.Style) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 78 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.

the class AndroidFindStyleApplicationsProcessor method collectResDir.

private static void collectResDir(Module module, XmlAttributeValue styleNameAttrValue, String styleName, List<VirtualFile> resDirs) {
    final AndroidFacet f = AndroidFacet.getInstance(module);
    if (f == null) {
        return;
    }
    final List<ValueResourceInfoImpl> resolvedStyles = f.getLocalResourceManager().findValueResourceInfos(ResourceType.STYLE.getName(), styleName, true, false);
    if (resolvedStyles.size() == 1) {
        final XmlAttributeValue resolvedStyleNameElement = resolvedStyles.get(0).computeXmlElement();
        if (resolvedStyleNameElement != null && resolvedStyleNameElement.equals(styleNameAttrValue)) {
            resDirs.addAll(f.getAllResourceDirectories());
        }
    }
}
Also used : XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) ValueResourceInfoImpl(org.jetbrains.android.resourceManagers.ValueResourceInfoImpl) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 79 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.

the class AndroidInlineUtil method getInlinableStyleData.

@Nullable
static MyStyleData getInlinableStyleData(@NotNull XmlTag tag) {
    final DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
    if (!(domElement instanceof Style)) {
        return null;
    }
    final Style style = (Style) domElement;
    final XmlAttributeValue nameAttrValue = style.getName().getXmlAttributeValue();
    if (nameAttrValue == null) {
        return null;
    }
    final String styleName = style.getName().getStringValue();
    if (styleName == null || styleName.length() == 0) {
        return null;
    }
    return new MyStyleData(styleName, style, nameAttrValue);
}
Also used : DomElement(com.intellij.util.xml.DomElement) Style(org.jetbrains.android.dom.resources.Style) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Example 80 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.

the class InlinedResource method getResolvedString.

@Nullable
public String getResolvedString() {
    if (myResourceRepository != null) {
        if (myResourceRepository.hasResourceItem(myType, myKey)) {
            FolderConfiguration referenceConfig = new FolderConfiguration();
            // Nonexistent language qualifier: trick it to fall back to the default locale
            referenceConfig.setLocaleQualifier(new LocaleQualifier("xx"));
            ResourceValue value = myResourceRepository.getConfiguredValue(myType, myKey, referenceConfig);
            if (value != null) {
                String text = value.getValue();
                if (text != null) {
                    if (myElement instanceof PsiMethodCallExpression) {
                        text = insertArguments((PsiMethodCallExpression) myElement, text);
                    }
                    if (myType == ResourceType.PLURALS && text.startsWith(STRING_PREFIX)) {
                        value = myResourceRepository.getConfiguredValue(ResourceType.STRING, text.substring(STRING_PREFIX.length()), referenceConfig);
                        if (value != null && value.getValue() != null) {
                            text = value.getValue();
                            return '"' + StringUtil.shortenTextWithEllipsis(text, FOLD_MAX_LENGTH - 2, 0) + '"';
                        }
                    }
                    if (myType == ResourceType.STRING || myElement instanceof XmlAttributeValue) {
                        return '"' + StringUtil.shortenTextWithEllipsis(text, FOLD_MAX_LENGTH - 2, 0) + '"';
                    } else if (text.length() <= 1) {
                        // This is similar to how IntelliJ 14 handles call parameters
                        return myKey + ": " + text;
                    } else {
                        return StringUtil.shortenTextWithEllipsis(text, FOLD_MAX_LENGTH, 0);
                    }
                }
            }
        }
    }
    return null;
}
Also used : ResourceValue(com.android.ide.common.rendering.api.ResourceValue) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)127 XmlAttribute (com.intellij.psi.xml.XmlAttribute)57 XmlTag (com.intellij.psi.xml.XmlTag)50 NotNull (org.jetbrains.annotations.NotNull)38 PsiElement (com.intellij.psi.PsiElement)31 Nullable (org.jetbrains.annotations.Nullable)24 PsiReference (com.intellij.psi.PsiReference)20 XmlFile (com.intellij.psi.xml.XmlFile)19 TextRange (com.intellij.openapi.util.TextRange)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 ArrayList (java.util.ArrayList)13 Project (com.intellij.openapi.project.Project)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 DomElement (com.intellij.util.xml.DomElement)9 PsiFile (com.intellij.psi.PsiFile)8 LazyValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper)7 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Module (com.intellij.openapi.module.Module)5 ValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)5