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
.xml
;
18 import com
.intellij
.formatting
.*;
19 import com
.intellij
.openapi
.util
.TextRange
;
20 import org
.jetbrains
.annotations
.NotNull
;
21 import org
.jetbrains
.annotations
.Nullable
;
23 import java
.util
.ArrayList
;
24 import java
.util
.List
;
26 final class InjectedLanguageBlockWrapper
implements Block
{
27 final Block myOriginal
;
28 private final int myOffset
;
29 private List
<Block
> myBlocks
;
31 public InjectedLanguageBlockWrapper(final Block original
, final int offset
) {
32 myOriginal
= original
;
36 public Indent
getIndent() {
37 return myOriginal
.getIndent();
41 public Alignment
getAlignment() {
42 return myOriginal
.getAlignment();
46 public TextRange
getTextRange() {
47 final TextRange range
= myOriginal
.getTextRange();
48 return new TextRange(myOffset
+ range
.getStartOffset(), myOffset
+ range
.getEndOffset());
52 public List
<Block
> getSubBlocks() {
53 if (myBlocks
== null) {
54 myBlocks
= buildBlocks(myOriginal
, myOffset
, null);
59 static List
<Block
> buildBlocks(Block myOriginal
, int myOffset
, TextRange range
) {
60 final List
<Block
> list
= myOriginal
.getSubBlocks();
61 if (list
.size() == 0) return AbstractXmlBlock
.EMPTY
;
63 final ArrayList
<Block
> result
= new ArrayList
<Block
>(list
.size());
65 for(Block b
:list
) result
.add(new InjectedLanguageBlockWrapper(b
, myOffset
));
67 collectBlocksIntersectingRange(list
, result
, range
, myOffset
);
73 private static void collectBlocksIntersectingRange(final List
<Block
> list
, final List
<Block
> result
, final TextRange range
,
74 int blockStartOffset
) {
76 final TextRange textRange
= b
.getTextRange();
77 if (range
.contains(textRange
)) {
78 result
.add(new InjectedLanguageBlockWrapper(b
, blockStartOffset
- range
.getStartOffset()));
79 } else if (textRange
.intersectsStrict(range
)) {
80 collectBlocksIntersectingRange(b
.getSubBlocks(), result
, range
, blockStartOffset
);
86 public Wrap
getWrap() {
87 return myOriginal
.getWrap();
90 @Nullable public Spacing
getSpacing(Block child1
, Block child2
) {
91 if (child1
instanceof InjectedLanguageBlockWrapper
) child1
= ((InjectedLanguageBlockWrapper
)child1
).myOriginal
;
92 if (child2
instanceof InjectedLanguageBlockWrapper
) child2
= ((InjectedLanguageBlockWrapper
)child2
).myOriginal
;
93 return myOriginal
.getSpacing(child1
, child2
);
97 public ChildAttributes
getChildAttributes(final int newChildIndex
) {
98 return myOriginal
.getChildAttributes(newChildIndex
);
101 public boolean isIncomplete() {
102 return myOriginal
.isIncomplete();
105 public boolean isLeaf() {
106 return myOriginal
.isLeaf();