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