Search in sources :

Example 1 with DbFile

use of com.android.tools.idea.lang.databinding.DbFile in project android by JetBrains.

the class DataBindingUtil method getBindingExprDefault.

@Nullable
public static String getBindingExprDefault(@NotNull XmlAttribute psiAttribute) {
    XmlAttributeValue attrValue = psiAttribute.getValueElement();
    if (attrValue instanceof PsiLanguageInjectionHost) {
        final Ref<PsiElement> injections = Ref.create();
        InjectedLanguageUtil.enumerate(attrValue, (injectedPsi, places) -> {
            if (injectedPsi instanceof DbFile) {
                injections.set(injectedPsi);
            }
        });
        if (injections.get() != null) {
            PsiDbDefaults defaults = PsiTreeUtil.getChildOfType(injections.get(), PsiDbDefaults.class);
            if (defaults != null) {
                PsiDbConstantValue constantValue = defaults.getConstantValue();
                ASTNode stringLiteral = constantValue.getNode().findChildByType(DbTokenTypes.STRING_LITERAL);
                if (stringLiteral == null) {
                    return constantValue.getText();
                } else {
                    String text = stringLiteral.getText();
                    if (text.length() > 1) {
                        // return unquoted string literal.
                        return text.substring(1, text.length() - 1);
                    } else {
                        return text;
                    }
                }
            }
        }
    }
    return null;
}
Also used : PsiDbConstantValue(com.android.tools.idea.lang.databinding.psi.PsiDbConstantValue) PsiDbDefaults(com.android.tools.idea.lang.databinding.psi.PsiDbDefaults) DbFile(com.android.tools.idea.lang.databinding.DbFile) ASTNode(com.intellij.lang.ASTNode) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

DbFile (com.android.tools.idea.lang.databinding.DbFile)1 PsiDbConstantValue (com.android.tools.idea.lang.databinding.psi.PsiDbConstantValue)1 PsiDbDefaults (com.android.tools.idea.lang.databinding.psi.PsiDbDefaults)1 ASTNode (com.intellij.lang.ASTNode)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 Nullable (org.jetbrains.annotations.Nullable)1