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.
17 package com
.intellij
.codeInspection
.ex
;
19 import com
.intellij
.codeInsight
.daemon
.DaemonCodeAnalyzer
;
20 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
21 import com
.intellij
.codeInspection
.LocalQuickFix
;
22 import com
.intellij
.codeInspection
.ProblemDescriptor
;
23 import com
.intellij
.codeInspection
.QuickFix
;
24 import com
.intellij
.openapi
.command
.undo
.UndoUtil
;
25 import com
.intellij
.openapi
.diagnostic
.Logger
;
26 import com
.intellij
.openapi
.editor
.Editor
;
27 import com
.intellij
.openapi
.project
.Project
;
28 import com
.intellij
.openapi
.util
.Comparing
;
29 import com
.intellij
.psi
.PsiElement
;
30 import com
.intellij
.psi
.PsiFile
;
31 import com
.intellij
.util
.IncorrectOperationException
;
32 import org
.jetbrains
.annotations
.NotNull
;
37 public class QuickFixWrapper
implements IntentionAction
{
39 private static final Logger LOG
= Logger
.getInstance("com.intellij.codeInspection.ex.QuickFixWrapper");
41 private final ProblemDescriptor myDescriptor
;
42 private final int myFixNumber
;
45 public static IntentionAction
wrap(@NotNull ProblemDescriptor descriptor
, int fixNumber
) {
46 LOG
.assertTrue(fixNumber
> -1);
47 LOG
.assertTrue(descriptor
.getFixes() != null && descriptor
.getFixes().length
> fixNumber
);
49 final QuickFix fix
= descriptor
.getFixes()[fixNumber
];
50 return fix
instanceof IntentionAction ?
(IntentionAction
)fix
: new QuickFixWrapper(descriptor
, fixNumber
);
53 private QuickFixWrapper(@NotNull ProblemDescriptor descriptor
, int fixNumber
) {
54 myDescriptor
= descriptor
;
55 myFixNumber
= fixNumber
;
59 public String
getText() {
60 return getFamilyName();
64 public String
getFamilyName() {
65 return myDescriptor
.getFixes()[myFixNumber
].getName();
68 public boolean isAvailable(@NotNull Project project
, Editor editor
, PsiFile file
) {
69 PsiElement psiElement
= myDescriptor
.getPsiElement();
70 if (psiElement
== null || !psiElement
.isValid()) return false;
71 final LocalQuickFix fix
= getFix();
72 return fix
instanceof IntentionAction ?
((IntentionAction
)fix
).isAvailable(project
, editor
, file
) : true;
75 public void invoke(@NotNull Project project
, Editor editor
, PsiFile file
) throws IncorrectOperationException
{
76 //if (!CodeInsightUtil.prepareFileForWrite(file)) return;
77 // consider all local quick fixes do it themselves
79 LocalQuickFix fix
= getFix();
80 fix
.applyFix(project
, myDescriptor
);
81 DaemonCodeAnalyzer
.getInstance(project
).restart();
82 final PsiElement element
= myDescriptor
.getPsiElement();
83 if (element
!= null) {
84 final PsiFile fileForUndo
= element
.getContainingFile();
85 if (!Comparing
.equal(fileForUndo
, file
)) {
86 UndoUtil
.markPsiFileForUndo(fileForUndo
);
91 public boolean startInWriteAction() {
92 final LocalQuickFix fix
= getFix();
93 return fix
instanceof IntentionAction ?
((IntentionAction
)fix
).startInWriteAction() : true;
96 public LocalQuickFix
getFix() {
97 return (LocalQuickFix
)myDescriptor
.getFixes()[myFixNumber
];
100 public String
toString() {