src/Pure/PIDE/markup_tree.scala
author wenzelm
Fri Aug 20 22:35:01 2010 +0200 (2010-08-20)
changeset 38571 f7d7b8054648
parent 38568 f117ba49a59c
child 38576 ce3eed2b16f7
permissions -rw-r--r--
Markup_Tree.select: misc simplification, proper restriction of parent in subtree;
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@34701
    10
wenzelm@34701
    11
import javax.swing.tree.DefaultMutableTreeNode
wenzelm@34701
    12
wenzelm@38479
    13
import scala.collection.immutable.SortedMap
wenzelm@38479
    14
import scala.collection.mutable
wenzelm@38479
    15
import scala.annotation.tailrec
wenzelm@38479
    16
wenzelm@38479
    17
wenzelm@38479
    18
object Markup_Tree
wenzelm@38479
    19
{
wenzelm@38564
    20
  case class Node[A](val range: Text.Range, val info: A)
wenzelm@38482
    21
  {
wenzelm@38564
    22
    def contains[B](that: Node[B]): Boolean = this.range contains that.range
wenzelm@38564
    23
    def restrict(r: Text.Range): Node[A] = Node(range.restrict(r), info)
wenzelm@38482
    24
  }
wenzelm@38479
    25
wenzelm@38479
    26
wenzelm@38566
    27
  /* branches sorted by quasi-order -- overlapping ranges appear as equivalent */
wenzelm@38479
    28
wenzelm@38479
    29
  object Branches
wenzelm@38479
    30
  {
wenzelm@38564
    31
    type Entry = (Node[Any], Markup_Tree)
wenzelm@38564
    32
    type T = SortedMap[Node[Any], Entry]
wenzelm@38479
    33
wenzelm@38564
    34
    val empty = SortedMap.empty[Node[Any], Entry](new scala.math.Ordering[Node[Any]]
wenzelm@38479
    35
      {
wenzelm@38564
    36
        def compare(x: Node[Any], y: Node[Any]): Int = x.range compare y.range
wenzelm@38479
    37
      })
wenzelm@38562
    38
wenzelm@38562
    39
    def update(branches: T, entry: Entry): T = branches + (entry._1 -> entry)
wenzelm@38562
    40
wenzelm@38562
    41
    def overlapping(range: Text.Range, branches: T): T =
wenzelm@38566
    42
    {
wenzelm@38568
    43
      val start = Node[Any](Text.Range(range.start), Nil)
wenzelm@38568
    44
      val stop = Node[Any](Text.Range(range.stop), Nil)
wenzelm@38566
    45
      branches.get(stop) match {
wenzelm@38566
    46
        case Some(end) if range overlaps end._1.range =>
wenzelm@38566
    47
          update(branches.range(start, stop), end)
wenzelm@38566
    48
        case _ => branches.range(start, stop)
wenzelm@38566
    49
      }
wenzelm@38566
    50
    }
wenzelm@38479
    51
  }
wenzelm@38479
    52
wenzelm@38479
    53
  val empty = new Markup_Tree(Branches.empty)
wenzelm@38479
    54
}
immler@34554
    55
immler@34393
    56
wenzelm@38479
    57
case class Markup_Tree(val branches: Markup_Tree.Branches.T)
wenzelm@34717
    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@38564
    67
  def + (new_node: Node[Any]): Markup_Tree =
wenzelm@34703
    68
  {
wenzelm@38482
    69
    branches.get(new_node) match {
wenzelm@38482
    70
      case None =>
wenzelm@38479
    71
        new Markup_Tree(Branches.update(branches, new_node -> empty))
wenzelm@38482
    72
      case Some((node, subtree)) =>
wenzelm@38482
    73
        if (node.range != new_node.range && node.contains(new_node))
wenzelm@38482
    74
          new Markup_Tree(Branches.update(branches, node -> (subtree + new_node)))
wenzelm@38482
    75
        else if (new_node.contains(branches.head._1) && new_node.contains(branches.last._1))
wenzelm@38482
    76
          new Markup_Tree(Branches.update(Branches.empty, (new_node -> this)))
wenzelm@38482
    77
        else {
wenzelm@38562
    78
          val body = Branches.overlapping(new_node.range, branches)
wenzelm@38562
    79
          if (body.forall(e => new_node.contains(e._1))) {
wenzelm@38562
    80
            val rest = (branches /: body) { case (bs, (e, _)) => bs - e }
wenzelm@38562
    81
            new Markup_Tree(Branches.update(rest, new_node -> new Markup_Tree(body)))
wenzelm@38482
    82
          }
wenzelm@38482
    83
          else { // FIXME split markup!?
wenzelm@38482
    84
            System.err.println("Ignored overlapping markup: " + new_node)
wenzelm@38482
    85
            this
wenzelm@38482
    86
          }
wenzelm@38482
    87
        }
wenzelm@34703
    88
    }
wenzelm@34703
    89
  }
wenzelm@34703
    90
wenzelm@38479
    91
  // FIXME depth-first with result markup stack
wenzelm@38479
    92
  // FIXME projection to given range
wenzelm@38564
    93
  def flatten(parent: Node[Any]): List[Node[Any]] =
wenzelm@34717
    94
  {
wenzelm@38564
    95
    val result = new mutable.ListBuffer[Node[Any]]
wenzelm@38479
    96
    var offset = parent.range.start
wenzelm@38479
    97
    for ((_, (node, subtree)) <- branches.iterator) {
wenzelm@38479
    98
      if (offset < node.range.start)
wenzelm@38479
    99
        result += new Node(Text.Range(offset, node.range.start), parent.info)
wenzelm@38479
   100
      result ++= subtree.flatten(node)
wenzelm@38479
   101
      offset = node.range.stop
wenzelm@38479
   102
    }
wenzelm@38479
   103
    if (offset < parent.range.stop)
wenzelm@38479
   104
      result += new Node(Text.Range(offset, parent.range.stop), parent.info)
wenzelm@38479
   105
    result.toList
wenzelm@38479
   106
  }
wenzelm@38479
   107
wenzelm@38564
   108
  def filter(pred: Node[Any] => Boolean): Markup_Tree =
wenzelm@38479
   109
  {
wenzelm@38479
   110
    val bs = branches.toList.flatMap(entry => {
wenzelm@38479
   111
      val (_, (node, subtree)) = entry
wenzelm@38479
   112
      if (pred(node)) List((node, (node, subtree.filter(pred))))
wenzelm@38479
   113
      else subtree.filter(pred).branches.toList
wenzelm@38479
   114
    })
wenzelm@38479
   115
    new Markup_Tree(Branches.empty ++ bs)
wenzelm@38479
   116
  }
wenzelm@38479
   117
wenzelm@38571
   118
  def select[A](root: Node[A])(sel: PartialFunction[Any, A]): Stream[Node[A]] =
wenzelm@38486
   119
  {
wenzelm@38571
   120
    def stream(parent: Node[A], bs: Branches.T): Stream[Node[A]] =
wenzelm@38486
   121
    {
wenzelm@38486
   122
      val substream =
wenzelm@38562
   123
        (for ((_, (node, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
wenzelm@38564
   124
          if (sel.isDefinedAt(node.info)) {
wenzelm@38571
   125
            val current = Node(node.range.restrict(parent.range), sel(node.info))
wenzelm@38564
   126
            stream(current, subtree.branches)
wenzelm@38564
   127
          }
wenzelm@38571
   128
          else stream(parent.restrict(node.range), subtree.branches)
wenzelm@38486
   129
        }).flatten
wenzelm@38486
   130
wenzelm@38571
   131
      def padding(last: Text.Offset, s: Stream[Node[A]]): Stream[Node[A]] =
wenzelm@38486
   132
        s match {
wenzelm@38562
   133
          case (node @ Node(Text.Range(start, stop), _)) #:: rest =>
wenzelm@38562
   134
            if (last < start)
wenzelm@38562
   135
              parent.restrict(Text.Range(last, start)) #:: node #:: padding(stop, rest)
wenzelm@38562
   136
            else node #:: padding(stop, rest)
wenzelm@38564
   137
          case Stream.Empty =>
wenzelm@38486
   138
            if (last < parent.range.stop)
wenzelm@38562
   139
              Stream(parent.restrict(Text.Range(last, parent.range.stop)))
wenzelm@38486
   140
            else Stream.Empty
wenzelm@38486
   141
        }
wenzelm@38566
   142
      if (substream.isEmpty) Stream(parent)
wenzelm@38566
   143
      else padding(parent.range.start, substream)
wenzelm@38486
   144
    }
wenzelm@38571
   145
    stream(root, branches)
wenzelm@38486
   146
  }
wenzelm@38486
   147
wenzelm@38564
   148
  def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node[Any] => DefaultMutableTreeNode)
wenzelm@38479
   149
  {
wenzelm@38479
   150
    for ((_, (node, subtree)) <- branches) {
wenzelm@38479
   151
      val current = swing_node(node)
wenzelm@38479
   152
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   153
      parent.add(current)
immler@34514
   154
    }
immler@34514
   155
  }
wenzelm@34717
   156
}
immler@34514
   157