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
.psi
.formatter
.java
;
18 import com
.intellij
.formatting
.*;
19 import com
.intellij
.lang
.ASTNode
;
20 import com
.intellij
.openapi
.util
.TextRange
;
21 import com
.intellij
.psi
.PsiFile
;
22 import com
.intellij
.psi
.codeStyle
.CodeStyleSettings
;
23 import com
.intellij
.psi
.formatter
.FormatterUtil
;
24 import com
.intellij
.psi
.impl
.source
.tree
.ElementType
;
25 import com
.intellij
.psi
.impl
.source
.tree
.JavaDocElementType
;
26 import com
.intellij
.psi
.impl
.source
.tree
.StdTokenSets
;
27 import com
.intellij
.psi
.tree
.IElementType
;
28 import org
.jetbrains
.annotations
.NotNull
;
30 import java
.util
.ArrayList
;
31 import java
.util
.HashMap
;
32 import java
.util
.List
;
36 public class SimpleJavaBlock
extends AbstractJavaBlock
{
37 private int myStartOffset
= -1;
38 private final Map
<IElementType
, Wrap
> myReservedWrap
= new HashMap
<IElementType
, Wrap
>();
40 public SimpleJavaBlock(final ASTNode node
, final Wrap wrap
, final Alignment alignment
, final Indent indent
, CodeStyleSettings settings
) {
41 super(node
, wrap
, alignment
, indent
,settings
);
44 protected List
<Block
> buildChildren() {
45 ASTNode child
= myNode
.getFirstChildNode();
46 int offset
= myStartOffset
!= -1 ? myStartOffset
: child
!= null ? child
.getTextRange().getStartOffset():0;
47 final ArrayList
<Block
> result
= new ArrayList
<Block
>();
50 while (child
!= null) {
51 if (StdTokenSets
.COMMENT_BIT_SET
.contains(child
.getElementType()) || child
.getElementType() == JavaDocElementType
.DOC_COMMENT
) {
52 result
.add(createJavaBlock(child
, mySettings
, Indent
.getNoneIndent(), null, null));
53 indent
= Indent
.getNoneIndent();
55 else if (!FormatterUtil
.containsWhiteSpacesOnly(child
)) {
59 offset
+= child
.getTextLength();
60 child
= child
.getTreeNext();
63 myReservedAlignment
= createChildAlignment();
64 myReservedAlignment2
= createChildAlignment2(myReservedAlignment
);
65 Wrap childWrap
= createChildWrap();
66 while (child
!= null) {
67 if (!FormatterUtil
.containsWhiteSpacesOnly(child
) && child
.getTextLength() > 0){
68 final ASTNode astNode
= child
;
69 child
= processChild(result
, astNode
, chooseAlignment(myReservedAlignment
, myReservedAlignment2
, child
) , childWrap
, indent
, offset
);
70 if (astNode
!= child
&& child
!= null) {
71 offset
= child
.getTextRange().getStartOffset();
73 if (indent
!= null && !(myNode
.getPsi() instanceof PsiFile
) && child
.getElementType() != ElementType
.MODIFIER_LIST
) {
74 indent
= Indent
.getContinuationIndent();
76 //indent = FormatterEx.getInstance().getContinuationIndent();
79 offset
+= child
.getTextLength();
80 child
= child
.getTreeNext();
88 public TextRange
getTextRange() {
89 if (myStartOffset
!= -1) {
90 return new TextRange(myStartOffset
, myStartOffset
+ myNode
.getTextLength());
92 return super.getTextRange();
97 public ChildAttributes
getChildAttributes(final int newChildIndex
) {
98 if (myNode
.getElementType() == ElementType
.CONDITIONAL_EXPRESSION
&& mySettings
.ALIGN_MULTILINE_TERNARY_OPERATION
) {
99 final Alignment usedAlignment
= getUsedAlignment(newChildIndex
);
100 if (usedAlignment
!= null) {
101 return new ChildAttributes(null, usedAlignment
);
103 return super.getChildAttributes(newChildIndex
);
106 return super.getChildAttributes(newChildIndex
);
110 protected Wrap
getReservedWrap(final IElementType elementType
) {
111 return myReservedWrap
.get(elementType
);
114 protected void setReservedWrap(final Wrap reservedWrap
, final IElementType operationType
) {
115 myReservedWrap
.put(operationType
, reservedWrap
);
118 public void setStartOffset(final int startOffset
) {
119 myStartOffset
= startOffset
;
120 //if (startOffset != -1 && startOffset != myNode.getTextRange().getStartOffset()) {