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
.codeInsight
.daemon
.impl
.quickfix
;
18 import com
.intellij
.codeInsight
.daemon
.QuickFixBundle
;
19 import com
.intellij
.codeInsight
.daemon
.impl
.analysis
.HighlightUtil
;
20 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
21 import com
.intellij
.codeInsight
.CodeInsightUtilBase
;
22 import com
.intellij
.openapi
.diagnostic
.Logger
;
23 import com
.intellij
.openapi
.editor
.Editor
;
24 import com
.intellij
.openapi
.project
.Project
;
25 import com
.intellij
.psi
.*;
26 import com
.intellij
.util
.IncorrectOperationException
;
27 import org
.jetbrains
.annotations
.NotNull
;
29 public class DeleteCatchFix
implements IntentionAction
{
30 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.DeleteCatchFix");
32 private final PsiParameter myCatchParameter
;
34 public DeleteCatchFix(PsiParameter myCatchParameter
) {
35 this.myCatchParameter
= myCatchParameter
;
39 public String
getText() {
40 return QuickFixBundle
.message("delete.catch.text", HighlightUtil
.formatType(myCatchParameter
.getType()));
44 public String
getFamilyName() {
45 return QuickFixBundle
.message("delete.catch.family");
48 public boolean isAvailable(@NotNull Project project
, Editor editor
, PsiFile file
) {
49 return myCatchParameter
!= null
50 && myCatchParameter
.isValid()
51 && PsiManager
.getInstance(project
).isInProject(myCatchParameter
.getContainingFile());
54 public void invoke(@NotNull Project project
, Editor editor
, PsiFile file
) {
55 if (!CodeInsightUtilBase
.prepareFileForWrite(myCatchParameter
.getContainingFile())) return;
57 PsiTryStatement tryStatement
= ((PsiCatchSection
)myCatchParameter
.getDeclarationScope()).getTryStatement();
58 PsiElement tryParent
= tryStatement
.getParent();
59 if (tryStatement
.getCatchBlocks().length
== 1 && tryStatement
.getFinallyBlock() == null) {
60 PsiCodeBlock tryBlock
= tryStatement
.getTryBlock();
61 PsiElement firstElement
= tryBlock
.getFirstBodyElement();
62 PsiElement lastAddedStatement
= null;
63 if (firstElement
!= null) {
64 PsiElement endElement
= tryBlock
.getLastBodyElement();
66 tryParent
.addRangeBefore(firstElement
, endElement
, tryStatement
);
67 lastAddedStatement
= tryStatement
.getPrevSibling();
68 while (lastAddedStatement
!= null && (lastAddedStatement
instanceof PsiWhiteSpace
|| lastAddedStatement
.getTextLength() == 0)) {
69 lastAddedStatement
= lastAddedStatement
.getPrevSibling();
72 tryStatement
.delete();
73 if (lastAddedStatement
!= null) {
74 editor
.getCaretModel().moveToOffset(lastAddedStatement
.getTextRange().getEndOffset());
80 // delete catch section
81 LOG
.assertTrue(myCatchParameter
.getParent() instanceof PsiCatchSection
);
82 final PsiElement catchSection
= myCatchParameter
.getParent();
83 //save previous element to move caret to
84 PsiElement previousElement
= catchSection
.getPrevSibling();
85 while (previousElement
instanceof PsiWhiteSpace
) {
86 previousElement
= previousElement
.getPrevSibling();
88 catchSection
.delete();
89 if (previousElement
!= null) {
90 //move caret to previous catch section
91 editor
.getCaretModel().moveToOffset(previousElement
.getTextRange().getEndOffset());
93 } catch (IncorrectOperationException e
) {
98 public boolean startInWriteAction() {