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