use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.
the class AndroidReferenceSearchExecutor method processQuery.
@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters params, @NotNull Processor<PsiReference> consumer) {
final PsiElement refElement = params.getElementToSearch();
if (!(refElement instanceof PsiFile)) {
return;
}
final VirtualFile vFile = ((PsiFile) refElement).getVirtualFile();
if (vFile == null) {
return;
}
LocalResourceManager manager = LocalResourceManager.getInstance(refElement);
if (manager == null) {
return;
}
String resType = manager.getFileResourceType((PsiFile) refElement);
if (resType != null) {
String resName = AndroidCommonUtils.getResourceName(resType, vFile.getName());
// unless references can be found by a simple CachedBasedRefSearcher
if (!resName.equals(vFile.getNameWithoutExtension()) && StringUtil.isNotEmpty(resName)) {
params.getOptimizer().searchWord(resName, params.getEffectiveSearchScope(), true, refElement);
}
}
}
use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.
the class AndroidResourceRenameResourceProcessor method prepareIdRenaming.
private static void prepareIdRenaming(XmlAttributeValue value, String newName, Map<PsiElement, String> allRenames, AndroidFacet facet) {
LocalResourceManager manager = facet.getLocalResourceManager();
allRenames.remove(value);
String id = AndroidResourceUtil.getResourceNameByReferenceText(value.getValue());
assert id != null;
List<XmlAttributeValue> idDeclarations = manager.findIdDeclarations(id);
for (XmlAttributeValue idDeclaration : idDeclarations) {
// framework which looks related to elements getting modified multiple times.
if (!ATTR_ID.equals(((XmlAttribute) idDeclaration.getParent()).getLocalName())) {
continue;
}
allRenames.put(new ValueResourceElementWrapper(idDeclaration), newName);
}
String name = AndroidResourceUtil.getResourceNameByReferenceText(newName);
if (name != null) {
for (PsiField resField : AndroidResourceUtil.findIdFields(value)) {
allRenames.put(resField, AndroidResourceUtil.getFieldNameByResourceName(name));
}
}
}
use of org.jetbrains.android.resourceManagers.LocalResourceManager in project android by JetBrains.
the class Configuration method computeBestDevice.
@Nullable
private Device computeBestDevice() {
for (Device device : myManager.getRecentDevices()) {
String stateName = myStateName;
if (stateName == null) {
stateName = device.getDefaultState().getName();
}
State selectedState = ConfigurationFileState.getState(device, stateName);
Module module = myManager.getModule();
FolderConfiguration currentConfig = getFolderConfig(module, selectedState, getLocale(), getTarget());
if (currentConfig != null) {
if (myEditedConfig.isMatchFor(currentConfig)) {
LocalResourceRepository resources = AppResourceRepository.getAppResources(module, true);
if (resources != null && myFile != null) {
ResourceFolderType folderType = ResourceHelper.getFolderType(myFile);
if (folderType != null) {
if (ResourceFolderType.VALUES.equals(folderType)) {
// If it's a file in the values folder, ResourceRepository.getMatchingFiles won't work.
// We get instead all the available folders and check that there is one compatible.
LocalResourceManager resourceManager = LocalResourceManager.getInstance(module);
if (resourceManager != null) {
for (PsiFile resourceFile : resourceManager.findResourceFiles("values")) {
if (myFile.equals(resourceFile.getVirtualFile()) && resourceFile.getParent() != null) {
FolderConfiguration folderConfiguration = FolderConfiguration.getConfigForFolder(resourceFile.getParent().getName());
if (currentConfig.isMatchFor(folderConfiguration)) {
return device;
}
}
}
}
} else {
List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
if (!types.isEmpty()) {
ResourceType type = types.get(0);
List<VirtualFile> matches = resources.getMatchingFiles(myFile, type, currentConfig);
if (matches.contains(myFile)) {
return device;
}
}
}
} else if ("Kotlin".equals(myFile.getFileType().getName())) {
return device;
} else if (myFile.equals(myManager.getProject().getProjectFile())) {
// takes care of correct device selection for Theme Editor
return device;
}
}
}
}
}
return myManager.getDefaultDevice();
}
use of org.jetbrains.android.resourceManagers.LocalResourceManager 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()]);
}
Aggregations