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.LocalInspectionToolSession;
19 import com.intellij.codeInspection.ProblemHighlightType;
20 import com.intellij.codeInspection.ProblemsHolder;
21 import com.intellij.lang.ASTNode;
22 import com.intellij.lang.properties.psi.Property;
23 import com.intellij.openapi.module.Module;
24 import com.intellij.openapi.module.ModuleUtil;
25 import com.intellij.openapi.progress.ProgressIndicator;
26 import com.intellij.openapi.progress.ProgressManager;
27 import com.intellij.openapi.util.text.StringUtil;
28 import com.intellij.psi.PsiElement;
29 import com.intellij.psi.PsiElementVisitor;
30 import com.intellij.psi.PsiFile;
31 import com.intellij.psi.PsiReference;
32 import com.intellij.psi.search.GlobalSearchScope;
33 import com.intellij.psi.search.PsiSearchHelper;
34 import com.intellij.psi.search.searches.ReferencesSearch;
35 import org.jetbrains.annotations.NotNull;
37 import java.util.List;
42 public class UnusedPropertyInspection extends PropertySuppressableInspectionBase {
44 public String getDisplayName() {
45 return PropertiesBundle.message("unused.property.inspection.display.name");
49 public String getShortName() {
50 return "UnusedProperty";
56 public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder,
57 final boolean isOnTheFly,
58 @NotNull final LocalInspectionToolSession session) {
59 final PsiFile file = session.getFile();
60 Module module = ModuleUtil.findModuleForPsiElement(file);
61 if (module == null) return super.buildVisitor(holder, isOnTheFly, session);
62 final GlobalSearchScope searchScope = GlobalSearchScope.moduleWithDependentsScope(module);
63 final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(file.getProject());
64 return new PsiElementVisitor() {
66 public void visitElement(PsiElement element) {
67 if (!(element instanceof Property)) return;
68 Property property = (Property)element;
70 final ProgressIndicator original = ProgressManager.getInstance().getProgressIndicator();
71 if (original != null) {
72 if (original.isCanceled()) return;
73 original.setText(PropertiesBundle.message("searching.for.property.key.progress.text", property.getUnescapedKey()));
76 String name = property.getName();
77 if (name == null) return;
79 final List<String> words = StringUtil.getWordsIn(name);
80 if (words.isEmpty()) {
84 final String lastWord = words.get(words.size() - 1);
85 PsiSearchHelper.SearchCostResult cheapEnough = searchHelper.isCheapEnoughToSearch(lastWord, searchScope, file, original);
86 if (cheapEnough == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) return;
88 cheapEnough = searchHelper.isCheapEnoughToSearch(name, searchScope, file, original);
89 if (cheapEnough == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) return;
91 final PsiReference usage = cheapEnough == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES ? null :
92 ReferencesSearch.search(property, searchScope, false).findFirst();
93 if (usage != null) return;
94 final ASTNode propertyNode = property.getNode();
95 assert propertyNode != null;
97 ASTNode[] nodes = propertyNode.getChildren(null);
98 PsiElement key = nodes.length == 0 ? property : nodes[0].getPsi();
99 String description = PropertiesBundle.message("unused.property.problem.descriptor.name");
101 holder.registerProblem(key, description, ProblemHighlightType.LIKE_UNUSED_SYMBOL,RemovePropertyLocalFix.INSTANCE);