use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.
the class UnusedMessageFormatParameterInspection method checkFile.
@Nullable
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
if (!(file instanceof PropertiesFile))
return null;
PropertiesFile propertiesFile = (PropertiesFile) file;
final List<IProperty> properties = propertiesFile.getProperties();
List<ProblemDescriptor> problemDescriptors = new ArrayList<>();
for (IProperty property : properties) {
@NonNls String name = property.getName();
if (name != null) {
if (name.startsWith("log4j"))
continue;
if (name.startsWith(REGEXP + ".") || name.endsWith("." + REGEXP))
continue;
}
String value = property.getValue();
Set<Integer> parameters = new HashSet<>();
if (value != null) {
int index = value.indexOf('{');
while (index != -1) {
value = value.substring(index + 1);
final int comma = value.indexOf(',');
final int brace = value.indexOf('}');
//misformatted string
if (brace == -1)
break;
if (comma == -1) {
index = brace;
} else {
index = Math.min(comma, brace);
}
try {
parameters.add(new Integer(value.substring(0, index)));
} catch (NumberFormatException e) {
break;
}
index = value.indexOf('{');
}
for (Integer integer : parameters) {
for (int i = 0; i < integer.intValue(); i++) {
if (!parameters.contains(new Integer(i))) {
ASTNode[] nodes = property.getPsiElement().getNode().getChildren(null);
PsiElement valElement = nodes.length < 3 ? property.getPsiElement() : nodes[2].getPsi();
final String message = PropertiesBundle.message("unused.message.format.parameter.problem.descriptor", integer.toString(), Integer.toString(i));
final String propertyKey = property.getKey();
final LocalQuickFix[] fixes = isOnTheFly ? new LocalQuickFix[] { new RenameElementFix(((Property) property), propertyKey == null ? REGEXP : propertyKey + "." + REGEXP) } : null;
problemDescriptors.add(manager.createProblemDescriptor(valElement, message, isOnTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
break;
}
}
}
}
}
return problemDescriptors.isEmpty() ? null : problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
}
use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.
the class BaseInspectionVisitor method registerErrorAtOffset.
protected final void registerErrorAtOffset(@NotNull PsiElement location, int offset, int length, ProblemHighlightType highlightType, Object... infos) {
assert !(location.getTextLength() == 0 || length == 0);
final LocalQuickFix[] fixes = createAndInitFixes(infos);
final String description = inspection.buildErrorString(infos);
final TextRange range = new TextRange(offset, offset + length);
holder.registerProblem(location, description, highlightType, range, fixes);
}
use of com.intellij.codeInspection.LocalQuickFix 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");
}
use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.
the class JavaClassReference method registerFixes.
@Nullable
private List<? extends LocalQuickFix> registerFixes() {
final List<LocalQuickFix> list = QuickFixFactory.getInstance().registerOrderEntryFixes(new QuickFixActionRegistrarImpl(null), this);
final String[] extendClasses = getExtendClassNames();
final String extendClass = extendClasses != null && extendClasses.length > 0 ? extendClasses[0] : null;
final JavaClassReference[] references = getJavaClassReferenceSet().getAllReferences();
PsiPackage contextPackage = null;
for (int i = myIndex; i >= 0; i--) {
final PsiElement context = references[i].getContext();
if (context != null) {
if (context instanceof PsiPackage) {
contextPackage = (PsiPackage) context;
}
break;
}
}
boolean createJavaClass = !canReferencePackage();
ClassKind kind = createJavaClass ? getClassKind() : null;
if (createJavaClass && kind == null)
kind = ClassKind.CLASS;
final String templateName = JavaClassReferenceProvider.CLASS_TEMPLATE.getValue(getOptions());
final TextRange range = new TextRange(references[0].getRangeInElement().getStartOffset(), getRangeInElement().getEndOffset());
final String qualifiedName = range.substring(getElement().getText());
final CreateClassOrPackageFix action = CreateClassOrPackageFix.createFix(qualifiedName, getScope(getJavaContextFile()), getElement(), contextPackage, kind, extendClass, templateName);
if (action != null) {
if (list == null) {
return Collections.singletonList(action);
} else {
final ArrayList<LocalQuickFix> fixes = new ArrayList<>(list.size() + 1);
fixes.addAll(list);
fixes.add(action);
return fixes;
}
}
return list;
}
use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.
the class DomElementsHighlightingUtil method createAnnotation.
@Nullable
public static Annotation createAnnotation(final DomElementProblemDescriptor problemDescriptor) {
return createProblemDescriptors(problemDescriptor, s -> {
String text = problemDescriptor.getDescriptionTemplate();
if (StringUtil.isEmpty(text))
text = null;
final HighlightSeverity severity = problemDescriptor.getHighlightSeverity();
TextRange range = s.first;
if (text == null)
range = TextRange.from(range.getStartOffset(), 0);
range = range.shiftRight(s.second.getTextRange().getStartOffset());
final Annotation annotation = createAnnotation(severity, range, text);
if (problemDescriptor instanceof DomElementResolveProblemDescriptor) {
annotation.setTextAttributes(CodeInsightColors.WRONG_REFERENCES_ATTRIBUTES);
}
for (LocalQuickFix fix : problemDescriptor.getFixes()) {
if (fix instanceof IntentionAction)
annotation.registerFix((IntentionAction) fix);
}
return annotation;
});
}
Aggregations