use of com.intellij.codeInspection.ProblemDescriptor in project intellij-community by JetBrains.
the class GroovyStaticTypeCheckVisitor method registerError.
@Override
protected void registerError(@NotNull final PsiElement location, @NotNull final String description, @Nullable final LocalQuickFix[] fixes, final ProblemHighlightType highlightType) {
if (highlightType != ProblemHighlightType.GENERIC_ERROR)
return;
final List<IntentionAction> intentions = ContainerUtil.newArrayList();
if (fixes != null) {
for (final LocalQuickFix fix : fixes) {
intentions.add(new IntentionAction() {
@NotNull
@Override
public String getText() {
return fix.getName();
}
@NotNull
@Override
public String getFamilyName() {
return fix.getFamilyName();
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return true;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final InspectionManager manager = InspectionManager.getInstance(project);
final ProblemDescriptor descriptor = manager.createProblemDescriptor(location, description, fixes, highlightType, fixes.length == 1, false);
fix.applyFix(project, descriptor);
}
@Override
public boolean startInWriteAction() {
return true;
}
});
}
}
registerError(location, description, intentions.toArray(new IntentionAction[intentions.size()]), highlightType);
}
use of com.intellij.codeInspection.ProblemDescriptor in project intellij-community by JetBrains.
the class GroovyRangeTypeCheckInspection method buildFix.
@Override
protected GroovyFix buildFix(@NotNull PsiElement location) {
final GrRangeExpression range = (GrRangeExpression) location;
final PsiType type = range.getType();
final List<GroovyFix> fixes = new ArrayList<>(3);
if (type instanceof GrRangeType) {
PsiType iterationType = ((GrRangeType) type).getIterationType();
if (!(iterationType instanceof PsiClassType))
return null;
final PsiClass psiClass = ((PsiClassType) iterationType).resolve();
if (!(psiClass instanceof GrTypeDefinition))
return null;
final GroovyResolveResult[] nexts = ResolveUtil.getMethodCandidates(iterationType, "next", range);
final GroovyResolveResult[] previouses = ResolveUtil.getMethodCandidates(iterationType, "previous", range);
final GroovyResolveResult[] compareTos = ResolveUtil.getMethodCandidates(iterationType, "compareTo", range, iterationType);
if (countImplementations(psiClass, nexts) == 0) {
fixes.add(GroovyQuickFixFactory.getInstance().createAddMethodFix("next", (GrTypeDefinition) psiClass));
}
if (countImplementations(psiClass, previouses) == 0) {
fixes.add(GroovyQuickFixFactory.getInstance().createAddMethodFix("previous", (GrTypeDefinition) psiClass));
}
if (!InheritanceUtil.isInheritor(iterationType, CommonClassNames.JAVA_LANG_COMPARABLE) || countImplementations(psiClass, compareTos) == 0) {
fixes.add(GroovyQuickFixFactory.getInstance().createAddClassToExtendsFix((GrTypeDefinition) psiClass, CommonClassNames.JAVA_LANG_COMPARABLE));
}
return new GroovyFix() {
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
for (GroovyFix fix : fixes) {
fix.applyFix(project, descriptor);
}
}
@NotNull
@Override
public String getName() {
return GroovyInspectionBundle.message("fix.class", psiClass.getName());
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return "Fix range class";
}
};
}
return null;
}
use of com.intellij.codeInspection.ProblemDescriptor 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.ProblemDescriptor 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.ProblemDescriptor in project intellij-community by JetBrains.
the class XmlImportOptimizer method processFile.
@NotNull
@Override
public CollectingInfoRunnable processFile(final PsiFile file) {
return new CollectingInfoRunnable() {
int myRemovedNameSpaces = 0;
@Override
public void run() {
XmlFile xmlFile = (XmlFile) file;
Project project = xmlFile.getProject();
HighlightDisplayKey key = HighlightDisplayKey.find(myInspection.getShortName());
if (!InspectionProjectProfileManager.getInstance(project).getCurrentProfile().isToolEnabled(key, xmlFile))
return;
ProblemsHolder holder = new ProblemsHolder(InspectionManager.getInstance(project), xmlFile, false);
final XmlElementVisitor visitor = (XmlElementVisitor) myInspection.buildVisitor(holder, false);
new PsiRecursiveElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof XmlAttribute) {
visitor.visitXmlAttribute((XmlAttribute) element);
} else {
super.visitElement(element);
}
}
}.visitFile(xmlFile);
ProblemDescriptor[] results = holder.getResultsArray();
List<ProblemDescriptor> list = ContainerUtil.filter(results, myCondition);
Map<XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix, ProblemDescriptor> fixes = new LinkedHashMap<>();
for (ProblemDescriptor result : list) {
for (QuickFix fix : result.getFixes()) {
if (fix instanceof XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix) {
fixes.put((XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix) fix, result);
}
}
}
SmartPsiElementPointer<XmlTag> pointer = null;
for (Map.Entry<XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix, ProblemDescriptor> fix : fixes.entrySet()) {
pointer = fix.getKey().doFix(project, fix.getValue(), false);
myRemovedNameSpaces++;
}
if (pointer != null) {
XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix.reformatStartTag(project, pointer);
}
}
@Nullable
@Override
public String getUserNotificationInfo() {
return myRemovedNameSpaces > 0 ? "Removed " + myRemovedNameSpaces + " namespace" + (myRemovedNameSpaces > 1 ? "s" : "") : null;
}
};
}
Aggregations