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