use of org.jetbrains.plugins.groovy.codeInspection.GroovyFix in project intellij-community by JetBrains.
the class ChangeToMethodInspection method getFix.
@Nullable
protected GroovyFix getFix(@NotNull Transformation<?> transformation) {
return new GroovyFix() {
@Nls
@NotNull
@Override
public String getFamilyName() {
return message("replace.with.method.fix", transformation.getMethod());
}
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
PsiElement call = descriptor.getPsiElement().getParent();
if (!(call instanceof GrExpression))
return;
if (!transformation.couldApplyRow((GrExpression) call))
return;
transformation.applyRow((GrExpression) call);
}
};
}
use of org.jetbrains.plugins.groovy.codeInspection.GroovyFix in project intellij-community by JetBrains.
the class GrAccessibilityChecker method buildFixes.
static GroovyFix[] buildFixes(PsiElement location, GroovyResolveResult resolveResult) {
final PsiElement element = resolveResult.getElement();
if (!(element instanceof PsiMember))
return GroovyFix.EMPTY_ARRAY;
final PsiMember refElement = (PsiMember) element;
if (refElement instanceof PsiCompiledElement)
return GroovyFix.EMPTY_ARRAY;
PsiModifierList modifierList = refElement.getModifierList();
if (modifierList == null)
return GroovyFix.EMPTY_ARRAY;
List<GroovyFix> fixes = new ArrayList<>();
try {
Project project = refElement.getProject();
JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
PsiModifierList modifierListCopy = facade.getElementFactory().createFieldFromText("int a;", null).getModifierList();
assert modifierListCopy != null;
modifierListCopy.setModifierProperty(PsiModifier.STATIC, modifierList.hasModifierProperty(PsiModifier.STATIC));
String minModifier = PsiModifier.PROTECTED;
if (refElement.hasModifierProperty(PsiModifier.PROTECTED)) {
minModifier = PsiModifier.PUBLIC;
}
String[] modifiers = { PsiModifier.PROTECTED, PsiModifier.PUBLIC, PsiModifier.PACKAGE_LOCAL };
PsiClass accessObjectClass = PsiTreeUtil.getParentOfType(location, PsiClass.class, false);
if (accessObjectClass == null) {
final PsiFile file = location.getContainingFile();
if (!(file instanceof GroovyFile))
return GroovyFix.EMPTY_ARRAY;
accessObjectClass = ((GroovyFile) file).getScriptClass();
}
for (int i = ArrayUtil.indexOf(modifiers, minModifier); i < modifiers.length; i++) {
String modifier = modifiers[i];
modifierListCopy.setModifierProperty(modifier, true);
if (facade.getResolveHelper().isAccessible(refElement, modifierListCopy, location, accessObjectClass, null)) {
fixes.add(new GrModifierFix(refElement, modifier, true, true, GrModifierFix.MODIFIER_LIST_OWNER));
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return fixes.toArray(new GroovyFix[fixes.size()]);
}
use of org.jetbrains.plugins.groovy.codeInspection.GroovyFix in project intellij-community by JetBrains.
the class GrAccessibilityChecker method registerFixes.
private void registerFixes(GrReferenceElement ref, GroovyResolveResult result, HighlightInfo info) {
PsiElement element = result.getElement();
assert element != null;
ProblemDescriptor descriptor = InspectionManager.getInstance(ref.getProject()).createProblemDescriptor(element, element, "", HighlightInfo.convertSeverityToProblemHighlight(info.getSeverity()), true, LocalQuickFix.EMPTY_ARRAY);
for (GroovyFix fix : buildFixes(ref, result)) {
QuickFixAction.registerQuickFixAction(info, new LocalQuickFixAsIntentionAdapter(fix, descriptor), myDisplayKey);
}
}
use of org.jetbrains.plugins.groovy.codeInspection.GroovyFix in project intellij-community by JetBrains.
the class ChangeToOperatorInspection method getFix.
@Nullable
protected GroovyFix getFix(@NotNull Transformation transformation, @NotNull String methodName) {
return new GroovyFix() {
@Nls
@NotNull
@Override
public String getFamilyName() {
return message("replace.with.operator.fix", methodName);
}
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
PsiElement call = descriptor.getPsiElement().getParent();
if (call == null)
return;
call = call.getParent();
if (!(call instanceof GrMethodCall))
return;
GrMethodCall methodCall = (GrMethodCall) call;
GrExpression invokedExpression = methodCall.getInvokedExpression();
if (!(invokedExpression instanceof GrReferenceExpression))
return;
Options options = getOptions();
if (!transformation.couldApply(methodCall, options))
return;
transformation.apply(methodCall, options);
}
};
}
use of org.jetbrains.plugins.groovy.codeInspection.GroovyFix in project intellij-community by JetBrains.
the class GroovyRangeTypeCheckTest method doTest.
public void doTest() {
myFixture.configureByFile(getTestName(false) + ".groovy");
final int offset = myFixture.getEditor().getCaretModel().getOffset();
final PsiElement atCaret = myFixture.getFile().findElementAt(offset);
final GrRangeExpression range = PsiTreeUtil.getParentOfType(atCaret, GrRangeExpression.class);
final GroovyRangeTypeCheckInspection inspection = new GroovyRangeTypeCheckInspection();
final GroovyFix fix = inspection.buildFix(range);
LocalQuickFix[] fixes = { fix };
final ProblemDescriptor descriptor = InspectionManager.getInstance(getProject()).createProblemDescriptor(range, "bla-bla", false, fixes, ProblemHighlightType.WEAK_WARNING);
WriteCommandAction.runWriteCommandAction(null, () -> {
fix.applyFix(myFixture.getProject(), descriptor);
PostprocessReformattingAspect.getInstance(getProject()).doPostponedFormatting();
});
myFixture.checkResultByFile(getTestName(false) + "_after.groovy");
}
Aggregations