use of org.jetbrains.android.dom.resources.DeclareStyleable in project android by JetBrains.
the class LocalResourceManager method findStyleableAttributesByFieldName.
public List<Attr> findStyleableAttributesByFieldName(@NotNull String fieldName) {
int index = fieldName.lastIndexOf('_');
// is "miny_moe".
while (index != -1) {
int prev = fieldName.lastIndexOf('_', index - 1);
if (prev == -1 || Character.isUpperCase(fieldName.charAt(prev + 1))) {
break;
}
index = prev;
}
if (index == -1) {
return Collections.emptyList();
}
String styleableName = fieldName.substring(0, index);
String attrName = fieldName.substring(index + 1);
List<Attr> list = new ArrayList<Attr>();
for (Pair<Resources, VirtualFile> pair : getResourceElements()) {
final Resources res = pair.getFirst();
for (DeclareStyleable styleable : res.getDeclareStyleables()) {
if (styleableName.equals(styleable.getName().getValue())) {
for (Attr attr : styleable.getAttrs()) {
if (attrName.equals(attr.getName().getValue())) {
list.add(attr);
}
}
}
}
}
return list;
}
use of org.jetbrains.android.dom.resources.DeclareStyleable in project android by JetBrains.
the class AndroidGotoDeclarationHandler method getGotoDeclarationTargets.
@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
if (!(sourceElement instanceof PsiIdentifier)) {
return null;
}
final PsiFile file = sourceElement.getContainingFile();
if (file == null) {
return null;
}
AndroidFacet facet = AndroidFacet.getInstance(file);
if (facet == null) {
return null;
}
final PsiReferenceExpression refExp = PsiTreeUtil.getParentOfType(sourceElement, PsiReferenceExpression.class);
if (refExp == null) {
return null;
}
AndroidResourceUtil.MyReferredResourceFieldInfo info = AndroidResourceUtil.getReferredResourceOrManifestField(facet, refExp, false);
if (info == null) {
PsiElement parent = refExp.getParent();
if (parent instanceof PsiReferenceExpression) {
info = AndroidResourceUtil.getReferredResourceOrManifestField(facet, (PsiReferenceExpression) parent, false);
}
if (info == null) {
parent = parent.getParent();
if (parent instanceof PsiReferenceExpression) {
info = AndroidResourceUtil.getReferredResourceOrManifestField(facet, (PsiReferenceExpression) parent, false);
}
}
}
if (info == null) {
return null;
}
final String nestedClassName = info.getClassName();
final String fieldName = info.getFieldName();
final List<PsiElement> resourceList = new ArrayList<PsiElement>();
if (info.isFromManifest()) {
collectManifestElements(nestedClassName, fieldName, facet, resourceList);
} else {
final ResourceManager manager = info.isSystem() ? facet.getSystemResourceManager(false) : facet.getLocalResourceManager();
if (manager == null) {
return null;
}
manager.collectLazyResourceElements(nestedClassName, fieldName, false, refExp, resourceList);
if (manager instanceof LocalResourceManager) {
final LocalResourceManager lrm = (LocalResourceManager) manager;
if (nestedClassName.equals(ResourceType.ATTR.getName())) {
for (Attr attr : lrm.findAttrs(fieldName)) {
resourceList.add(attr.getName().getXmlAttributeValue());
}
} else if (nestedClassName.equals(ResourceType.STYLEABLE.getName())) {
for (DeclareStyleable styleable : lrm.findStyleables(fieldName)) {
resourceList.add(styleable.getName().getXmlAttributeValue());
}
for (Attr styleable : lrm.findStyleableAttributesByFieldName(fieldName)) {
resourceList.add(styleable.getName().getXmlAttributeValue());
}
}
}
}
if (resourceList.size() > 1) {
// Sort to ensure the output is stable, and to prefer the base folders
Collections.sort(resourceList, AndroidResourceUtil.RESOURCE_ELEMENT_COMPARATOR);
}
return resourceList.toArray(new PsiElement[resourceList.size()]);
}
use of org.jetbrains.android.dom.resources.DeclareStyleable in project kotlin by JetBrains.
the class KotlinAndroidGotoDeclarationHandler method getGotoDeclarationTargets.
@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
KtSimpleNameExpression referenceExpression = GotoResourceHelperKt.getReferenceExpression(sourceElement);
if (referenceExpression == null) {
return null;
}
AndroidFacet facet = AndroidUtilKt.getAndroidFacetForFile(referenceExpression);
if (facet == null) {
return null;
}
AndroidResourceUtil.MyReferredResourceFieldInfo info = GotoResourceHelperKt.getInfo(referenceExpression, facet);
if (info == null)
return null;
String nestedClassName = info.getClassName();
String fieldName = info.getFieldName();
List<PsiElement> resourceList = new ArrayList<PsiElement>();
if (info.isFromManifest()) {
collectManifestElements(nestedClassName, fieldName, facet, resourceList);
} else {
ResourceManager manager = info.isSystem() ? facet.getSystemResourceManager(false) : facet.getLocalResourceManager();
if (manager == null) {
return null;
}
manager.collectLazyResourceElements(nestedClassName, fieldName, false, referenceExpression, resourceList);
if (manager instanceof LocalResourceManager) {
LocalResourceManager lrm = (LocalResourceManager) manager;
if (nestedClassName.equals(ResourceType.ATTR.getName())) {
for (Attr attr : lrm.findAttrs(fieldName)) {
resourceList.add(attr.getName().getXmlAttributeValue());
}
} else if (nestedClassName.equals(ResourceType.STYLEABLE.getName())) {
for (DeclareStyleable styleable : lrm.findStyleables(fieldName)) {
resourceList.add(styleable.getName().getXmlAttributeValue());
}
for (Attr styleable : lrm.findStyleableAttributesByFieldName(fieldName)) {
resourceList.add(styleable.getName().getXmlAttributeValue());
}
}
}
}
if (resourceList.size() > 1) {
// Sort to ensure the output is stable, and to prefer the base folders
Collections.sort(resourceList, AndroidResourceUtil.RESOURCE_ELEMENT_COMPARATOR);
}
return resourceList.toArray(new PsiElement[resourceList.size()]);
}
Aggregations