Allow to customize comment language for suppressCommentFixes

This commit is contained in:
Alexander Zolotov
2013-05-08 19:33:19 +04:00
parent 4247ee484a
commit 4f4f3eed0f
3 changed files with 36 additions and 13 deletions
@@ -22,6 +22,7 @@ import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.SuppressQuickFix;
import com.intellij.codeInspection.SuppressionUtil;
import com.intellij.icons.AllIcons;
import com.intellij.lang.Language;
import com.intellij.openapi.command.undo.UndoUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Iconable;
@@ -97,13 +98,23 @@ public abstract class AbstractBatchSuppressByNoInspectionCommentFix implements S
}
protected final void replaceSuppressionComment(@NotNull final PsiElement comment) {
SuppressionUtil.replaceSuppressionComment(comment, myID, myReplaceOtherSuppressionIds);
SuppressionUtil.replaceSuppressionComment(comment, myID, myReplaceOtherSuppressionIds, getCommentLanguage(comment));
}
protected void createSuppression(@NotNull Project project,
@NotNull PsiElement element,
@NotNull PsiElement container) throws IncorrectOperationException {
SuppressionUtil.createSuppression(project, element, container, myID);
SuppressionUtil.createSuppression(project, container, myID, getCommentLanguage(element));
}
/**
* @param element quickfix target or existing comment element
* @return language that will be used for comment creating.
* In common case language will be the same as language of quickfix target
*/
@NotNull
protected Language getCommentLanguage(@NotNull PsiElement element) {
return element.getLanguage();
}
@Override
@@ -17,6 +17,7 @@
package com.intellij.codeInspection;
import com.intellij.lang.Commenter;
import com.intellij.lang.Language;
import com.intellij.lang.LanguageCommenters;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
@@ -107,9 +108,11 @@ public class SuppressionUtil {
}
@NotNull
public static PsiComment createComment(@NotNull Project project, @NotNull PsiElement element, @NotNull String commentText) {
public static PsiComment createComment(@NotNull Project project,
@NotNull String commentText,
@NotNull Language language) {
final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
return parserFacade.createLineOrBlockCommentFromText(element.getLanguage(), commentText);
return parserFacade.createLineOrBlockCommentFromText(language, commentText);
}
@Nullable
@@ -143,7 +146,8 @@ public class SuppressionUtil {
&& commentText.endsWith(prefixSuffixPair.second);
}
public static void replaceSuppressionComment(@NotNull PsiElement comment, @NotNull String id, boolean replaceOtherSuppressionIds) {
public static void replaceSuppressionComment(@NotNull PsiElement comment, @NotNull String id,
boolean replaceOtherSuppressionIds, @NotNull Language commentLanguage) {
final String oldSuppressionCommentText = comment.getText();
final String lineCommentPrefix = getLineCommentPrefix(comment);
Pair<String, String> blockPrefixSuffix = null;
@@ -168,17 +172,14 @@ public class SuppressionUtil {
else {
newText = oldSuppressionCommentText.substring(lineCommentPrefix.length()) + "," + id;
}
PsiElement parent = comment.getParent();
comment.replace(createComment(comment.getProject(), parent != null ? parent : comment, newText));
comment.replace(createComment(comment.getProject(), newText, commentLanguage));
}
public static void createSuppression(@NotNull Project project,
@NotNull PsiElement element,
@NotNull PsiElement container,
@NotNull String id) {
@NotNull String id, @NotNull Language commentLanguage) {
final String text = SUPPRESS_INSPECTIONS_TAG_NAME + " " + id;
PsiComment comment = createComment(project, element, text);
PsiComment comment = createComment(project, text, commentLanguage);
container.getParent().addBefore(comment, container);
}
}
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.SuppressIntentionAction;
import com.intellij.codeInspection.SuppressionUtil;
import com.intellij.lang.Language;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.undo.UndoUtil;
import com.intellij.openapi.editor.Editor;
@@ -58,13 +59,23 @@ public abstract class AbstractSuppressByNoInspectionCommentFix extends SuppressI
}
protected final void replaceSuppressionComment(@NotNull final PsiElement comment) {
SuppressionUtil.replaceSuppressionComment(comment, myID, myReplaceOtherSuppressionIds);
SuppressionUtil.replaceSuppressionComment(comment, myID, myReplaceOtherSuppressionIds, getCommentLanguage(comment));
}
protected void createSuppression(@NotNull Project project,
@NotNull PsiElement element,
@NotNull PsiElement container) throws IncorrectOperationException {
SuppressionUtil.createSuppression(project, element, container, myID);
SuppressionUtil.createSuppression(project, container, myID, getCommentLanguage(element));
}
/**
* @param element quickfix target or existing comment element
* @return language that will be used for comment creating.
* In common case language will be the same as language of quickfix target
*/
@NotNull
protected Language getCommentLanguage(@NotNull PsiElement element) {
return element.getLanguage();
}
@Override