src/Pure/PIDE/markup_tree.scala
author wenzelm
Sat Jul 09 12:56:51 2011 +0200 (2011-07-09)
changeset 43714 3749d1e6dde9
parent 43520 cec9b95fa35d
child 44379 1079ab6b342b
permissions -rw-r--r--
tuned signature;
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@43520
    10
import java.lang.System
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@39178
    46
wenzelm@39178
    47
  type Select[A] = PartialFunction[Text.Info[Any], A]
wenzelm@38479
    48
}
immler@34554
    49
immler@34393
    50
wenzelm@43714
    51
sealed case class Markup_Tree(val branches: Markup_Tree.Branches.T)
wenzelm@34717
    52
{
wenzelm@38479
    53
  import Markup_Tree._
immler@34557
    54
wenzelm@38563
    55
  override def toString =
wenzelm@38563
    56
    branches.toList.map(_._2) match {
wenzelm@38563
    57
      case Nil => "Empty"
wenzelm@38563
    58
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
    59
    }
wenzelm@38563
    60
wenzelm@38577
    61
  def + (new_info: Text.Info[Any]): Markup_Tree =
wenzelm@34703
    62
  {
wenzelm@38578
    63
    val new_range = new_info.range
wenzelm@38578
    64
    branches.get(new_range) match {
wenzelm@38482
    65
      case None =>
wenzelm@38577
    66
        new Markup_Tree(Branches.update(branches, new_info -> empty))
wenzelm@38577
    67
      case Some((info, subtree)) =>
wenzelm@38578
    68
        val range = info.range
wenzelm@38661
    69
        if (range.contains(new_range))
wenzelm@38577
    70
          new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
wenzelm@38578
    71
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@38657
    72
          new Markup_Tree(Branches.single(new_info -> this))
wenzelm@38482
    73
        else {
wenzelm@38578
    74
          val body = Branches.overlapping(new_range, branches)
wenzelm@38578
    75
          if (body.forall(e => new_range.contains(e._1))) {
wenzelm@38659
    76
            val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
wenzelm@38657
    77
              if (body.size > 1)
wenzelm@38657
    78
                (Branches.empty /: branches)((rest, entry) =>
wenzelm@38657
    79
                  if (body.isDefinedAt(entry._1)) rest else rest + entry)
wenzelm@38657
    80
              else branches
wenzelm@38577
    81
            new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
wenzelm@38482
    82
          }
wenzelm@38482
    83
          else { // FIXME split markup!?
wenzelm@38577
    84
            System.err.println("Ignored overlapping markup information: " + new_info)
wenzelm@38482
    85
            this
wenzelm@38482
    86
          }
wenzelm@38482
    87
        }
wenzelm@34703
    88
    }
wenzelm@34703
    89
  }
wenzelm@34703
    90
wenzelm@38659
    91
  private def overlapping(range: Text.Range): Stream[(Text.Range, Branches.Entry)] =
wenzelm@38659
    92
    Branches.overlapping(range, branches).toStream
wenzelm@38659
    93
wenzelm@39178
    94
  def select[A](root_range: Text.Range)(result: Markup_Tree.Select[A])
wenzelm@39177
    95
    : Stream[Text.Info[Option[A]]] =
wenzelm@38486
    96
  {
wenzelm@39177
    97
    def stream(
wenzelm@39177
    98
      last: Text.Offset,
wenzelm@39177
    99
      stack: List[(Text.Info[Option[A]], Stream[(Text.Range, Branches.Entry)])])
wenzelm@39177
   100
        : Stream[Text.Info[Option[A]]] =
wenzelm@38486
   101
    {
wenzelm@38659
   102
      stack match {
wenzelm@38659
   103
        case (parent, (range, (info, tree)) #:: more) :: rest =>
wenzelm@38659
   104
          val subrange = range.restrict(root_range)
wenzelm@38659
   105
          val subtree = tree.overlapping(subrange)
wenzelm@38659
   106
          val start = subrange.start
wenzelm@38486
   107
wenzelm@38659
   108
          if (result.isDefinedAt(info)) {
wenzelm@39177
   109
            val next = Text.Info[Option[A]](subrange, Some(result(info)))
wenzelm@38659
   110
            val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@38659
   111
            if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@38659
   112
            else nexts
wenzelm@38659
   113
          }
wenzelm@38659
   114
          else stream(last, (parent, subtree #::: more) :: rest)
wenzelm@38659
   115
wenzelm@38659
   116
        case (parent, Stream.Empty) :: rest =>
wenzelm@38659
   117
          val stop = parent.range.stop
wenzelm@38659
   118
          val nexts = stream(stop, rest)
wenzelm@38659
   119
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@38659
   120
          else nexts
wenzelm@38659
   121
wenzelm@38726
   122
        case Nil =>
wenzelm@38726
   123
          val stop = root_range.stop
wenzelm@39177
   124
          if (last < stop) Stream(Text.Info(Text.Range(last, stop), None))
wenzelm@38726
   125
          else Stream.empty
wenzelm@38659
   126
      }
wenzelm@38486
   127
    }
wenzelm@39177
   128
    stream(root_range.start, List((Text.Info(root_range, None), overlapping(root_range))))
wenzelm@38486
   129
  }
wenzelm@38486
   130
wenzelm@38577
   131
  def swing_tree(parent: DefaultMutableTreeNode)
wenzelm@38577
   132
    (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
wenzelm@38479
   133
  {
wenzelm@38577
   134
    for ((_, (info, subtree)) <- branches) {
wenzelm@38577
   135
      val current = swing_node(info)
wenzelm@38479
   136
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   137
      parent.add(current)
immler@34514
   138
    }
immler@34514
   139
  }
wenzelm@34717
   140
}
immler@34514
   141