src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Aug 19 22:52:00 2010 +0200 (2010-08-19)
changeset 38564 a6e2715fac5f
parent 38563 f6c9a4f9f66f
child 38566 8176107637ce
permissions -rw-r--r--
parameterized type Markup_Tree.Node;
Markup_Tree.select: allow arbitrary interpretations, not just filtering;
renamed Text.Range.intersect to Text.Range.restrict -- emphasize that it is not directly related to contains/overlaps;
     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 intervals 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       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   override def toString =
    54     branches.toList.map(_._2) match {
    55       case Nil => "Empty"
    56       case list => list.mkString("Tree(", ",", ")")
    57     }
    58 
    59   def + (new_node: Node[Any]): Markup_Tree =
    60   {
    61     branches.get(new_node) match {
    62       case None =>
    63         new Markup_Tree(Branches.update(branches, new_node -> empty))
    64       case Some((node, subtree)) =>
    65         if (node.range != new_node.range && node.contains(new_node))
    66           new Markup_Tree(Branches.update(branches, node -> (subtree + new_node)))
    67         else if (new_node.contains(branches.head._1) && new_node.contains(branches.last._1))
    68           new Markup_Tree(Branches.update(Branches.empty, (new_node -> this)))
    69         else {
    70           val body = Branches.overlapping(new_node.range, branches)
    71           if (body.forall(e => new_node.contains(e._1))) {
    72             val rest = (branches /: body) { case (bs, (e, _)) => bs - e }
    73             new Markup_Tree(Branches.update(rest, new_node -> new Markup_Tree(body)))
    74           }
    75           else { // FIXME split markup!?
    76             System.err.println("Ignored overlapping markup: " + new_node)
    77             this
    78           }
    79         }
    80     }
    81   }
    82 
    83   // FIXME depth-first with result markup stack
    84   // FIXME projection to given range
    85   def flatten(parent: Node[Any]): List[Node[Any]] =
    86   {
    87     val result = new mutable.ListBuffer[Node[Any]]
    88     var offset = parent.range.start
    89     for ((_, (node, subtree)) <- branches.iterator) {
    90       if (offset < node.range.start)
    91         result += new Node(Text.Range(offset, node.range.start), parent.info)
    92       result ++= subtree.flatten(node)
    93       offset = node.range.stop
    94     }
    95     if (offset < parent.range.stop)
    96       result += new Node(Text.Range(offset, parent.range.stop), parent.info)
    97     result.toList
    98   }
    99 
   100   def filter(pred: Node[Any] => Boolean): Markup_Tree =
   101   {
   102     val bs = branches.toList.flatMap(entry => {
   103       val (_, (node, subtree)) = entry
   104       if (pred(node)) List((node, (node, subtree.filter(pred))))
   105       else subtree.filter(pred).branches.toList
   106     })
   107     new Markup_Tree(Branches.empty ++ bs)
   108   }
   109 
   110   def select[A](range: Text.Range)(sel: PartialFunction[Any, A]): Stream[Node[List[A]]] =
   111   {
   112     def stream(parent: Node[List[A]], bs: Branches.T): Stream[Node[List[A]]] =
   113     {
   114       val substream =
   115         (for ((_, (node, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
   116           if (sel.isDefinedAt(node.info)) {
   117             val current = Node(node.range.restrict(parent.range), List(sel(node.info)))
   118             stream(current, subtree.branches)
   119           }
   120           else stream(parent, subtree.branches)
   121         }).flatten
   122 
   123       def padding(last: Text.Offset, s: Stream[Node[List[A]]]): Stream[Node[List[A]]] =
   124         s match {
   125           case (node @ Node(Text.Range(start, stop), _)) #:: rest =>
   126             if (last < start)
   127               parent.restrict(Text.Range(last, start)) #:: node #:: padding(stop, rest)
   128             else node #:: padding(stop, rest)
   129           case Stream.Empty =>
   130             if (last < parent.range.stop)
   131               Stream(parent.restrict(Text.Range(last, parent.range.stop)))
   132             else Stream.Empty
   133         }
   134       padding(parent.range.start, substream)
   135     }
   136     stream(Node(range, Nil), branches)
   137   }
   138 
   139   def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node[Any] => DefaultMutableTreeNode)
   140   {
   141     for ((_, (node, subtree)) <- branches) {
   142       val current = swing_node(node)
   143       subtree.swing_tree(current)(swing_node)
   144       parent.add(current)
   145     }
   146   }
   147 }
   148