src/Pure/PIDE/markup_tree.scala
author wenzelm
Mon Jan 09 23:09:03 2012 +0100 (2012-01-09)
changeset 46165 0e131ca93a49
parent 45673 cd41e3903fbf
child 46178 1c5c88f6feb5
permissions -rw-r--r--
proper cumulation of bulk arguments;
wenzelm@38479
     1
/*  Title:      Pure/PIDE/markup_tree.scala
wenzelm@45673
     2
    Module:     PIDE
wenzelm@36676
     3
    Author:     Fabian Immler, TU Munich
wenzelm@36676
     4
    Author:     Makarius
wenzelm@36676
     5
wenzelm@38479
     6
Markup trees over nested / non-overlapping text ranges.
wenzelm@36676
     7
*/
immler@34393
     8
wenzelm@34871
     9
package isabelle
immler@34393
    10
wenzelm@43520
    11
import java.lang.System
wenzelm@34701
    12
import javax.swing.tree.DefaultMutableTreeNode
wenzelm@34701
    13
wenzelm@38479
    14
import scala.collection.immutable.SortedMap
wenzelm@38479
    15
wenzelm@38479
    16
wenzelm@38479
    17
object Markup_Tree
wenzelm@38479
    18
{
wenzelm@45474
    19
  sealed case class Cumulate[A](
wenzelm@45474
    20
    info: A, result: PartialFunction[(A, Text.Markup), A], elements: Option[Set[String]])
wenzelm@45474
    21
  sealed case class Select[A](
wenzelm@45474
    22
    result: PartialFunction[Text.Markup, A], elements: Option[Set[String]])
wenzelm@45456
    23
wenzelm@45456
    24
  val empty: Markup_Tree = new Markup_Tree(Branches.empty)
wenzelm@38479
    25
wenzelm@45473
    26
  object Entry
wenzelm@45473
    27
  {
wenzelm@45473
    28
    def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
wenzelm@45474
    29
      Entry(markup.range, List(markup.info), Set(markup.info.markup.name), subtree)
wenzelm@45473
    30
  }
wenzelm@45473
    31
wenzelm@45474
    32
  sealed case class Entry(
wenzelm@45474
    33
    range: Text.Range,
wenzelm@45474
    34
    rev_markup: List[XML.Elem],
wenzelm@45474
    35
    elements: Set[String],
wenzelm@45474
    36
    subtree: Markup_Tree)
wenzelm@45469
    37
  {
wenzelm@45474
    38
    def + (info: XML.Elem): Entry =
wenzelm@45474
    39
      if (elements(info.markup.name)) copy(rev_markup = info :: rev_markup)
wenzelm@45474
    40
      else copy(rev_markup = info :: rev_markup, elements = elements + info.markup.name)
wenzelm@45474
    41
wenzelm@45470
    42
    def markup: List[XML.Elem] = rev_markup.reverse
wenzelm@45469
    43
  }
wenzelm@45469
    44
wenzelm@38479
    45
  object Branches
wenzelm@38479
    46
  {
wenzelm@38578
    47
    type T = SortedMap[Text.Range, Entry]
wenzelm@45456
    48
    val empty: T = SortedMap.empty(Text.Range.Ordering)
wenzelm@38479
    49
  }
wenzelm@38479
    50
}
immler@34554
    51
immler@34393
    52
wenzelm@45456
    53
class Markup_Tree private(branches: Markup_Tree.Branches.T)
wenzelm@34717
    54
{
wenzelm@45469
    55
  private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
wenzelm@45469
    56
    this(branches + (entry.range -> entry))
wenzelm@45469
    57
wenzelm@45469
    58
wenzelm@38479
    59
  import Markup_Tree._
immler@34557
    60
wenzelm@38563
    61
  override def toString =
wenzelm@38563
    62
    branches.toList.map(_._2) match {
wenzelm@38563
    63
      case Nil => "Empty"
wenzelm@38563
    64
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
    65
    }
wenzelm@38563
    66
wenzelm@45469
    67
  private def overlapping(range: Text.Range): Branches.T =
wenzelm@45457
    68
  {
wenzelm@45457
    69
    val start = Text.Range(range.start)
wenzelm@45457
    70
    val stop = Text.Range(range.stop)
wenzelm@45457
    71
    val bs = branches.range(start, stop)
wenzelm@45457
    72
    // FIXME check after Scala 2.8.x
wenzelm@45457
    73
    branches.get(stop) match {
wenzelm@45469
    74
      case Some(end) if range overlaps end.range => bs + (end.range -> end)
wenzelm@45457
    75
      case _ => bs
wenzelm@45457
    76
    }
wenzelm@45457
    77
  }
wenzelm@45457
    78
wenzelm@45469
    79
  def + (new_markup: Text.Markup): Markup_Tree =
wenzelm@34703
    80
  {
wenzelm@45469
    81
    val new_range = new_markup.range
wenzelm@45469
    82
wenzelm@38578
    83
    branches.get(new_range) match {
wenzelm@45473
    84
      case None => new Markup_Tree(branches, Entry(new_markup, empty))
wenzelm@45469
    85
      case Some(entry) =>
wenzelm@45469
    86
        if (entry.range == new_range)
wenzelm@45473
    87
          new Markup_Tree(branches, entry + new_markup.info)
wenzelm@45469
    88
        else if (entry.range.contains(new_range))
wenzelm@45469
    89
          new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
wenzelm@38578
    90
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@45473
    91
          new Markup_Tree(Branches.empty, Entry(new_markup, this))
wenzelm@38482
    92
        else {
wenzelm@45457
    93
          val body = overlapping(new_range)
wenzelm@38578
    94
          if (body.forall(e => new_range.contains(e._1))) {
wenzelm@45469
    95
            val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME
wenzelm@38657
    96
              if (body.size > 1)
wenzelm@38657
    97
                (Branches.empty /: branches)((rest, entry) =>
wenzelm@38657
    98
                  if (body.isDefinedAt(entry._1)) rest else rest + entry)
wenzelm@38657
    99
              else branches
wenzelm@45473
   100
            new Markup_Tree(rest, Entry(new_markup, new Markup_Tree(body)))
wenzelm@38482
   101
          }
wenzelm@38482
   102
          else { // FIXME split markup!?
wenzelm@45469
   103
            System.err.println("Ignored overlapping markup information: " + new_markup)
wenzelm@38482
   104
            this
wenzelm@38482
   105
          }
wenzelm@38482
   106
        }
wenzelm@34703
   107
    }
wenzelm@34703
   108
  }
wenzelm@34703
   109
wenzelm@45467
   110
  def cumulate[A](root_range: Text.Range)(body: Cumulate[A]): Stream[Text.Info[A]] =
wenzelm@45459
   111
  {
wenzelm@45467
   112
    val root_info = body.info
wenzelm@45469
   113
wenzelm@45469
   114
    def result(x: A, entry: Entry): Option[A] =
wenzelm@45474
   115
      if (body.elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
wenzelm@45474
   116
        val (y, changed) =
wenzelm@45474
   117
          (entry.markup :\ (x, false))((info, res) =>
wenzelm@45474
   118
            {
wenzelm@45474
   119
              val (y, changed) = res
wenzelm@46165
   120
              val arg = (y, Text.Info(entry.range, info))
wenzelm@45474
   121
              if (body.result.isDefinedAt(arg)) (body.result(arg), true)
wenzelm@45474
   122
              else res
wenzelm@45474
   123
            })
wenzelm@45474
   124
        if (changed) Some(y) else None
wenzelm@45474
   125
      }
wenzelm@45474
   126
      else None
wenzelm@45467
   127
wenzelm@45459
   128
    def stream(
wenzelm@45459
   129
      last: Text.Offset,
wenzelm@45469
   130
      stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
wenzelm@45459
   131
    {
wenzelm@45459
   132
      stack match {
wenzelm@45469
   133
        case (parent, (range, entry) #:: more) :: rest =>
wenzelm@45467
   134
          val subrange = range.restrict(root_range)
wenzelm@45469
   135
          val subtree = entry.subtree.overlapping(subrange).toStream
wenzelm@45459
   136
          val start = subrange.start
wenzelm@45459
   137
wenzelm@45469
   138
          result(parent.info, entry) match {
wenzelm@45469
   139
            case Some(res) =>
wenzelm@45469
   140
              val next = Text.Info(subrange, res)
wenzelm@45469
   141
              val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@45469
   142
              if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@45469
   143
              else nexts
wenzelm@45469
   144
            case None => stream(last, (parent, subtree #::: more) :: rest)
wenzelm@45459
   145
          }
wenzelm@45459
   146
wenzelm@45459
   147
        case (parent, Stream.Empty) :: rest =>
wenzelm@45459
   148
          val stop = parent.range.stop
wenzelm@45459
   149
          val nexts = stream(stop, rest)
wenzelm@45459
   150
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@45459
   151
          else nexts
wenzelm@45459
   152
wenzelm@45459
   153
        case Nil =>
wenzelm@45467
   154
          val stop = root_range.stop
wenzelm@45467
   155
          if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
wenzelm@45459
   156
          else Stream.empty
wenzelm@45459
   157
      }
wenzelm@45459
   158
    }
wenzelm@45467
   159
    stream(root_range.start,
wenzelm@45467
   160
      List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
wenzelm@45459
   161
  }
wenzelm@45459
   162
wenzelm@38577
   163
  def swing_tree(parent: DefaultMutableTreeNode)
wenzelm@45455
   164
    (swing_node: Text.Markup => DefaultMutableTreeNode)
wenzelm@38479
   165
  {
wenzelm@45469
   166
    for ((_, entry) <- branches) {
wenzelm@45469
   167
      var current = parent
wenzelm@45469
   168
      for (info <- entry.markup) {
wenzelm@45469
   169
        val node = swing_node(Text.Info(entry.range, info))
wenzelm@45469
   170
        current.add(node)
wenzelm@45469
   171
        current = node
wenzelm@45469
   172
      }
wenzelm@45469
   173
      entry.subtree.swing_tree(current)(swing_node)
immler@34514
   174
    }
immler@34514
   175
  }
wenzelm@34717
   176
}
immler@34514
   177