src/Pure/PIDE/markup_tree.scala
author wenzelm
Sun Aug 22 16:43:20 2010 +0200 (2010-08-22)
changeset 38576 ce3eed2b16f7
parent 38571 f7d7b8054648
child 38577 4e4d3ea3725a
permissions -rw-r--r--
removed obsolete Markup_Tree.flatten/filter;
     1 /*  Title:      Pure/PIDE/markup_tree.scala
     2     Author:     Fabian Immler, TU Munich
     3     Author:     Makarius
     4 
     5 Markup trees over nested / non-overlapping text ranges.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import javax.swing.tree.DefaultMutableTreeNode
    12 
    13 import scala.collection.immutable.SortedMap
    14 import scala.collection.mutable
    15 import scala.annotation.tailrec
    16 
    17 
    18 object Markup_Tree
    19 {
    20   case class Node[A](val range: Text.Range, val info: A)
    21   {
    22     def contains[B](that: Node[B]): Boolean = this.range contains that.range
    23     def restrict(r: Text.Range): Node[A] = Node(range.restrict(r), info)
    24   }
    25 
    26 
    27   /* branches sorted by quasi-order -- overlapping ranges appear as equivalent */
    28 
    29   object Branches
    30   {
    31     type Entry = (Node[Any], Markup_Tree)
    32     type T = SortedMap[Node[Any], Entry]
    33 
    34     val empty = SortedMap.empty[Node[Any], Entry](new scala.math.Ordering[Node[Any]]
    35       {
    36         def compare(x: Node[Any], y: Node[Any]): Int = x.range compare y.range
    37       })
    38 
    39     def update(branches: T, entry: Entry): T = branches + (entry._1 -> entry)
    40 
    41     def overlapping(range: Text.Range, branches: T): T =
    42     {
    43       val start = Node[Any](Text.Range(range.start), Nil)
    44       val stop = Node[Any](Text.Range(range.stop), Nil)
    45       branches.get(stop) match {
    46         case Some(end) if range overlaps end._1.range =>
    47           update(branches.range(start, stop), end)
    48         case _ => branches.range(start, stop)
    49       }
    50     }
    51   }
    52 
    53   val empty = new Markup_Tree(Branches.empty)
    54 }
    55 
    56 
    57 case class Markup_Tree(val branches: Markup_Tree.Branches.T)
    58 {
    59   import Markup_Tree._
    60 
    61   override def toString =
    62     branches.toList.map(_._2) match {
    63       case Nil => "Empty"
    64       case list => list.mkString("Tree(", ",", ")")
    65     }
    66 
    67   def + (new_node: Node[Any]): Markup_Tree =
    68   {
    69     branches.get(new_node) match {
    70       case None =>
    71         new Markup_Tree(Branches.update(branches, new_node -> empty))
    72       case Some((node, subtree)) =>
    73         if (node.range != new_node.range && node.contains(new_node))
    74           new Markup_Tree(Branches.update(branches, node -> (subtree + new_node)))
    75         else if (new_node.contains(branches.head._1) && new_node.contains(branches.last._1))
    76           new Markup_Tree(Branches.update(Branches.empty, (new_node -> this)))
    77         else {
    78           val body = Branches.overlapping(new_node.range, branches)
    79           if (body.forall(e => new_node.contains(e._1))) {
    80             val rest = (branches /: body) { case (bs, (e, _)) => bs - e }
    81             new Markup_Tree(Branches.update(rest, new_node -> new Markup_Tree(body)))
    82           }
    83           else { // FIXME split markup!?
    84             System.err.println("Ignored overlapping markup: " + new_node)
    85             this
    86           }
    87         }
    88     }
    89   }
    90 
    91   def select[A](root: Node[A])(sel: PartialFunction[Any, A]): Stream[Node[A]] =
    92   {
    93     def stream(parent: Node[A], bs: Branches.T): Stream[Node[A]] =
    94     {
    95       val substream =
    96         (for ((_, (node, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
    97           if (sel.isDefinedAt(node.info)) {
    98             val current = Node(node.range.restrict(parent.range), sel(node.info))
    99             stream(current, subtree.branches)
   100           }
   101           else stream(parent.restrict(node.range), subtree.branches)
   102         }).flatten
   103 
   104       def padding(last: Text.Offset, s: Stream[Node[A]]): Stream[Node[A]] =
   105         s match {
   106           case (node @ Node(Text.Range(start, stop), _)) #:: rest =>
   107             if (last < start)
   108               parent.restrict(Text.Range(last, start)) #:: node #:: padding(stop, rest)
   109             else node #:: padding(stop, rest)
   110           case Stream.Empty =>
   111             if (last < parent.range.stop)
   112               Stream(parent.restrict(Text.Range(last, parent.range.stop)))
   113             else Stream.Empty
   114         }
   115       if (substream.isEmpty) Stream(parent)
   116       else padding(parent.range.start, substream)
   117     }
   118     stream(root, branches)
   119   }
   120 
   121   def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node[Any] => DefaultMutableTreeNode)
   122   {
   123     for ((_, (node, subtree)) <- branches) {
   124       val current = swing_node(node)
   125       subtree.swing_tree(current)(swing_node)
   126       parent.add(current)
   127     }
   128   }
   129 }
   130