Search in sources :

Example 1 with DataBindingInfo

use of com.android.tools.idea.res.DataBindingInfo in project android by JetBrains.

the class DataBindingConverter method toString.

@Nullable
@Override
public String toString(@Nullable final PsiClass psiClass, final ConvertContext context) {
    if (psiClass == null) {
        return null;
    }
    final String qualifiedName = psiClass.getName();
    // try and replace with import if possible.
    final DataBindingInfo bindingInfo = getDataBindingInfo(context);
    if (bindingInfo != null) {
        int longestPrefix = 0;
        PsiDataBindingResourceItem longestImport = null;
        for (final PsiDataBindingResourceItem anImport : bindingInfo.getItems(DataBindingResourceType.IMPORT)) {
            // Try and find the longest matching import. For inner classes, either the outer or the inner class may be imported.
            final String prefix = getLongestPrefix(anImport.getTypeDeclaration(), qualifiedName);
            if (prefix.length() > longestPrefix) {
                if (qualifiedName.length() == prefix.length()) {
                    return AndroidLayoutUtil.getAlias(anImport);
                }
                final char c = qualifiedName.charAt(prefix.length());
                if (c == '.') {
                    longestPrefix = prefix.length();
                    longestImport = anImport;
                }
            }
        }
        if (longestImport != null) {
            return AndroidLayoutUtil.getAlias(longestImport) + qualifiedName.substring(longestPrefix);
        }
    }
    if (qualifiedName.startsWith(JAVA_LANG)) {
        return qualifiedName.substring(JAVA_LANG.length());
    }
    return qualifiedName;
}
Also used : PsiDataBindingResourceItem(com.android.tools.idea.res.PsiDataBindingResourceItem) DataBindingInfo(com.android.tools.idea.res.DataBindingInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with DataBindingInfo

use of com.android.tools.idea.res.DataBindingInfo in project android by JetBrains.

the class DataBindingConverter method createReferences.

@NotNull
@Override
public PsiReference[] createReferences(final GenericDomValue<PsiClass> value, final PsiElement element, final ConvertContext context) {
    assert element instanceof XmlAttributeValue;
    final XmlAttributeValue attrValue = (XmlAttributeValue) element;
    final String strValue = attrValue.getValue();
    final int start = attrValue.getValueTextRange().getStartOffset() - attrValue.getTextRange().getStartOffset();
    List<PsiReference> result = new ArrayList<>();
    final String[] nameParts = strValue.split("[$.]");
    Module module = context.getModule();
    if (nameParts.length == 0 || module == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    int offset = start;
    // Check if the first namePart is an alias.
    DataBindingInfo bindingInfo = getDataBindingInfo(context);
    // for iterating over the nameParts.
    int idx = 0;
    // difference in lengths of the "type" and the "alias". Used in range computation later.
    int diff = 0;
    String fullType = strValue.replace('$', '.');
    if (bindingInfo != null) {
        String alias = nameParts[idx];
        PsiDataBindingResourceItem anImport = getImport(alias, bindingInfo);
        if (anImport != null) {
            // Found an import matching the first namePart. Add a reference from this to the type.
            idx++;
            TextRange range = new TextRange(offset, offset += alias.length());
            // Skip the next dot or dollar separator (if any)
            offset++;
            String type = anImport.getTypeDeclaration();
            result.add(new AliasedReference(element, range, type, module));
            fullType = type + fullType.substring(alias.length());
            diff = type.length() - alias.length();
        } else {
            //  Check java.lang and primitives
            if (nameParts.length == 1) {
                if (alias.length() > 0) {
                    if (Character.isLowerCase(alias.charAt(0))) {
                        final PsiPrimitiveType primitive = PsiJavaParserFacadeImpl.getPrimitiveType(alias);
                        if (primitive != null) {
                            result.add(new PsiReferenceBase<PsiElement>(element, true) {

                                @Nullable
                                @Override
                                public PsiElement resolve() {
                                    return myElement;
                                }

                                @NotNull
                                @Override
                                public Object[] getVariants() {
                                    return ArrayUtil.EMPTY_OBJECT_ARRAY;
                                }
                            });
                        }
                    } else {
                        // java.lang
                        PsiClass aClass = JavaPsiFacade.getInstance(context.getProject()).findClass(JAVA_LANG + alias, GlobalSearchScope.moduleWithLibrariesScope(module));
                        if (aClass != null) {
                            final TextRange range = new TextRange(offset, offset += alias.length());
                            result.add(new ClassReference(element, range, aClass));
                        }
                    }
                    idx++;
                }
            }
        }
    }
    for (; idx < nameParts.length; idx++, offset++) {
        final String packageName = nameParts[idx];
        if (packageName.length() > 0) {
            final TextRange range = new TextRange(offset, offset += packageName.length());
            result.add(new AliasedReference(element, range, fullType.substring(0, diff + offset - start), module));
        }
    }
    return result.toArray(new PsiReference[result.size()]);
}
Also used : PsiDataBindingResourceItem(com.android.tools.idea.res.PsiDataBindingResourceItem) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) DataBindingInfo(com.android.tools.idea.res.DataBindingInfo) NotNull(org.jetbrains.annotations.NotNull) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with DataBindingInfo

use of com.android.tools.idea.res.DataBindingInfo in project android by JetBrains.

the class DataBindingConverter method fromString.

@Nullable
@Override
public PsiClass fromString(@Nullable @NonNls final String s, final ConvertContext context) {
    final DataBindingInfo bindingInfo = getDataBindingInfo(context);
    final String qualifiedName = getQualifiedType(s, bindingInfo);
    if (qualifiedName == null) {
        return null;
    }
    final Module module = context.getModule();
    if (module == null) {
        return null;
    }
    Project project = context.getProject();
    JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    GlobalSearchScope scope = module.getModuleWithDependenciesAndLibrariesScope(false);
    if (qualifiedName.length() > 0 && qualifiedName.indexOf('.') < 0) {
        if (Character.isLowerCase(qualifiedName.charAt(0))) {
            PsiPrimitiveType primitiveType = PsiJavaParserFacadeImpl.getPrimitiveType(qualifiedName);
            if (primitiveType != null) {
                PsiClassType boxedType = primitiveType.getBoxedType(PsiManager.getInstance(project), scope);
                if (boxedType != null) {
                    return boxedType.resolve();
                }
                return null;
            }
        } else {
            PsiClass aClass = psiFacade.findClass(JAVA_LANG + qualifiedName, scope);
            if (aClass != null) {
                return aClass;
            }
        }
    }
    return psiFacade.findClass(qualifiedName, scope);
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) DataBindingInfo(com.android.tools.idea.res.DataBindingInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with DataBindingInfo

use of com.android.tools.idea.res.DataBindingInfo in project android by JetBrains.

the class LightBindingClass method getSuperClass.

@Override
public PsiClass getSuperClass() {
    DataBindingInfo mergedInfo = myInfo.getMergedInfo();
    String superClassName = mergedInfo == null ? SdkConstants.CLASS_DATA_BINDING_BASE_BINDING : mergedInfo.getQualifiedName();
    return JavaPsiFacade.getInstance(myInfo.getProject()).findClass(superClassName, myFacet.getModule().getModuleWithDependenciesAndLibrariesScope(false));
}
Also used : DataBindingInfo(com.android.tools.idea.res.DataBindingInfo)

Example 5 with DataBindingInfo

use of com.android.tools.idea.res.DataBindingInfo in project android by JetBrains.

the class DataBindingShortNamesCache method traverseAllClasses.

private void traverseAllClasses(Function<PsiClass, Void> receiver) {
    for (List<DataBindingInfo> infoList : myNameCache.getValue().values()) {
        for (DataBindingInfo info : infoList) {
            PsiClass psiClass = DataBindingUtil.getOrCreatePsiClass(info);
            receiver.fun(psiClass);
        }
    }
}
Also used : PsiClass(com.intellij.psi.PsiClass) DataBindingInfo(com.android.tools.idea.res.DataBindingInfo)

Aggregations

DataBindingInfo (com.android.tools.idea.res.DataBindingInfo)11 Nullable (org.jetbrains.annotations.Nullable)5 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)4 Module (com.intellij.openapi.module.Module)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 NotNull (org.jetbrains.annotations.NotNull)3 PsiDataBindingResourceItem (com.android.tools.idea.res.PsiDataBindingResourceItem)2 PsiClass (com.intellij.psi.PsiClass)2 ResourceUrl (com.android.ide.common.resources.ResourceUrl)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 ArrayList (java.util.ArrayList)1