src/Pure/PIDE/markup_tree.scala
author wenzelm
Tue Aug 24 21:20:08 2010 +0200 (2010-08-24)
changeset 38659 afac51977705
parent 38657 2e0ebdaac59b
child 38661 f1ba2ae8e58a
permissions -rw-r--r--
Markup_Tree.select: more straight-forward recursion producing one main stream, avoid fragmentation of parent info due to ignored subtree;
tuned;
wenzelm@38479
     1
/*  Title:      Pure/PIDE/markup_tree.scala
wenzelm@36676
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36676
     3
    Author:     Makarius
wenzelm@36676
     4
wenzelm@38479
     5
Markup trees over nested / non-overlapping text ranges.
wenzelm@36676
     6
*/
immler@34393
     7
wenzelm@34871
     8
package isabelle
immler@34393
     9
wenzelm@34701
    10
wenzelm@34701
    11
import javax.swing.tree.DefaultMutableTreeNode
wenzelm@34701
    12
wenzelm@38479
    13
import scala.collection.immutable.SortedMap
wenzelm@38479
    14
wenzelm@38479
    15
wenzelm@38479
    16
object Markup_Tree
wenzelm@38479
    17
{
wenzelm@38566
    18
  /* branches sorted by quasi-order -- overlapping ranges appear as equivalent */
wenzelm@38479
    19
wenzelm@38479
    20
  object Branches
wenzelm@38479
    21
  {
wenzelm@38577
    22
    type Entry = (Text.Info[Any], Markup_Tree)
wenzelm@38578
    23
    type T = SortedMap[Text.Range, Entry]
wenzelm@38479
    24
wenzelm@38578
    25
    val empty = SortedMap.empty[Text.Range, Entry](new scala.math.Ordering[Text.Range]
wenzelm@38479
    26
      {
wenzelm@38578
    27
        def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
wenzelm@38479
    28
      })
wenzelm@38562
    29
wenzelm@38578
    30
    def update(branches: T, entry: Entry): T = branches + (entry._1.range -> entry)
wenzelm@38657
    31
    def single(entry: Entry): T = update(empty, entry)
wenzelm@38562
    32
wenzelm@38659
    33
    def overlapping(range: Text.Range, branches: T): T =  // FIXME special cases!?
wenzelm@38566
    34
    {
wenzelm@38578
    35
      val start = Text.Range(range.start)
wenzelm@38578
    36
      val stop = Text.Range(range.stop)
wenzelm@38659
    37
      val bs = branches.range(start, stop)
wenzelm@38566
    38
      branches.get(stop) match {
wenzelm@38659
    39
        case Some(end) if range overlaps end._1.range => update(bs, end)
wenzelm@38659
    40
        case _ => bs
wenzelm@38566
    41
      }
wenzelm@38566
    42
    }
wenzelm@38479
    43
  }
wenzelm@38479
    44
wenzelm@38479
    45
  val empty = new Markup_Tree(Branches.empty)
wenzelm@38479
    46
}
immler@34554
    47
immler@34393
    48
wenzelm@38479
    49
case class Markup_Tree(val branches: Markup_Tree.Branches.T)
wenzelm@34717
    50
{
wenzelm@38479
    51
  import Markup_Tree._
immler@34557
    52
wenzelm@38563
    53
  override def toString =
wenzelm@38563
    54
    branches.toList.map(_._2) match {
wenzelm@38563
    55
      case Nil => "Empty"
wenzelm@38563
    56
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
    57
    }
wenzelm@38563
    58
wenzelm@38577
    59
  def + (new_info: Text.Info[Any]): Markup_Tree =
wenzelm@34703
    60
  {
wenzelm@38578
    61
    val new_range = new_info.range
wenzelm@38578
    62
    branches.get(new_range) match {
wenzelm@38482
    63
      case None =>
wenzelm@38577
    64
        new Markup_Tree(Branches.update(branches, new_info -> empty))
wenzelm@38577
    65
      case Some((info, subtree)) =>
wenzelm@38578
    66
        val range = info.range
wenzelm@38659
    67
        if (range == new_range)  // FIXME append, not cons (!??)
wenzelm@38657
    68
          new Markup_Tree(Branches.update(branches,
wenzelm@38657
    69
            new_info -> new Markup_Tree(Branches.single((info, subtree)))))
wenzelm@38657
    70
        else if (range.contains(new_range))
wenzelm@38577
    71
          new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
wenzelm@38578
    72
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@38657
    73
          new Markup_Tree(Branches.single(new_info -> this))
wenzelm@38482
    74
        else {
wenzelm@38578
    75
          val body = Branches.overlapping(new_range, branches)
wenzelm@38578
    76
          if (body.forall(e => new_range.contains(e._1))) {
wenzelm@38659
    77
            val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
wenzelm@38657
    78
              if (body.size > 1)
wenzelm@38657
    79
                (Branches.empty /: branches)((rest, entry) =>
wenzelm@38657
    80
                  if (body.isDefinedAt(entry._1)) rest else rest + entry)
wenzelm@38657
    81
              else branches
wenzelm@38577
    82
            new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
wenzelm@38482
    83
          }
wenzelm@38482
    84
          else { // FIXME split markup!?
wenzelm@38577
    85
            System.err.println("Ignored overlapping markup information: " + new_info)
wenzelm@38482
    86
            this
wenzelm@38482
    87
          }
wenzelm@38482
    88
        }
wenzelm@34703
    89
    }
wenzelm@34703
    90
  }
wenzelm@34703
    91
wenzelm@38659
    92
  private def overlapping(range: Text.Range): Stream[(Text.Range, Branches.Entry)] =
wenzelm@38659
    93
    Branches.overlapping(range, branches).toStream
wenzelm@38659
    94
wenzelm@38582
    95
  def select[A](root_range: Text.Range)
wenzelm@38582
    96
    (result: PartialFunction[Text.Info[Any], A])(default: A): Stream[Text.Info[A]] =
wenzelm@38486
    97
  {
wenzelm@38659
    98
    def stream(last: Text.Offset, stack: List[(Text.Info[A], Stream[(Text.Range, Branches.Entry)])])
wenzelm@38659
    99
      : Stream[Text.Info[A]] =
wenzelm@38486
   100
    {
wenzelm@38659
   101
      stack match {
wenzelm@38659
   102
        case (parent, (range, (info, tree)) #:: more) :: rest =>
wenzelm@38659
   103
          val subrange = range.restrict(root_range)
wenzelm@38659
   104
          val subtree = tree.overlapping(subrange)
wenzelm@38659
   105
          val start = subrange.start
wenzelm@38486
   106
wenzelm@38659
   107
          if (result.isDefinedAt(info)) {
wenzelm@38659
   108
            val next = Text.Info(subrange, result(info))
wenzelm@38659
   109
            val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@38659
   110
            if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@38659
   111
            else nexts
wenzelm@38659
   112
          }
wenzelm@38659
   113
          else stream(last, (parent, subtree #::: more) :: rest)
wenzelm@38659
   114
wenzelm@38659
   115
        case (parent, Stream.Empty) :: rest =>
wenzelm@38659
   116
          val stop = parent.range.stop
wenzelm@38659
   117
          val nexts = stream(stop, rest)
wenzelm@38659
   118
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@38659
   119
          else nexts
wenzelm@38659
   120
wenzelm@38659
   121
        case Nil => Stream(Text.Info(Text.Range(last, root_range.stop), default))
wenzelm@38659
   122
      }
wenzelm@38486
   123
    }
wenzelm@38659
   124
    stream(root_range.start, List((Text.Info(root_range, default), overlapping(root_range))))
wenzelm@38486
   125
  }
wenzelm@38486
   126
wenzelm@38577
   127
  def swing_tree(parent: DefaultMutableTreeNode)
wenzelm@38577
   128
    (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
wenzelm@38479
   129
  {
wenzelm@38577
   130
    for ((_, (info, subtree)) <- branches) {
wenzelm@38577
   131
      val current = swing_node(info)
wenzelm@38479
   132
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   133
      parent.add(current)
immler@34514
   134
    }
immler@34514
   135
  }
wenzelm@34717
   136
}
immler@34514
   137