prtty printer update plus node::\ method

git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@6751 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
emir 2006-03-14 16:17:17 +00:00
parent 53a0a19766
commit ff2b477088
2 changed files with 13 additions and 7 deletions

View File

@ -83,7 +83,7 @@ abstract class Node extends NodeSeq {
/** descendant axis (all descendants of this node, not including not itself) */
def descendant: List[Node] =
child.toList.flatMap { x => x::x.descendant } ;
child.toList.flatMap { x => if(x.typeTag$ != -1) x::x.descendant else Nil } ;
/** descendant axis (all descendants of this node, including this node) */
def descendant_or_self: List[Node] = this :: descendant;

View File

@ -53,7 +53,7 @@ class PrettyPrinter( width:Int, step:Int ) {
var i = s.indexOf(' ');
if(i > tmp || i == -1) throw new BrokenException(); // cannot break
var last:List[Int] = i::Nil;
var last:List[Int] = Nil;
while(i < tmp) {
last = i::last;
i = s.indexOf(' ', i );
@ -143,12 +143,15 @@ class PrettyPrinter( width:Int, step:Int ) {
/** @param tail: what we'd like to sqeeze in */
protected def traverse(node: Node, pscope: NamespaceBinding, ind: Int): Unit = node match {
case Text(s) if s.trim() == "" =>
case _:Atom[Any] | _:Molecule[Any] | _:Comment | _:EntityRef | _:ProcInstr =>
makeBox( ind, node.toString() );
makeBox( ind, node.toString().trim() );
case _:Node =>
val test = { val sb = new StringBuffer(); Utility.toXML(node, pscope, sb, false); sb.toString()};
val test = { val sb = new StringBuffer(); Utility.toXML(node, pscope, sb, false);
val tb = TextBuffer.fromString(sb.toString()); tb.toText(0)._data};
if(childrenAreLeaves(node) && fits(test)) {
makeBox( ind, test );
} else {
@ -203,13 +206,14 @@ class PrettyPrinter( width:Int, step:Int ) {
}
def format(n: Node, pscope:NamespaceBinding, sb: StringBuffer): Unit = { // entry point
var lastwasbreak = false;
reset();
traverse( n, pscope, 0 );
var cur = 0;
//Console.println( items.reverse );
for( val b <- items.reverse ) b match {
case Break =>
sb.append('\n'); // on windows: \r\n ?
if(!lastwasbreak) sb.append('\n'); // on windows: \r\n ?
lastwasbreak = true
cur = 0;
// while( cur < last ) {
// sb.append(' ');
@ -217,12 +221,14 @@ class PrettyPrinter( width:Int, step:Int ) {
// }
case Box(i, s) =>
lastwasbreak = false
while( cur < i ) {
sb.append(' ');
cur = cur + 1;
}
sb.append( s );
case Para( s ) =>
lastwasbreak = false
sb.append( s );
}
}