src/Pure/PIDE/markup_tree.scala
author wenzelm
Wed Apr 18 16:53:00 2012 +0200 (2012-04-18)
changeset 47539 436ae5ea4f80
parent 46712 8650d9a95736
child 48762 9ff86bf6ad19
permissions -rw-r--r--
flat presentation of collective 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 
    40   object Branches
    41   {
    42     type T = SortedMap[Text.Range, Entry]
    43     val empty: T = SortedMap.empty(Text.Range.Ordering)
    44   }
    45 }
    46 
    47 
    48 final class Markup_Tree private(branches: Markup_Tree.Branches.T)
    49 {
    50   private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
    51     this(branches + (entry.range -> entry))
    52 
    53 
    54   import Markup_Tree._
    55 
    56   override def toString =
    57     branches.toList.map(_._2) match {
    58       case Nil => "Empty"
    59       case list => list.mkString("Tree(", ",", ")")
    60     }
    61 
    62   private def overlapping(range: Text.Range): Branches.T =
    63   {
    64     val start = Text.Range(range.start)
    65     val stop = Text.Range(range.stop)
    66     val bs = branches.range(start, stop)
    67     // FIXME check after Scala 2.8.x
    68     branches.get(stop) match {
    69       case Some(end) if range overlaps end.range => bs + (end.range -> end)
    70       case _ => bs
    71     }
    72   }
    73 
    74   def + (new_markup: Text.Markup): Markup_Tree =
    75   {
    76     val new_range = new_markup.range
    77 
    78     branches.get(new_range) match {
    79       case None => new Markup_Tree(branches, Entry(new_markup, empty))
    80       case Some(entry) =>
    81         if (entry.range == new_range)
    82           new Markup_Tree(branches, entry + new_markup.info)
    83         else if (entry.range.contains(new_range))
    84           new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
    85         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    86           new Markup_Tree(Branches.empty, Entry(new_markup, this))
    87         else {
    88           val body = overlapping(new_range)
    89           if (body.forall(e => new_range.contains(e._1))) {
    90             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME
    91               if (body.size > 1)
    92                 (Branches.empty /: branches)((rest, entry) =>
    93                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
    94               else branches
    95             new Markup_Tree(rest, Entry(new_markup, new Markup_Tree(body)))
    96           }
    97           else { // FIXME split markup!?
    98             System.err.println("Ignored overlapping markup information: " + new_markup)
    99             this
   100           }
   101         }
   102     }
   103   }
   104 
   105   def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
   106     result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
   107   {
   108     def results(x: A, entry: Entry): Option[A] =
   109       if (result_elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
   110         val (y, changed) =
   111           (entry.markup :\ (x, false))((info, res) =>
   112             {
   113               val (y, changed) = res
   114               val arg = (y, Text.Info(entry.range, info))
   115               if (result.isDefinedAt(arg)) (result(arg), true)
   116               else res
   117             })
   118         if (changed) Some(y) else None
   119       }
   120       else None
   121 
   122     def stream(
   123       last: Text.Offset,
   124       stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
   125     {
   126       stack match {
   127         case (parent, (range, entry) #:: more) :: rest =>
   128           val subrange = range.restrict(root_range)
   129           val subtree = entry.subtree.overlapping(subrange).toStream
   130           val start = subrange.start
   131 
   132           results(parent.info, entry) match {
   133             case Some(res) =>
   134               val next = Text.Info(subrange, res)
   135               val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   136               if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   137               else nexts
   138             case None => stream(last, (parent, subtree #::: more) :: rest)
   139           }
   140 
   141         case (parent, Stream.Empty) :: rest =>
   142           val stop = parent.range.stop
   143           val nexts = stream(stop, rest)
   144           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   145           else nexts
   146 
   147         case Nil =>
   148           val stop = root_range.stop
   149           if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
   150           else Stream.empty
   151       }
   152     }
   153     stream(root_range.start,
   154       List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
   155   }
   156 
   157   def swing_tree(parent: DefaultMutableTreeNode)
   158     (swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
   159   {
   160     for ((_, entry) <- branches) {
   161       var current = parent
   162       val node = swing_node(Text.Info(entry.range, entry.markup))
   163       current.add(node)
   164       current = node
   165       entry.subtree.swing_tree(current)(swing_node)
   166     }
   167   }
   168 }
   169