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.
16 package com
.intellij
.codeInspection
;
18 import com
.intellij
.codeInsight
.CodeInsightUtilBase
;
19 import com
.intellij
.codeInsight
.generation
.surroundWith
.JavaWithIfSurrounder
;
20 import com
.intellij
.openapi
.diagnostic
.Logger
;
21 import com
.intellij
.openapi
.editor
.Document
;
22 import com
.intellij
.openapi
.editor
.Editor
;
23 import com
.intellij
.openapi
.editor
.ScrollType
;
24 import com
.intellij
.openapi
.fileEditor
.FileEditor
;
25 import com
.intellij
.openapi
.fileEditor
.FileEditorManager
;
26 import com
.intellij
.openapi
.fileEditor
.TextEditor
;
27 import com
.intellij
.openapi
.project
.Project
;
28 import com
.intellij
.openapi
.util
.TextRange
;
29 import com
.intellij
.psi
.*;
30 import com
.intellij
.psi
.util
.PsiTreeUtil
;
31 import com
.intellij
.util
.IncorrectOperationException
;
32 import org
.jetbrains
.annotations
.NonNls
;
33 import org
.jetbrains
.annotations
.NotNull
;
38 public class SurroundWithIfFix
implements LocalQuickFix
{
39 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInspection.SurroundWithIfFix");
40 private final PsiExpression myExpression
;
41 private final String myText
;
44 public String
getName() {
45 return InspectionsBundle
.message("inspection.surround.if.quickfix", myText
);
48 public SurroundWithIfFix(@NotNull PsiExpression expressionToAssert
) {
49 myExpression
= expressionToAssert
;
50 myText
= myExpression
.getText();
53 private static Editor
getEditor(Project project
, PsiElement element
) {
54 FileEditor
[] editors
= FileEditorManager
.getInstance(project
).getEditors(element
.getContainingFile().getVirtualFile());
55 for (FileEditor fileEditor
: editors
) {
56 if (fileEditor
instanceof TextEditor
) return ((TextEditor
)fileEditor
).getEditor();
61 public void applyFix(@NotNull Project project
, @NotNull ProblemDescriptor descriptor
) {
62 PsiElement element
= descriptor
.getPsiElement();
63 PsiStatement anchorStatement
= PsiTreeUtil
.getParentOfType(element
, PsiStatement
.class);
64 LOG
.assertTrue(anchorStatement
!= null);
65 Editor editor
= getEditor(project
, element
);
66 if (editor
== null) return;
67 PsiFile file
= element
.getContainingFile();
68 PsiDocumentManager documentManager
= PsiDocumentManager
.getInstance(project
);
69 Document document
= documentManager
.getDocument(file
);
70 if (!CodeInsightUtilBase
.prepareFileForWrite(file
)) return;
71 PsiElement
[] elements
= {anchorStatement
};
72 PsiElement prev
= PsiTreeUtil
.skipSiblingsBackward(anchorStatement
, PsiWhiteSpace
.class);
73 if (prev
instanceof PsiComment
&& SuppressManager
.getInstance().getSuppressedInspectionIdsIn(prev
) != null) {
74 elements
= new PsiElement
[]{prev
, anchorStatement
};
77 TextRange textRange
= new JavaWithIfSurrounder().surroundElements(project
, editor
, elements
);
78 if (textRange
== null) return;
80 @NonNls String newText
= myText
+ " != null";
81 document
.replaceString(textRange
.getStartOffset(), textRange
.getEndOffset(),newText
);
82 editor
.getCaretModel().moveToOffset(textRange
.getEndOffset() + newText
.length());
83 editor
.getScrollingModel().scrollToCaret(ScrollType
.RELATIVE
);
86 catch (IncorrectOperationException e
) {
92 public String
getFamilyName() {
93 return InspectionsBundle
.message("inspection.surround.if.family");
96 public boolean isAvailable() {
97 if (!myExpression
.isValid() || myText
== null) {
100 PsiStatement statement
= PsiTreeUtil
.getParentOfType(myExpression
, PsiStatement
.class);
101 if (statement
== null) return false;
102 PsiElement parent
= statement
.getParent();
103 return !(parent
instanceof PsiForStatement
);