update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / psi / formatter / xml / InjectedLanguageBlockWrapper.java
blobb3ff2bb19e9ae9166abd49063bd242c9d2e6f55b
1 /*
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;
33 myOffset = offset;
36 public Indent getIndent() {
37 return myOriginal.getIndent();
40 @Nullable
41 public Alignment getAlignment() {
42 return myOriginal.getAlignment();
45 @NotNull
46 public TextRange getTextRange() {
47 final TextRange range = myOriginal.getTextRange();
48 return new TextRange(myOffset + range.getStartOffset(), myOffset + range.getEndOffset());
51 @NotNull
52 public List<Block> getSubBlocks() {
53 if (myBlocks == null) {
54 myBlocks = buildBlocks(myOriginal, myOffset, null);
56 return myBlocks;
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;
62 else {
63 final ArrayList<Block> result = new ArrayList<Block>(list.size());
64 if (range == null) {
65 for(Block b:list) result.add(new InjectedLanguageBlockWrapper(b, myOffset));
66 } else {
67 collectBlocksIntersectingRange(list, result, range, myOffset);
69 return result;
73 private static void collectBlocksIntersectingRange(final List<Block> list, final List<Block> result, final TextRange range,
74 int blockStartOffset) {
75 for(Block b:list) {
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);
85 @Nullable
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);
96 @NotNull
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();