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 Alignment alignment = block.getAlignment();
61 if (alignment != null) {
62 out.print(" <Alignment: " + Integer.toHexString(alignment.hashCode()).substring(0,4) + "...>");
64 out.print(" " + block.getTextRange() + " ");
65 if (block instanceof ASTBlock) {
66 ASTNode node = ((ASTBlock)block).getNode();
68 out.print(" " + node.getElementType());
69 String text = node.getText();
70 int eolPos = text.indexOf('\n');
72 text = text.substring(0, eolPos) + "...";
74 out.print(" \"" + text + "\"");
79 List<Block> subBlocks = getSubBlocks(block);
80 if (subBlocks != null && subBlocks.size() > 0) {
81 out.println(indent + "{");
82 for (Block child : subBlocks) {
83 dumpBlockTree(out, child, indent + " ", true);
85 out.println(indent + "}");
90 private static List<Block> getSubBlocks(Block root) {
91 return root.getSubBlocks();