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