src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Aug 19 17:40:44 2010 +0200 (2010-08-19)
changeset 38486 f5bbfc019937
parent 38482 7b6ee937b75f
child 38562 3de5f0424caa
permissions -rw-r--r--
Markup_Tree.select: crude version of stream-based filtering;
     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 intersect(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     def update(branches: T, entries: Entry*): T =
    39       branches ++ entries.map(e => (e._1 -> e))
    40   }
    41 
    42   val empty = new Markup_Tree(Branches.empty)
    43 }
    44 
    45 
    46 case class Markup_Tree(val branches: Markup_Tree.Branches.T)
    47 {
    48   import Markup_Tree._
    49 
    50   def + (new_node: Node): Markup_Tree =
    51   {
    52     branches.get(new_node) match {
    53       case None =>
    54         new Markup_Tree(Branches.update(branches, new_node -> empty))
    55       case Some((node, subtree)) =>
    56         if (node.range != new_node.range && node.contains(new_node))
    57           new Markup_Tree(Branches.update(branches, node -> (subtree + new_node)))
    58         else if (new_node.contains(branches.head._1) && new_node.contains(branches.last._1))
    59           new Markup_Tree(Branches.update(Branches.empty, (new_node -> this)))
    60         else {
    61           var overlapping = Branches.empty
    62           var rest = branches
    63           while (rest.isDefinedAt(new_node)) {
    64             overlapping = Branches.update(overlapping, rest(new_node))
    65             rest -= new_node
    66           }
    67           if (overlapping.forall(e => new_node.contains(e._1)))
    68             new Markup_Tree(Branches.update(rest, new_node -> new Markup_Tree(overlapping)))
    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 start = Node(parent.range.start_range, Nil)
   109       val stop = Node(parent.range.stop_range, Nil)
   110       val substream =
   111         (for ((_, (node, subtree)) <- bs.range(start, stop).toStream) yield {
   112           if (sel.isDefinedAt(node))
   113             stream(node.intersect(parent.range), subtree.branches)
   114           else stream(parent, subtree.branches)
   115         }).flatten
   116 
   117       def padding(last: Text.Offset, s: Stream[Node]): Stream[Node] =
   118         s match {
   119           case node #:: rest =>
   120             if (last < node.range.start)
   121               parent.intersect(Text.Range(last, node.range.start)) #::
   122                 node #:: padding(node.range.stop, rest)
   123             else node #:: padding(node.range.stop, rest)
   124           case Stream.Empty =>  // FIXME
   125             if (last < parent.range.stop)
   126             Stream(parent.intersect(Text.Range(last, parent.range.stop)))
   127             else Stream.Empty
   128         }
   129       padding(parent.range.start, substream)
   130     }
   131     // FIXME handle disjoint range!?
   132     stream(Node(range, Nil), branches)
   133   }
   134 
   135   def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node => DefaultMutableTreeNode)
   136   {
   137     for ((_, (node, subtree)) <- branches) {
   138       val current = swing_node(node)
   139       subtree.swing_tree(current)(swing_node)
   140       parent.add(current)
   141     }
   142   }
   143 }
   144