Search in sources :

Example 1 with CreateValueResourceQuickFix

use of org.jetbrains.android.inspections.CreateValueResourceQuickFix in project android by JetBrains.

the class AndroidValueResourcesTest method doCreateValueResourceFromUsage.

private void doCreateValueResourceFromUsage(VirtualFile virtualFile) {
    myFixture.configureFromExistingVirtualFile(virtualFile);
    List<HighlightInfo> infos = myFixture.doHighlighting();
    List<IntentionAction> actions = new ArrayList<>();
    for (HighlightInfo info : infos) {
        List<Pair<HighlightInfo.IntentionActionDescriptor, TextRange>> ranges = info.quickFixActionRanges;
        if (ranges != null) {
            for (Pair<HighlightInfo.IntentionActionDescriptor, TextRange> pair : ranges) {
                IntentionAction action = pair.getFirst().getAction();
                if (action instanceof CreateValueResourceQuickFix) {
                    actions.add(action);
                }
            }
        }
    }
    assertEquals(1, actions.size());
    new WriteCommandAction.Simple(getProject()) {

        @Override
        protected void run() throws Throwable {
            actions.get(0).invoke(getProject(), myFixture.getEditor(), myFixture.getFile());
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) CreateValueResourceQuickFix(org.jetbrains.android.inspections.CreateValueResourceQuickFix) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) Pair(com.intellij.openapi.util.Pair)

Example 2 with CreateValueResourceQuickFix

use of org.jetbrains.android.inspections.CreateValueResourceQuickFix in project android by JetBrains.

the class ResourceReferenceConverter method getQuickFixes.

@Override
public LocalQuickFix[] getQuickFixes(ConvertContext context) {
    AndroidFacet facet = AndroidFacet.getInstance(context);
    if (facet != null) {
        final DomElement domElement = context.getInvocationElement();
        if (domElement instanceof GenericDomValue) {
            final String value = ((GenericDomValue) domElement).getStringValue();
            if (value != null) {
                ResourceValue resourceValue = ResourceValue.parse(value, false, myWithPrefix, true);
                if (resourceValue != null) {
                    String aPackage = resourceValue.getNamespace();
                    ResourceType resType = resourceValue.getType();
                    if (resType == null && myResourceTypes.size() == 1) {
                        resType = myResourceTypes.iterator().next();
                    }
                    final String resourceName = resourceValue.getResourceName();
                    if (aPackage == null && resType != null && resourceName != null && AndroidResourceUtil.isCorrectAndroidResourceName(resourceName)) {
                        final List<LocalQuickFix> fixes = new ArrayList<>();
                        ResourceFolderType folderType = AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.get(resType);
                        if (folderType != null) {
                            fixes.add(new CreateFileResourceQuickFix(facet, folderType, resourceName, context.getFile(), false));
                        }
                        if (VALUE_RESOURCE_TYPES.contains(resType) && resType != ResourceType.LAYOUT) {
                            // layouts: aliases only
                            fixes.add(new CreateValueResourceQuickFix(facet, resType, resourceName, context.getFile(), false));
                        }
                        return fixes.toArray(new LocalQuickFix[fixes.size()]);
                    }
                }
            }
        }
    }
    return LocalQuickFix.EMPTY_ARRAY;
}
Also used : CreateValueResourceQuickFix(org.jetbrains.android.inspections.CreateValueResourceQuickFix) ResourceFolderType(com.android.resources.ResourceFolderType) ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) ResourceType(com.android.resources.ResourceType) AndroidResourceType(org.jetbrains.android.dom.AndroidResourceType) CreateFileResourceQuickFix(org.jetbrains.android.inspections.CreateFileResourceQuickFix) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 3 with CreateValueResourceQuickFix

use of org.jetbrains.android.inspections.CreateValueResourceQuickFix in project android by JetBrains.

the class AndroidLayoutDomTest method testCreateResourceFromUsage.

public void testCreateResourceFromUsage() throws Throwable {
    VirtualFile virtualFile = copyFileToProject(getTestName(true) + ".xml");
    myFixture.configureFromExistingVirtualFile(virtualFile);
    List<HighlightInfo> infos = myFixture.doHighlighting();
    List<IntentionAction> actions = new ArrayList<>();
    for (HighlightInfo info : infos) {
        List<Pair<HighlightInfo.IntentionActionDescriptor, TextRange>> ranges = info.quickFixActionRanges;
        if (ranges != null) {
            for (Pair<HighlightInfo.IntentionActionDescriptor, TextRange> pair : ranges) {
                IntentionAction action = pair.getFirst().getAction();
                if (action instanceof CreateValueResourceQuickFix) {
                    actions.add(action);
                }
            }
        }
    }
    assertEquals(1, actions.size());
    new WriteCommandAction.Simple(getProject()) {

        @Override
        protected void run() throws Throwable {
            actions.get(0).invoke(getProject(), myFixture.getEditor(), myFixture.getFile());
        }
    }.execute();
    myFixture.checkResultByFile("res/values/drawables.xml", myTestFolder + '/' + getTestName(true) + "_drawable_after.xml", true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) CreateValueResourceQuickFix(org.jetbrains.android.inspections.CreateValueResourceQuickFix) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) Pair(com.intellij.openapi.util.Pair)

Aggregations

CreateValueResourceQuickFix (org.jetbrains.android.inspections.CreateValueResourceQuickFix)3 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 Pair (com.intellij.openapi.util.Pair)2 TextRange (com.intellij.openapi.util.TextRange)2 ArrayList (java.util.ArrayList)2 ResourceFolderType (com.android.resources.ResourceFolderType)1 ResourceType (com.android.resources.ResourceType)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 AndroidResourceType (org.jetbrains.android.dom.AndroidResourceType)1 ResourceValue (org.jetbrains.android.dom.resources.ResourceValue)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 CreateFileResourceQuickFix (org.jetbrains.android.inspections.CreateFileResourceQuickFix)1