use of org.jetbrains.android.resourceManagers.ResourceManager 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.resourceManagers.ResourceManager in project android by JetBrains.
the class AndroidCompletionContributor method isFrameworkAttributeDeprecated.
private static boolean isFrameworkAttributeDeprecated(AndroidFacet facet, XmlAttribute attribute, String attributeName) {
final ResourceManager manager = facet.getResourceManager(AndroidUtils.SYSTEM_RESOURCE_PACKAGE, attribute.getParent());
if (manager == null) {
return false;
}
final AttributeDefinitions attributes = manager.getAttributeDefinitions();
if (attributes == null) {
return false;
}
final AttributeDefinition attributeDefinition = attributes.getAttrDefByName(attributeName);
return attributeDefinition != null && attributeDefinition.isAttributeDeprecated();
}
use of org.jetbrains.android.resourceManagers.ResourceManager in project android by JetBrains.
the class AndroidResourceRenameResourceProcessor method prepareResourceFileRenaming.
private static void prepareResourceFileRenaming(PsiFile file, String newName, Map<PsiElement, String> allRenames, AndroidFacet facet) {
Project project = file.getProject();
ResourceManager manager = facet.getLocalResourceManager();
String type = manager.getFileResourceType(file);
if (type == null)
return;
String name = file.getName();
if (AndroidCommonUtils.getResourceName(type, name).equals(AndroidCommonUtils.getResourceName(type, newName))) {
return;
}
List<PsiFile> resourceFiles = manager.findResourceFiles(type, AndroidCommonUtils.getResourceName(type, name), true, false);
List<PsiFile> alternativeResources = new ArrayList<PsiFile>();
for (PsiFile resourceFile : resourceFiles) {
if (!resourceFile.getManager().areElementsEquivalent(file, resourceFile) && resourceFile.getName().equals(name)) {
alternativeResources.add(resourceFile);
}
}
if (alternativeResources.size() > 0) {
int r = 0;
if (ASK) {
r = Messages.showDialog(project, message("rename.alternate.resources.question"), message("rename.dialog.title"), new String[] { Messages.YES_BUTTON, Messages.NO_BUTTON }, 1, Messages.getQuestionIcon());
}
if (r == 0) {
for (PsiFile candidate : alternativeResources) {
allRenames.put(candidate, newName);
}
} else {
return;
}
}
PsiField[] resFields = AndroidResourceUtil.findResourceFieldsForFileResource(file, false);
for (PsiField resField : resFields) {
String newFieldName = AndroidCommonUtils.getResourceName(type, newName);
allRenames.put(resField, AndroidResourceUtil.getFieldNameByResourceName(newFieldName));
}
}
use of org.jetbrains.android.resourceManagers.ResourceManager in project android by JetBrains.
the class AndroidResourceRenameResourceProcessor method prepareValueResourceRenaming.
private static void prepareValueResourceRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames, final AndroidFacet facet) {
ResourceManager manager = facet.getLocalResourceManager();
XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
assert tag != null;
String type = manager.getValueResourceType(tag);
assert type != null;
Project project = tag.getProject();
DomElement domElement = DomManager.getDomManager(project).getDomElement(tag);
assert domElement instanceof ResourceElement;
String name = ((ResourceElement) domElement).getName().getValue();
assert name != null;
List<ResourceElement> resources = manager.findValueResources(type, name);
for (ResourceElement resource : resources) {
XmlElement xmlElement = resource.getName().getXmlAttributeValue();
if (!element.getManager().areElementsEquivalent(element, xmlElement)) {
allRenames.put(xmlElement, newName);
}
}
if (getResourceType(element) == ResourceType.STYLE) {
// For styles, try also to find child styles defined by name (i.e. "ParentName.StyleName") and add them
// to the rename list. This will allow the rename processor to also handle the references to those. For example,
// If you rename "MyTheme" and your manifest theme is "MyTheme.NoActionBar", this will make sure that
// the reference from the manifest is also updated by adding "MyTheme.NoActionBar" to the rename list.
// We iterate the styles in order to cascade any changes to children down the hierarchy.
// List of styles that will be renamed.
HashSet<String> renamedStyles = Sets.newHashSet();
renamedStyles.add(name);
final String stylePrefix = name + ".";
Collection<String> renameCandidates;
ResourceType resourceType = ResourceType.getEnum(type);
if (resourceType == null) {
renameCandidates = Collections.emptyList();
} else {
renameCandidates = Collections2.filter(manager.getResourceNames(resourceType), new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith(stylePrefix);
}
});
}
for (String resourceName : ORDER_BY_LENGTH.sortedCopy(renameCandidates)) {
// resourceName.lastIndexOf will never return -1 because we've filtered all names that
// do not contain stylePrefix
String parentName = resourceName.substring(0, resourceName.lastIndexOf('.'));
if (!renamedStyles.contains(parentName)) {
// This resource's parent wasn't affected by the rename
continue;
}
for (ResourceElement resource : manager.findValueResources(type, resourceName)) {
if (!(resource instanceof Style) || ((Style) resource).getParentStyle().getXmlAttributeValue() != null) {
// This element is not a style or does have an explicit parent so we do not rename it.
continue;
}
XmlAttributeValue xmlElement = resource.getName().getXmlAttributeValue();
if (xmlElement != null) {
String newStyleName = newName + StringUtil.trimStart(resourceName, name);
allRenames.put(new ValueResourceElementWrapper(xmlElement), newStyleName);
renamedStyles.add(resourceName);
}
}
}
}
PsiField[] resFields = AndroidResourceUtil.findResourceFieldsForValueResource(tag, false);
for (PsiField resField : resFields) {
String escaped = AndroidResourceUtil.getFieldNameByResourceName(newName);
allRenames.put(resField, escaped);
}
// Also rename the dependent fields, e.g. if you rename <declare-styleable name="Foo">,
// we have to rename not just R.styleable.Foo but the also R.styleable.Foo_* attributes
PsiField[] styleableFields = AndroidResourceUtil.findStyleableAttributeFields(tag, false);
if (styleableFields.length > 0) {
String tagName = tag.getName();
boolean isDeclareStyleable = tagName.equals(TAG_DECLARE_STYLEABLE);
boolean isAttr = !isDeclareStyleable && tagName.equals(TAG_ATTR) && tag.getParentTag() != null;
assert isDeclareStyleable || isAttr;
String style = isAttr ? tag.getParentTag().getAttributeValue(ATTR_NAME) : null;
for (PsiField resField : styleableFields) {
String fieldName = resField.getName();
String newAttributeName;
if (isDeclareStyleable && fieldName.startsWith(name)) {
newAttributeName = newName + fieldName.substring(name.length());
} else if (isAttr && style != null) {
newAttributeName = style + '_' + newName;
} else {
newAttributeName = name;
}
String escaped = AndroidResourceUtil.getFieldNameByResourceName(newAttributeName);
allRenames.put(resField, escaped);
}
}
}
use of org.jetbrains.android.resourceManagers.ResourceManager in project android by JetBrains.
the class AndroidResourceReferenceBase method collectTargets.
private void collectTargets(AndroidFacet facet, ResourceValue resValue, List<PsiElement> elements, boolean attrReference) {
ResourceType resType = resValue.getType();
if (resType == null) {
return;
}
ResourceManager manager = facet.getResourceManager(resValue.getNamespace(), myElement);
if (manager != null) {
String resName = resValue.getResourceName();
if (resName != null) {
manager.collectLazyResourceElements(resType.getName(), resName, attrReference, myElement, elements);
}
}
}
Aggregations