src/Pure/PIDE/markup_tree.scala
author wenzelm
Tue Sep 18 15:55:29 2012 +0200 (2012-09-18)
changeset 49417 c5a8592fb5d3
parent 48762 9ff86bf6ad19
child 49465 76ecbc7f3683
permissions -rw-r--r--
recover order of stacked markup;
     1 /*  Title:      Pure/PIDE/markup_tree.scala
     2     Module:     PIDE
     3     Author:     Fabian Immler, TU Munich
     4     Author:     Makarius
     5 
     6 Markup trees over nested / non-overlapping text ranges.
     7 */
     8 
     9 package isabelle
    10 
    11 import java.lang.System
    12 import javax.swing.tree.DefaultMutableTreeNode
    13 
    14 import scala.collection.immutable.SortedMap
    15 
    16 
    17 object Markup_Tree
    18 {
    19   val empty: Markup_Tree = new Markup_Tree(Branches.empty)
    20 
    21   object Entry
    22   {
    23     def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
    24       Entry(markup.range, List(markup.info), Set(markup.info.markup.name), subtree)
    25   }
    26 
    27   sealed case class Entry(
    28     range: Text.Range,
    29     rev_markup: List[XML.Elem],
    30     elements: Set[String],
    31     subtree: Markup_Tree)
    32   {
    33     def + (info: XML.Elem): Entry =
    34       if (elements(info.markup.name)) copy(rev_markup = info :: rev_markup)
    35       else copy(rev_markup = info :: rev_markup, elements = elements + info.markup.name)
    36 
    37     def markup: List[XML.Elem] = rev_markup.reverse
    38 
    39     def reverse_markup: Entry =
    40       copy(rev_markup = rev_markup.reverse, subtree = subtree.reverse_markup)
    41   }
    42 
    43   object Branches
    44   {
    45     type T = SortedMap[Text.Range, Entry]
    46     val empty: T = SortedMap.empty(Text.Range.Ordering)
    47 
    48     def reverse_markup(branches: T): T =
    49       (empty /: branches.iterator) { case (bs, (r, entry)) => bs + (r -> entry.reverse_markup) }
    50   }
    51 }
    52 
    53 
    54 final class Markup_Tree private(branches: Markup_Tree.Branches.T)
    55 {
    56   import Markup_Tree._
    57 
    58   private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
    59     this(branches + (entry.range -> entry))
    60 
    61   def reverse_markup: Markup_Tree = new Markup_Tree(Branches.reverse_markup(branches))
    62 
    63   override def toString =
    64     branches.toList.map(_._2) match {
    65       case Nil => "Empty"
    66       case list => list.mkString("Tree(", ",", ")")
    67     }
    68 
    69   private def overlapping(range: Text.Range): Branches.T =
    70   {
    71     val start = Text.Range(range.start)
    72     val stop = Text.Range(range.stop)
    73     val bs = branches.range(start, stop)
    74     // FIXME check after Scala 2.8.x
    75     branches.get(stop) match {
    76       case Some(end) if range overlaps end.range => bs + (end.range -> end)
    77       case _ => bs
    78     }
    79   }
    80 
    81   def + (new_markup: Text.Markup): Markup_Tree =
    82   {
    83     val new_range = new_markup.range
    84 
    85     branches.get(new_range) match {
    86       case None => new Markup_Tree(branches, Entry(new_markup, empty))
    87       case Some(entry) =>
    88         if (entry.range == new_range)
    89           new Markup_Tree(branches, entry + new_markup.info)
    90         else if (entry.range.contains(new_range))
    91           new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
    92         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    93           new Markup_Tree(Branches.empty, Entry(new_markup, this))
    94         else {
    95           val body = overlapping(new_range)
    96           if (body.forall(e => new_range.contains(e._1))) {
    97             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME
    98               if (body.size > 1)
    99                 (Branches.empty /: branches)((rest, entry) =>
   100                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
   101               else branches
   102             new Markup_Tree(rest, Entry(new_markup, new Markup_Tree(body)))
   103           }
   104           else { // FIXME split markup!?
   105             System.err.println("Ignored overlapping markup information: " + new_markup +
   106               body.filter(e => !new_range.contains(e._1)).mkString("\n"))
   107             this
   108           }
   109         }
   110     }
   111   }
   112 
   113   def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
   114     result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
   115   {
   116     def results(x: A, entry: Entry): Option[A] =
   117       if (result_elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
   118         val (y, changed) =
   119           (entry.markup :\ (x, false))((info, res) =>
   120             {
   121               val (y, changed) = res
   122               val arg = (y, Text.Info(entry.range, info))
   123               if (result.isDefinedAt(arg)) (result(arg), true)
   124               else res
   125             })
   126         if (changed) Some(y) else None
   127       }
   128       else None
   129 
   130     def stream(
   131       last: Text.Offset,
   132       stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
   133     {
   134       stack match {
   135         case (parent, (range, entry) #:: more) :: rest =>
   136           val subrange = range.restrict(root_range)
   137           val subtree = entry.subtree.overlapping(subrange).toStream
   138           val start = subrange.start
   139 
   140           results(parent.info, entry) match {
   141             case Some(res) =>
   142               val next = Text.Info(subrange, res)
   143               val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   144               if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   145               else nexts
   146             case None => stream(last, (parent, subtree #::: more) :: rest)
   147           }
   148 
   149         case (parent, Stream.Empty) :: rest =>
   150           val stop = parent.range.stop
   151           val nexts = stream(stop, rest)
   152           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   153           else nexts
   154 
   155         case Nil =>
   156           val stop = root_range.stop
   157           if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
   158           else Stream.empty
   159       }
   160     }
   161     stream(root_range.start,
   162       List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
   163   }
   164 
   165   def swing_tree(parent: DefaultMutableTreeNode)
   166     (swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
   167   {
   168     for ((_, entry) <- branches) {
   169       var current = parent
   170       val node = swing_node(Text.Info(entry.range, entry.markup))
   171       current.add(node)
   172       current = node
   173       entry.subtree.swing_tree(current)(swing_node)
   174     }
   175   }
   176 }
   177