src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Sep 20 13:34:27 2012 +0200 (2012-09-20)
changeset 49467 25b7e843e124
parent 49466 99ed1f422635
child 49469 00c301c8d569
permissions -rw-r--r--
more direct Markup_Tree.from_XML;
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@49467
    15
import scala.annotation.tailrec
wenzelm@38479
    16
wenzelm@38479
    17
wenzelm@38479
    18
object Markup_Tree
wenzelm@38479
    19
{
wenzelm@45456
    20
  val empty: Markup_Tree = new Markup_Tree(Branches.empty)
wenzelm@38479
    21
wenzelm@49467
    22
  def merge_disjoint(trees: List[Markup_Tree]): Markup_Tree =
wenzelm@49467
    23
    trees match {
wenzelm@49467
    24
      case Nil => empty
wenzelm@49467
    25
      case head :: tail =>
wenzelm@49467
    26
        new Markup_Tree(
wenzelm@49467
    27
          (head.branches /: tail) {
wenzelm@49467
    28
            case (branches, tree) =>
wenzelm@49467
    29
              (branches /: tree.branches) {
wenzelm@49467
    30
                case (bs, (r, entry)) =>
wenzelm@49467
    31
                  require(!bs.isDefinedAt(r))
wenzelm@49467
    32
                  bs + (r -> entry)
wenzelm@49467
    33
              }
wenzelm@49467
    34
          })
wenzelm@49467
    35
    }
wenzelm@49467
    36
wenzelm@45473
    37
  object Entry
wenzelm@45473
    38
  {
wenzelm@45473
    39
    def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
wenzelm@45474
    40
      Entry(markup.range, List(markup.info), Set(markup.info.markup.name), subtree)
wenzelm@45473
    41
  }
wenzelm@45473
    42
wenzelm@45474
    43
  sealed case class Entry(
wenzelm@45474
    44
    range: Text.Range,
wenzelm@45474
    45
    rev_markup: List[XML.Elem],
wenzelm@45474
    46
    elements: Set[String],
wenzelm@45474
    47
    subtree: Markup_Tree)
wenzelm@45469
    48
  {
wenzelm@45474
    49
    def + (info: XML.Elem): Entry =
wenzelm@45474
    50
      if (elements(info.markup.name)) copy(rev_markup = info :: rev_markup)
wenzelm@45474
    51
      else copy(rev_markup = info :: rev_markup, elements = elements + info.markup.name)
wenzelm@45474
    52
wenzelm@45470
    53
    def markup: List[XML.Elem] = rev_markup.reverse
wenzelm@45469
    54
  }
wenzelm@45469
    55
wenzelm@38479
    56
  object Branches
wenzelm@38479
    57
  {
wenzelm@38578
    58
    type T = SortedMap[Text.Range, Entry]
wenzelm@45456
    59
    val empty: T = SortedMap.empty(Text.Range.Ordering)
wenzelm@38479
    60
  }
wenzelm@49466
    61
wenzelm@49466
    62
wenzelm@49466
    63
  /* XML representation */
wenzelm@49466
    64
wenzelm@49467
    65
  @tailrec private def strip_elems(markups: List[Markup], body: XML.Body): (List[Markup], XML.Body) =
wenzelm@49467
    66
    body match {
wenzelm@49467
    67
      case List(XML.Elem(markup1, body1)) => strip_elems(markup1 :: markups, body1)
wenzelm@49467
    68
      case _ => (markups, body)
wenzelm@49467
    69
    }
wenzelm@49467
    70
wenzelm@49467
    71
  private def make_trees(acc: (Int, List[Markup_Tree]), tree: XML.Tree): (Int, List[Markup_Tree]) =
wenzelm@49467
    72
    {
wenzelm@49467
    73
      val (offset, markup_trees) = acc
wenzelm@49467
    74
wenzelm@49467
    75
      strip_elems(Nil, List(tree)) match {
wenzelm@49467
    76
        case (Nil, body) =>
wenzelm@49467
    77
          (offset + XML.text_length(body), markup_trees)
wenzelm@49466
    78
wenzelm@49467
    79
        case (markups, body) =>
wenzelm@49467
    80
          val (end_offset, markup_trees1) = (acc /: body)(make_trees)
wenzelm@49467
    81
          val range = Text.Range(offset, end_offset)
wenzelm@49467
    82
          val new_markup_tree =
wenzelm@49467
    83
            (merge_disjoint(markup_trees1) /: markups) {
wenzelm@49467
    84
              case (markup_tree, markup) =>
wenzelm@49467
    85
                markup_tree + Text.Info(range, XML.Elem(markup, Nil))
wenzelm@49467
    86
            }
wenzelm@49467
    87
          (end_offset, List(new_markup_tree))
wenzelm@49466
    88
      }
wenzelm@49467
    89
    }
wenzelm@49466
    90
wenzelm@49467
    91
  def from_XML(body: XML.Body): Markup_Tree =
wenzelm@49467
    92
    merge_disjoint(((0, Nil: List[Markup_Tree]) /: body)(make_trees)._2)
wenzelm@38479
    93
}
immler@34554
    94
immler@34393
    95
wenzelm@49467
    96
final class Markup_Tree private(private val branches: Markup_Tree.Branches.T)
wenzelm@34717
    97
{
wenzelm@49417
    98
  import Markup_Tree._
wenzelm@49417
    99
wenzelm@45469
   100
  private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
wenzelm@45469
   101
    this(branches + (entry.range -> entry))
wenzelm@45469
   102
wenzelm@38563
   103
  override def toString =
wenzelm@38563
   104
    branches.toList.map(_._2) match {
wenzelm@38563
   105
      case Nil => "Empty"
wenzelm@38563
   106
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
   107
    }
wenzelm@38563
   108
wenzelm@45469
   109
  private def overlapping(range: Text.Range): Branches.T =
wenzelm@45457
   110
  {
wenzelm@45457
   111
    val start = Text.Range(range.start)
wenzelm@45457
   112
    val stop = Text.Range(range.stop)
wenzelm@45457
   113
    val bs = branches.range(start, stop)
wenzelm@45457
   114
    // FIXME check after Scala 2.8.x
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@38578
   136
          if (body.forall(e => new_range.contains(e._1))) {
wenzelm@45469
   137
            val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME
wenzelm@38657
   138
              if (body.size > 1)
wenzelm@38657
   139
                (Branches.empty /: branches)((rest, entry) =>
wenzelm@38657
   140
                  if (body.isDefinedAt(entry._1)) rest else rest + entry)
wenzelm@38657
   141
              else branches
wenzelm@45473
   142
            new Markup_Tree(rest, Entry(new_markup, new Markup_Tree(body)))
wenzelm@38482
   143
          }
wenzelm@38482
   144
          else { // FIXME split markup!?
wenzelm@48762
   145
            System.err.println("Ignored overlapping markup information: " + new_markup +
wenzelm@48762
   146
              body.filter(e => !new_range.contains(e._1)).mkString("\n"))
wenzelm@38482
   147
            this
wenzelm@38482
   148
          }
wenzelm@38482
   149
        }
wenzelm@34703
   150
    }
wenzelm@34703
   151
  }
wenzelm@34703
   152
wenzelm@46178
   153
  def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
wenzelm@46178
   154
    result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
wenzelm@45459
   155
  {
wenzelm@46178
   156
    def results(x: A, entry: Entry): Option[A] =
wenzelm@46178
   157
      if (result_elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
wenzelm@45474
   158
        val (y, changed) =
wenzelm@45474
   159
          (entry.markup :\ (x, false))((info, res) =>
wenzelm@45474
   160
            {
wenzelm@45474
   161
              val (y, changed) = res
wenzelm@46165
   162
              val arg = (y, Text.Info(entry.range, info))
wenzelm@46178
   163
              if (result.isDefinedAt(arg)) (result(arg), true)
wenzelm@45474
   164
              else res
wenzelm@45474
   165
            })
wenzelm@45474
   166
        if (changed) Some(y) else None
wenzelm@45474
   167
      }
wenzelm@45474
   168
      else None
wenzelm@45467
   169
wenzelm@45459
   170
    def stream(
wenzelm@45459
   171
      last: Text.Offset,
wenzelm@45469
   172
      stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
wenzelm@45459
   173
    {
wenzelm@45459
   174
      stack match {
wenzelm@45469
   175
        case (parent, (range, entry) #:: more) :: rest =>
wenzelm@45467
   176
          val subrange = range.restrict(root_range)
wenzelm@45469
   177
          val subtree = entry.subtree.overlapping(subrange).toStream
wenzelm@45459
   178
          val start = subrange.start
wenzelm@45459
   179
wenzelm@46178
   180
          results(parent.info, entry) match {
wenzelm@45469
   181
            case Some(res) =>
wenzelm@45469
   182
              val next = Text.Info(subrange, res)
wenzelm@45469
   183
              val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@45469
   184
              if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@45469
   185
              else nexts
wenzelm@45469
   186
            case None => stream(last, (parent, subtree #::: more) :: rest)
wenzelm@45459
   187
          }
wenzelm@45459
   188
wenzelm@45459
   189
        case (parent, Stream.Empty) :: rest =>
wenzelm@45459
   190
          val stop = parent.range.stop
wenzelm@45459
   191
          val nexts = stream(stop, rest)
wenzelm@45459
   192
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@45459
   193
          else nexts
wenzelm@45459
   194
wenzelm@45459
   195
        case Nil =>
wenzelm@45467
   196
          val stop = root_range.stop
wenzelm@45467
   197
          if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
wenzelm@45459
   198
          else Stream.empty
wenzelm@45459
   199
      }
wenzelm@45459
   200
    }
wenzelm@45467
   201
    stream(root_range.start,
wenzelm@45467
   202
      List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
wenzelm@45459
   203
  }
wenzelm@45459
   204
wenzelm@49465
   205
  def swing_tree(parent: DefaultMutableTreeNode,
wenzelm@49465
   206
    swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
wenzelm@38479
   207
  {
wenzelm@45469
   208
    for ((_, entry) <- branches) {
wenzelm@47539
   209
      val node = swing_node(Text.Info(entry.range, entry.markup))
wenzelm@49465
   210
      entry.subtree.swing_tree(node, swing_node)
wenzelm@49465
   211
      parent.add(node)
immler@34514
   212
    }
immler@34514
   213
  }
wenzelm@34717
   214
}
immler@34514
   215