Search in sources :

Example 1 with AndroidLintQuickFix

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

the class AndroidLintAnimatorKeepInspection method getQuickFixes.

@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    return new AndroidLintQuickFix[] { new AndroidLintQuickFix() {

        @Override
        public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
            if (!ObjectAnimatorDetector.isAddKeepErrorMessage(message, TextFormat.RAW)) {
                return;
            }
            PsiModifierListOwner container = PsiTreeUtil.getParentOfType(startElement, PsiModifierListOwner.class);
            if (container == null) {
                return;
            }
            if (!FileModificationService.getInstance().preparePsiElementForWrite(container)) {
                return;
            }
            final PsiModifierList modifierList = container.getModifierList();
            if (modifierList != null) {
                PsiAnnotation annotation = AnnotationUtil.findAnnotation(container, KEEP_ANNOTATION);
                if (annotation == null) {
                    Project project = startElement.getProject();
                    new AddAnnotationFix(KEEP_ANNOTATION, container).invoke(project, null, container.getContainingFile());
                }
            }
        }

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

        @NotNull
        @Override
        public String getName() {
            return "Annotate with @Keep";
        }
    } };
}
Also used : Project(com.intellij.openapi.project.Project) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) PsiAnnotation(com.intellij.psi.PsiAnnotation) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix) PsiModifierList(com.intellij.psi.PsiModifierList) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with AndroidLintQuickFix

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

the class AndroidLintConvertToWebpInspection method getQuickFixes.

@Override
@NotNull
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    return new AndroidLintQuickFix[] { new DefaultLintQuickFix("Convert images to WebP...") {

        @Override
        public boolean isApplicable(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.ContextType contextType) {
            return AndroidFacet.getInstance(startElement) != null;
        }

        @Override
        public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
            AndroidFacet facet = AndroidFacet.getInstance(startElement);
            if (facet != null) {
                AndroidModuleInfo info = AndroidModuleInfo.get(facet);
                int minSdkVersion = info.getMinSdkVersion().getApiLevel();
                List<VirtualFile> folders = facet.getResourceFolderManager().getFolders();
                ConvertToWebpAction action = new ConvertToWebpAction();
                action.perform(startElement.getProject(), minSdkVersion, folders.toArray(VirtualFile.EMPTY_ARRAY));
            }
        }
    } };
}
Also used : AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ConvertToWebpAction(com.android.tools.idea.rendering.webp.ConvertToWebpAction) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with AndroidLintQuickFix

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

the class AndroidLintGoogleAppIndexingApiWarningInspection method getAppIndexingQuickFix.

@NotNull
static AndroidLintQuickFix[] getAppIndexingQuickFix(PsiElement startElement, String message) {
    AppIndexingApiDetector.IssueType type = AppIndexingApiDetector.IssueType.parse(message);
    switch(type) {
        case SCHEME_MISSING:
        case URL_MISSING:
            return new AndroidLintQuickFix[] { new SetAttributeQuickFix("Set scheme", ATTR_SCHEME, "http") };
        case HOST_MISSING:
            return new AndroidLintQuickFix[] { new SetAttributeQuickFix("Set host", ATTR_HOST, null) };
        case MISSING_SLASH:
            PsiElement parent = startElement.getParent();
            if (parent instanceof XmlAttribute) {
                XmlAttribute attr = (XmlAttribute) parent;
                String path = attr.getValue();
                if (path != null) {
                    return new AndroidLintQuickFix[] { new ReplaceStringQuickFix("Replace with /" + path, path, "/" + path) };
                }
            }
            break;
        default:
            break;
    }
    return AndroidLintQuickFix.EMPTY_ARRAY;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) AppIndexingApiDetector(com.android.tools.lint.checks.AppIndexingApiDetector) SetAttributeQuickFix(org.jetbrains.android.inspections.lint.SetAttributeQuickFix) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with AndroidLintQuickFix

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

the class AndroidLintInvalidUsesTagAttributeInspection method getQuickFixes.

@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    final XmlAttribute attribute = PsiTreeUtil.getParentOfType(startElement, XmlAttribute.class);
    XmlAttributeValue attributeValue = attribute == null ? null : attribute.getValueElement();
    if (attributeValue != null && attributeValue.getTextLength() != 0) {
        String value = StringUtil.unquoteString(attributeValue.getText());
        String regexp = "(" + value + ")";
        String[] suggestions = AndroidAutoDetector.getAllowedAutomotiveAppTypes();
        List<AndroidLintQuickFix> fixes = Lists.newArrayListWithExpectedSize(suggestions.length);
        for (String suggestion : suggestions) {
            fixes.add(new ReplaceStringQuickFix("Replace with \"" + suggestion + "\"", regexp, suggestion));
        }
        return fixes.toArray(new AndroidLintQuickFix[fixes.size()]);
    } else {
        return AndroidLintQuickFix.EMPTY_ARRAY;
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with AndroidLintQuickFix

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

the class AndroidLintNetworkSecurityConfigInspection method getQuickFixes.

@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    if (NetworkSecurityConfigDetector.isInvalidDigestAlgorithmMessage(message)) {
        List<String> digestAlgs = NetworkSecurityConfigDetector.getSupportedPinDigestAlgorithms();
        AndroidLintQuickFix[] digestFixes = new AndroidLintQuickFix[digestAlgs.size()];
        for (int i = 0; i < digestFixes.length; i++) {
            String algorithm = digestAlgs.get(i);
            digestFixes[i] = new ReplaceStringQuickFix(String.format("Set digest to \"%1$s\"", algorithm), null, algorithm);
        }
        return digestFixes;
    } else if (NetworkSecurityConfigDetector.isAttributeSpellingError(message)) {
        XmlTag parentTag = PsiTreeUtil.getParentOfType(startElement, XmlTag.class, false);
        XmlAttribute currentAttr = PsiTreeUtil.getParentOfType(startElement, XmlAttribute.class, false);
        assert parentTag != null;
        assert currentAttr != null;
        List<String> suggestions = NetworkSecurityConfigDetector.getAttributeSpellingSuggestions(currentAttr.getName(), parentTag.getName());
        AndroidLintQuickFix[] attrFixes = new AndroidLintQuickFix[suggestions.size()];
        for (int i = 0; i < attrFixes.length; i++) {
            attrFixes[i] = new RenameAttributeQuickFix(null, /* no namespace */
            suggestions.get(i));
        }
        return attrFixes;
    } else if (NetworkSecurityConfigDetector.isTagSpellingError(message)) {
        XmlTag currentTag = PsiTreeUtil.getParentOfType(startElement, XmlTag.class, false);
        assert currentTag != null;
        XmlTag parentTag = currentTag.getParentTag();
        assert parentTag != null;
        List<String> suggestions = NetworkSecurityConfigDetector.getTagSpellingSuggestions(currentTag.getName(), parentTag.getName());
        AndroidLintQuickFix[] elementQuickFixes = new AndroidLintQuickFix[suggestions.size()];
        for (int i = 0; i < elementQuickFixes.length; i++) {
            elementQuickFixes[i] = new RenameXmlTagQuickFix(suggestions.get(i));
        }
        return elementQuickFixes;
    } else {
        return AndroidLintQuickFix.EMPTY_ARRAY;
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) List(java.util.List) RenameAttributeQuickFix(org.jetbrains.android.inspections.lint.RenameAttributeQuickFix) XmlTag(com.intellij.psi.xml.XmlTag) 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