src/Pure/PIDE/markup_tree.scala
author wenzelm
Tue Aug 24 21:34:38 2010 +0200 (2010-08-24)
changeset 38661 f1ba2ae8e58a
parent 38659 afac51977705
child 38726 6d5f9af42eca
permissions -rw-r--r--
Markup_Tree.+: new info tends to sink to bottom, where it is prefered by select;
     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 
    15 
    16 object Markup_Tree
    17 {
    18   /* branches sorted by quasi-order -- overlapping ranges appear as equivalent */
    19 
    20   object Branches
    21   {
    22     type Entry = (Text.Info[Any], Markup_Tree)
    23     type T = SortedMap[Text.Range, Entry]
    24 
    25     val empty = SortedMap.empty[Text.Range, Entry](new scala.math.Ordering[Text.Range]
    26       {
    27         def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
    28       })
    29 
    30     def update(branches: T, entry: Entry): T = branches + (entry._1.range -> entry)
    31     def single(entry: Entry): T = update(empty, entry)
    32 
    33     def overlapping(range: Text.Range, branches: T): T =  // FIXME special cases!?
    34     {
    35       val start = Text.Range(range.start)
    36       val stop = Text.Range(range.stop)
    37       val bs = branches.range(start, stop)
    38       branches.get(stop) match {
    39         case Some(end) if range overlaps end._1.range => update(bs, end)
    40         case _ => bs
    41       }
    42     }
    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_info: Text.Info[Any]): Markup_Tree =
    60   {
    61     val new_range = new_info.range
    62     branches.get(new_range) match {
    63       case None =>
    64         new Markup_Tree(Branches.update(branches, new_info -> empty))
    65       case Some((info, subtree)) =>
    66         val range = info.range
    67         if (range.contains(new_range))
    68           new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
    69         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    70           new Markup_Tree(Branches.single(new_info -> this))
    71         else {
    72           val body = Branches.overlapping(new_range, branches)
    73           if (body.forall(e => new_range.contains(e._1))) {
    74             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
    75               if (body.size > 1)
    76                 (Branches.empty /: branches)((rest, entry) =>
    77                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
    78               else branches
    79             new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
    80           }
    81           else { // FIXME split markup!?
    82             System.err.println("Ignored overlapping markup information: " + new_info)
    83             this
    84           }
    85         }
    86     }
    87   }
    88 
    89   private def overlapping(range: Text.Range): Stream[(Text.Range, Branches.Entry)] =
    90     Branches.overlapping(range, branches).toStream
    91 
    92   def select[A](root_range: Text.Range)
    93     (result: PartialFunction[Text.Info[Any], A])(default: A): Stream[Text.Info[A]] =
    94   {
    95     def stream(last: Text.Offset, stack: List[(Text.Info[A], Stream[(Text.Range, Branches.Entry)])])
    96       : Stream[Text.Info[A]] =
    97     {
    98       stack match {
    99         case (parent, (range, (info, tree)) #:: more) :: rest =>
   100           val subrange = range.restrict(root_range)
   101           val subtree = tree.overlapping(subrange)
   102           val start = subrange.start
   103 
   104           if (result.isDefinedAt(info)) {
   105             val next = Text.Info(subrange, result(info))
   106             val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   107             if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   108             else nexts
   109           }
   110           else stream(last, (parent, subtree #::: more) :: rest)
   111 
   112         case (parent, Stream.Empty) :: rest =>
   113           val stop = parent.range.stop
   114           val nexts = stream(stop, rest)
   115           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   116           else nexts
   117 
   118         case Nil => Stream(Text.Info(Text.Range(last, root_range.stop), default))
   119       }
   120     }
   121     stream(root_range.start, List((Text.Info(root_range, default), overlapping(root_range))))
   122   }
   123 
   124   def swing_tree(parent: DefaultMutableTreeNode)
   125     (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
   126   {
   127     for ((_, (info, subtree)) <- branches) {
   128       val current = swing_node(info)
   129       subtree.swing_tree(current)(swing_node)
   130       parent.add(current)
   131     }
   132   }
   133 }
   134