src/Pure/PIDE/markup_tree.scala
author wenzelm
Tue Aug 24 21:34:38 2010 +0200 (2010-08-24)
changeset 38661 f1ba2ae8e58a
parent 38659 afac51977705
child 38726 6d5f9af42eca
permissions -rw-r--r--
Markup_Tree.+: new info tends to sink to bottom, where it is prefered by select;
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@38661
    67
        if (range.contains(new_range))
wenzelm@38577
    68
          new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
wenzelm@38578
    69
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@38657
    70
          new Markup_Tree(Branches.single(new_info -> this))
wenzelm@38482
    71
        else {
wenzelm@38578
    72
          val body = Branches.overlapping(new_range, branches)
wenzelm@38578
    73
          if (body.forall(e => new_range.contains(e._1))) {
wenzelm@38659
    74
            val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
wenzelm@38657
    75
              if (body.size > 1)
wenzelm@38657
    76
                (Branches.empty /: branches)((rest, entry) =>
wenzelm@38657
    77
                  if (body.isDefinedAt(entry._1)) rest else rest + entry)
wenzelm@38657
    78
              else branches
wenzelm@38577
    79
            new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
wenzelm@38482
    80
          }
wenzelm@38482
    81
          else { // FIXME split markup!?
wenzelm@38577
    82
            System.err.println("Ignored overlapping markup information: " + new_info)
wenzelm@38482
    83
            this
wenzelm@38482
    84
          }
wenzelm@38482
    85
        }
wenzelm@34703
    86
    }
wenzelm@34703
    87
  }
wenzelm@34703
    88
wenzelm@38659
    89
  private def overlapping(range: Text.Range): Stream[(Text.Range, Branches.Entry)] =
wenzelm@38659
    90
    Branches.overlapping(range, branches).toStream
wenzelm@38659
    91
wenzelm@38582
    92
  def select[A](root_range: Text.Range)
wenzelm@38582
    93
    (result: PartialFunction[Text.Info[Any], A])(default: A): Stream[Text.Info[A]] =
wenzelm@38486
    94
  {
wenzelm@38659
    95
    def stream(last: Text.Offset, stack: List[(Text.Info[A], Stream[(Text.Range, Branches.Entry)])])
wenzelm@38659
    96
      : Stream[Text.Info[A]] =
wenzelm@38486
    97
    {
wenzelm@38659
    98
      stack match {
wenzelm@38659
    99
        case (parent, (range, (info, tree)) #:: more) :: rest =>
wenzelm@38659
   100
          val subrange = range.restrict(root_range)
wenzelm@38659
   101
          val subtree = tree.overlapping(subrange)
wenzelm@38659
   102
          val start = subrange.start
wenzelm@38486
   103
wenzelm@38659
   104
          if (result.isDefinedAt(info)) {
wenzelm@38659
   105
            val next = Text.Info(subrange, result(info))
wenzelm@38659
   106
            val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@38659
   107
            if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@38659
   108
            else nexts
wenzelm@38659
   109
          }
wenzelm@38659
   110
          else stream(last, (parent, subtree #::: more) :: rest)
wenzelm@38659
   111
wenzelm@38659
   112
        case (parent, Stream.Empty) :: rest =>
wenzelm@38659
   113
          val stop = parent.range.stop
wenzelm@38659
   114
          val nexts = stream(stop, rest)
wenzelm@38659
   115
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@38659
   116
          else nexts
wenzelm@38659
   117
wenzelm@38659
   118
        case Nil => Stream(Text.Info(Text.Range(last, root_range.stop), default))
wenzelm@38659
   119
      }
wenzelm@38486
   120
    }
wenzelm@38659
   121
    stream(root_range.start, List((Text.Info(root_range, default), overlapping(root_range))))
wenzelm@38486
   122
  }
wenzelm@38486
   123
wenzelm@38577
   124
  def swing_tree(parent: DefaultMutableTreeNode)
wenzelm@38577
   125
    (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
wenzelm@38479
   126
  {
wenzelm@38577
   127
    for ((_, (info, subtree)) <- branches) {
wenzelm@38577
   128
      val current = swing_node(info)
wenzelm@38479
   129
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   130
      parent.add(current)
immler@34514
   131
    }
immler@34514
   132
  }
wenzelm@34717
   133
}
immler@34514
   134