src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Sep 27 19:34:31 2012 +0200 (2012-09-27)
changeset 49614 0009a6ebc83b
parent 49608 ce1c34c98eeb
child 49650 9fad6480300d
permissions -rw-r--r--
operations to turn markup into XML;
tuned;
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@45456
    21
  val empty: Markup_Tree = new Markup_Tree(Branches.empty)
wenzelm@38479
    22
wenzelm@49467
    23
  def merge_disjoint(trees: List[Markup_Tree]): Markup_Tree =
wenzelm@49467
    24
    trees match {
wenzelm@49467
    25
      case Nil => empty
wenzelm@49467
    26
      case head :: tail =>
wenzelm@49467
    27
        new Markup_Tree(
wenzelm@49467
    28
          (head.branches /: tail) {
wenzelm@49467
    29
            case (branches, tree) =>
wenzelm@49467
    30
              (branches /: tree.branches) {
wenzelm@49467
    31
                case (bs, (r, entry)) =>
wenzelm@49467
    32
                  require(!bs.isDefinedAt(r))
wenzelm@49467
    33
                  bs + (r -> entry)
wenzelm@49467
    34
              }
wenzelm@49467
    35
          })
wenzelm@49467
    36
    }
wenzelm@49467
    37
wenzelm@45473
    38
  object Entry
wenzelm@45473
    39
  {
wenzelm@45473
    40
    def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
wenzelm@45474
    41
      Entry(markup.range, List(markup.info), Set(markup.info.markup.name), subtree)
wenzelm@49469
    42
wenzelm@49469
    43
    def apply(range: Text.Range, rev_markups: List[XML.Elem], subtree: Markup_Tree): Entry =
wenzelm@49469
    44
      Entry(range, rev_markups, Set.empty ++ rev_markups.iterator.map(_.markup.name), subtree)
wenzelm@45473
    45
  }
wenzelm@45473
    46
wenzelm@45474
    47
  sealed case class Entry(
wenzelm@45474
    48
    range: Text.Range,
wenzelm@45474
    49
    rev_markup: List[XML.Elem],
wenzelm@45474
    50
    elements: Set[String],
wenzelm@45474
    51
    subtree: Markup_Tree)
wenzelm@45469
    52
  {
wenzelm@45474
    53
    def + (info: XML.Elem): Entry =
wenzelm@45474
    54
      if (elements(info.markup.name)) copy(rev_markup = info :: rev_markup)
wenzelm@45474
    55
      else copy(rev_markup = info :: rev_markup, elements = elements + info.markup.name)
wenzelm@45474
    56
wenzelm@45470
    57
    def markup: List[XML.Elem] = rev_markup.reverse
wenzelm@45469
    58
  }
wenzelm@45469
    59
wenzelm@38479
    60
  object Branches
wenzelm@38479
    61
  {
wenzelm@38578
    62
    type T = SortedMap[Text.Range, Entry]
wenzelm@45456
    63
    val empty: T = SortedMap.empty(Text.Range.Ordering)
wenzelm@38479
    64
  }
wenzelm@49466
    65
wenzelm@49466
    66
wenzelm@49466
    67
  /* XML representation */
wenzelm@49466
    68
wenzelm@49614
    69
  // FIXME decode markup body
wenzelm@49467
    70
  @tailrec private def strip_elems(markups: List[Markup], body: XML.Body): (List[Markup], XML.Body) =
wenzelm@49467
    71
    body match {
wenzelm@49467
    72
      case List(XML.Elem(markup1, body1)) => strip_elems(markup1 :: markups, body1)
wenzelm@49467
    73
      case _ => (markups, body)
wenzelm@49467
    74
    }
wenzelm@49467
    75
wenzelm@49467
    76
  private def make_trees(acc: (Int, List[Markup_Tree]), tree: XML.Tree): (Int, List[Markup_Tree]) =
wenzelm@49467
    77
    {
wenzelm@49467
    78
      val (offset, markup_trees) = acc
wenzelm@49467
    79
wenzelm@49467
    80
      strip_elems(Nil, List(tree)) match {
wenzelm@49467
    81
        case (Nil, body) =>
wenzelm@49467
    82
          (offset + XML.text_length(body), markup_trees)
wenzelm@49466
    83
wenzelm@49469
    84
        case (elems, body) =>
wenzelm@49469
    85
          val (end_offset, subtrees) = ((offset, Nil: List[Markup_Tree]) /: body)(make_trees)
wenzelm@49467
    86
          val range = Text.Range(offset, end_offset)
wenzelm@49469
    87
          val entry = Entry(range, elems.map(XML.Elem(_, Nil)), merge_disjoint(subtrees))
wenzelm@49469
    88
          (end_offset, new Markup_Tree(Branches.empty, entry) :: markup_trees)
wenzelm@49466
    89
      }
wenzelm@49467
    90
    }
wenzelm@49466
    91
wenzelm@49467
    92
  def from_XML(body: XML.Body): Markup_Tree =
wenzelm@49467
    93
    merge_disjoint(((0, Nil: List[Markup_Tree]) /: body)(make_trees)._2)
wenzelm@38479
    94
}
immler@34554
    95
immler@34393
    96
wenzelm@49467
    97
final class Markup_Tree private(private val branches: Markup_Tree.Branches.T)
wenzelm@34717
    98
{
wenzelm@49417
    99
  import Markup_Tree._
wenzelm@49417
   100
wenzelm@45469
   101
  private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
wenzelm@45469
   102
    this(branches + (entry.range -> entry))
wenzelm@45469
   103
wenzelm@38563
   104
  override def toString =
wenzelm@38563
   105
    branches.toList.map(_._2) match {
wenzelm@38563
   106
      case Nil => "Empty"
wenzelm@38563
   107
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
   108
    }
wenzelm@38563
   109
wenzelm@45469
   110
  private def overlapping(range: Text.Range): Branches.T =
wenzelm@45457
   111
  {
wenzelm@45457
   112
    val start = Text.Range(range.start)
wenzelm@45457
   113
    val stop = Text.Range(range.stop)
wenzelm@45457
   114
    val bs = branches.range(start, stop)
wenzelm@45457
   115
    branches.get(stop) match {
wenzelm@45469
   116
      case Some(end) if range overlaps end.range => bs + (end.range -> end)
wenzelm@45457
   117
      case _ => bs
wenzelm@45457
   118
    }
wenzelm@45457
   119
  }
wenzelm@45457
   120
wenzelm@45469
   121
  def + (new_markup: Text.Markup): Markup_Tree =
wenzelm@34703
   122
  {
wenzelm@45469
   123
    val new_range = new_markup.range
wenzelm@45469
   124
wenzelm@38578
   125
    branches.get(new_range) match {
wenzelm@45473
   126
      case None => new Markup_Tree(branches, Entry(new_markup, empty))
wenzelm@45469
   127
      case Some(entry) =>
wenzelm@45469
   128
        if (entry.range == new_range)
wenzelm@45473
   129
          new Markup_Tree(branches, entry + new_markup.info)
wenzelm@45469
   130
        else if (entry.range.contains(new_range))
wenzelm@45469
   131
          new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
wenzelm@38578
   132
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@45473
   133
          new Markup_Tree(Branches.empty, Entry(new_markup, this))
wenzelm@38482
   134
        else {
wenzelm@45457
   135
          val body = overlapping(new_range)
wenzelm@49607
   136
          if (body.forall(e => new_range.contains(e._1)))
wenzelm@49607
   137
            new Markup_Tree(branches -- body.keys, Entry(new_markup, new Markup_Tree(body)))
wenzelm@49608
   138
          else {
wenzelm@49608
   139
            java.lang.System.err.println("Ignored overlapping markup information: " + new_markup +
wenzelm@48762
   140
              body.filter(e => !new_range.contains(e._1)).mkString("\n"))
wenzelm@38482
   141
            this
wenzelm@38482
   142
          }
wenzelm@38482
   143
        }
wenzelm@34703
   144
    }
wenzelm@34703
   145
  }
wenzelm@34703
   146
wenzelm@49614
   147
  def to_XML(root_range: Text.Range, text: CharSequence, filter: XML.Elem => Boolean): XML.Body =
wenzelm@49614
   148
  {
wenzelm@49614
   149
    def make_text(start: Text.Offset, stop: Text.Offset): XML.Body =
wenzelm@49614
   150
      if (start == stop) Nil
wenzelm@49614
   151
      else List(XML.Text(text.subSequence(start, stop).toString))
wenzelm@49614
   152
wenzelm@49614
   153
    def make_elems(rev_markups: List[XML.Elem], body: XML.Body): XML.Body =
wenzelm@49614
   154
      (body /: rev_markups) {
wenzelm@49614
   155
        case (b, elem) => // FIXME encode markup body
wenzelm@49614
   156
          if (filter(elem)) List(XML.Elem(elem.markup, b)) else b
wenzelm@49614
   157
      }
wenzelm@49614
   158
wenzelm@49614
   159
    def make_body(elem_range: Text.Range, elem_markup: List[XML.Elem], entries: Branches.T)
wenzelm@49614
   160
      : XML.Body =
wenzelm@49614
   161
    {
wenzelm@49614
   162
      val body = new mutable.ListBuffer[XML.Tree]
wenzelm@49614
   163
      var last = elem_range.start
wenzelm@49614
   164
      for ((range, entry) <- entries) {
wenzelm@49614
   165
        val subrange = range.restrict(elem_range)
wenzelm@49614
   166
        body ++= make_text(last, subrange.start)
wenzelm@49614
   167
        body ++= make_body(subrange, entry.rev_markup, entry.subtree.overlapping(subrange))
wenzelm@49614
   168
        last = subrange.stop
wenzelm@49614
   169
      }
wenzelm@49614
   170
      body ++= make_text(last, elem_range.stop)
wenzelm@49614
   171
      make_elems(elem_markup, body.toList)
wenzelm@49614
   172
    }
wenzelm@49614
   173
   make_body(root_range, Nil, overlapping(root_range))
wenzelm@49614
   174
  }
wenzelm@49614
   175
wenzelm@49614
   176
  def to_XML(text: CharSequence): XML.Body =
wenzelm@49614
   177
    to_XML(Text.Range(0, text.length), text, (_: XML.Elem) => true)
wenzelm@49614
   178
wenzelm@46178
   179
  def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
wenzelm@46178
   180
    result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
wenzelm@45459
   181
  {
wenzelm@46178
   182
    def results(x: A, entry: Entry): Option[A] =
wenzelm@46178
   183
      if (result_elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
wenzelm@45474
   184
        val (y, changed) =
wenzelm@49614
   185
          ((x, false) /: entry.rev_markup)((res, info) =>  // FIXME proper order!?
wenzelm@45474
   186
            {
wenzelm@45474
   187
              val (y, changed) = res
wenzelm@46165
   188
              val arg = (y, Text.Info(entry.range, info))
wenzelm@46178
   189
              if (result.isDefinedAt(arg)) (result(arg), true)
wenzelm@45474
   190
              else res
wenzelm@45474
   191
            })
wenzelm@45474
   192
        if (changed) Some(y) else None
wenzelm@45474
   193
      }
wenzelm@45474
   194
      else None
wenzelm@45467
   195
wenzelm@45459
   196
    def stream(
wenzelm@45459
   197
      last: Text.Offset,
wenzelm@45469
   198
      stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
wenzelm@45459
   199
    {
wenzelm@45459
   200
      stack match {
wenzelm@45469
   201
        case (parent, (range, entry) #:: more) :: rest =>
wenzelm@45467
   202
          val subrange = range.restrict(root_range)
wenzelm@45469
   203
          val subtree = entry.subtree.overlapping(subrange).toStream
wenzelm@45459
   204
          val start = subrange.start
wenzelm@45459
   205
wenzelm@46178
   206
          results(parent.info, entry) match {
wenzelm@45469
   207
            case Some(res) =>
wenzelm@45469
   208
              val next = Text.Info(subrange, res)
wenzelm@45469
   209
              val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@45469
   210
              if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@45469
   211
              else nexts
wenzelm@45469
   212
            case None => stream(last, (parent, subtree #::: more) :: rest)
wenzelm@45459
   213
          }
wenzelm@45459
   214
wenzelm@45459
   215
        case (parent, Stream.Empty) :: rest =>
wenzelm@45459
   216
          val stop = parent.range.stop
wenzelm@45459
   217
          val nexts = stream(stop, rest)
wenzelm@45459
   218
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@45459
   219
          else nexts
wenzelm@45459
   220
wenzelm@45459
   221
        case Nil =>
wenzelm@45467
   222
          val stop = root_range.stop
wenzelm@45467
   223
          if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
wenzelm@45459
   224
          else Stream.empty
wenzelm@45459
   225
      }
wenzelm@45459
   226
    }
wenzelm@45467
   227
    stream(root_range.start,
wenzelm@45467
   228
      List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
wenzelm@45459
   229
  }
wenzelm@45459
   230
wenzelm@49465
   231
  def swing_tree(parent: DefaultMutableTreeNode,
wenzelm@49465
   232
    swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
wenzelm@38479
   233
  {
wenzelm@45469
   234
    for ((_, entry) <- branches) {
wenzelm@47539
   235
      val node = swing_node(Text.Info(entry.range, entry.markup))
wenzelm@49465
   236
      entry.subtree.swing_tree(node, swing_node)
wenzelm@49465
   237
      parent.add(node)
immler@34514
   238
    }
immler@34514
   239
  }
wenzelm@34717
   240
}
immler@34514
   241