2 * Copyright 2000-2010 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.formatting;
18 import com.intellij.lang.ASTNode;
20 import java.io.PrintStream;
21 import java.util.List;
24 * Convenience utilities to print out various formatting blocks information.
28 public class BlockDebugUtil {
31 * Prints out a dump of nested formatting blocks.
32 * @param out The output stream.
33 * @param block The root block to start with.
35 public static void dumpBlockTree(PrintStream out, Block block) {
36 out.println("--- BLOCK TREE DUMP ---");
37 dumpBlockTree(out, block, "", true);
38 out.println("--- END OF DUMP ---\n\n");
43 * Print out a single block info without child blocks.
44 * @param out The output stream.
45 * @param block The block to print the info for.
47 public static void dumpBlock(PrintStream out, Block block) {
48 dumpBlockTree(out, block, "", false);
51 private static void dumpBlockTree(PrintStream out, Block block, String indent, boolean withChildren) {
52 if (block == null) return;
53 out.print(indent + block.getClass().getSimpleName());
54 if (block.getIndent() != null) {
55 out.print(" " + block.getIndent());
58 out.print(" <Indent: -- null -->");
60 out.print(" " + block.getTextRange() + " ");
61 if (block instanceof ASTBlock) {
62 ASTNode node = ((ASTBlock)block).getNode();
64 out.print(" " + node.getElementType());
65 String text = node.getText();
66 int eolPos = text.indexOf('\n');
68 text = text.substring(0, eolPos) + "...";
70 out.print(" \"" + text + "\"");
75 List<Block> subBlocks = getSubBlocks(block);
76 if (subBlocks != null && subBlocks.size() > 0) {
77 out.println(indent + "{");
78 for (Block child : subBlocks) {
79 dumpBlockTree(out, child, indent + " ", true);
81 out.println(indent + "}");
86 private static List<Block> getSubBlocks(Block root) {
87 return root.getSubBlocks();