revert: default min size for OptionsEditorDialog
[fedora-idea.git] / lang-impl / src / com / intellij / codeInsight / daemon / impl / actions / AddNoInspectionCommentFix.java
blob68f47d0da076b4a738b5de37cb368945576953d9
1 package com.intellij.codeInsight.daemon.impl.actions;
3 import com.intellij.codeInsight.daemon.HighlightDisplayKey;
4 import com.intellij.codeInspection.InspectionsBundle;
5 import com.intellij.codeInspection.SuppressIntentionAction;
6 import com.intellij.codeInspection.SuppressionUtil;
7 import com.intellij.lang.Commenter;
8 import com.intellij.lang.LanguageCommenters;
9 import com.intellij.openapi.command.undo.UndoUtil;
10 import com.intellij.openapi.editor.Editor;
11 import com.intellij.openapi.fileTypes.LanguageFileType;
12 import com.intellij.openapi.project.Project;
13 import com.intellij.openapi.vfs.ReadonlyStatusHandler;
14 import com.intellij.psi.PsiComment;
15 import com.intellij.psi.PsiElement;
16 import com.intellij.psi.PsiParserFacade;
17 import com.intellij.psi.PsiWhiteSpace;
18 import com.intellij.psi.util.PsiTreeUtil;
19 import com.intellij.util.IncorrectOperationException;
20 import org.jetbrains.annotations.NonNls;
21 import org.jetbrains.annotations.NotNull;
22 import org.jetbrains.annotations.Nullable;
24 /**
25 * @author ven
27 public class AddNoInspectionCommentFix extends SuppressIntentionAction {
28 private static final @NonNls String COMMENT_START_TEXT = "//noinspection ";
30 protected final String myID;
31 protected Class<? extends PsiElement> mySuppressionHolderClass;
33 public AddNoInspectionCommentFix(HighlightDisplayKey key, Class<? extends PsiElement> suppressionHolderClass) {
34 this(key.getID());
35 mySuppressionHolderClass = suppressionHolderClass;
38 private AddNoInspectionCommentFix(final String ID) {
39 myID = ID;
42 @NotNull
43 public String getText() {
44 return InspectionsBundle.message("suppress.inspection.statement");
47 @Nullable
48 protected PsiElement getContainer(PsiElement context) {
49 return PsiTreeUtil.getParentOfType(context, mySuppressionHolderClass);
52 @NotNull
53 public String getFamilyName() {
54 return InspectionsBundle.message("suppress.inspection.family");
57 public boolean isAvailable(@NotNull final Project project, final Editor editor, @Nullable final PsiElement context) {
58 return context != null && context.getManager().isInProject(context) && getContainer(context) != null;
61 public void invoke(final Project project, @Nullable Editor editor, final PsiElement element) throws IncorrectOperationException {
62 PsiElement container = getContainer(element);
63 if (container == null) return;
65 final ReadonlyStatusHandler.OperationStatus status = ReadonlyStatusHandler.getInstance(project)
66 .ensureFilesWritable(container.getContainingFile().getVirtualFile());
67 if (status.hasReadonlyFiles()) return;
68 PsiElement prev = PsiTreeUtil.skipSiblingsBackward(container, PsiWhiteSpace.class);
69 if (prev instanceof PsiComment) {
70 String text = prev.getText();
71 if (text.startsWith(COMMENT_START_TEXT)) {
72 replaceSuppressionComment(prev, text);
73 return;
76 boolean caretWasBeforeStatement = editor != null && editor.getCaretModel().getOffset() == container.getTextRange().getStartOffset();
77 createSuppression(project, editor, element, container);
79 if (caretWasBeforeStatement) {
80 editor.getCaretModel().moveToOffset(container.getTextRange().getStartOffset());
82 UndoUtil.markPsiFileForUndo(element.getContainingFile());
85 private void replaceSuppressionComment(final PsiElement prev, final String text) throws IncorrectOperationException {
86 Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(prev.getLanguage());
87 assert commenter != null;
88 final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(prev.getProject());
89 final String prefix = commenter.getLineCommentPrefix();
90 assert prefix != null && text.startsWith(prefix) : "Unexpected suppression comment " + text;
91 String newText = text.substring(prefix.length()) + "," + myID;
92 final PsiComment newComment = parserFacade.createLineCommentFromText((LanguageFileType)prev.getContainingFile().getFileType(), newText);
93 prev.replace(newComment);
96 protected void createSuppression(final Project project,
97 final Editor editor,
98 final PsiElement element,
99 final PsiElement container) throws IncorrectOperationException {
100 final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
101 final String text = SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME + " " + myID;
102 PsiComment comment = parserFacade.createLineOrBlockCommentFromText(element.getLanguage(), text);
103 container.getParent().addBefore(comment, container);
106 public boolean startInWriteAction() {
107 return true;