src/Pure/PIDE/markup_tree.scala
author wenzelm
Sat Nov 12 18:56:49 2011 +0100 (2011-11-12)
changeset 45473 66395afbd915
parent 45470 81b85d4ed269
child 45474 f793dd5d84b2
permissions -rw-r--r--
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 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   sealed case class Cumulate[A](info: A, result: PartialFunction[(A, Text.Markup), A])
    19   sealed case class Select[A](result: PartialFunction[Text.Markup, A])
    20 
    21   val empty: Markup_Tree = new Markup_Tree(Branches.empty)
    22 
    23   object Entry
    24   {
    25     def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
    26       Entry(markup.range, List(markup.info), subtree)
    27   }
    28 
    29   sealed case class Entry(range: Text.Range, rev_markup: List[XML.Elem], subtree: Markup_Tree)
    30   {
    31     def + (m: XML.Elem): Entry = copy(rev_markup = m :: rev_markup)
    32     def markup: List[XML.Elem] = rev_markup.reverse
    33   }
    34 
    35   object Branches
    36   {
    37     type T = SortedMap[Text.Range, Entry]
    38     val empty: T = SortedMap.empty(Text.Range.Ordering)
    39   }
    40 }
    41 
    42 
    43 class Markup_Tree private(branches: Markup_Tree.Branches.T)
    44 {
    45   private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
    46     this(branches + (entry.range -> entry))
    47 
    48 
    49   import Markup_Tree._
    50 
    51   override def toString =
    52     branches.toList.map(_._2) match {
    53       case Nil => "Empty"
    54       case list => list.mkString("Tree(", ",", ")")
    55     }
    56 
    57   private def overlapping(range: Text.Range): Branches.T =
    58   {
    59     val start = Text.Range(range.start)
    60     val stop = Text.Range(range.stop)
    61     val bs = branches.range(start, stop)
    62     // FIXME check after Scala 2.8.x
    63     branches.get(stop) match {
    64       case Some(end) if range overlaps end.range => bs + (end.range -> end)
    65       case _ => bs
    66     }
    67   }
    68 
    69   def + (new_markup: Text.Markup): Markup_Tree =
    70   {
    71     val new_range = new_markup.range
    72 
    73     branches.get(new_range) match {
    74       case None => new Markup_Tree(branches, Entry(new_markup, empty))
    75       case Some(entry) =>
    76         if (entry.range == new_range)
    77           new Markup_Tree(branches, entry + new_markup.info)
    78         else if (entry.range.contains(new_range))
    79           new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
    80         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    81           new Markup_Tree(Branches.empty, Entry(new_markup, this))
    82         else {
    83           val body = overlapping(new_range)
    84           if (body.forall(e => new_range.contains(e._1))) {
    85             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME
    86               if (body.size > 1)
    87                 (Branches.empty /: branches)((rest, entry) =>
    88                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
    89               else branches
    90             new Markup_Tree(rest, Entry(new_markup, new Markup_Tree(body)))
    91           }
    92           else { // FIXME split markup!?
    93             System.err.println("Ignored overlapping markup information: " + new_markup)
    94             this
    95           }
    96         }
    97     }
    98   }
    99 
   100   def cumulate[A](root_range: Text.Range)(body: Cumulate[A]): Stream[Text.Info[A]] =
   101   {
   102     val root_info = body.info
   103 
   104     def result(x: A, entry: Entry): Option[A] =
   105     {
   106       val (y, changed) =
   107         (entry.markup :\ (x, false))((info, res) =>
   108           {
   109             val (y, changed) = res
   110             val arg = (x, Text.Info(entry.range, info))
   111             if (body.result.isDefinedAt(arg)) (body.result(arg), true)
   112             else res
   113           })
   114       if (changed) Some(y) else None
   115     }
   116 
   117     def stream(
   118       last: Text.Offset,
   119       stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
   120     {
   121       stack match {
   122         case (parent, (range, entry) #:: more) :: rest =>
   123           val subrange = range.restrict(root_range)
   124           val subtree = entry.subtree.overlapping(subrange).toStream
   125           val start = subrange.start
   126 
   127           result(parent.info, entry) match {
   128             case Some(res) =>
   129               val next = Text.Info(subrange, res)
   130               val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   131               if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   132               else nexts
   133             case None => stream(last, (parent, subtree #::: more) :: rest)
   134           }
   135 
   136         case (parent, Stream.Empty) :: rest =>
   137           val stop = parent.range.stop
   138           val nexts = stream(stop, rest)
   139           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   140           else nexts
   141 
   142         case Nil =>
   143           val stop = root_range.stop
   144           if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
   145           else Stream.empty
   146       }
   147     }
   148     stream(root_range.start,
   149       List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
   150   }
   151 
   152   def swing_tree(parent: DefaultMutableTreeNode)
   153     (swing_node: Text.Markup => DefaultMutableTreeNode)
   154   {
   155     for ((_, entry) <- branches) {
   156       var current = parent
   157       for (info <- entry.markup) {
   158         val node = swing_node(Text.Info(entry.range, info))
   159         current.add(node)
   160         current = node
   161       }
   162       entry.subtree.swing_tree(current)(swing_node)
   163     }
   164   }
   165 }
   166