src/Pure/PIDE/markup_tree.scala
author wenzelm
Sat Nov 12 11:45:49 2011 +0100 (2011-11-12)
changeset 45467 3f290b6288cf
parent 45459 a5c1599f664d
child 45468 33e946d3f449
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@45467
    18
  sealed case class Cumulate[A](info: A, result: PartialFunction[(A, Text.Markup), A])
wenzelm@45456
    19
  type Select[A] = PartialFunction[Text.Markup, A]
wenzelm@45456
    20
wenzelm@45456
    21
  val empty: Markup_Tree = new Markup_Tree(Branches.empty)
wenzelm@38479
    22
wenzelm@38479
    23
  object Branches
wenzelm@38479
    24
  {
wenzelm@45455
    25
    type Entry = (Text.Markup, Markup_Tree)
wenzelm@38578
    26
    type T = SortedMap[Text.Range, Entry]
wenzelm@38479
    27
wenzelm@45456
    28
    val empty: T = SortedMap.empty(Text.Range.Ordering)
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@38479
    31
  }
wenzelm@38479
    32
}
immler@34554
    33
immler@34393
    34
wenzelm@45456
    35
class Markup_Tree private(branches: Markup_Tree.Branches.T)
wenzelm@34717
    36
{
wenzelm@38479
    37
  import Markup_Tree._
immler@34557
    38
wenzelm@38563
    39
  override def toString =
wenzelm@38563
    40
    branches.toList.map(_._2) match {
wenzelm@38563
    41
      case Nil => "Empty"
wenzelm@38563
    42
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
    43
    }
wenzelm@38563
    44
wenzelm@45457
    45
  private def overlapping(range: Text.Range): Branches.T =  // FIXME special cases!?
wenzelm@45457
    46
  {
wenzelm@45457
    47
    val start = Text.Range(range.start)
wenzelm@45457
    48
    val stop = Text.Range(range.stop)
wenzelm@45457
    49
    val bs = branches.range(start, stop)
wenzelm@45457
    50
    // FIXME check after Scala 2.8.x
wenzelm@45457
    51
    branches.get(stop) match {
wenzelm@45457
    52
      case Some(end) if range overlaps end._1.range => Branches.update(bs, end)
wenzelm@45457
    53
      case _ => bs
wenzelm@45457
    54
    }
wenzelm@45457
    55
  }
wenzelm@45457
    56
wenzelm@45455
    57
  def + (new_info: Text.Markup): Markup_Tree =
wenzelm@34703
    58
  {
wenzelm@38578
    59
    val new_range = new_info.range
wenzelm@38578
    60
    branches.get(new_range) match {
wenzelm@38482
    61
      case None =>
wenzelm@38577
    62
        new Markup_Tree(Branches.update(branches, new_info -> empty))
wenzelm@38577
    63
      case Some((info, subtree)) =>
wenzelm@38578
    64
        val range = info.range
wenzelm@38661
    65
        if (range.contains(new_range))
wenzelm@38577
    66
          new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
wenzelm@38578
    67
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@38657
    68
          new Markup_Tree(Branches.single(new_info -> this))
wenzelm@38482
    69
        else {
wenzelm@45457
    70
          val body = overlapping(new_range)
wenzelm@38578
    71
          if (body.forall(e => new_range.contains(e._1))) {
wenzelm@38659
    72
            val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
wenzelm@38657
    73
              if (body.size > 1)
wenzelm@38657
    74
                (Branches.empty /: branches)((rest, entry) =>
wenzelm@38657
    75
                  if (body.isDefinedAt(entry._1)) rest else rest + entry)
wenzelm@38657
    76
              else branches
wenzelm@38577
    77
            new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
wenzelm@38482
    78
          }
wenzelm@38482
    79
          else { // FIXME split markup!?
wenzelm@38577
    80
            System.err.println("Ignored overlapping markup information: " + new_info)
wenzelm@38482
    81
            this
wenzelm@38482
    82
          }
wenzelm@38482
    83
        }
wenzelm@34703
    84
    }
wenzelm@34703
    85
  }
wenzelm@34703
    86
wenzelm@45467
    87
  def cumulate[A](root_range: Text.Range)(body: Cumulate[A]): Stream[Text.Info[A]] =
wenzelm@45459
    88
  {
wenzelm@45467
    89
    val root_info = body.info
wenzelm@45467
    90
    val result = body.result
wenzelm@45467
    91
wenzelm@45459
    92
    def stream(
wenzelm@45459
    93
      last: Text.Offset,
wenzelm@45459
    94
      stack: List[(Text.Info[A], Stream[(Text.Range, Branches.Entry)])]): Stream[Text.Info[A]] =
wenzelm@45459
    95
    {
wenzelm@45459
    96
      stack match {
wenzelm@45459
    97
        case (parent, (range, (info, tree)) #:: more) :: rest =>
wenzelm@45467
    98
          val subrange = range.restrict(root_range)
wenzelm@45459
    99
          val subtree = tree.overlapping(subrange).toStream
wenzelm@45459
   100
          val start = subrange.start
wenzelm@45459
   101
wenzelm@45459
   102
          val arg = (parent.info, info)
wenzelm@45459
   103
          if (result.isDefinedAt(arg)) {
wenzelm@45459
   104
            val next = Text.Info(subrange, result(arg))
wenzelm@45459
   105
            val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@45459
   106
            if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@45459
   107
            else nexts
wenzelm@45459
   108
          }
wenzelm@45459
   109
          else stream(last, (parent, subtree #::: more) :: rest)
wenzelm@45459
   110
wenzelm@45459
   111
        case (parent, Stream.Empty) :: rest =>
wenzelm@45459
   112
          val stop = parent.range.stop
wenzelm@45459
   113
          val nexts = stream(stop, rest)
wenzelm@45459
   114
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@45459
   115
          else nexts
wenzelm@45459
   116
wenzelm@45459
   117
        case Nil =>
wenzelm@45467
   118
          val stop = root_range.stop
wenzelm@45467
   119
          if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
wenzelm@45459
   120
          else Stream.empty
wenzelm@45459
   121
      }
wenzelm@45459
   122
    }
wenzelm@45467
   123
    stream(root_range.start,
wenzelm@45467
   124
      List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
wenzelm@45459
   125
  }
wenzelm@45459
   126
wenzelm@45457
   127
  def select[A](root_range: Text.Range)(result: Select[A]): Stream[Text.Info[Option[A]]] =
wenzelm@38486
   128
  {
wenzelm@39177
   129
    def stream(
wenzelm@39177
   130
      last: Text.Offset,
wenzelm@39177
   131
      stack: List[(Text.Info[Option[A]], Stream[(Text.Range, Branches.Entry)])])
wenzelm@39177
   132
        : Stream[Text.Info[Option[A]]] =
wenzelm@38486
   133
    {
wenzelm@38659
   134
      stack match {
wenzelm@38659
   135
        case (parent, (range, (info, tree)) #:: more) :: rest =>
wenzelm@38659
   136
          val subrange = range.restrict(root_range)
wenzelm@45457
   137
          val subtree = tree.overlapping(subrange).toStream
wenzelm@38659
   138
          val start = subrange.start
wenzelm@38486
   139
wenzelm@38659
   140
          if (result.isDefinedAt(info)) {
wenzelm@39177
   141
            val next = Text.Info[Option[A]](subrange, Some(result(info)))
wenzelm@38659
   142
            val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@38659
   143
            if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@38659
   144
            else nexts
wenzelm@38659
   145
          }
wenzelm@38659
   146
          else stream(last, (parent, subtree #::: more) :: rest)
wenzelm@38659
   147
wenzelm@38659
   148
        case (parent, Stream.Empty) :: rest =>
wenzelm@38659
   149
          val stop = parent.range.stop
wenzelm@38659
   150
          val nexts = stream(stop, rest)
wenzelm@38659
   151
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@38659
   152
          else nexts
wenzelm@38659
   153
wenzelm@38726
   154
        case Nil =>
wenzelm@38726
   155
          val stop = root_range.stop
wenzelm@39177
   156
          if (last < stop) Stream(Text.Info(Text.Range(last, stop), None))
wenzelm@38726
   157
          else Stream.empty
wenzelm@38659
   158
      }
wenzelm@38486
   159
    }
wenzelm@45457
   160
    stream(root_range.start,
wenzelm@45457
   161
      List((Text.Info(root_range, None), overlapping(root_range).toStream)))
wenzelm@38486
   162
  }
wenzelm@38486
   163
wenzelm@38577
   164
  def swing_tree(parent: DefaultMutableTreeNode)
wenzelm@45455
   165
    (swing_node: Text.Markup => DefaultMutableTreeNode)
wenzelm@38479
   166
  {
wenzelm@38577
   167
    for ((_, (info, subtree)) <- branches) {
wenzelm@38577
   168
      val current = swing_node(info)
wenzelm@38479
   169
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   170
      parent.add(current)
immler@34514
   171
    }
immler@34514
   172
  }
wenzelm@34717
   173
}
immler@34514
   174