src/Pure/PIDE/markup_tree.scala
author wenzelm
Sun Aug 22 16:43:20 2010 +0200 (2010-08-22)
changeset 38576 ce3eed2b16f7
parent 38571 f7d7b8054648
child 38577 4e4d3ea3725a
permissions -rw-r--r--
removed obsolete Markup_Tree.flatten/filter;
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@38571
    91
  def select[A](root: Node[A])(sel: PartialFunction[Any, A]): Stream[Node[A]] =
wenzelm@38486
    92
  {
wenzelm@38571
    93
    def stream(parent: Node[A], bs: Branches.T): Stream[Node[A]] =
wenzelm@38486
    94
    {
wenzelm@38486
    95
      val substream =
wenzelm@38562
    96
        (for ((_, (node, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
wenzelm@38564
    97
          if (sel.isDefinedAt(node.info)) {
wenzelm@38571
    98
            val current = Node(node.range.restrict(parent.range), sel(node.info))
wenzelm@38564
    99
            stream(current, subtree.branches)
wenzelm@38564
   100
          }
wenzelm@38571
   101
          else stream(parent.restrict(node.range), subtree.branches)
wenzelm@38486
   102
        }).flatten
wenzelm@38486
   103
wenzelm@38571
   104
      def padding(last: Text.Offset, s: Stream[Node[A]]): Stream[Node[A]] =
wenzelm@38486
   105
        s match {
wenzelm@38562
   106
          case (node @ Node(Text.Range(start, stop), _)) #:: rest =>
wenzelm@38562
   107
            if (last < start)
wenzelm@38562
   108
              parent.restrict(Text.Range(last, start)) #:: node #:: padding(stop, rest)
wenzelm@38562
   109
            else node #:: padding(stop, rest)
wenzelm@38564
   110
          case Stream.Empty =>
wenzelm@38486
   111
            if (last < parent.range.stop)
wenzelm@38562
   112
              Stream(parent.restrict(Text.Range(last, parent.range.stop)))
wenzelm@38486
   113
            else Stream.Empty
wenzelm@38486
   114
        }
wenzelm@38566
   115
      if (substream.isEmpty) Stream(parent)
wenzelm@38566
   116
      else padding(parent.range.start, substream)
wenzelm@38486
   117
    }
wenzelm@38571
   118
    stream(root, branches)
wenzelm@38486
   119
  }
wenzelm@38486
   120
wenzelm@38564
   121
  def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node[Any] => DefaultMutableTreeNode)
wenzelm@38479
   122
  {
wenzelm@38479
   123
    for ((_, (node, subtree)) <- branches) {
wenzelm@38479
   124
      val current = swing_node(node)
wenzelm@38479
   125
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   126
      parent.add(current)
immler@34514
   127
    }
immler@34514
   128
  }
wenzelm@34717
   129
}
immler@34514
   130