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);
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@50642
   121
          if (offset == end_offset) acc
wenzelm@50642
   122
          else {
wenzelm@50642
   123
            val range = Text.Range(offset, end_offset)
wenzelm@50642
   124
            val entry = Entry(range, elems, merge_disjoint(subtrees))
wenzelm@50642
   125
            (end_offset, new Markup_Tree(Branches.empty, entry) :: markup_trees)
wenzelm@50642
   126
          }
wenzelm@49466
   127
      }
wenzelm@49467
   128
    }
wenzelm@49466
   129
wenzelm@49467
   130
  def from_XML(body: XML.Body): Markup_Tree =
wenzelm@49467
   131
    merge_disjoint(((0, Nil: List[Markup_Tree]) /: body)(make_trees)._2)
wenzelm@38479
   132
}
immler@34554
   133
immler@34393
   134
wenzelm@49467
   135
final class Markup_Tree private(private val branches: Markup_Tree.Branches.T)
wenzelm@34717
   136
{
wenzelm@49417
   137
  import Markup_Tree._
wenzelm@49417
   138
wenzelm@45469
   139
  private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
wenzelm@45469
   140
    this(branches + (entry.range -> entry))
wenzelm@45469
   141
wenzelm@38563
   142
  override def toString =
wenzelm@38563
   143
    branches.toList.map(_._2) match {
wenzelm@38563
   144
      case Nil => "Empty"
wenzelm@38563
   145
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
   146
    }
wenzelm@38563
   147
wenzelm@45469
   148
  private def overlapping(range: Text.Range): Branches.T =
wenzelm@45457
   149
  {
wenzelm@45457
   150
    val start = Text.Range(range.start)
wenzelm@45457
   151
    val stop = Text.Range(range.stop)
wenzelm@45457
   152
    val bs = branches.range(start, stop)
wenzelm@45457
   153
    branches.get(stop) match {
wenzelm@45469
   154
      case Some(end) if range overlaps end.range => bs + (end.range -> end)
wenzelm@45457
   155
      case _ => bs
wenzelm@45457
   156
    }
wenzelm@45457
   157
  }
wenzelm@45457
   158
wenzelm@50552
   159
  def make_elements: Elements =
wenzelm@50552
   160
    (Elements.empty /: branches)(
wenzelm@50552
   161
      { case (elements, (_, entry)) => elements ++ entry.subtree_elements ++ entry.elements })
wenzelm@50552
   162
wenzelm@45469
   163
  def + (new_markup: Text.Markup): Markup_Tree =
wenzelm@34703
   164
  {
wenzelm@45469
   165
    val new_range = new_markup.range
wenzelm@45469
   166
wenzelm@38578
   167
    branches.get(new_range) match {
wenzelm@45473
   168
      case None => new Markup_Tree(branches, Entry(new_markup, empty))
wenzelm@45469
   169
      case Some(entry) =>
wenzelm@45469
   170
        if (entry.range == new_range)
wenzelm@50552
   171
          new Markup_Tree(branches, entry + new_markup)
wenzelm@45469
   172
        else if (entry.range.contains(new_range))
wenzelm@50552
   173
          new Markup_Tree(branches, entry \ new_markup)
wenzelm@38578
   174
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@45473
   175
          new Markup_Tree(Branches.empty, Entry(new_markup, this))
wenzelm@38482
   176
        else {
wenzelm@45457
   177
          val body = overlapping(new_range)
wenzelm@49607
   178
          if (body.forall(e => new_range.contains(e._1)))
wenzelm@49607
   179
            new Markup_Tree(branches -- body.keys, Entry(new_markup, new Markup_Tree(body)))
wenzelm@49608
   180
          else {
wenzelm@49608
   181
            java.lang.System.err.println("Ignored overlapping markup information: " + new_markup +
wenzelm@48762
   182
              body.filter(e => !new_range.contains(e._1)).mkString("\n"))
wenzelm@38482
   183
            this
wenzelm@38482
   184
          }
wenzelm@38482
   185
        }
wenzelm@34703
   186
    }
wenzelm@34703
   187
  }
wenzelm@34703
   188
wenzelm@49614
   189
  def to_XML(root_range: Text.Range, text: CharSequence, filter: XML.Elem => Boolean): XML.Body =
wenzelm@49614
   190
  {
wenzelm@49614
   191
    def make_text(start: Text.Offset, stop: Text.Offset): XML.Body =
wenzelm@49614
   192
      if (start == stop) Nil
wenzelm@49614
   193
      else List(XML.Text(text.subSequence(start, stop).toString))
wenzelm@49614
   194
wenzelm@49614
   195
    def make_elems(rev_markups: List[XML.Elem], body: XML.Body): XML.Body =
wenzelm@49614
   196
      (body /: rev_markups) {
wenzelm@49650
   197
        case (b, elem) =>
wenzelm@49650
   198
          if (!filter(elem)) b
wenzelm@49650
   199
          else if (elem.body.isEmpty) List(XML.Elem(elem.markup, b))
wenzelm@49650
   200
          else List(XML.Wrapped_Elem(elem.markup, elem.body, b))
wenzelm@49614
   201
      }
wenzelm@49614
   202
wenzelm@49614
   203
    def make_body(elem_range: Text.Range, elem_markup: List[XML.Elem], entries: Branches.T)
wenzelm@49614
   204
      : XML.Body =
wenzelm@49614
   205
    {
wenzelm@49614
   206
      val body = new mutable.ListBuffer[XML.Tree]
wenzelm@49614
   207
      var last = elem_range.start
wenzelm@49614
   208
      for ((range, entry) <- entries) {
wenzelm@49614
   209
        val subrange = range.restrict(elem_range)
wenzelm@49614
   210
        body ++= make_text(last, subrange.start)
wenzelm@49614
   211
        body ++= make_body(subrange, entry.rev_markup, entry.subtree.overlapping(subrange))
wenzelm@49614
   212
        last = subrange.stop
wenzelm@49614
   213
      }
wenzelm@49614
   214
      body ++= make_text(last, elem_range.stop)
wenzelm@49614
   215
      make_elems(elem_markup, body.toList)
wenzelm@49614
   216
    }
wenzelm@49614
   217
   make_body(root_range, Nil, overlapping(root_range))
wenzelm@49614
   218
  }
wenzelm@49614
   219
wenzelm@49614
   220
  def to_XML(text: CharSequence): XML.Body =
wenzelm@49614
   221
    to_XML(Text.Range(0, text.length), text, (_: XML.Elem) => true)
wenzelm@49614
   222
wenzelm@46178
   223
  def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
wenzelm@46178
   224
    result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
wenzelm@45459
   225
  {
wenzelm@50551
   226
    val notable: Elements => Boolean =
wenzelm@50551
   227
      result_elements match {
wenzelm@50551
   228
        case Some(res) => (elements: Elements) => res.exists(elements.contains)
wenzelm@50551
   229
        case None => (elements: Elements) => true
wenzelm@50551
   230
      }
wenzelm@50551
   231
wenzelm@46178
   232
    def results(x: A, entry: Entry): Option[A] =
wenzelm@50552
   233
    {
wenzelm@50552
   234
      val (y, changed) =
wenzelm@50552
   235
        // FIXME proper cumulation order (including status markup) (!?)
wenzelm@50552
   236
        ((x, false) /: entry.rev_markup)((res, info) =>
wenzelm@50552
   237
          {
wenzelm@50552
   238
            val (y, changed) = res
wenzelm@50552
   239
            val arg = (y, Text.Info(entry.range, info))
wenzelm@50552
   240
            if (result.isDefinedAt(arg)) (result(arg), true)
wenzelm@50552
   241
            else res
wenzelm@50552
   242
          })
wenzelm@50552
   243
      if (changed) Some(y) else None
wenzelm@50552
   244
    }
wenzelm@45467
   245
wenzelm@45459
   246
    def stream(
wenzelm@45459
   247
      last: Text.Offset,
wenzelm@45469
   248
      stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
wenzelm@45459
   249
    {
wenzelm@45459
   250
      stack match {
wenzelm@45469
   251
        case (parent, (range, entry) #:: more) :: rest =>
wenzelm@45467
   252
          val subrange = range.restrict(root_range)
wenzelm@50552
   253
          val subtree =
wenzelm@50552
   254
            if (notable(entry.subtree_elements))
wenzelm@50552
   255
              entry.subtree.overlapping(subrange).toStream
wenzelm@50552
   256
            else Stream.empty
wenzelm@45459
   257
          val start = subrange.start
wenzelm@45459
   258
wenzelm@50552
   259
          (if (notable(entry.elements)) results(parent.info, entry) else None) match {
wenzelm@45469
   260
            case Some(res) =>
wenzelm@45469
   261
              val next = Text.Info(subrange, res)
wenzelm@45469
   262
              val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@45469
   263
              if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@45469
   264
              else nexts
wenzelm@45469
   265
            case None => stream(last, (parent, subtree #::: more) :: rest)
wenzelm@45459
   266
          }
wenzelm@45459
   267
wenzelm@45459
   268
        case (parent, Stream.Empty) :: rest =>
wenzelm@45459
   269
          val stop = parent.range.stop
wenzelm@45459
   270
          val nexts = stream(stop, rest)
wenzelm@45459
   271
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@45459
   272
          else nexts
wenzelm@45459
   273
wenzelm@45459
   274
        case Nil =>
wenzelm@45467
   275
          val stop = root_range.stop
wenzelm@45467
   276
          if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
wenzelm@45459
   277
          else Stream.empty
wenzelm@45459
   278
      }
wenzelm@45459
   279
    }
wenzelm@45467
   280
    stream(root_range.start,
wenzelm@45467
   281
      List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
wenzelm@45459
   282
  }
wenzelm@45459
   283
wenzelm@49465
   284
  def swing_tree(parent: DefaultMutableTreeNode,
wenzelm@49465
   285
    swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
wenzelm@38479
   286
  {
wenzelm@45469
   287
    for ((_, entry) <- branches) {
wenzelm@47539
   288
      val node = swing_node(Text.Info(entry.range, entry.markup))
wenzelm@49465
   289
      entry.subtree.swing_tree(node, swing_node)
wenzelm@49465
   290
      parent.add(node)
immler@34514
   291
    }
immler@34514
   292
  }
wenzelm@34717
   293
}
immler@34514
   294