src/Pure/PIDE/markup_tree.scala
author wenzelm
Sun Aug 22 20:25:15 2010 +0200 (2010-08-22)
changeset 38582 3a6ce43d99b1
parent 38578 1ebc6b76e5ff
child 38584 9f63135f3cbb
permissions -rw-r--r--
tuned signature;
     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 /: body) { case (bs, (e, _)) => bs - e }
    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   def select[A](root_range: Text.Range)
    87     (result: PartialFunction[Text.Info[Any], A])(default: A): Stream[Text.Info[A]] =
    88   {
    89     def stream(parent: Text.Info[A], bs: Branches.T): Stream[Text.Info[A]] =
    90     {
    91       val range = parent.range
    92       val substream =
    93         (for ((info_range, (info, subtree)) <- Branches.overlapping(range, bs).toStream) yield {
    94           if (result.isDefinedAt(info)) {
    95             val current = Text.Info(info_range.restrict(range), result(info))
    96             stream(current, subtree.branches)
    97           }
    98           else stream(parent.restrict(info_range), subtree.branches)
    99         }).flatten
   100 
   101       def padding(last: Text.Offset, s: Stream[Text.Info[A]]): Stream[Text.Info[A]] =
   102         s match {
   103           case info #:: rest =>
   104             val Text.Range(start, stop) = info.range
   105             if (last < start)
   106               parent.restrict(Text.Range(last, start)) #:: info #:: padding(stop, rest)
   107             else info #:: padding(stop, rest)
   108           case Stream.Empty =>
   109             if (last < range.stop)
   110               Stream(parent.restrict(Text.Range(last, range.stop)))
   111             else Stream.Empty
   112         }
   113       if (substream.isEmpty) Stream(parent)
   114       else padding(range.start, substream)
   115     }
   116     stream(Text.Info(root_range, default), branches)
   117   }
   118 
   119   def swing_tree(parent: DefaultMutableTreeNode)
   120     (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
   121   {
   122     for ((_, (info, subtree)) <- branches) {
   123       val current = swing_node(info)
   124       subtree.swing_tree(current)(swing_node)
   125       parent.add(current)
   126     }
   127   }
   128 }
   129