src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Aug 19 22:26:15 2010 +0200 (2010-08-19)
changeset 38563 f6c9a4f9f66f
parent 38562 3de5f0424caa
child 38564 a6e2715fac5f
permissions -rw-r--r--
added toString methods;
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@38479
    20
  case class Node(val range: Text.Range, val info: Any)
wenzelm@38482
    21
  {
wenzelm@38482
    22
    def contains(that: Node): Boolean = this.range contains that.range
wenzelm@38562
    23
    def restrict(r: Text.Range): Node = Node(range.intersect(r), info)
wenzelm@38482
    24
  }
wenzelm@38479
    25
wenzelm@38479
    26
wenzelm@38479
    27
  /* branches sorted by quasi-order -- overlapping intervals appear as equivalent */
wenzelm@38479
    28
wenzelm@38479
    29
  object Branches
wenzelm@38479
    30
  {
wenzelm@38479
    31
    type Entry = (Node, Markup_Tree)
wenzelm@38479
    32
    type T = SortedMap[Node, Entry]
wenzelm@38479
    33
wenzelm@38479
    34
    val empty = SortedMap.empty[Node, Entry](new scala.math.Ordering[Node]
wenzelm@38479
    35
      {
wenzelm@38479
    36
        def compare(x: Node, y: Node): 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@38562
    42
      branches.range(Node(range.start_range, Nil), Node(range.stop_range, Nil))
wenzelm@38479
    43
  }
wenzelm@38479
    44
wenzelm@38479
    45
  val empty = new Markup_Tree(Branches.empty)
wenzelm@38479
    46
}
immler@34554
    47
immler@34393
    48
wenzelm@38479
    49
case class Markup_Tree(val branches: Markup_Tree.Branches.T)
wenzelm@34717
    50
{
wenzelm@38479
    51
  import Markup_Tree._
immler@34557
    52
wenzelm@38563
    53
  override def toString =
wenzelm@38563
    54
    branches.toList.map(_._2) match {
wenzelm@38563
    55
      case Nil => "Empty"
wenzelm@38563
    56
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
    57
    }
wenzelm@38563
    58
wenzelm@38479
    59
  def + (new_node: Node): Markup_Tree =
wenzelm@34703
    60
  {
wenzelm@38482
    61
    branches.get(new_node) match {
wenzelm@38482
    62
      case None =>
wenzelm@38479
    63
        new Markup_Tree(Branches.update(branches, new_node -> empty))
wenzelm@38482
    64
      case Some((node, subtree)) =>
wenzelm@38482
    65
        if (node.range != new_node.range && node.contains(new_node))
wenzelm@38482
    66
          new Markup_Tree(Branches.update(branches, node -> (subtree + new_node)))
wenzelm@38482
    67
        else if (new_node.contains(branches.head._1) && new_node.contains(branches.last._1))
wenzelm@38482
    68
          new Markup_Tree(Branches.update(Branches.empty, (new_node -> this)))
wenzelm@38482
    69
        else {
wenzelm@38562
    70
          val body = Branches.overlapping(new_node.range, branches)
wenzelm@38562
    71
          if (body.forall(e => new_node.contains(e._1))) {
wenzelm@38562
    72
            val rest = (branches /: body) { case (bs, (e, _)) => bs - e }
wenzelm@38562
    73
            new Markup_Tree(Branches.update(rest, new_node -> new Markup_Tree(body)))
wenzelm@38482
    74
          }
wenzelm@38482
    75
          else { // FIXME split markup!?
wenzelm@38482
    76
            System.err.println("Ignored overlapping markup: " + new_node)
wenzelm@38482
    77
            this
wenzelm@38482
    78
          }
wenzelm@38482
    79
        }
wenzelm@34703
    80
    }
wenzelm@34703
    81
  }
wenzelm@34703
    82
wenzelm@38479
    83
  // FIXME depth-first with result markup stack
wenzelm@38479
    84
  // FIXME projection to given range
wenzelm@38479
    85
  def flatten(parent: Node): List[Node] =
wenzelm@34717
    86
  {
wenzelm@38479
    87
    val result = new mutable.ListBuffer[Node]
wenzelm@38479
    88
    var offset = parent.range.start
wenzelm@38479
    89
    for ((_, (node, subtree)) <- branches.iterator) {
wenzelm@38479
    90
      if (offset < node.range.start)
wenzelm@38479
    91
        result += new Node(Text.Range(offset, node.range.start), parent.info)
wenzelm@38479
    92
      result ++= subtree.flatten(node)
wenzelm@38479
    93
      offset = node.range.stop
wenzelm@38479
    94
    }
wenzelm@38479
    95
    if (offset < parent.range.stop)
wenzelm@38479
    96
      result += new Node(Text.Range(offset, parent.range.stop), parent.info)
wenzelm@38479
    97
    result.toList
wenzelm@38479
    98
  }
wenzelm@38479
    99
wenzelm@38479
   100
  def filter(pred: Node => Boolean): Markup_Tree =
wenzelm@38479
   101
  {
wenzelm@38479
   102
    val bs = branches.toList.flatMap(entry => {
wenzelm@38479
   103
      val (_, (node, subtree)) = entry
wenzelm@38479
   104
      if (pred(node)) List((node, (node, subtree.filter(pred))))
wenzelm@38479
   105
      else subtree.filter(pred).branches.toList
wenzelm@38479
   106
    })
wenzelm@38479
   107
    new Markup_Tree(Branches.empty ++ bs)
wenzelm@38479
   108
  }
wenzelm@38479
   109
wenzelm@38486
   110
  def select[A](range: Text.Range)(sel: PartialFunction[Node, A]): Stream[Node] =
wenzelm@38486
   111
  {
wenzelm@38486
   112
    def stream(parent: Node, bs: Branches.T): Stream[Node] =
wenzelm@38486
   113
    {
wenzelm@38486
   114
      val substream =
wenzelm@38562
   115
        (for ((_, (node, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
wenzelm@38486
   116
          if (sel.isDefinedAt(node))
wenzelm@38562
   117
            stream(node.restrict(parent.range), subtree.branches)
wenzelm@38486
   118
          else stream(parent, subtree.branches)
wenzelm@38486
   119
        }).flatten
wenzelm@38486
   120
wenzelm@38486
   121
      def padding(last: Text.Offset, s: Stream[Node]): Stream[Node] =
wenzelm@38486
   122
        s match {
wenzelm@38562
   123
          case (node @ Node(Text.Range(start, stop), _)) #:: rest =>
wenzelm@38562
   124
            if (last < start)
wenzelm@38562
   125
              parent.restrict(Text.Range(last, start)) #:: node #:: padding(stop, rest)
wenzelm@38562
   126
            else node #:: padding(stop, rest)
wenzelm@38486
   127
          case Stream.Empty =>  // FIXME
wenzelm@38486
   128
            if (last < parent.range.stop)
wenzelm@38562
   129
              Stream(parent.restrict(Text.Range(last, parent.range.stop)))
wenzelm@38486
   130
            else Stream.Empty
wenzelm@38486
   131
        }
wenzelm@38486
   132
      padding(parent.range.start, substream)
wenzelm@38486
   133
    }
wenzelm@38486
   134
    stream(Node(range, Nil), branches)
wenzelm@38486
   135
  }
wenzelm@38486
   136
wenzelm@38479
   137
  def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node => DefaultMutableTreeNode)
wenzelm@38479
   138
  {
wenzelm@38479
   139
    for ((_, (node, subtree)) <- branches) {
wenzelm@38479
   140
      val current = swing_node(node)
wenzelm@38479
   141
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   142
      parent.add(current)
immler@34514
   143
    }
immler@34514
   144
  }
wenzelm@34717
   145
}
immler@34514
   146