src/Pure/PIDE/markup_tree.scala
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 49650 9fad6480300d
child 50551 67d934cdc9b9
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
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@49650
    69
  @tailrec private def strip_elems(
wenzelm@49650
    70
      elems: List[XML.Elem], body: XML.Body): (List[XML.Elem], XML.Body) =
wenzelm@49467
    71
    body match {
wenzelm@49650
    72
      case List(XML.Wrapped_Elem(markup1, body1, body2)) =>
wenzelm@49650
    73
        strip_elems(XML.Elem(markup1, body1) :: elems, body2)
wenzelm@49650
    74
      case List(XML.Elem(markup1, body1)) =>
wenzelm@49650
    75
        strip_elems(XML.Elem(markup1, Nil) :: elems, body1)
wenzelm@49650
    76
      case _ => (elems, body)
wenzelm@49467
    77
    }
wenzelm@49467
    78
wenzelm@49467
    79
  private def make_trees(acc: (Int, List[Markup_Tree]), tree: XML.Tree): (Int, List[Markup_Tree]) =
wenzelm@49467
    80
    {
wenzelm@49467
    81
      val (offset, markup_trees) = acc
wenzelm@49467
    82
wenzelm@49467
    83
      strip_elems(Nil, List(tree)) match {
wenzelm@49467
    84
        case (Nil, body) =>
wenzelm@49467
    85
          (offset + XML.text_length(body), markup_trees)
wenzelm@49466
    86
wenzelm@49469
    87
        case (elems, body) =>
wenzelm@49469
    88
          val (end_offset, subtrees) = ((offset, Nil: List[Markup_Tree]) /: body)(make_trees)
wenzelm@49467
    89
          val range = Text.Range(offset, end_offset)
wenzelm@49650
    90
          val entry = Entry(range, elems, merge_disjoint(subtrees))
wenzelm@49469
    91
          (end_offset, new Markup_Tree(Branches.empty, entry) :: markup_trees)
wenzelm@49466
    92
      }
wenzelm@49467
    93
    }
wenzelm@49466
    94
wenzelm@49467
    95
  def from_XML(body: XML.Body): Markup_Tree =
wenzelm@49467
    96
    merge_disjoint(((0, Nil: List[Markup_Tree]) /: body)(make_trees)._2)
wenzelm@38479
    97
}
immler@34554
    98
immler@34393
    99
wenzelm@49467
   100
final class Markup_Tree private(private val branches: Markup_Tree.Branches.T)
wenzelm@34717
   101
{
wenzelm@49417
   102
  import Markup_Tree._
wenzelm@49417
   103
wenzelm@45469
   104
  private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
wenzelm@45469
   105
    this(branches + (entry.range -> entry))
wenzelm@45469
   106
wenzelm@38563
   107
  override def toString =
wenzelm@38563
   108
    branches.toList.map(_._2) match {
wenzelm@38563
   109
      case Nil => "Empty"
wenzelm@38563
   110
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
   111
    }
wenzelm@38563
   112
wenzelm@45469
   113
  private def overlapping(range: Text.Range): Branches.T =
wenzelm@45457
   114
  {
wenzelm@45457
   115
    val start = Text.Range(range.start)
wenzelm@45457
   116
    val stop = Text.Range(range.stop)
wenzelm@45457
   117
    val bs = branches.range(start, stop)
wenzelm@45457
   118
    branches.get(stop) match {
wenzelm@45469
   119
      case Some(end) if range overlaps end.range => bs + (end.range -> end)
wenzelm@45457
   120
      case _ => bs
wenzelm@45457
   121
    }
wenzelm@45457
   122
  }
wenzelm@45457
   123
wenzelm@45469
   124
  def + (new_markup: Text.Markup): Markup_Tree =
wenzelm@34703
   125
  {
wenzelm@45469
   126
    val new_range = new_markup.range
wenzelm@45469
   127
wenzelm@38578
   128
    branches.get(new_range) match {
wenzelm@45473
   129
      case None => new Markup_Tree(branches, Entry(new_markup, empty))
wenzelm@45469
   130
      case Some(entry) =>
wenzelm@45469
   131
        if (entry.range == new_range)
wenzelm@45473
   132
          new Markup_Tree(branches, entry + new_markup.info)
wenzelm@45469
   133
        else if (entry.range.contains(new_range))
wenzelm@45469
   134
          new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
wenzelm@38578
   135
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@45473
   136
          new Markup_Tree(Branches.empty, Entry(new_markup, this))
wenzelm@38482
   137
        else {
wenzelm@45457
   138
          val body = overlapping(new_range)
wenzelm@49607
   139
          if (body.forall(e => new_range.contains(e._1)))
wenzelm@49607
   140
            new Markup_Tree(branches -- body.keys, Entry(new_markup, new Markup_Tree(body)))
wenzelm@49608
   141
          else {
wenzelm@49608
   142
            java.lang.System.err.println("Ignored overlapping markup information: " + new_markup +
wenzelm@48762
   143
              body.filter(e => !new_range.contains(e._1)).mkString("\n"))
wenzelm@38482
   144
            this
wenzelm@38482
   145
          }
wenzelm@38482
   146
        }
wenzelm@34703
   147
    }
wenzelm@34703
   148
  }
wenzelm@34703
   149
wenzelm@49614
   150
  def to_XML(root_range: Text.Range, text: CharSequence, filter: XML.Elem => Boolean): XML.Body =
wenzelm@49614
   151
  {
wenzelm@49614
   152
    def make_text(start: Text.Offset, stop: Text.Offset): XML.Body =
wenzelm@49614
   153
      if (start == stop) Nil
wenzelm@49614
   154
      else List(XML.Text(text.subSequence(start, stop).toString))
wenzelm@49614
   155
wenzelm@49614
   156
    def make_elems(rev_markups: List[XML.Elem], body: XML.Body): XML.Body =
wenzelm@49614
   157
      (body /: rev_markups) {
wenzelm@49650
   158
        case (b, elem) =>
wenzelm@49650
   159
          if (!filter(elem)) b
wenzelm@49650
   160
          else if (elem.body.isEmpty) List(XML.Elem(elem.markup, b))
wenzelm@49650
   161
          else List(XML.Wrapped_Elem(elem.markup, elem.body, b))
wenzelm@49614
   162
      }
wenzelm@49614
   163
wenzelm@49614
   164
    def make_body(elem_range: Text.Range, elem_markup: List[XML.Elem], entries: Branches.T)
wenzelm@49614
   165
      : XML.Body =
wenzelm@49614
   166
    {
wenzelm@49614
   167
      val body = new mutable.ListBuffer[XML.Tree]
wenzelm@49614
   168
      var last = elem_range.start
wenzelm@49614
   169
      for ((range, entry) <- entries) {
wenzelm@49614
   170
        val subrange = range.restrict(elem_range)
wenzelm@49614
   171
        body ++= make_text(last, subrange.start)
wenzelm@49614
   172
        body ++= make_body(subrange, entry.rev_markup, entry.subtree.overlapping(subrange))
wenzelm@49614
   173
        last = subrange.stop
wenzelm@49614
   174
      }
wenzelm@49614
   175
      body ++= make_text(last, elem_range.stop)
wenzelm@49614
   176
      make_elems(elem_markup, body.toList)
wenzelm@49614
   177
    }
wenzelm@49614
   178
   make_body(root_range, Nil, overlapping(root_range))
wenzelm@49614
   179
  }
wenzelm@49614
   180
wenzelm@49614
   181
  def to_XML(text: CharSequence): XML.Body =
wenzelm@49614
   182
    to_XML(Text.Range(0, text.length), text, (_: XML.Elem) => true)
wenzelm@49614
   183
wenzelm@46178
   184
  def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
wenzelm@46178
   185
    result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
wenzelm@45459
   186
  {
wenzelm@46178
   187
    def results(x: A, entry: Entry): Option[A] =
wenzelm@46178
   188
      if (result_elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
wenzelm@45474
   189
        val (y, changed) =
wenzelm@49614
   190
          ((x, false) /: entry.rev_markup)((res, info) =>  // FIXME proper order!?
wenzelm@45474
   191
            {
wenzelm@45474
   192
              val (y, changed) = res
wenzelm@46165
   193
              val arg = (y, Text.Info(entry.range, info))
wenzelm@46178
   194
              if (result.isDefinedAt(arg)) (result(arg), true)
wenzelm@45474
   195
              else res
wenzelm@45474
   196
            })
wenzelm@45474
   197
        if (changed) Some(y) else None
wenzelm@45474
   198
      }
wenzelm@45474
   199
      else None
wenzelm@45467
   200
wenzelm@45459
   201
    def stream(
wenzelm@45459
   202
      last: Text.Offset,
wenzelm@45469
   203
      stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
wenzelm@45459
   204
    {
wenzelm@45459
   205
      stack match {
wenzelm@45469
   206
        case (parent, (range, entry) #:: more) :: rest =>
wenzelm@45467
   207
          val subrange = range.restrict(root_range)
wenzelm@45469
   208
          val subtree = entry.subtree.overlapping(subrange).toStream
wenzelm@45459
   209
          val start = subrange.start
wenzelm@45459
   210
wenzelm@46178
   211
          results(parent.info, entry) match {
wenzelm@45469
   212
            case Some(res) =>
wenzelm@45469
   213
              val next = Text.Info(subrange, res)
wenzelm@45469
   214
              val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@45469
   215
              if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@45469
   216
              else nexts
wenzelm@45469
   217
            case None => stream(last, (parent, subtree #::: more) :: rest)
wenzelm@45459
   218
          }
wenzelm@45459
   219
wenzelm@45459
   220
        case (parent, Stream.Empty) :: rest =>
wenzelm@45459
   221
          val stop = parent.range.stop
wenzelm@45459
   222
          val nexts = stream(stop, rest)
wenzelm@45459
   223
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@45459
   224
          else nexts
wenzelm@45459
   225
wenzelm@45459
   226
        case Nil =>
wenzelm@45467
   227
          val stop = root_range.stop
wenzelm@45467
   228
          if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
wenzelm@45459
   229
          else Stream.empty
wenzelm@45459
   230
      }
wenzelm@45459
   231
    }
wenzelm@45467
   232
    stream(root_range.start,
wenzelm@45467
   233
      List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
wenzelm@45459
   234
  }
wenzelm@45459
   235
wenzelm@49465
   236
  def swing_tree(parent: DefaultMutableTreeNode,
wenzelm@49465
   237
    swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
wenzelm@38479
   238
  {
wenzelm@45469
   239
    for ((_, entry) <- branches) {
wenzelm@47539
   240
      val node = swing_node(Text.Info(entry.range, entry.markup))
wenzelm@49465
   241
      entry.subtree.swing_tree(node, swing_node)
wenzelm@49465
   242
      parent.add(node)
immler@34514
   243
    }
immler@34514
   244
  }
wenzelm@34717
   245
}
immler@34514
   246