src/Pure/PIDE/markup_tree.scala
author wenzelm
Fri Nov 11 14:24:38 2011 +0100 (2011-11-11)
changeset 45455 4f974c0c5c2f
parent 44379 1079ab6b342b
child 45456 9ba615ac75fb
permissions -rw-r--r--
prefer statically typed Text.Markup;
     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.Markup, Markup_Tree)
    23     type T = SortedMap[Text.Range, Entry]
    24 
    25     val empty = SortedMap.empty[Text.Range, Entry](Text.Range.Ordering)
    26 
    27     def update(branches: T, entry: Entry): T = branches + (entry._1.range -> entry)
    28     def single(entry: Entry): T = update(empty, entry)
    29 
    30     def overlapping(range: Text.Range, branches: T): T =  // FIXME special cases!?
    31     {
    32       val start = Text.Range(range.start)
    33       val stop = Text.Range(range.stop)
    34       val bs = branches.range(start, stop)
    35       branches.get(stop) match {
    36         case Some(end) if range overlaps end._1.range => update(bs, end)
    37         case _ => bs
    38       }
    39     }
    40   }
    41 
    42   val empty = new Markup_Tree(Branches.empty)
    43 
    44   type Select[A] = PartialFunction[Text.Markup, A]
    45 }
    46 
    47 
    48 sealed case class Markup_Tree(val branches: Markup_Tree.Branches.T)
    49 {
    50   import Markup_Tree._
    51 
    52   override def toString =
    53     branches.toList.map(_._2) match {
    54       case Nil => "Empty"
    55       case list => list.mkString("Tree(", ",", ")")
    56     }
    57 
    58   def + (new_info: Text.Markup): Markup_Tree =
    59   {
    60     val new_range = new_info.range
    61     branches.get(new_range) match {
    62       case None =>
    63         new Markup_Tree(Branches.update(branches, new_info -> empty))
    64       case Some((info, subtree)) =>
    65         val range = info.range
    66         if (range.contains(new_range))
    67           new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
    68         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    69           new Markup_Tree(Branches.single(new_info -> this))
    70         else {
    71           val body = Branches.overlapping(new_range, branches)
    72           if (body.forall(e => new_range.contains(e._1))) {
    73             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
    74               if (body.size > 1)
    75                 (Branches.empty /: branches)((rest, entry) =>
    76                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
    77               else branches
    78             new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
    79           }
    80           else { // FIXME split markup!?
    81             System.err.println("Ignored overlapping markup information: " + new_info)
    82             this
    83           }
    84         }
    85     }
    86   }
    87 
    88   private def overlapping(range: Text.Range): Stream[(Text.Range, Branches.Entry)] =
    89     Branches.overlapping(range, branches).toStream
    90 
    91   def select[A](root_range: Text.Range)(result: Markup_Tree.Select[A])
    92     : Stream[Text.Info[Option[A]]] =
    93   {
    94     def stream(
    95       last: Text.Offset,
    96       stack: List[(Text.Info[Option[A]], Stream[(Text.Range, Branches.Entry)])])
    97         : Stream[Text.Info[Option[A]]] =
    98     {
    99       stack match {
   100         case (parent, (range, (info, tree)) #:: more) :: rest =>
   101           val subrange = range.restrict(root_range)
   102           val subtree = tree.overlapping(subrange)
   103           val start = subrange.start
   104 
   105           if (result.isDefinedAt(info)) {
   106             val next = Text.Info[Option[A]](subrange, Some(result(info)))
   107             val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   108             if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   109             else nexts
   110           }
   111           else stream(last, (parent, subtree #::: more) :: rest)
   112 
   113         case (parent, Stream.Empty) :: rest =>
   114           val stop = parent.range.stop
   115           val nexts = stream(stop, rest)
   116           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   117           else nexts
   118 
   119         case Nil =>
   120           val stop = root_range.stop
   121           if (last < stop) Stream(Text.Info(Text.Range(last, stop), None))
   122           else Stream.empty
   123       }
   124     }
   125     stream(root_range.start, List((Text.Info(root_range, None), overlapping(root_range))))
   126   }
   127 
   128   def swing_tree(parent: DefaultMutableTreeNode)
   129     (swing_node: Text.Markup => DefaultMutableTreeNode)
   130   {
   131     for ((_, (info, subtree)) <- branches) {
   132       val current = swing_node(info)
   133       subtree.swing_tree(current)(swing_node)
   134       parent.add(current)
   135     }
   136   }
   137 }
   138