update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInspection / SuppressionUtil.java
blob79fdfcf0473a56efb8db94b0a1490cc143745212
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.
17 package com.intellij.codeInspection;
19 import com.intellij.openapi.application.ApplicationManager;
20 import com.intellij.openapi.util.NullableComputable;
21 import com.intellij.openapi.util.text.StringUtil;
22 import com.intellij.psi.PsiComment;
23 import com.intellij.psi.PsiElement;
24 import com.intellij.psi.PsiWhiteSpace;
25 import com.intellij.psi.util.PsiTreeUtil;
26 import org.jetbrains.annotations.NonNls;
27 import org.jetbrains.annotations.Nullable;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
32 /**
33 * @author yole
35 public class SuppressionUtil {
36 @NonNls public static final String SUPPRESS_INSPECTIONS_TAG_NAME = "noinspection";
38 /**
39 * Common part of regexp for suppressing in line comments for different languages.
40 * Comment start prefix isn't included, e.g. add '//' for Java/C/JS or '#' for Ruby
42 public static final String COMMON_SUPPRESS_REGEXP = "\\s*"
43 + SUPPRESS_INSPECTIONS_TAG_NAME
44 + "\\s+(\\w+(\\s*,\\w+)*\\s*\\w*)";
46 @NonNls public static final Pattern SUPPRESS_IN_LINE_COMMENT_PATTERN =
47 Pattern.compile("//" + COMMON_SUPPRESS_REGEXP); // for Java, C, JS line comments
49 private SuppressionUtil() {
52 public static boolean isInspectionToolIdMentioned(String inspectionsList, String inspectionToolID) {
53 Iterable<String> ids = StringUtil.tokenize(inspectionsList, "[, ]");
55 for (@NonNls String id : ids) {
56 if (id.trim().equals(inspectionToolID) || id.trim().equals("ALL")) return true;
58 return false;
61 @Nullable
62 public static PsiElement getStatementToolSuppressedIn(final PsiElement place,
63 final String toolId,
64 final Class<? extends PsiElement> statementClass) {
65 return getStatementToolSuppressedIn(place, toolId, statementClass, SUPPRESS_IN_LINE_COMMENT_PATTERN);
68 @Nullable
69 public static PsiElement getStatementToolSuppressedIn(final PsiElement place,
70 final String toolId,
71 final Class<? extends PsiElement> statementClass,
72 final Pattern suppressInLineCommentPattern) {
73 PsiElement statement = PsiTreeUtil.getNonStrictParentOfType(place, statementClass);
74 if (statement != null) {
75 PsiElement prev = PsiTreeUtil.skipSiblingsBackward(statement, PsiWhiteSpace.class);
76 if (prev instanceof PsiComment) {
77 String text = prev.getText();
78 Matcher matcher = suppressInLineCommentPattern.matcher(text);
79 if (matcher.matches() && isInspectionToolIdMentioned(matcher.group(1), toolId)) {
80 return prev;
84 return null;
87 public static boolean isSuppressedInStatement(final PsiElement place,
88 final String toolId,
89 final Class<? extends PsiElement> statementClass) {
90 return ApplicationManager.getApplication().runReadAction(new NullableComputable<PsiElement>() {
91 public PsiElement compute() {
92 return getStatementToolSuppressedIn(place, toolId, statementClass);
94 }) != null;