Merged from 9.0.x (Maia) branch

This commit is contained in:
Rustam.Vishnyakov
2010-04-14 16:34:22 +04:00
parent 965412ad42
commit 2f6fcabf0b
4 changed files with 91 additions and 4 deletions
@@ -0,0 +1,80 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.formatting;
import com.intellij.formatting.templateLanguages.DataLanguageBlockWrapper;
import com.intellij.lang.ASTNode;
import com.intellij.psi.formatter.common.AbstractBlock;
import java.io.PrintStream;
import java.util.List;
/**
* Convenience utilities to print out various formatting blocks information.
*
* @author rvishnyakov
*/
public class BlockDebugUtil {
/**
* Prints out a dump of nested formatting blocks.
* @param out The output stream.
* @param block The root block to start with.
*/
public static void dumpBlockTree(PrintStream out, Block block) {
out.println("--- BLOCK TREE DUMP ---");
dumpBlockTree(out, block, "");
out.println("--- END OF DUMP ---\n\n");
}
private static void dumpBlockTree(PrintStream out, Block block, String indent) {
if (block == null) return;
out.print(indent + block.getClass().getSimpleName());
if (block.getIndent() != null) {
out.print(" " + block.getIndent());
}
else {
out.print(" <NO INDENT>");
}
if (block instanceof ASTBlock) {
ASTNode node = ((ASTBlock)block).getNode();
if (node != null) {
out.print(" " + node.getElementType());
String text = node.getText();
int eolPos = text.indexOf('\n');
if (eolPos > 0) {
text = text.substring(0, eolPos) + "...";
}
out.print(" \"" + text + "\"");
}
}
System.out.println();
List<Block> subBlocks = getSubBlocks(block);
if (subBlocks != null && subBlocks.size() > 0) {
out.println(indent + "{");
for (Block child : subBlocks) {
dumpBlockTree(out, child, indent + " ");
}
out.println(indent + "}");
}
}
private static List<Block> getSubBlocks(Block root) {
if (root instanceof AbstractBlock) return ((AbstractBlock)root).getSubBlocks();
if (root instanceof DataLanguageBlockWrapper) return ((DataLanguageBlockWrapper)root).getSubBlocks();
return null;
}
}
@@ -64,7 +64,7 @@ public abstract class TemplateLanguageBlock extends AbstractBlock implements Blo
for (ASTNode childNode = getNode().getFirstChildNode(); childNode != null; childNode = childNode.getTreeNext()) {
if (FormatterUtil.containsWhiteSpacesOnly(childNode)) continue;
if (shouldBuildBlockFor(childNode)) {
final TemplateLanguageBlock childBlock = myBlockFactory.createTemplateLanguageBlock(childNode, null, mySettings);
final TemplateLanguageBlock childBlock = myBlockFactory.createTemplateLanguageBlock(childNode, createChildWrap(childNode), null, mySettings);
childBlock.setParent(this);
tlChildren.add(childBlock);
}
@@ -129,5 +129,9 @@ public abstract class TemplateLanguageBlock extends AbstractBlock implements Blo
public boolean isRequiredRange(TextRange range) {
return true;
}
protected Wrap createChildWrap(ASTNode child) {
return Wrap.createWrap(Wrap.NONE, false);
}
}
@@ -15,6 +15,7 @@
*/
package com.intellij.formatting.templateLanguages;
import com.intellij.formatting.Wrap;
import com.intellij.lang.ASTNode;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import org.jetbrains.annotations.NotNull;
@@ -28,6 +29,8 @@ import java.util.List;
* Time: 6:56:55 PM
*/
public interface TemplateLanguageBlockFactory {
TemplateLanguageBlock createTemplateLanguageBlock(@NotNull ASTNode node, @Nullable List<DataLanguageBlockWrapper> foreignChildren,
TemplateLanguageBlock createTemplateLanguageBlock(@NotNull ASTNode node,
@Nullable Wrap wrap,
@Nullable List<DataLanguageBlockWrapper> foreignChildren,
@NotNull CodeStyleSettings codeStyleSettings);
}
@@ -59,14 +59,14 @@ public abstract class TemplateLanguageFormattingModelBuilder implements Delegati
return createDummyBlock(node);
}
if (builder == null) {
return createTemplateLanguageBlock(node, Collections.<DataLanguageBlockWrapper>emptyList(), settings);
return createTemplateLanguageBlock(node, Wrap.createWrap(WrapType.NONE, false), Collections.<DataLanguageBlockWrapper>emptyList(), settings);
}
final FormattingModel model = builder.createModel(viewProvider.getPsi(dataLanguage), settings);
List<DataLanguageBlockWrapper> childWrappers = buildChildWrappers(model.getRootBlock());
if (childWrappers.size() == 1) {
childWrappers = buildChildWrappers(childWrappers.get(0).getOriginal());
}
return createTemplateLanguageBlock(node, filterBlocksByRange(childWrappers, node.getTextRange()), settings);
return createTemplateLanguageBlock(node, Wrap.createWrap(WrapType.NONE, false), filterBlocksByRange(childWrappers, node.getTextRange()), settings);
}
protected AbstractBlock createDummyBlock(final ASTNode node) {