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;
}
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()]);
}
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);
}
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));
}
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);
}
}
}
Aggregations