src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Aug 19 12:41:40 2010 +0200 (2010-08-19)
changeset 38482 7b6ee937b75f
parent 38479 e628da370072
child 38486 f5bbfc019937
permissions -rw-r--r--
tuned Markup_Tree, using SortedMap more carefully;
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@38482
    23
  }
wenzelm@38479
    24
wenzelm@38479
    25
wenzelm@38479
    26
  /* branches sorted by quasi-order -- overlapping intervals appear as equivalent */
wenzelm@38479
    27
wenzelm@38479
    28
  object Branches
wenzelm@38479
    29
  {
wenzelm@38479
    30
    type Entry = (Node, Markup_Tree)
wenzelm@38479
    31
    type T = SortedMap[Node, Entry]
wenzelm@38479
    32
wenzelm@38479
    33
    val empty = SortedMap.empty[Node, Entry](new scala.math.Ordering[Node]
wenzelm@38479
    34
      {
wenzelm@38479
    35
        def compare(x: Node, y: Node): Int = x.range compare y.range
wenzelm@38479
    36
      })
wenzelm@38479
    37
    def update(branches: T, entries: Entry*): T =
wenzelm@38479
    38
      branches ++ entries.map(e => (e._1 -> e))
wenzelm@38479
    39
  }
wenzelm@38479
    40
wenzelm@38479
    41
  val empty = new Markup_Tree(Branches.empty)
wenzelm@38479
    42
}
immler@34554
    43
immler@34393
    44
wenzelm@38479
    45
case class Markup_Tree(val branches: Markup_Tree.Branches.T)
wenzelm@34717
    46
{
wenzelm@38479
    47
  import Markup_Tree._
immler@34557
    48
wenzelm@38479
    49
  def + (new_node: Node): Markup_Tree =
wenzelm@34703
    50
  {
wenzelm@38482
    51
    branches.get(new_node) match {
wenzelm@38482
    52
      case None =>
wenzelm@38479
    53
        new Markup_Tree(Branches.update(branches, new_node -> empty))
wenzelm@38482
    54
      case Some((node, subtree)) =>
wenzelm@38482
    55
        if (node.range != new_node.range && node.contains(new_node))
wenzelm@38482
    56
          new Markup_Tree(Branches.update(branches, node -> (subtree + new_node)))
wenzelm@38482
    57
        else if (new_node.contains(branches.head._1) && new_node.contains(branches.last._1))
wenzelm@38482
    58
          new Markup_Tree(Branches.update(Branches.empty, (new_node -> this)))
wenzelm@38482
    59
        else {
wenzelm@38482
    60
          var overlapping = Branches.empty
wenzelm@38482
    61
          var rest = branches
wenzelm@38482
    62
          while (rest.isDefinedAt(new_node)) {
wenzelm@38482
    63
            overlapping = Branches.update(overlapping, rest(new_node))
wenzelm@38482
    64
            rest -= new_node
wenzelm@38482
    65
          }
wenzelm@38482
    66
          if (overlapping.forall(e => new_node.contains(e._1)))
wenzelm@38482
    67
            new Markup_Tree(Branches.update(rest, new_node -> new Markup_Tree(overlapping)))
wenzelm@38482
    68
          else { // FIXME split markup!?
wenzelm@38482
    69
            System.err.println("Ignored overlapping markup: " + new_node)
wenzelm@38482
    70
            this
wenzelm@38482
    71
          }
wenzelm@38482
    72
        }
wenzelm@34703
    73
    }
wenzelm@34703
    74
  }
wenzelm@34703
    75
wenzelm@38479
    76
  // FIXME depth-first with result markup stack
wenzelm@38479
    77
  // FIXME projection to given range
wenzelm@38479
    78
  def flatten(parent: Node): List[Node] =
wenzelm@34717
    79
  {
wenzelm@38479
    80
    val result = new mutable.ListBuffer[Node]
wenzelm@38479
    81
    var offset = parent.range.start
wenzelm@38479
    82
    for ((_, (node, subtree)) <- branches.iterator) {
wenzelm@38479
    83
      if (offset < node.range.start)
wenzelm@38479
    84
        result += new Node(Text.Range(offset, node.range.start), parent.info)
wenzelm@38479
    85
      result ++= subtree.flatten(node)
wenzelm@38479
    86
      offset = node.range.stop
wenzelm@38479
    87
    }
wenzelm@38479
    88
    if (offset < parent.range.stop)
wenzelm@38479
    89
      result += new Node(Text.Range(offset, parent.range.stop), parent.info)
wenzelm@38479
    90
    result.toList
wenzelm@38479
    91
  }
wenzelm@38479
    92
wenzelm@38479
    93
  def filter(pred: Node => Boolean): Markup_Tree =
wenzelm@38479
    94
  {
wenzelm@38479
    95
    val bs = branches.toList.flatMap(entry => {
wenzelm@38479
    96
      val (_, (node, subtree)) = entry
wenzelm@38479
    97
      if (pred(node)) List((node, (node, subtree.filter(pred))))
wenzelm@38479
    98
      else subtree.filter(pred).branches.toList
wenzelm@38479
    99
    })
wenzelm@38479
   100
    new Markup_Tree(Branches.empty ++ bs)
wenzelm@38479
   101
  }
wenzelm@38479
   102
wenzelm@38479
   103
  def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node => DefaultMutableTreeNode)
wenzelm@38479
   104
  {
wenzelm@38479
   105
    for ((_, (node, subtree)) <- branches) {
wenzelm@38479
   106
      val current = swing_node(node)
wenzelm@38479
   107
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   108
      parent.add(current)
immler@34514
   109
    }
immler@34514
   110
  }
wenzelm@34717
   111
}
immler@34514
   112