Inspections - pass onTheFly into ProblemDescriptors & use it to create LAZY refs...
[fedora-idea.git] / plugins / java-i18n / src / com / intellij / lang / properties / UnusedMessageFormatParameterInspection.java
blob87d0208c59614c65c8f4f2d2aea4aa5046bef894
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.lang.properties;
18 import com.intellij.codeInspection.InspectionManager;
19 import com.intellij.codeInspection.LocalQuickFix;
20 import com.intellij.codeInspection.ProblemDescriptor;
21 import com.intellij.codeInspection.ProblemHighlightType;
22 import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
23 import com.intellij.lang.ASTNode;
24 import com.intellij.lang.properties.psi.PropertiesFile;
25 import com.intellij.lang.properties.psi.Property;
26 import com.intellij.psi.PsiElement;
27 import com.intellij.psi.PsiFile;
28 import org.jetbrains.annotations.NonNls;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
32 import java.util.ArrayList;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
37 /**
38 * User: anna
39 * Date: 07-Sep-2005
41 public class UnusedMessageFormatParameterInspection extends BaseLocalInspectionTool {
42 @NotNull
43 public String getGroupDisplayName() {
44 return PropertiesBundle.message("properties.files.inspection.group.display.name");
47 @NotNull
48 public String getDisplayName() {
49 return PropertiesBundle.message("unused.message.format.parameter.display.name");
52 @NotNull
53 @NonNls
54 public String getShortName() {
55 return "UnusedMessageFormatParameter";
58 @Nullable
59 public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
60 if (!(file instanceof PropertiesFile)) return null;
61 PropertiesFile propertiesFile = (PropertiesFile)file;
62 final List<Property> properties = propertiesFile.getProperties();
63 List<ProblemDescriptor> problemDescriptors = new ArrayList<ProblemDescriptor>();
64 for (Property property : properties) {
65 @NonNls String name = property.getName();
66 if (name != null && name.startsWith("log4j")) continue;
67 String value = property.getValue();
68 Set<Integer> parameters = new HashSet<Integer>();
69 if (value != null) {
70 int index = value.indexOf('{');
71 while (index != -1) {
72 value = value.substring(index + 1);
73 final int comma = value.indexOf(',');
74 final int brace = value.indexOf('}');
75 if (brace == -1) break; //misformatted string
76 if (comma == -1) {
77 index = brace;
79 else {
80 index = Math.min(comma, brace);
82 try {
83 parameters.add(new Integer(value.substring(0, index)));
85 catch (NumberFormatException e) {
86 break;
88 index = value.indexOf('{');
90 for (Integer integer : parameters) {
91 for (int i = 0; i < integer.intValue(); i++) {
92 if (!parameters.contains(new Integer(i))) {
93 ASTNode[] nodes = property.getNode().getChildren(null);
94 PsiElement valElement = nodes.length < 3 ? property : nodes[2].getPsi();
95 problemDescriptors.add(manager.createProblemDescriptor(valElement, PropertiesBundle.message(
96 "unused.message.format.parameter.problem.descriptor", integer.toString(), Integer.toString(i)),
97 (LocalQuickFix[])null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
98 isOnTheFly));
99 break;
105 return problemDescriptors.isEmpty() ? null : problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);