src/Pure/PIDE/markup_tree.scala
author wenzelm
Tue Aug 24 21:20:08 2010 +0200 (2010-08-24)
changeset 38659 afac51977705
parent 38657 2e0ebdaac59b
child 38661 f1ba2ae8e58a
permissions -rw-r--r--
Markup_Tree.select: more straight-forward recursion producing one main stream, avoid fragmentation of parent info due to ignored subtree;
tuned;
     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 == new_range)  // FIXME append, not cons (!??)
    68           new Markup_Tree(Branches.update(branches,
    69             new_info -> new Markup_Tree(Branches.single((info, subtree)))))
    70         else if (range.contains(new_range))
    71           new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
    72         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    73           new Markup_Tree(Branches.single(new_info -> this))
    74         else {
    75           val body = Branches.overlapping(new_range, branches)
    76           if (body.forall(e => new_range.contains(e._1))) {
    77             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
    78               if (body.size > 1)
    79                 (Branches.empty /: branches)((rest, entry) =>
    80                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
    81               else branches
    82             new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
    83           }
    84           else { // FIXME split markup!?
    85             System.err.println("Ignored overlapping markup information: " + new_info)
    86             this
    87           }
    88         }
    89     }
    90   }
    91 
    92   private def overlapping(range: Text.Range): Stream[(Text.Range, Branches.Entry)] =
    93     Branches.overlapping(range, branches).toStream
    94 
    95   def select[A](root_range: Text.Range)
    96     (result: PartialFunction[Text.Info[Any], A])(default: A): Stream[Text.Info[A]] =
    97   {
    98     def stream(last: Text.Offset, stack: List[(Text.Info[A], Stream[(Text.Range, Branches.Entry)])])
    99       : Stream[Text.Info[A]] =
   100     {
   101       stack match {
   102         case (parent, (range, (info, tree)) #:: more) :: rest =>
   103           val subrange = range.restrict(root_range)
   104           val subtree = tree.overlapping(subrange)
   105           val start = subrange.start
   106 
   107           if (result.isDefinedAt(info)) {
   108             val next = Text.Info(subrange, result(info))
   109             val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   110             if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   111             else nexts
   112           }
   113           else stream(last, (parent, subtree #::: more) :: rest)
   114 
   115         case (parent, Stream.Empty) :: rest =>
   116           val stop = parent.range.stop
   117           val nexts = stream(stop, rest)
   118           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   119           else nexts
   120 
   121         case Nil => Stream(Text.Info(Text.Range(last, root_range.stop), default))
   122       }
   123     }
   124     stream(root_range.start, List((Text.Info(root_range, default), overlapping(root_range))))
   125   }
   126 
   127   def swing_tree(parent: DefaultMutableTreeNode)
   128     (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
   129   {
   130     for ((_, (info, subtree)) <- branches) {
   131       val current = swing_node(info)
   132       subtree.swing_tree(current)(swing_node)
   133       parent.add(current)
   134     }
   135   }
   136 }
   137