From 4f4f3eed0f381f8fec5d519c20a5009867e19fef Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Wed, 8 May 2013 19:05:37 +0400 Subject: [PATCH] Allow to customize comment language for suppressCommentFixes --- ...BatchSuppressByNoInspectionCommentFix.java | 15 +++++++++++++-- .../codeInspection/SuppressionUtil.java | 19 ++++++++++--------- ...tractSuppressByNoInspectionCommentFix.java | 15 +++++++++++++-- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/actions/AbstractBatchSuppressByNoInspectionCommentFix.java b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/actions/AbstractBatchSuppressByNoInspectionCommentFix.java index c06185f03ccf..7a8c3e3c6cde 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/actions/AbstractBatchSuppressByNoInspectionCommentFix.java +++ b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/actions/AbstractBatchSuppressByNoInspectionCommentFix.java @@ -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 diff --git a/platform/analysis-impl/src/com/intellij/codeInspection/SuppressionUtil.java b/platform/analysis-impl/src/com/intellij/codeInspection/SuppressionUtil.java index 46ba2d991070..e98bcc37a3f4 100644 --- a/platform/analysis-impl/src/com/intellij/codeInspection/SuppressionUtil.java +++ b/platform/analysis-impl/src/com/intellij/codeInspection/SuppressionUtil.java @@ -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 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); } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/AbstractSuppressByNoInspectionCommentFix.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/AbstractSuppressByNoInspectionCommentFix.java index b219ef5d4367..214c3ca08c33 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/AbstractSuppressByNoInspectionCommentFix.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/AbstractSuppressByNoInspectionCommentFix.java @@ -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