src/Pure/PIDE/markup_tree.scala
author wenzelm
Fri Aug 10 21:22:40 2012 +0200 (2012-08-10)
changeset 48762 9ff86bf6ad19
parent 47539 436ae5ea4f80
child 49417 c5a8592fb5d3
permissions -rw-r--r--
tuned message;
     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               body.filter(e => !new_range.contains(e._1)).mkString("\n"))
   100             this
   101           }
   102         }
   103     }
   104   }
   105 
   106   def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
   107     result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
   108   {
   109     def results(x: A, entry: Entry): Option[A] =
   110       if (result_elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
   111         val (y, changed) =
   112           (entry.markup :\ (x, false))((info, res) =>
   113             {
   114               val (y, changed) = res
   115               val arg = (y, Text.Info(entry.range, info))
   116               if (result.isDefinedAt(arg)) (result(arg), true)
   117               else res
   118             })
   119         if (changed) Some(y) else None
   120       }
   121       else None
   122 
   123     def stream(
   124       last: Text.Offset,
   125       stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
   126     {
   127       stack match {
   128         case (parent, (range, entry) #:: more) :: rest =>
   129           val subrange = range.restrict(root_range)
   130           val subtree = entry.subtree.overlapping(subrange).toStream
   131           val start = subrange.start
   132 
   133           results(parent.info, entry) match {
   134             case Some(res) =>
   135               val next = Text.Info(subrange, res)
   136               val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   137               if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   138               else nexts
   139             case None => stream(last, (parent, subtree #::: more) :: rest)
   140           }
   141 
   142         case (parent, Stream.Empty) :: rest =>
   143           val stop = parent.range.stop
   144           val nexts = stream(stop, rest)
   145           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   146           else nexts
   147 
   148         case Nil =>
   149           val stop = root_range.stop
   150           if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
   151           else Stream.empty
   152       }
   153     }
   154     stream(root_range.start,
   155       List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
   156   }
   157 
   158   def swing_tree(parent: DefaultMutableTreeNode)
   159     (swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
   160   {
   161     for ((_, entry) <- branches) {
   162       var current = parent
   163       val node = swing_node(Text.Info(entry.range, entry.markup))
   164       current.add(node)
   165       current = node
   166       entry.subtree.swing_tree(current)(swing_node)
   167     }
   168   }
   169 }
   170