Search in sources :

Example 6 with AndroidLintQuickFix

use of org.jetbrains.android.inspections.lint.AndroidLintQuickFix in project android by JetBrains.

the class AndroidLintNewApiInspection method getApiDetectorFixes.

public static AndroidLintQuickFix[] getApiDetectorFixes(@NotNull Issue issue, @NotNull PsiElement startElement, @SuppressWarnings("UnusedParameters") @NotNull PsiElement endElement, @NotNull String message) {
    // TODO: Return one for each parent context (declaration, method, class, outer class(es)
    int api = ApiDetector.getRequiredVersion(issue, message, RAW);
    if (api != -1) {
        List<AndroidLintQuickFix> list = Lists.newArrayList();
        PsiFile file = startElement.getContainingFile();
        boolean isXml = false;
        if (file instanceof XmlFile) {
            isXml = true;
            ResourceFolderType folderType = ResourceHelper.getFolderType(file);
            if (folderType != null) {
                FolderConfiguration config = ResourceHelper.getFolderConfiguration(file);
                if (config != null) {
                    config.setVersionQualifier(new VersionQualifier(api));
                    String folder = config.getFolderName(folderType);
                    list.add(OverrideResourceAction.createFix(folder));
                }
            }
        }
        list.add(new AddTargetVersionCheckQuickFix(api));
        list.add(new AddTargetApiQuickFix(api, false, startElement));
        ApplicationManager.getApplication().assertReadAccessAllowed();
        Project project = startElement.getProject();
        if (!isXml && JavaPsiFacade.getInstance(project).findClass(REQUIRES_API_ANNOTATION, GlobalSearchScope.allScope(project)) != null) {
            list.add(new AddTargetApiQuickFix(api, true, startElement));
        }
        return list.toArray(new AndroidLintQuickFix[list.size()]);
    }
    return AndroidLintQuickFix.EMPTY_ARRAY;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Project(com.intellij.openapi.project.Project) ResourceFolderType(com.android.resources.ResourceFolderType) PsiFile(com.intellij.psi.PsiFile)

Example 7 with AndroidLintQuickFix

use of org.jetbrains.android.inspections.lint.AndroidLintQuickFix in project android by JetBrains.

the class AndroidLintRtlCompatInspection method getQuickFixes.

@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull String message) {
    if (message.startsWith("To support older versions than API 17")) {
        Matcher matcher = QUOTED_PARAMETER.matcher(message);
        if (matcher.find()) {
            final String name = matcher.group(1);
            final String value = matcher.group(2);
            return new AndroidLintQuickFix[] { new SetAttributeQuickFix(String.format("Set %s", name), name, value) };
        }
    }
    return AndroidLintQuickFix.EMPTY_ARRAY;
}
Also used : Matcher(java.util.regex.Matcher) SetAttributeQuickFix(org.jetbrains.android.inspections.lint.SetAttributeQuickFix) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with AndroidLintQuickFix

use of org.jetbrains.android.inspections.lint.AndroidLintQuickFix in project android by JetBrains.

the class AndroidLintSwitchIntDefInspection method getQuickFixes.

@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    final List<String> missingCases = AnnotationDetector.getMissingCases(message, RAW);
    if (missingCases != null && !missingCases.isEmpty()) {
        return new AndroidLintQuickFix[] { new AndroidLintQuickFix() {

            @Override
            public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
                if (startElement.getParent() instanceof PsiSwitchStatement) {
                    PsiSwitchStatement switchStatement = (PsiSwitchStatement) startElement.getParent();
                    Project project = switchStatement.getProject();
                    PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
                    PsiCodeBlock body = switchStatement.getBody();
                    if (body == null) {
                        return;
                    }
                    PsiElement anchor = body.getLastChild();
                    for (String constant : missingCases) {
                        PsiElement parent = anchor.getParent();
                        PsiStatement caseStatement = factory.createStatementFromText("case " + constant + ":", anchor);
                        parent.addBefore(caseStatement, anchor);
                        PsiStatement breakStatement = factory.createStatementFromText("break;", anchor);
                        parent.addBefore(breakStatement, anchor);
                    }
                    CodeStyleManager.getInstance(project).reformat(switchStatement);
                }
            }

            @Override
            public boolean isApplicable(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.ContextType contextType) {
                return startElement.isValid();
            }

            @NotNull
            @Override
            public String getName() {
                return "Add Missing @IntDef Constants";
            }
        } };
    }
    return AndroidLintQuickFix.EMPTY_ARRAY;
}
Also used : Project(com.intellij.openapi.project.Project) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with AndroidLintQuickFix

use of org.jetbrains.android.inspections.lint.AndroidLintQuickFix in project android by JetBrains.

the class AndroidLintGridLayoutInspection method getQuickFixes.

@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull final PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    String obsolete = GridLayoutDetector.getOldValue(message, RAW);
    String available = GridLayoutDetector.getNewValue(message, RAW);
    if (obsolete != null && available != null) {
        return new AndroidLintQuickFix[] { new ReplaceStringQuickFix("Update to " + available, obsolete, available) {

            @Override
            protected void editBefore(@NotNull Document document) {
                Project project = startElement.getProject();
                final XmlFile file = PsiTreeUtil.getParentOfType(startElement, XmlFile.class);
                if (file != null) {
                    AndroidResourceUtil.ensureNamespaceImported(file, AUTO_URI, null);
                    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
                }
            }
        } };
    }
    return AndroidLintQuickFix.EMPTY_ARRAY;
}
Also used : Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with AndroidLintQuickFix

use of org.jetbrains.android.inspections.lint.AndroidLintQuickFix in project android by JetBrains.

the class AndroidLintMipmapIconsInspection method getQuickFixes.

@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    PsiElement parent = startElement.getParent();
    if (parent instanceof XmlAttribute) {
        XmlAttribute attribute = (XmlAttribute) parent;
        String value = attribute.getValue();
        if (value != null) {
            ResourceUrl url = ResourceUrl.parse(value);
            if (url != null && !url.framework) {
                return new AndroidLintQuickFix[] { new MigrateDrawableToMipmapFix(url) };
            }
        }
    }
    return AndroidLintQuickFix.EMPTY_ARRAY;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) PsiElement(com.intellij.psi.PsiElement) ResourceUrl(com.android.ide.common.resources.ResourceUrl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AndroidLintQuickFix (org.jetbrains.android.inspections.lint.AndroidLintQuickFix)11 NotNull (org.jetbrains.annotations.NotNull)9 Project (com.intellij.openapi.project.Project)4 PsiElement (com.intellij.psi.PsiElement)4 XmlAttribute (com.intellij.psi.xml.XmlAttribute)4 XmlFile (com.intellij.psi.xml.XmlFile)2 SetAttributeQuickFix (org.jetbrains.android.inspections.lint.SetAttributeQuickFix)2 ResourceUrl (com.android.ide.common.resources.ResourceUrl)1 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)1 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)1 ResourceFolderType (com.android.resources.ResourceFolderType)1 SuppressLintIntentionAction (com.android.tools.idea.lint.SuppressLintIntentionAction)1 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)1 ConvertToWebpAction (com.android.tools.idea.rendering.webp.ConvertToWebpAction)1 NlModel (com.android.tools.idea.uibuilder.model.NlModel)1 AppIndexingApiDetector (com.android.tools.lint.checks.AppIndexingApiDetector)1 Issue (com.android.tools.lint.detector.api.Issue)1 HtmlBuilder (com.android.utils.HtmlBuilder)1 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)1 AddAnnotationFix (com.intellij.codeInsight.intention.AddAnnotationFix)1