src/Pure/PIDE/markup_tree.scala
author wenzelm
Sat Jul 13 12:39:45 2013 +0200 (2013-07-13)
changeset 52642 84eb792224a8
parent 51618 a3577cd80c41
child 52889 ea3338812e67
permissions -rw-r--r--
full merge of Command.State, which enables Command.prints to augment markup as well (assuming that these dynamic overlays are relatively few);
     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 
    12 import scala.collection.immutable.SortedMap
    13 import scala.collection.mutable
    14 import scala.annotation.tailrec
    15 
    16 
    17 object Markup_Tree
    18 {
    19   /* construct trees */
    20 
    21   val empty: Markup_Tree = new Markup_Tree(Branches.empty)
    22 
    23   def merge_disjoint(trees: List[Markup_Tree]): Markup_Tree =
    24     trees match {
    25       case Nil => empty
    26       case head :: tail =>
    27         new Markup_Tree(
    28           (head.branches /: tail) {
    29             case (branches, tree) =>
    30               (branches /: tree.branches) {
    31                 case (bs, (r, entry)) =>
    32                   require(!bs.isDefinedAt(r))
    33                   bs + (r -> entry)
    34               }
    35           })
    36     }
    37 
    38 
    39   /* tree building blocks */
    40 
    41   object Elements
    42   {
    43     val empty = new Elements(Set.empty)
    44   }
    45 
    46   final class Elements private(private val rep: Set[String])
    47   {
    48     def contains(name: String): Boolean = rep.contains(name)
    49 
    50     def + (name: String): Elements =
    51       if (contains(name)) this
    52       else new Elements(rep + name)
    53 
    54     def + (elem: XML.Elem): Elements = this + elem.markup.name
    55     def ++ (elems: Iterable[XML.Elem]): Elements = (this /: elems.iterator)(_ + _)
    56 
    57     def ++ (other: Elements): Elements =
    58       if (this eq other) this
    59       else if (rep.isEmpty) other
    60       else (this /: other.rep)(_ + _)
    61   }
    62 
    63   object Entry
    64   {
    65     def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
    66       Entry(markup.range, List(markup.info), Elements.empty + markup.info,
    67         subtree, subtree.make_elements)
    68 
    69     def apply(range: Text.Range, rev_markups: List[XML.Elem], subtree: Markup_Tree): Entry =
    70       Entry(range, rev_markups, Elements.empty ++ rev_markups,
    71         subtree, subtree.make_elements)
    72   }
    73 
    74   sealed case class Entry(
    75     range: Text.Range,
    76     rev_markup: List[XML.Elem],
    77     elements: Elements,
    78     subtree: Markup_Tree,
    79     subtree_elements: Elements)
    80   {
    81     def markup: List[XML.Elem] = rev_markup.reverse
    82 
    83     def + (markup: Text.Markup): Entry =
    84       copy(rev_markup = markup.info :: rev_markup, elements = elements + markup.info)
    85 
    86     def \ (markup: Text.Markup): Entry =
    87       copy(subtree = subtree + markup, subtree_elements = subtree_elements + markup.info)
    88   }
    89 
    90   object Branches
    91   {
    92     type T = SortedMap[Text.Range, Entry]
    93     val empty: T = SortedMap.empty(Text.Range.Ordering)
    94   }
    95 
    96 
    97   /* XML representation */
    98 
    99   @tailrec private def strip_elems(
   100       elems: List[XML.Elem], body: XML.Body): (List[XML.Elem], XML.Body) =
   101     body match {
   102       case List(XML.Wrapped_Elem(markup1, body1, body2)) =>
   103         strip_elems(XML.Elem(markup1, body1) :: elems, body2)
   104       case List(XML.Elem(markup1, body1)) =>
   105         strip_elems(XML.Elem(markup1, Nil) :: elems, body1)
   106       case _ => (elems, body)
   107     }
   108 
   109   private def make_trees(acc: (Int, List[Markup_Tree]), tree: XML.Tree): (Int, List[Markup_Tree]) =
   110     {
   111       val (offset, markup_trees) = acc
   112 
   113       strip_elems(Nil, List(tree)) match {
   114         case (Nil, body) =>
   115           (offset + XML.text_length(body), markup_trees)
   116 
   117         case (elems, body) =>
   118           val (end_offset, subtrees) = ((offset, Nil: List[Markup_Tree]) /: body)(make_trees)
   119           if (offset == end_offset) acc
   120           else {
   121             val range = Text.Range(offset, end_offset)
   122             val entry = Entry(range, elems, merge_disjoint(subtrees))
   123             (end_offset, new Markup_Tree(Branches.empty, entry) :: markup_trees)
   124           }
   125       }
   126     }
   127 
   128   def from_XML(body: XML.Body): Markup_Tree =
   129     merge_disjoint(((0, Nil: List[Markup_Tree]) /: body)(make_trees)._2)
   130 }
   131 
   132 
   133 final class Markup_Tree private(val branches: Markup_Tree.Branches.T)
   134 {
   135   import Markup_Tree._
   136 
   137   private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
   138     this(branches + (entry.range -> entry))
   139 
   140   override def toString =
   141     branches.toList.map(_._2) match {
   142       case Nil => "Empty"
   143       case list => list.mkString("Tree(", ",", ")")
   144     }
   145 
   146   private def overlapping(range: Text.Range): Branches.T =
   147   {
   148     val start = Text.Range(range.start)
   149     val stop = Text.Range(range.stop)
   150     val bs = branches.range(start, stop)
   151     branches.get(stop) match {
   152       case Some(end) if range overlaps end.range => bs + (end.range -> end)
   153       case _ => bs
   154     }
   155   }
   156 
   157   def make_elements: Elements =
   158     (Elements.empty /: branches)(
   159       { case (elements, (_, entry)) => elements ++ entry.subtree_elements ++ entry.elements })
   160 
   161   def + (new_markup: Text.Markup): Markup_Tree =
   162   {
   163     val new_range = new_markup.range
   164 
   165     branches.get(new_range) match {
   166       case None => new Markup_Tree(branches, Entry(new_markup, empty))
   167       case Some(entry) =>
   168         if (entry.range == new_range)
   169           new Markup_Tree(branches, entry + new_markup)
   170         else if (entry.range.contains(new_range))
   171           new Markup_Tree(branches, entry \ new_markup)
   172         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
   173           new Markup_Tree(Branches.empty, Entry(new_markup, this))
   174         else {
   175           val body = overlapping(new_range)
   176           if (body.forall(e => new_range.contains(e._1)))
   177             new Markup_Tree(branches -- body.keys, Entry(new_markup, new Markup_Tree(body)))
   178           else {
   179             java.lang.System.err.println("Ignored overlapping markup information: " + new_markup +
   180               body.filter(e => !new_range.contains(e._1)).mkString("\n"))
   181             this
   182           }
   183         }
   184     }
   185   }
   186 
   187   def ++ (other: Markup_Tree): Markup_Tree =
   188     (this /: other.branches)({ case (tree, (range, entry)) =>
   189       ((tree ++ entry.subtree) /: entry.markup)({ case (t, elem) => t + Text.Info(range, elem) }) })
   190 
   191   def to_XML(root_range: Text.Range, text: CharSequence, filter: XML.Elem => Boolean): XML.Body =
   192   {
   193     def make_text(start: Text.Offset, stop: Text.Offset): XML.Body =
   194       if (start == stop) Nil
   195       else List(XML.Text(text.subSequence(start, stop).toString))
   196 
   197     def make_elems(rev_markups: List[XML.Elem], body: XML.Body): XML.Body =
   198       (body /: rev_markups) {
   199         case (b, elem) =>
   200           if (!filter(elem)) b
   201           else if (elem.body.isEmpty) List(XML.Elem(elem.markup, b))
   202           else List(XML.Wrapped_Elem(elem.markup, elem.body, b))
   203       }
   204 
   205     def make_body(elem_range: Text.Range, elem_markup: List[XML.Elem], entries: Branches.T)
   206       : XML.Body =
   207     {
   208       val body = new mutable.ListBuffer[XML.Tree]
   209       var last = elem_range.start
   210       for ((range, entry) <- entries) {
   211         val subrange = range.restrict(elem_range)
   212         body ++= make_text(last, subrange.start)
   213         body ++= make_body(subrange, entry.rev_markup, entry.subtree.overlapping(subrange))
   214         last = subrange.stop
   215       }
   216       body ++= make_text(last, elem_range.stop)
   217       make_elems(elem_markup, body.toList)
   218     }
   219    make_body(root_range, Nil, overlapping(root_range))
   220   }
   221 
   222   def to_XML(text: CharSequence): XML.Body =
   223     to_XML(Text.Range(0, text.length), text, (_: XML.Elem) => true)
   224 
   225   def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
   226     result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
   227   {
   228     val notable: Elements => Boolean =
   229       result_elements match {
   230         case Some(res) => (elements: Elements) => res.exists(elements.contains)
   231         case None => (elements: Elements) => true
   232       }
   233 
   234     def results(x: A, entry: Entry): Option[A] =
   235     {
   236       val (y, changed) =
   237         // FIXME proper cumulation order (including status markup) (!?)
   238         ((x, false) /: entry.rev_markup)((res, info) =>
   239           {
   240             val (y, changed) = res
   241             val arg = (y, Text.Info(entry.range, info))
   242             if (result.isDefinedAt(arg)) (result(arg), true)
   243             else res
   244           })
   245       if (changed) Some(y) else None
   246     }
   247 
   248     def stream(
   249       last: Text.Offset,
   250       stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
   251     {
   252       stack match {
   253         case (parent, (range, entry) #:: more) :: rest =>
   254           val subrange = range.restrict(root_range)
   255           val subtree =
   256             if (notable(entry.subtree_elements))
   257               entry.subtree.overlapping(subrange).toStream
   258             else Stream.empty
   259           val start = subrange.start
   260 
   261           (if (notable(entry.elements)) results(parent.info, entry) else None) match {
   262             case Some(res) =>
   263               val next = Text.Info(subrange, res)
   264               val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   265               if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   266               else nexts
   267             case None => stream(last, (parent, subtree #::: more) :: rest)
   268           }
   269 
   270         case (parent, Stream.Empty) :: rest =>
   271           val stop = parent.range.stop
   272           val nexts = stream(stop, rest)
   273           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   274           else nexts
   275 
   276         case Nil =>
   277           val stop = root_range.stop
   278           if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
   279           else Stream.empty
   280       }
   281     }
   282     stream(root_range.start,
   283       List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
   284   }
   285 }
   286