3 * Copyright 2000-2009 JetBrains s.r.o.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package com
.intellij
.codeInsight
.generation
.surroundWith
;
19 import com
.intellij
.codeInsight
.CodeInsightBundle
;
20 import com
.intellij
.codeInsight
.CodeInsightUtilBase
;
21 import com
.intellij
.openapi
.editor
.Editor
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.util
.TextRange
;
24 import com
.intellij
.psi
.*;
25 import com
.intellij
.psi
.codeStyle
.CodeStyleManager
;
26 import com
.intellij
.util
.IncorrectOperationException
;
27 import org
.jetbrains
.annotations
.NonNls
;
29 class JavaWithIfElseSurrounder
extends JavaStatementsSurrounder
{
30 public String
getTemplateDescription() {
31 return CodeInsightBundle
.message("surround.with.ifelse.template");
34 public TextRange
surroundStatements(Project project
, Editor editor
, PsiElement container
, PsiElement
[] statements
) throws IncorrectOperationException
{
35 PsiManager manager
= PsiManager
.getInstance(project
);
36 PsiElementFactory factory
= JavaPsiFacade
.getInstance(manager
.getProject()).getElementFactory();
37 CodeStyleManager codeStyleManager
= CodeStyleManager
.getInstance(project
);
39 statements
= SurroundWithUtil
.moveDeclarationsOut(container
, statements
, false);
40 if (statements
.length
== 0){
44 @NonNls String text
= "if(a){\n}else{\n}";
45 PsiIfStatement ifStatement
= (PsiIfStatement
)factory
.createStatementFromText(text
, null);
46 ifStatement
= (PsiIfStatement
)codeStyleManager
.reformat(ifStatement
);
48 ifStatement
= (PsiIfStatement
)container
.addAfter(ifStatement
, statements
[statements
.length
- 1]);
50 PsiCodeBlock thenBlock
= ((PsiBlockStatement
)ifStatement
.getThenBranch()).getCodeBlock();
51 thenBlock
.addRange(statements
[0], statements
[statements
.length
- 1]);
52 container
.deleteChildRange(statements
[0], statements
[statements
.length
- 1]);
53 ifStatement
= CodeInsightUtilBase
.forcePsiPostprocessAndRestoreElement(ifStatement
);
54 TextRange range
= ifStatement
.getCondition().getTextRange();
55 editor
.getDocument().deleteString(range
.getStartOffset(), range
.getEndOffset());
56 return new TextRange(range
.getStartOffset(), range
.getStartOffset());