src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Jun 23 14:52:32 2011 +0200 (2011-06-23)
changeset 43520 cec9b95fa35d
parent 39178 83e9f3ccea9f
child 43714 3749d1e6dde9
permissions -rw-r--r--
explicit import java.lang.System to prevent odd scope problems;
     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   /* 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   type Select[A] = PartialFunction[Text.Info[Any], A]
    48 }
    49 
    50 
    51 case class Markup_Tree(val branches: Markup_Tree.Branches.T)
    52 {
    53   import Markup_Tree._
    54 
    55   override def toString =
    56     branches.toList.map(_._2) match {
    57       case Nil => "Empty"
    58       case list => list.mkString("Tree(", ",", ")")
    59     }
    60 
    61   def + (new_info: Text.Info[Any]): Markup_Tree =
    62   {
    63     val new_range = new_info.range
    64     branches.get(new_range) match {
    65       case None =>
    66         new Markup_Tree(Branches.update(branches, new_info -> empty))
    67       case Some((info, subtree)) =>
    68         val range = info.range
    69         if (range.contains(new_range))
    70           new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
    71         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    72           new Markup_Tree(Branches.single(new_info -> this))
    73         else {
    74           val body = Branches.overlapping(new_range, branches)
    75           if (body.forall(e => new_range.contains(e._1))) {
    76             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
    77               if (body.size > 1)
    78                 (Branches.empty /: branches)((rest, entry) =>
    79                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
    80               else branches
    81             new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
    82           }
    83           else { // FIXME split markup!?
    84             System.err.println("Ignored overlapping markup information: " + new_info)
    85             this
    86           }
    87         }
    88     }
    89   }
    90 
    91   private def overlapping(range: Text.Range): Stream[(Text.Range, Branches.Entry)] =
    92     Branches.overlapping(range, branches).toStream
    93 
    94   def select[A](root_range: Text.Range)(result: Markup_Tree.Select[A])
    95     : Stream[Text.Info[Option[A]]] =
    96   {
    97     def stream(
    98       last: Text.Offset,
    99       stack: List[(Text.Info[Option[A]], Stream[(Text.Range, Branches.Entry)])])
   100         : Stream[Text.Info[Option[A]]] =
   101     {
   102       stack match {
   103         case (parent, (range, (info, tree)) #:: more) :: rest =>
   104           val subrange = range.restrict(root_range)
   105           val subtree = tree.overlapping(subrange)
   106           val start = subrange.start
   107 
   108           if (result.isDefinedAt(info)) {
   109             val next = Text.Info[Option[A]](subrange, Some(result(info)))
   110             val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   111             if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   112             else nexts
   113           }
   114           else stream(last, (parent, subtree #::: more) :: rest)
   115 
   116         case (parent, Stream.Empty) :: rest =>
   117           val stop = parent.range.stop
   118           val nexts = stream(stop, rest)
   119           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   120           else nexts
   121 
   122         case Nil =>
   123           val stop = root_range.stop
   124           if (last < stop) Stream(Text.Info(Text.Range(last, stop), None))
   125           else Stream.empty
   126       }
   127     }
   128     stream(root_range.start, List((Text.Info(root_range, None), overlapping(root_range))))
   129   }
   130 
   131   def swing_tree(parent: DefaultMutableTreeNode)
   132     (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
   133   {
   134     for ((_, (info, subtree)) <- branches) {
   135       val current = swing_node(info)
   136       subtree.swing_tree(current)(swing_node)
   137       parent.add(current)
   138     }
   139   }
   140 }
   141