src/Pure/PIDE/markup_tree.scala
author wenzelm
Fri Aug 20 12:12:28 2010 +0200 (2010-08-20)
changeset 38568 f117ba49a59c
parent 38566 8176107637ce
child 38571 f7d7b8054648
permissions -rw-r--r--
alternative constructor for Range singularities;
     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   // FIXME depth-first with result markup stack
    92   // FIXME projection to given range
    93   def flatten(parent: Node[Any]): List[Node[Any]] =
    94   {
    95     val result = new mutable.ListBuffer[Node[Any]]
    96     var offset = parent.range.start
    97     for ((_, (node, subtree)) <- branches.iterator) {
    98       if (offset < node.range.start)
    99         result += new Node(Text.Range(offset, node.range.start), parent.info)
   100       result ++= subtree.flatten(node)
   101       offset = node.range.stop
   102     }
   103     if (offset < parent.range.stop)
   104       result += new Node(Text.Range(offset, parent.range.stop), parent.info)
   105     result.toList
   106   }
   107 
   108   def filter(pred: Node[Any] => Boolean): Markup_Tree =
   109   {
   110     val bs = branches.toList.flatMap(entry => {
   111       val (_, (node, subtree)) = entry
   112       if (pred(node)) List((node, (node, subtree.filter(pred))))
   113       else subtree.filter(pred).branches.toList
   114     })
   115     new Markup_Tree(Branches.empty ++ bs)
   116   }
   117 
   118   def select[A](range: Text.Range)(sel: PartialFunction[Any, A]): Stream[Node[List[A]]] =
   119   {
   120     def stream(parent: Node[List[A]], bs: Branches.T): Stream[Node[List[A]]] =
   121     {
   122       val substream =
   123         (for ((_, (node, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
   124           if (sel.isDefinedAt(node.info)) {
   125             val current = Node(node.range.restrict(parent.range), List(sel(node.info)))
   126             stream(current, subtree.branches)
   127           }
   128           else stream(parent, subtree.branches)
   129         }).flatten
   130 
   131       def padding(last: Text.Offset, s: Stream[Node[List[A]]]): Stream[Node[List[A]]] =
   132         s match {
   133           case (node @ Node(Text.Range(start, stop), _)) #:: rest =>
   134             if (last < start)
   135               parent.restrict(Text.Range(last, start)) #:: node #:: padding(stop, rest)
   136             else node #:: padding(stop, rest)
   137           case Stream.Empty =>
   138             if (last < parent.range.stop)
   139               Stream(parent.restrict(Text.Range(last, parent.range.stop)))
   140             else Stream.Empty
   141         }
   142       if (substream.isEmpty) Stream(parent)
   143       else padding(parent.range.start, substream)
   144     }
   145     stream(Node(range, Nil), branches)
   146   }
   147 
   148   def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node[Any] => DefaultMutableTreeNode)
   149   {
   150     for ((_, (node, subtree)) <- branches) {
   151       val current = swing_node(node)
   152       subtree.swing_tree(current)(swing_node)
   153       parent.add(current)
   154     }
   155   }
   156 }
   157