src/Pure/PIDE/markup_tree.scala
author wenzelm
Sun Aug 22 22:28:24 2010 +0200 (2010-08-22)
changeset 38584 9f63135f3cbb
parent 38582 3a6ce43d99b1
child 38657 2e0ebdaac59b
permissions -rw-r--r--
tuned Markup_Tree.+ : slightly more expensive version to rebuild rest avoids crash of RedBlack.scala:120 (version Scala 2.8.0), e.g. on the following input:
ML {* fun f x = (x + 1, "aaa") *}
     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   /* branches sorted by quasi-order -- overlapping ranges appear as equivalent */
    21 
    22   object Branches
    23   {
    24     type Entry = (Text.Info[Any], Markup_Tree)
    25     type T = SortedMap[Text.Range, Entry]
    26 
    27     val empty = SortedMap.empty[Text.Range, Entry](new scala.math.Ordering[Text.Range]
    28       {
    29         def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
    30       })
    31 
    32     def update(branches: T, entry: Entry): T = branches + (entry._1.range -> entry)
    33 
    34     def overlapping(range: Text.Range, branches: T): T =
    35     {
    36       val start = Text.Range(range.start)
    37       val stop = Text.Range(range.stop)
    38       branches.get(stop) match {
    39         case Some(end) if range overlaps end._1.range =>
    40           update(branches.range(start, stop), end)
    41         case _ => branches.range(start, stop)
    42       }
    43     }
    44   }
    45 
    46   val empty = new Markup_Tree(Branches.empty)
    47 }
    48 
    49 
    50 case class Markup_Tree(val branches: Markup_Tree.Branches.T)
    51 {
    52   import Markup_Tree._
    53 
    54   override def toString =
    55     branches.toList.map(_._2) match {
    56       case Nil => "Empty"
    57       case list => list.mkString("Tree(", ",", ")")
    58     }
    59 
    60   def + (new_info: Text.Info[Any]): Markup_Tree =
    61   {
    62     val new_range = new_info.range
    63     branches.get(new_range) match {
    64       case None =>
    65         new Markup_Tree(Branches.update(branches, new_info -> empty))
    66       case Some((info, subtree)) =>
    67         val range = info.range
    68         if (range != new_range && range.contains(new_range))
    69           new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
    70         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    71           new Markup_Tree(Branches.update(Branches.empty, (new_info -> this)))
    72         else {
    73           val body = Branches.overlapping(new_range, branches)
    74           if (body.forall(e => new_range.contains(e._1))) {
    75             val rest = (Branches.empty /: branches)((rest, entry) =>
    76               if (body.isDefinedAt(entry._1)) rest else rest + entry)
    77             new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
    78           }
    79           else { // FIXME split markup!?
    80             System.err.println("Ignored overlapping markup information: " + new_info)
    81             this
    82           }
    83         }
    84     }
    85   }
    86 
    87   def select[A](root_range: Text.Range)
    88     (result: PartialFunction[Text.Info[Any], A])(default: A): Stream[Text.Info[A]] =
    89   {
    90     def stream(parent: Text.Info[A], bs: Branches.T): Stream[Text.Info[A]] =
    91     {
    92       val range = parent.range
    93       val substream =
    94         (for ((info_range, (info, subtree)) <- Branches.overlapping(range, bs).toStream) yield {
    95           if (result.isDefinedAt(info)) {
    96             val current = Text.Info(info_range.restrict(range), result(info))
    97             stream(current, subtree.branches)
    98           }
    99           else stream(parent.restrict(info_range), subtree.branches)
   100         }).flatten
   101 
   102       def padding(last: Text.Offset, s: Stream[Text.Info[A]]): Stream[Text.Info[A]] =
   103         s match {
   104           case info #:: rest =>
   105             val Text.Range(start, stop) = info.range
   106             if (last < start)
   107               parent.restrict(Text.Range(last, start)) #:: info #:: padding(stop, rest)
   108             else info #:: padding(stop, rest)
   109           case Stream.Empty =>
   110             if (last < range.stop)
   111               Stream(parent.restrict(Text.Range(last, range.stop)))
   112             else Stream.Empty
   113         }
   114       if (substream.isEmpty) Stream(parent)
   115       else padding(range.start, substream)
   116     }
   117     stream(Text.Info(root_range, default), branches)
   118   }
   119 
   120   def swing_tree(parent: DefaultMutableTreeNode)
   121     (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
   122   {
   123     for ((_, (info, subtree)) <- branches) {
   124       val current = swing_node(info)
   125       subtree.swing_tree(current)(swing_node)
   126       parent.add(current)
   127     }
   128   }
   129 }
   130