src/Pure/PIDE/markup_tree.scala
author wenzelm
Sun Aug 22 18:46:16 2010 +0200 (2010-08-22)
changeset 38577 4e4d3ea3725a
parent 38576 ce3eed2b16f7
child 38578 1ebc6b76e5ff
permissions -rw-r--r--
renamed Markup_Tree.Node to Text.Info;
Markup_Tree.select: body may depend on full Text.Info, including range;
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 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.Info[Any], Entry]
    26 
    27     val empty = SortedMap.empty[Text.Info[Any], Entry](new scala.math.Ordering[Text.Info[Any]]
    28       {
    29         def compare(x: Text.Info[Any], y: Text.Info[Any]): Int = x.range compare y.range
    30       })
    31 
    32     def update(branches: T, entry: Entry): T = branches + (entry._1 -> entry)
    33 
    34     def overlapping(range: Text.Range, branches: T): T =
    35     {
    36       val start = Text.Info[Any](Text.Range(range.start), Nil)
    37       val stop = Text.Info[Any](Text.Range(range.stop), Nil)
    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     branches.get(new_info) match {
    63       case None =>
    64         new Markup_Tree(Branches.update(branches, new_info -> empty))
    65       case Some((info, subtree)) =>
    66         if (info.range != new_info.range && info.contains(new_info))
    67           new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
    68         else if (new_info.contains(branches.head._1) && new_info.contains(branches.last._1))
    69           new Markup_Tree(Branches.update(Branches.empty, (new_info -> this)))
    70         else {
    71           val body = Branches.overlapping(new_info.range, branches)
    72           if (body.forall(e => new_info.contains(e._1))) {
    73             val rest = (branches /: body) { case (bs, (e, _)) => bs - e }
    74             new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
    75           }
    76           else { // FIXME split markup!?
    77             System.err.println("Ignored overlapping markup information: " + new_info)
    78             this
    79           }
    80         }
    81     }
    82   }
    83 
    84   def select[A](root: Text.Info[A])(sel: PartialFunction[Text.Info[Any], A]): Stream[Text.Info[A]] =
    85   {
    86     def stream(parent: Text.Info[A], bs: Branches.T): Stream[Text.Info[A]] =
    87     {
    88       val substream =
    89         (for ((_, (info, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
    90           if (sel.isDefinedAt(info)) {
    91             val current = Text.Info(info.range.restrict(parent.range), sel(info))
    92             stream(current, subtree.branches)
    93           }
    94           else stream(parent.restrict(info.range), subtree.branches)
    95         }).flatten
    96 
    97       def padding(last: Text.Offset, s: Stream[Text.Info[A]]): Stream[Text.Info[A]] =
    98         s match {
    99           case info #:: rest =>
   100             val Text.Range(start, stop) = info.range
   101             if (last < start)
   102               parent.restrict(Text.Range(last, start)) #:: info #:: padding(stop, rest)
   103             else info #:: padding(stop, rest)
   104           case Stream.Empty =>
   105             if (last < parent.range.stop)
   106               Stream(parent.restrict(Text.Range(last, parent.range.stop)))
   107             else Stream.Empty
   108         }
   109       if (substream.isEmpty) Stream(parent)
   110       else padding(parent.range.start, substream)
   111     }
   112     stream(root, branches)
   113   }
   114 
   115   def swing_tree(parent: DefaultMutableTreeNode)
   116     (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
   117   {
   118     for ((_, (info, subtree)) <- branches) {
   119       val current = swing_node(info)
   120       subtree.swing_tree(current)(swing_node)
   121       parent.add(current)
   122     }
   123   }
   124 }
   125