use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.
the class ResourceNameConverter method getStyleNameVariants.
private static Collection<String> getStyleNameVariants(ConvertContext context, GenericAttributeValue element) {
final Module module = context.getModule();
if (module == null) {
return Collections.emptyList();
}
final LocalResourceManager manager = LocalResourceManager.getInstance(module);
if (manager == null) {
return Collections.emptyList();
}
final Collection<String> styleNames = manager.getResourceNames(ResourceType.STYLE);
final List<String> result = new ArrayList<>();
final String currentValue = element.getStringValue();
for (String name : styleNames) {
if (currentValue == null || !currentValue.startsWith(name)) {
result.add(name + '.');
}
}
return result;
}
use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.
the class AndroidResourceRenameResourceProcessor method getResourceName.
/** Looks up the resource name for the given refactored element. Uses the same
* instanceof chain checkups as is done in {@link #canProcessElement} */
@Nullable
private static String getResourceName(PsiElement originalElement) {
PsiElement element = LazyValueResourceElementWrapper.computeLazyElement(originalElement);
if (element == null) {
return null;
}
if (element instanceof PsiFile) {
PsiFile file = (PsiFile) element;
LocalResourceManager manager = LocalResourceManager.getInstance(element);
if (manager != null) {
String type = manager.getFileResourceType(file);
if (type != null) {
String name = file.getName();
return AndroidCommonUtils.getResourceName(type, name);
}
}
return LintUtils.getBaseName(file.getName());
} else if (element instanceof PsiField) {
PsiField field = (PsiField) element;
return field.getName();
} else if (element instanceof XmlAttributeValue) {
if (AndroidResourceUtil.isIdDeclaration((XmlAttributeValue) element)) {
return AndroidResourceUtil.getResourceNameByReferenceText(((XmlAttributeValue) element).getValue());
}
XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
if (tag != null) {
DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
if (domElement instanceof ResourceElement) {
return ((ResourceElement) domElement).getName().getValue();
}
}
}
return null;
}
use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.
the class AndroidResourceRenameResourceProcessor method prepareCustomViewRenaming.
private static void prepareCustomViewRenaming(PsiClass cls, String newName, Map<PsiElement, String> allRenames, AndroidFacet facet) {
AppResourceRepository appResources = AppResourceRepository.getAppResources(facet, true);
String oldName = cls.getName();
if (appResources.hasResourceItem(DECLARE_STYLEABLE, oldName)) {
LocalResourceManager manager = facet.getLocalResourceManager();
for (PsiElement element : manager.findResourcesByFieldName(STYLEABLE.getName(), oldName)) {
if (element instanceof XmlAttributeValue) {
if (element.getParent() instanceof XmlAttribute) {
XmlTag tag = ((XmlAttribute) element.getParent()).getParent();
String tagName = tag.getName();
if (tagName.equals(TAG_DECLARE_STYLEABLE)) {
// Rename main styleable field
for (PsiField field : AndroidResourceUtil.findResourceFields(facet, STYLEABLE.getName(), oldName, false)) {
String escaped = AndroidResourceUtil.getFieldNameByResourceName(newName);
allRenames.put(field, escaped);
}
// Rename dependent attribute fields
PsiField[] styleableFields = AndroidResourceUtil.findStyleableAttributeFields(tag, false);
if (styleableFields.length > 0) {
for (PsiField resField : styleableFields) {
String fieldName = resField.getName();
String newAttributeName;
if (fieldName.startsWith(oldName)) {
newAttributeName = newName + fieldName.substring(oldName.length());
} else {
newAttributeName = oldName;
}
String escaped = AndroidResourceUtil.getFieldNameByResourceName(newAttributeName);
allRenames.put(resField, escaped);
}
}
}
}
}
}
}
}
use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.
the class CreateResourceDialogUtils method findResourceDirectory.
@Nullable
public static PsiDirectory findResourceDirectory(@NotNull DataContext dataContext) {
// Look at the set of selected files and see if one *specific* resource directory is implied (selected, or a parent
// of all selected nodes); if so, use it; otherwise return null.
//
// In the Android Project View we don't want to do this, since there is only ever a single "res" node,
// even when you have other overlays.
// If you're in the Android View, we want to ask you not just the filename but also let you
// create other resource folder configurations
Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project != null) {
AbstractProjectViewPane pane = ProjectView.getInstance(project).getCurrentProjectViewPane();
if (pane instanceof AndroidProjectViewPane) {
return null;
}
}
VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
if (file != null) {
// See if it's inside a res folder (or is equal to a resource folder)
Module module = LangDataKeys.MODULE.getData(dataContext);
if (module != null) {
LocalResourceManager manager = LocalResourceManager.getInstance(module);
if (manager != null) {
VirtualFile resFolder = getResFolderParent(manager, file);
if (resFolder != null) {
return AndroidPsiUtils.getPsiDirectorySafely(module.getProject(), resFolder);
}
}
}
}
return null;
}
use of org.jetbrains.android.resourceManagers.LocalResourceManager 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