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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations