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