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