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