src/Pure/PIDE/markup_tree.scala
author wenzelm
Fri Nov 11 15:25:22 2011 +0100 (2011-11-11)
changeset 45456 9ba615ac75fb
parent 45455 4f974c0c5c2f
child 45457 615ba724b269
permissions -rw-r--r--
more abstract Markup_Tree;
     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 import java.lang.System
    11 import javax.swing.tree.DefaultMutableTreeNode
    12 
    13 import scala.collection.immutable.SortedMap
    14 
    15 
    16 object Markup_Tree
    17 {
    18   type Select[A] = PartialFunction[Text.Markup, A]
    19 
    20   val empty: Markup_Tree = new Markup_Tree(Branches.empty)
    21 
    22   object Branches
    23   {
    24     type Entry = (Text.Markup, Markup_Tree)
    25     type T = SortedMap[Text.Range, Entry]
    26 
    27     val empty: T = SortedMap.empty(Text.Range.Ordering)
    28 
    29     def update(branches: T, entry: Entry): T = branches + (entry._1.range -> entry)
    30     def single(entry: Entry): T = update(empty, entry)
    31 
    32     def overlapping(range: Text.Range, branches: T): T =  // FIXME special cases!?
    33     {
    34       val start = Text.Range(range.start)
    35       val stop = Text.Range(range.stop)
    36       val bs = branches.range(start, stop)
    37       branches.get(stop) match {
    38         case Some(end) if range overlaps end._1.range => update(bs, end)
    39         case _ => bs
    40       }
    41     }
    42   }
    43 }
    44 
    45 
    46 class Markup_Tree private(branches: Markup_Tree.Branches.T)
    47 {
    48   import Markup_Tree._
    49 
    50   override def toString =
    51     branches.toList.map(_._2) match {
    52       case Nil => "Empty"
    53       case list => list.mkString("Tree(", ",", ")")
    54     }
    55 
    56   def + (new_info: Text.Markup): Markup_Tree =
    57   {
    58     val new_range = new_info.range
    59     branches.get(new_range) match {
    60       case None =>
    61         new Markup_Tree(Branches.update(branches, new_info -> empty))
    62       case Some((info, subtree)) =>
    63         val range = info.range
    64         if (range.contains(new_range))
    65           new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
    66         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    67           new Markup_Tree(Branches.single(new_info -> this))
    68         else {
    69           val body = Branches.overlapping(new_range, branches)
    70           if (body.forall(e => new_range.contains(e._1))) {
    71             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
    72               if (body.size > 1)
    73                 (Branches.empty /: branches)((rest, entry) =>
    74                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
    75               else branches
    76             new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
    77           }
    78           else { // FIXME split markup!?
    79             System.err.println("Ignored overlapping markup information: " + new_info)
    80             this
    81           }
    82         }
    83     }
    84   }
    85 
    86   private def overlapping(range: Text.Range): Stream[(Text.Range, Branches.Entry)] =
    87     Branches.overlapping(range, branches).toStream
    88 
    89   def select[A](root_range: Text.Range)(result: Markup_Tree.Select[A])
    90     : Stream[Text.Info[Option[A]]] =
    91   {
    92     def stream(
    93       last: Text.Offset,
    94       stack: List[(Text.Info[Option[A]], Stream[(Text.Range, Branches.Entry)])])
    95         : Stream[Text.Info[Option[A]]] =
    96     {
    97       stack match {
    98         case (parent, (range, (info, tree)) #:: more) :: rest =>
    99           val subrange = range.restrict(root_range)
   100           val subtree = tree.overlapping(subrange)
   101           val start = subrange.start
   102 
   103           if (result.isDefinedAt(info)) {
   104             val next = Text.Info[Option[A]](subrange, Some(result(info)))
   105             val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   106             if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   107             else nexts
   108           }
   109           else stream(last, (parent, subtree #::: more) :: rest)
   110 
   111         case (parent, Stream.Empty) :: rest =>
   112           val stop = parent.range.stop
   113           val nexts = stream(stop, rest)
   114           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   115           else nexts
   116 
   117         case Nil =>
   118           val stop = root_range.stop
   119           if (last < stop) Stream(Text.Info(Text.Range(last, stop), None))
   120           else Stream.empty
   121       }
   122     }
   123     stream(root_range.start, List((Text.Info(root_range, None), overlapping(root_range))))
   124   }
   125 
   126   def swing_tree(parent: DefaultMutableTreeNode)
   127     (swing_node: Text.Markup => DefaultMutableTreeNode)
   128   {
   129     for ((_, (info, subtree)) <- branches) {
   130       val current = swing_node(info)
   131       subtree.swing_tree(current)(swing_node)
   132       parent.add(current)
   133     }
   134   }
   135 }
   136