mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
SuppressInspectionCommentFix: add support of language contains block-comments only
This commit is contained in:
+53
-20
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.intellij.codeInsight.daemon.impl.actions;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.intellij.codeInsight.FileModificationService;
|
||||
import com.intellij.codeInspection.InspectionsBundle;
|
||||
import com.intellij.codeInspection.SuppressIntentionAction;
|
||||
@@ -24,8 +25,8 @@ import com.intellij.lang.Commenter;
|
||||
import com.intellij.lang.LanguageCommenters;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.fileTypes.LanguageFileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiParserFacade;
|
||||
@@ -50,9 +51,9 @@ public abstract class AbstractSuppressByNoInspectionCommentFix extends SuppressI
|
||||
protected abstract PsiElement getContainer(final PsiElement context);
|
||||
|
||||
/**
|
||||
* @param ID Inspection ID
|
||||
* @param ID Inspection ID
|
||||
* @param replaceOtherSuppressionIds Merge suppression policy. If false new tool id will be append to the end
|
||||
* otherwize replace other ids
|
||||
* otherwise replace other ids
|
||||
*/
|
||||
public AbstractSuppressByNoInspectionCommentFix(final String ID, final boolean replaceOtherSuppressionIds) {
|
||||
myID = ID;
|
||||
@@ -61,41 +62,66 @@ public abstract class AbstractSuppressByNoInspectionCommentFix extends SuppressI
|
||||
|
||||
protected final void replaceSuppressionComment(@NotNull final PsiElement comment) {
|
||||
final String oldSuppressionCommentText = comment.getText();
|
||||
final String prefix = getLineCommentPrefix(comment);
|
||||
assert prefix != null && oldSuppressionCommentText.startsWith(prefix) : "Unexpected suppression comment " + oldSuppressionCommentText;
|
||||
|
||||
final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(comment.getProject());
|
||||
final String lineCommentPrefix = getLineCommentPrefix(comment);
|
||||
Pair<String, String> blockPrefixSuffix = null;
|
||||
if (lineCommentPrefix == null) {
|
||||
blockPrefixSuffix = getBlockPrefixSuffixPair(comment);
|
||||
}
|
||||
assert (blockPrefixSuffix != null && oldSuppressionCommentText.startsWith(blockPrefixSuffix.first)) && oldSuppressionCommentText.endsWith(blockPrefixSuffix.second)
|
||||
|| (lineCommentPrefix != null && oldSuppressionCommentText.startsWith(lineCommentPrefix))
|
||||
: "Unexpected suppression comment " + oldSuppressionCommentText;
|
||||
|
||||
// append new suppression tool id or replace
|
||||
final String newText = myReplaceOtherSuppressionIds
|
||||
? SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME + " " + myID
|
||||
: oldSuppressionCommentText.substring(prefix.length()) + "," + myID;
|
||||
final String newText;
|
||||
if(myReplaceOtherSuppressionIds) {
|
||||
newText = SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME + " " + myID;
|
||||
} else {
|
||||
if (lineCommentPrefix != null) {
|
||||
newText = oldSuppressionCommentText.substring(lineCommentPrefix.length()) + "," + myID;
|
||||
} else {
|
||||
newText = oldSuppressionCommentText.substring(blockPrefixSuffix.first.length(),
|
||||
oldSuppressionCommentText.length() - blockPrefixSuffix.second.length()) + "," + myID;
|
||||
}
|
||||
}
|
||||
|
||||
PsiElement parent = comment.getParent();
|
||||
LanguageFileType fileType = parent == null ? null : parent.getLanguage().getAssociatedFileType();
|
||||
if (fileType == null) {
|
||||
fileType = (LanguageFileType)comment.getContainingFile().getFileType();
|
||||
}
|
||||
final PsiComment newComment = parserFacade.createLineCommentFromText(fileType, newText);
|
||||
comment.replace(newComment);
|
||||
comment.replace(createComment(comment.getProject(), parent != null ? parent : comment, newText));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected static String getLineCommentPrefix(@NotNull final PsiElement comment) {
|
||||
private static String getLineCommentPrefix(@NotNull final PsiElement comment) {
|
||||
final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(comment.getLanguage());
|
||||
return commenter == null ? null : commenter.getLineCommentPrefix();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Pair<String, String> getBlockPrefixSuffixPair(PsiElement comment) {
|
||||
final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(comment.getLanguage());
|
||||
if (commenter != null) {
|
||||
final String prefix = commenter.getBlockCommentPrefix();
|
||||
final String suffix = commenter.getBlockCommentSuffix();
|
||||
if (prefix != null || suffix != null) {
|
||||
return Pair.create(Strings.nullToEmpty(prefix), Strings.nullToEmpty(suffix));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void createSuppression(final Project project,
|
||||
final Editor editor,
|
||||
final PsiElement element,
|
||||
final PsiElement container) throws IncorrectOperationException {
|
||||
final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
|
||||
final String text = SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME + " " + myID;
|
||||
PsiComment comment = parserFacade.createLineOrBlockCommentFromText(element.getLanguage(), text);
|
||||
PsiComment comment = createComment(project, element, text);
|
||||
container.getParent().addBefore(comment, container);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected PsiComment createComment(Project project, PsiElement element, String commentText) {
|
||||
final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
|
||||
return parserFacade.createLineOrBlockCommentFromText(element.getLanguage(), commentText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement context) {
|
||||
return context.isValid() && context.getManager().isInProject(context) && getContainer(context) != null;
|
||||
@@ -129,7 +155,14 @@ public abstract class AbstractSuppressByNoInspectionCommentFix extends SuppressI
|
||||
|
||||
public static boolean isSuppressionComment(PsiElement comment) {
|
||||
final String prefix = getLineCommentPrefix(comment);
|
||||
return prefix != null && comment.getText().startsWith(prefix + SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME);
|
||||
final String commentText = comment.getText();
|
||||
if (prefix != null) {
|
||||
return commentText.startsWith(prefix + SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME);
|
||||
}
|
||||
final Pair<String, String> prefixSuffixPair = getBlockPrefixSuffixPair(comment);
|
||||
return prefixSuffixPair != null
|
||||
&& commentText.startsWith(prefixSuffixPair.first + SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME)
|
||||
&& commentText.endsWith(prefixSuffixPair.second);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
Reference in New Issue
Block a user