src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Aug 19 22:04:20 2010 +0200 (2010-08-19)
changeset 38562 3de5f0424caa
parent 38486 f5bbfc019937
child 38563 f6c9a4f9f66f
permissions -rw-r--r--
misc tuning and simplification;
     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(val range: Text.Range, val info: Any)
    21   {
    22     def contains(that: Node): Boolean = this.range contains that.range
    23     def restrict(r: Text.Range): Node = Node(range.intersect(r), info)
    24   }
    25 
    26 
    27   /* branches sorted by quasi-order -- overlapping intervals appear as equivalent */
    28 
    29   object Branches
    30   {
    31     type Entry = (Node, Markup_Tree)
    32     type T = SortedMap[Node, Entry]
    33 
    34     val empty = SortedMap.empty[Node, Entry](new scala.math.Ordering[Node]
    35       {
    36         def compare(x: Node, y: Node): 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       branches.range(Node(range.start_range, Nil), Node(range.stop_range, Nil))
    43   }
    44 
    45   val empty = new Markup_Tree(Branches.empty)
    46 }
    47 
    48 
    49 case class Markup_Tree(val branches: Markup_Tree.Branches.T)
    50 {
    51   import Markup_Tree._
    52 
    53   def + (new_node: Node): Markup_Tree =
    54   {
    55     branches.get(new_node) match {
    56       case None =>
    57         new Markup_Tree(Branches.update(branches, new_node -> empty))
    58       case Some((node, subtree)) =>
    59         if (node.range != new_node.range && node.contains(new_node))
    60           new Markup_Tree(Branches.update(branches, node -> (subtree + new_node)))
    61         else if (new_node.contains(branches.head._1) && new_node.contains(branches.last._1))
    62           new Markup_Tree(Branches.update(Branches.empty, (new_node -> this)))
    63         else {
    64           val body = Branches.overlapping(new_node.range, branches)
    65           if (body.forall(e => new_node.contains(e._1))) {
    66             val rest = (branches /: body) { case (bs, (e, _)) => bs - e }
    67             new Markup_Tree(Branches.update(rest, new_node -> new Markup_Tree(body)))
    68           }
    69           else { // FIXME split markup!?
    70             System.err.println("Ignored overlapping markup: " + new_node)
    71             this
    72           }
    73         }
    74     }
    75   }
    76 
    77   // FIXME depth-first with result markup stack
    78   // FIXME projection to given range
    79   def flatten(parent: Node): List[Node] =
    80   {
    81     val result = new mutable.ListBuffer[Node]
    82     var offset = parent.range.start
    83     for ((_, (node, subtree)) <- branches.iterator) {
    84       if (offset < node.range.start)
    85         result += new Node(Text.Range(offset, node.range.start), parent.info)
    86       result ++= subtree.flatten(node)
    87       offset = node.range.stop
    88     }
    89     if (offset < parent.range.stop)
    90       result += new Node(Text.Range(offset, parent.range.stop), parent.info)
    91     result.toList
    92   }
    93 
    94   def filter(pred: Node => Boolean): Markup_Tree =
    95   {
    96     val bs = branches.toList.flatMap(entry => {
    97       val (_, (node, subtree)) = entry
    98       if (pred(node)) List((node, (node, subtree.filter(pred))))
    99       else subtree.filter(pred).branches.toList
   100     })
   101     new Markup_Tree(Branches.empty ++ bs)
   102   }
   103 
   104   def select[A](range: Text.Range)(sel: PartialFunction[Node, A]): Stream[Node] =
   105   {
   106     def stream(parent: Node, bs: Branches.T): Stream[Node] =
   107     {
   108       val substream =
   109         (for ((_, (node, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
   110           if (sel.isDefinedAt(node))
   111             stream(node.restrict(parent.range), subtree.branches)
   112           else stream(parent, subtree.branches)
   113         }).flatten
   114 
   115       def padding(last: Text.Offset, s: Stream[Node]): Stream[Node] =
   116         s match {
   117           case (node @ Node(Text.Range(start, stop), _)) #:: rest =>
   118             if (last < start)
   119               parent.restrict(Text.Range(last, start)) #:: node #:: padding(stop, rest)
   120             else node #:: padding(stop, rest)
   121           case Stream.Empty =>  // FIXME
   122             if (last < parent.range.stop)
   123               Stream(parent.restrict(Text.Range(last, parent.range.stop)))
   124             else Stream.Empty
   125         }
   126       padding(parent.range.start, substream)
   127     }
   128     stream(Node(range, Nil), branches)
   129   }
   130 
   131   def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node => DefaultMutableTreeNode)
   132   {
   133     for ((_, (node, subtree)) <- branches) {
   134       val current = swing_node(node)
   135       subtree.swing_tree(current)(swing_node)
   136       parent.add(current)
   137     }
   138   }
   139 }
   140