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