mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-74391 replace hardcoded string resource lists by ResourceType enum objects
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.jetbrains.android.util;
|
||||
|
||||
import com.android.resources.ResourceFolderType;
|
||||
import com.android.sdklib.IAndroidTarget;
|
||||
import com.android.sdklib.ISdkLog;
|
||||
import com.android.sdklib.SdkConstants;
|
||||
@@ -39,7 +40,7 @@ public class AndroidCommonUtils {
|
||||
private static Pattern R_PATTERN = Pattern.compile("R(\\$.*)?\\.class");
|
||||
|
||||
public static final Pattern COMPILER_MESSAGE_PATTERN = Pattern.compile("(.+):(\\d+):.+");
|
||||
public static final String[] FILE_RESOURCE_TYPES = new String[]{"drawable", "anim", "layout", "values", "menu", "xml", "raw", "color"};
|
||||
|
||||
@NonNls public static final String PNG_EXTENSION = "png";
|
||||
private static final String[] DRAWABLE_EXTENSIONS = new String[]{PNG_EXTENSION, "jpg", "gif"};
|
||||
|
||||
@@ -242,7 +243,7 @@ public class AndroidCommonUtils {
|
||||
public static String getResourceTypeByDirName(@NotNull String name) {
|
||||
final int index = name.indexOf('-');
|
||||
final String type = index >= 0 ? name.substring(0, index) : name;
|
||||
return ArrayUtil.find(FILE_RESOURCE_TYPES, type) >= 0 ? type : null;
|
||||
return ResourceFolderType.getTypeByName(type) != null ? type : null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.jetbrains.android;
|
||||
|
||||
import com.android.resources.ResourceType;
|
||||
import com.intellij.codeHighlighting.Pass;
|
||||
import com.intellij.codeInsight.daemon.GutterIconNavigationHandler;
|
||||
import com.intellij.codeInsight.daemon.LineMarkerInfo;
|
||||
@@ -175,9 +176,9 @@ public class AndroidResourcesLineMarkerProvider implements LineMarkerProvider {
|
||||
final Map<MyResourceEntry, List<PsiElement>> result = new HashMap<MyResourceEntry, List<PsiElement>>();
|
||||
Collection<Resources> resourceFiles = resManager.getResourceElements();
|
||||
for (Resources res : resourceFiles) {
|
||||
for (String valueResourceType : AndroidResourceUtil.VALUE_RESOURCE_TYPES) {
|
||||
for (ResourceElement valueResource : AndroidResourceUtil.getValueResourcesFromElement(valueResourceType, res)) {
|
||||
addResource(valueResourceType, valueResource, result);
|
||||
for (ResourceType valueResourceType : AndroidResourceUtil.VALUE_RESOURCE_TYPES) {
|
||||
for (ResourceElement valueResource : AndroidResourceUtil.getValueResourcesFromElement(valueResourceType.getName(), res)) {
|
||||
addResource(valueResourceType.getName(), valueResource, result);
|
||||
}
|
||||
}
|
||||
for (Attr attr : res.getAttrs()) {
|
||||
|
||||
@@ -661,8 +661,8 @@ public class AndroidCompileUtil {
|
||||
final LocalResourceManager manager = facet.getLocalResourceManager();
|
||||
final Project project = facet.getModule().getProject();
|
||||
|
||||
for (final String resType : ResourceType.getNames()) {
|
||||
for (final ResourceElement element : manager.getValueResources(resType)) {
|
||||
for (final ResourceType resType : AndroidResourceUtil.VALUE_RESOURCE_TYPES) {
|
||||
for (final ResourceElement element : manager.getValueResources(resType.getName())) {
|
||||
waitForSmartMode(project);
|
||||
|
||||
ApplicationManager.getApplication().runReadAction(new Runnable() {
|
||||
@@ -674,7 +674,7 @@ public class AndroidCompileUtil {
|
||||
final String name = element.getName().getValue();
|
||||
|
||||
if (name != null) {
|
||||
resourceSet.add(new ResourceEntry(resType, name));
|
||||
resourceSet.add(new ResourceEntry(resType.getName(), name));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -128,7 +128,7 @@ public class AndroidDomUtil {
|
||||
if (containsReference) {
|
||||
if (resourceTypes.contains("color")) resourceTypes.add("drawable");
|
||||
if (resourceTypes.size() == 0) {
|
||||
resourceTypes.addAll(AndroidResourceUtil.REFERABLE_RESOURCE_TYPES);
|
||||
resourceTypes.addAll(AndroidResourceUtil.getNames(AndroidResourceUtil.REFERRABLE_RESOURCE_TYPES));
|
||||
}
|
||||
}
|
||||
if (resourceTypes.size() > 0) {
|
||||
|
||||
+17
-13
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.jetbrains.android.dom.converters;
|
||||
|
||||
import com.android.resources.ResourceType;
|
||||
import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
@@ -29,7 +30,6 @@ import com.intellij.psi.PsiReference;
|
||||
import com.intellij.psi.xml.XmlAttribute;
|
||||
import com.intellij.psi.xml.XmlElement;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.PsiNavigateUtil;
|
||||
import com.intellij.util.xml.*;
|
||||
import org.jetbrains.android.dom.AdditionalConverter;
|
||||
@@ -55,8 +55,8 @@ import static org.jetbrains.android.util.AndroidUtils.SYSTEM_RESOURCE_PACKAGE;
|
||||
* @author yole
|
||||
*/
|
||||
public class ResourceReferenceConverter extends ResolvingConverter<ResourceValue> implements CustomReferenceConverter<ResourceValue> {
|
||||
private static final Set<String> FIXABLE_RESOURCE_TYPES =
|
||||
new HashSet<String>(Arrays.asList("anim", "layout", "style", "menu", "xml", "dimen", "color", "string", "array", "id", "drawable"));
|
||||
private static final Set<ResourceType> XML_FILE_RESOURCE_TYPES =
|
||||
EnumSet.of(ResourceType.ANIM, ResourceType.LAYOUT, ResourceType.MENU, ResourceType.XML, ResourceType.COLOR, ResourceType.DRAWABLE);
|
||||
|
||||
private final List<String> myResourceTypes;
|
||||
private ResolvingConverter<String> myAdditionalConverter;
|
||||
@@ -145,7 +145,8 @@ public class ResourceReferenceConverter extends ResolvingConverter<ResourceValue
|
||||
? null
|
||||
: getResourceTypesInCurrentModule(facet);
|
||||
|
||||
for (String type : AndroidResourceUtil.REFERABLE_RESOURCE_TYPES) {
|
||||
for (ResourceType resourceType : ResourceType.values()) {
|
||||
final String type = resourceType.getName();
|
||||
String typePrefix = getTypePrefix(resourcePackage, type);
|
||||
if (value.startsWith(typePrefix)) {
|
||||
addResourceReferenceValues(facet, type, resourcePackage, result, true);
|
||||
@@ -203,7 +204,7 @@ public class ResourceReferenceConverter extends ResolvingConverter<ResourceValue
|
||||
if (s != null) types.add(s);
|
||||
}
|
||||
if (types.size() == 0) {
|
||||
types.addAll(AndroidResourceUtil.REFERABLE_RESOURCE_TYPES);
|
||||
types.addAll(AndroidResourceUtil.getNames(AndroidResourceUtil.VALUE_RESOURCE_TYPES));
|
||||
}
|
||||
return types;
|
||||
}
|
||||
@@ -290,14 +291,16 @@ public class ResourceReferenceConverter extends ResolvingConverter<ResourceValue
|
||||
ResourceValue resourceValue = ResourceValue.parse(value, false, myWithPrefix);
|
||||
if (resourceValue != null) {
|
||||
String aPackage = resourceValue.getPackage();
|
||||
String resourceType = resourceValue.getResourceType();
|
||||
if (resourceType == null && myResourceTypes.size() == 1) {
|
||||
resourceType = myResourceTypes.get(0);
|
||||
String resTypeName = resourceValue.getResourceType();
|
||||
if (resTypeName == null && myResourceTypes.size() == 1) {
|
||||
resTypeName = myResourceTypes.get(0);
|
||||
}
|
||||
final String resourceName = resourceValue.getResourceName();
|
||||
if (aPackage == null && resourceType != null && resourceName != null) {
|
||||
if (FIXABLE_RESOURCE_TYPES.contains(resourceType) && AndroidResourceUtil.isCorrectAndroidResourceName(resourceName)) {
|
||||
return new LocalQuickFix[]{new MyLocalQuickFix(facet, resourceType, resourceName, context.getFile())};
|
||||
final ResourceType resType = resTypeName != null ? ResourceType.getEnum(resTypeName) : null;
|
||||
if (aPackage == null && resType != null && resourceName != null) {
|
||||
if ((AndroidResourceUtil.VALUE_RESOURCE_TYPES.contains(resType) || XML_FILE_RESOURCE_TYPES.contains(resType)) &&
|
||||
AndroidResourceUtil.isCorrectAndroidResourceName(resourceName)) {
|
||||
return new LocalQuickFix[]{new MyLocalQuickFix(facet, resType.getName(), resourceName, context.getFile())};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -343,7 +346,7 @@ public class ResourceReferenceConverter extends ResolvingConverter<ResourceValue
|
||||
@NotNull
|
||||
public String getName() {
|
||||
String containerName;
|
||||
if (ArrayUtil.find(AndroidResourceUtil.VALUE_RESOURCE_TYPES, myResourceType) >= 0) {
|
||||
if (AndroidResourceUtil.isValueResourceType(myResourceType)) {
|
||||
containerName = AndroidResourceUtil.getDefaultResourceFileName(myResourceType);
|
||||
}
|
||||
else {
|
||||
@@ -359,7 +362,8 @@ public class ResourceReferenceConverter extends ResolvingConverter<ResourceValue
|
||||
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
LocalResourceManager manager = myFacet.getLocalResourceManager();
|
||||
if (ArrayUtil.find(AndroidResourceUtil.VALUE_RESOURCE_TYPES, myResourceType) >= 0) {
|
||||
|
||||
if (AndroidResourceUtil.isValueResourceType(myResourceType)) {
|
||||
String initialValue = !myResourceType.equals("id") ? "value" : null;
|
||||
ResourceElement resElement = manager.addValueResource(myResourceType, myResourceName, initialValue);
|
||||
if (resElement != null) {
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.jetbrains.android.dom.resources;
|
||||
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.xml.Convert;
|
||||
import com.intellij.util.xml.GenericAttributeValue;
|
||||
import org.jetbrains.android.dom.attrs.AttributeFormat;
|
||||
@@ -38,7 +37,7 @@ import java.util.List;
|
||||
public interface Item extends ResourceElement {
|
||||
class TypeConverter extends StaticEnumConverter {
|
||||
public TypeConverter() {
|
||||
super(ArrayUtil.toStringArray(AndroidResourceUtil.REFERABLE_RESOURCE_TYPES));
|
||||
super(AndroidResourceUtil.getNamesArray(AndroidResourceUtil.REFERRABLE_RESOURCE_TYPES));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -143,8 +143,8 @@ public class LocalResourceManager extends ResourceManager {
|
||||
final FileBasedIndex index = FileBasedIndex.getInstance();
|
||||
final GlobalSearchScope scope = GlobalSearchScope.projectScope(myModule.getProject());
|
||||
|
||||
for (String resourceType : ResourceType.getNames()) {
|
||||
final ResourceEntry typeMarkerEntry = AndroidValueResourcesIndex.createTypeMarkerEntry(resourceType);
|
||||
for (ResourceType resourceType : AndroidResourceUtil.VALUE_RESOURCE_TYPES) {
|
||||
final ResourceEntry typeMarkerEntry = AndroidValueResourcesIndex.createTypeMarkerEntry(resourceType.getName());
|
||||
|
||||
for (Set<ResourceEntry> entrySet : index.getValues(AndroidValueResourcesIndex.INDEX_ID, typeMarkerEntry, scope)) {
|
||||
for (ResourceEntry entry : entrySet) {
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.jetbrains.android.util;
|
||||
|
||||
import com.android.resources.ResourceFolderType;
|
||||
import com.android.resources.ResourceType;
|
||||
import com.android.sdklib.SdkConstants;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleManager;
|
||||
@@ -42,31 +44,34 @@ import org.jetbrains.android.sdk.AndroidSdkUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.util.Collections.addAll;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Eugene.Kudelevsky
|
||||
*/
|
||||
public class AndroidResourceUtil {
|
||||
public static final String NEW_ID_PREFIX = "@+id/";
|
||||
public static final String[] VALUE_RESOURCE_TYPES =
|
||||
new String[]{"drawable", "dimen", "color", "string", "style", "array", "id", "bool", "integer", "integer-array"};
|
||||
public static final Set<String> REFERABLE_RESOURCE_TYPES = new HashSet<String>();
|
||||
|
||||
static {
|
||||
addAll(REFERABLE_RESOURCE_TYPES, AndroidCommonUtils.FILE_RESOURCE_TYPES);
|
||||
addAll(REFERABLE_RESOURCE_TYPES, VALUE_RESOURCE_TYPES);
|
||||
REFERABLE_RESOURCE_TYPES.remove("values");
|
||||
}
|
||||
public static final Set<ResourceType> VALUE_RESOURCE_TYPES = EnumSet.of(ResourceType.DRAWABLE, ResourceType.COLOR, ResourceType.DIMEN,
|
||||
ResourceType.STRING, ResourceType.STYLE, ResourceType.ARRAY,
|
||||
ResourceType.ID, ResourceType.BOOL, ResourceType.INTEGER);
|
||||
|
||||
public static final Set<ResourceType> REFERRABLE_RESOURCE_TYPES = EnumSet.noneOf(ResourceType.class);
|
||||
|
||||
private AndroidResourceUtil() {
|
||||
}
|
||||
|
||||
static {
|
||||
REFERRABLE_RESOURCE_TYPES.addAll(Arrays.asList(ResourceType.values()));
|
||||
REFERRABLE_RESOURCE_TYPES.remove(ResourceType.ATTR);
|
||||
REFERRABLE_RESOURCE_TYPES.remove(ResourceType.STYLEABLE);
|
||||
}
|
||||
|
||||
public static boolean isValueResourceType(@NotNull String resTypeName) {
|
||||
final ResourceType type = ResourceType.getEnum(resTypeName);
|
||||
return type != null && VALUE_RESOURCE_TYPES.contains(type);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PsiField[] findResourceFields(@NotNull AndroidFacet facet,
|
||||
@NotNull String resClassName,
|
||||
@@ -383,7 +388,7 @@ public class AndroidResourceUtil {
|
||||
|
||||
@NotNull
|
||||
public static List<VirtualFile> getResourceSubdirs(@Nullable String resourceType, @NotNull VirtualFile[] resourceDirs) {
|
||||
if (ArrayUtil.find(AndroidCommonUtils.FILE_RESOURCE_TYPES, resourceType) < 0 && resourceType != null) {
|
||||
if (resourceType != null && ResourceFolderType.getTypeByName(resourceType) == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
final List<VirtualFile> dirs = new ArrayList<VirtualFile>();
|
||||
@@ -407,10 +412,7 @@ public class AndroidResourceUtil {
|
||||
|
||||
@Nullable
|
||||
public static String getDefaultResourceFileName(@NotNull String resourceType) {
|
||||
if (ArrayUtil.find(VALUE_RESOURCE_TYPES, resourceType) < 0) {
|
||||
return null;
|
||||
}
|
||||
return resourceType + "s.xml";
|
||||
return isValueResourceType(resourceType) ? resourceType + "s.xml" : null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -540,4 +542,22 @@ public class AndroidResourceUtil {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static List<String> getNames(@NotNull Collection<ResourceType> resourceTypes) {
|
||||
if (resourceTypes.size() == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
final List<String> result = new ArrayList<String>();
|
||||
|
||||
for (ResourceType type : resourceTypes) {
|
||||
result.add(type.getName());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String[] getNamesArray(@NotNull Collection<ResourceType> resourceTypes) {
|
||||
final List<String> names = getNames(resourceTypes);
|
||||
return ArrayUtil.toStringArray(names);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<item type="integer-arr<caret>"></item>
|
||||
</resources>
|
||||
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<item type="integer-arr"></item>
|
||||
</resources>
|
||||
@@ -6,7 +6,6 @@ import com.intellij.testFramework.UsefulTestCase;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.jetbrains.android.AndroidTestCase;
|
||||
import org.jetbrains.android.inspections.AndroidDomInspection;
|
||||
import org.jetbrains.android.util.AndroidResourceUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@@ -53,14 +52,6 @@ abstract class AndroidDomTest extends AndroidTestCase {
|
||||
UsefulTestCase.assertSameElements(lookupElementStrings, variants);
|
||||
}
|
||||
|
||||
protected static List<String> getAllResources() {
|
||||
List<String> list = new ArrayList<String>();
|
||||
for (String type : AndroidResourceUtil.REFERABLE_RESOURCE_TYPES) {
|
||||
list.add('@' + type + '/');
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
protected void doTestHighlighting() throws Throwable {
|
||||
doTestHighlighting(getTestName(true) + ".xml");
|
||||
}
|
||||
|
||||
@@ -81,6 +81,10 @@ public class AndroidValueResourcesTest extends AndroidDomTest {
|
||||
toTestCompletion("attrs1.xml", "attrs1_after.xml");
|
||||
}
|
||||
|
||||
public void testResourceTypeCompletion() throws Throwable {
|
||||
doTestCompletion();
|
||||
}
|
||||
|
||||
public void testStyles5() throws Throwable {
|
||||
toTestCompletion("styles5.xml", "styles5_after.xml");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user