src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Sep 20 10:43:04 2012 +0200 (2012-09-20)
changeset 49465 76ecbc7f3683
parent 49417 c5a8592fb5d3
child 49466 99ed1f422635
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@38479
    15
wenzelm@38479
    16
wenzelm@38479
    17
object Markup_Tree
wenzelm@38479
    18
{
wenzelm@45456
    19
  val empty: Markup_Tree = new Markup_Tree(Branches.empty)
wenzelm@38479
    20
wenzelm@45473
    21
  object Entry
wenzelm@45473
    22
  {
wenzelm@45473
    23
    def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
wenzelm@45474
    24
      Entry(markup.range, List(markup.info), Set(markup.info.markup.name), subtree)
wenzelm@45473
    25
  }
wenzelm@45473
    26
wenzelm@45474
    27
  sealed case class Entry(
wenzelm@45474
    28
    range: Text.Range,
wenzelm@45474
    29
    rev_markup: List[XML.Elem],
wenzelm@45474
    30
    elements: Set[String],
wenzelm@45474
    31
    subtree: Markup_Tree)
wenzelm@45469
    32
  {
wenzelm@45474
    33
    def + (info: XML.Elem): Entry =
wenzelm@45474
    34
      if (elements(info.markup.name)) copy(rev_markup = info :: rev_markup)
wenzelm@45474
    35
      else copy(rev_markup = info :: rev_markup, elements = elements + info.markup.name)
wenzelm@45474
    36
wenzelm@45470
    37
    def markup: List[XML.Elem] = rev_markup.reverse
wenzelm@49417
    38
wenzelm@49417
    39
    def reverse_markup: Entry =
wenzelm@49417
    40
      copy(rev_markup = rev_markup.reverse, subtree = subtree.reverse_markup)
wenzelm@45469
    41
  }
wenzelm@45469
    42
wenzelm@38479
    43
  object Branches
wenzelm@38479
    44
  {
wenzelm@38578
    45
    type T = SortedMap[Text.Range, Entry]
wenzelm@45456
    46
    val empty: T = SortedMap.empty(Text.Range.Ordering)
wenzelm@49417
    47
wenzelm@49417
    48
    def reverse_markup(branches: T): T =
wenzelm@49417
    49
      (empty /: branches.iterator) { case (bs, (r, entry)) => bs + (r -> entry.reverse_markup) }
wenzelm@38479
    50
  }
wenzelm@38479
    51
}
immler@34554
    52
immler@34393
    53
wenzelm@46712
    54
final class Markup_Tree private(branches: Markup_Tree.Branches.T)
wenzelm@34717
    55
{
wenzelm@49417
    56
  import Markup_Tree._
wenzelm@49417
    57
wenzelm@45469
    58
  private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
wenzelm@45469
    59
    this(branches + (entry.range -> entry))
wenzelm@45469
    60
wenzelm@49417
    61
  def reverse_markup: Markup_Tree = new Markup_Tree(Branches.reverse_markup(branches))
immler@34557
    62
wenzelm@38563
    63
  override def toString =
wenzelm@38563
    64
    branches.toList.map(_._2) match {
wenzelm@38563
    65
      case Nil => "Empty"
wenzelm@38563
    66
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
    67
    }
wenzelm@38563
    68
wenzelm@45469
    69
  private def overlapping(range: Text.Range): Branches.T =
wenzelm@45457
    70
  {
wenzelm@45457
    71
    val start = Text.Range(range.start)
wenzelm@45457
    72
    val stop = Text.Range(range.stop)
wenzelm@45457
    73
    val bs = branches.range(start, stop)
wenzelm@45457
    74
    // FIXME check after Scala 2.8.x
wenzelm@45457
    75
    branches.get(stop) match {
wenzelm@45469
    76
      case Some(end) if range overlaps end.range => bs + (end.range -> end)
wenzelm@45457
    77
      case _ => bs
wenzelm@45457
    78
    }
wenzelm@45457
    79
  }
wenzelm@45457
    80
wenzelm@45469
    81
  def + (new_markup: Text.Markup): Markup_Tree =
wenzelm@34703
    82
  {
wenzelm@45469
    83
    val new_range = new_markup.range
wenzelm@45469
    84
wenzelm@38578
    85
    branches.get(new_range) match {
wenzelm@45473
    86
      case None => new Markup_Tree(branches, Entry(new_markup, empty))
wenzelm@45469
    87
      case Some(entry) =>
wenzelm@45469
    88
        if (entry.range == new_range)
wenzelm@45473
    89
          new Markup_Tree(branches, entry + new_markup.info)
wenzelm@45469
    90
        else if (entry.range.contains(new_range))
wenzelm@45469
    91
          new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
wenzelm@38578
    92
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@45473
    93
          new Markup_Tree(Branches.empty, Entry(new_markup, this))
wenzelm@38482
    94
        else {
wenzelm@45457
    95
          val body = overlapping(new_range)
wenzelm@38578
    96
          if (body.forall(e => new_range.contains(e._1))) {
wenzelm@45469
    97
            val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME
wenzelm@38657
    98
              if (body.size > 1)
wenzelm@38657
    99
                (Branches.empty /: branches)((rest, entry) =>
wenzelm@38657
   100
                  if (body.isDefinedAt(entry._1)) rest else rest + entry)
wenzelm@38657
   101
              else branches
wenzelm@45473
   102
            new Markup_Tree(rest, Entry(new_markup, new Markup_Tree(body)))
wenzelm@38482
   103
          }
wenzelm@38482
   104
          else { // FIXME split markup!?
wenzelm@48762
   105
            System.err.println("Ignored overlapping markup information: " + new_markup +
wenzelm@48762
   106
              body.filter(e => !new_range.contains(e._1)).mkString("\n"))
wenzelm@38482
   107
            this
wenzelm@38482
   108
          }
wenzelm@38482
   109
        }
wenzelm@34703
   110
    }
wenzelm@34703
   111
  }
wenzelm@34703
   112
wenzelm@46178
   113
  def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
wenzelm@46178
   114
    result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
wenzelm@45459
   115
  {
wenzelm@46178
   116
    def results(x: A, entry: Entry): Option[A] =
wenzelm@46178
   117
      if (result_elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
wenzelm@45474
   118
        val (y, changed) =
wenzelm@45474
   119
          (entry.markup :\ (x, false))((info, res) =>
wenzelm@45474
   120
            {
wenzelm@45474
   121
              val (y, changed) = res
wenzelm@46165
   122
              val arg = (y, Text.Info(entry.range, info))
wenzelm@46178
   123
              if (result.isDefinedAt(arg)) (result(arg), true)
wenzelm@45474
   124
              else res
wenzelm@45474
   125
            })
wenzelm@45474
   126
        if (changed) Some(y) else None
wenzelm@45474
   127
      }
wenzelm@45474
   128
      else None
wenzelm@45467
   129
wenzelm@45459
   130
    def stream(
wenzelm@45459
   131
      last: Text.Offset,
wenzelm@45469
   132
      stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
wenzelm@45459
   133
    {
wenzelm@45459
   134
      stack match {
wenzelm@45469
   135
        case (parent, (range, entry) #:: more) :: rest =>
wenzelm@45467
   136
          val subrange = range.restrict(root_range)
wenzelm@45469
   137
          val subtree = entry.subtree.overlapping(subrange).toStream
wenzelm@45459
   138
          val start = subrange.start
wenzelm@45459
   139
wenzelm@46178
   140
          results(parent.info, entry) match {
wenzelm@45469
   141
            case Some(res) =>
wenzelm@45469
   142
              val next = Text.Info(subrange, res)
wenzelm@45469
   143
              val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@45469
   144
              if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@45469
   145
              else nexts
wenzelm@45469
   146
            case None => stream(last, (parent, subtree #::: more) :: rest)
wenzelm@45459
   147
          }
wenzelm@45459
   148
wenzelm@45459
   149
        case (parent, Stream.Empty) :: rest =>
wenzelm@45459
   150
          val stop = parent.range.stop
wenzelm@45459
   151
          val nexts = stream(stop, rest)
wenzelm@45459
   152
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@45459
   153
          else nexts
wenzelm@45459
   154
wenzelm@45459
   155
        case Nil =>
wenzelm@45467
   156
          val stop = root_range.stop
wenzelm@45467
   157
          if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
wenzelm@45459
   158
          else Stream.empty
wenzelm@45459
   159
      }
wenzelm@45459
   160
    }
wenzelm@45467
   161
    stream(root_range.start,
wenzelm@45467
   162
      List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
wenzelm@45459
   163
  }
wenzelm@45459
   164
wenzelm@49465
   165
  def swing_tree(parent: DefaultMutableTreeNode,
wenzelm@49465
   166
    swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
wenzelm@38479
   167
  {
wenzelm@45469
   168
    for ((_, entry) <- branches) {
wenzelm@47539
   169
      val node = swing_node(Text.Info(entry.range, entry.markup))
wenzelm@49465
   170
      entry.subtree.swing_tree(node, swing_node)
wenzelm@49465
   171
      parent.add(node)
immler@34514
   172
    }
immler@34514
   173
  }
wenzelm@34717
   174
}
immler@34514
   175