src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Aug 19 17:40:44 2010 +0200 (2010-08-19)
changeset 38486 f5bbfc019937
parent 38482 7b6ee937b75f
child 38562 3de5f0424caa
permissions -rw-r--r--
Markup_Tree.select: crude version of stream-based filtering;
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@38486
    23
    def intersect(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@38479
    38
    def update(branches: T, entries: Entry*): T =
wenzelm@38479
    39
      branches ++ entries.map(e => (e._1 -> e))
wenzelm@38479
    40
  }
wenzelm@38479
    41
wenzelm@38479
    42
  val empty = new Markup_Tree(Branches.empty)
wenzelm@38479
    43
}
immler@34554
    44
immler@34393
    45
wenzelm@38479
    46
case class Markup_Tree(val branches: Markup_Tree.Branches.T)
wenzelm@34717
    47
{
wenzelm@38479
    48
  import Markup_Tree._
immler@34557
    49
wenzelm@38479
    50
  def + (new_node: Node): Markup_Tree =
wenzelm@34703
    51
  {
wenzelm@38482
    52
    branches.get(new_node) match {
wenzelm@38482
    53
      case None =>
wenzelm@38479
    54
        new Markup_Tree(Branches.update(branches, new_node -> empty))
wenzelm@38482
    55
      case Some((node, subtree)) =>
wenzelm@38482
    56
        if (node.range != new_node.range && node.contains(new_node))
wenzelm@38482
    57
          new Markup_Tree(Branches.update(branches, node -> (subtree + new_node)))
wenzelm@38482
    58
        else if (new_node.contains(branches.head._1) && new_node.contains(branches.last._1))
wenzelm@38482
    59
          new Markup_Tree(Branches.update(Branches.empty, (new_node -> this)))
wenzelm@38482
    60
        else {
wenzelm@38482
    61
          var overlapping = Branches.empty
wenzelm@38482
    62
          var rest = branches
wenzelm@38482
    63
          while (rest.isDefinedAt(new_node)) {
wenzelm@38482
    64
            overlapping = Branches.update(overlapping, rest(new_node))
wenzelm@38482
    65
            rest -= new_node
wenzelm@38482
    66
          }
wenzelm@38482
    67
          if (overlapping.forall(e => new_node.contains(e._1)))
wenzelm@38482
    68
            new Markup_Tree(Branches.update(rest, new_node -> new Markup_Tree(overlapping)))
wenzelm@38482
    69
          else { // FIXME split markup!?
wenzelm@38482
    70
            System.err.println("Ignored overlapping markup: " + new_node)
wenzelm@38482
    71
            this
wenzelm@38482
    72
          }
wenzelm@38482
    73
        }
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@38486
   104
  def select[A](range: Text.Range)(sel: PartialFunction[Node, A]): Stream[Node] =
wenzelm@38486
   105
  {
wenzelm@38486
   106
    def stream(parent: Node, bs: Branches.T): Stream[Node] =
wenzelm@38486
   107
    {
wenzelm@38486
   108
      val start = Node(parent.range.start_range, Nil)
wenzelm@38486
   109
      val stop = Node(parent.range.stop_range, Nil)
wenzelm@38486
   110
      val substream =
wenzelm@38486
   111
        (for ((_, (node, subtree)) <- bs.range(start, stop).toStream) yield {
wenzelm@38486
   112
          if (sel.isDefinedAt(node))
wenzelm@38486
   113
            stream(node.intersect(parent.range), subtree.branches)
wenzelm@38486
   114
          else stream(parent, subtree.branches)
wenzelm@38486
   115
        }).flatten
wenzelm@38486
   116
wenzelm@38486
   117
      def padding(last: Text.Offset, s: Stream[Node]): Stream[Node] =
wenzelm@38486
   118
        s match {
wenzelm@38486
   119
          case node #:: rest =>
wenzelm@38486
   120
            if (last < node.range.start)
wenzelm@38486
   121
              parent.intersect(Text.Range(last, node.range.start)) #::
wenzelm@38486
   122
                node #:: padding(node.range.stop, rest)
wenzelm@38486
   123
            else node #:: padding(node.range.stop, rest)
wenzelm@38486
   124
          case Stream.Empty =>  // FIXME
wenzelm@38486
   125
            if (last < parent.range.stop)
wenzelm@38486
   126
            Stream(parent.intersect(Text.Range(last, parent.range.stop)))
wenzelm@38486
   127
            else Stream.Empty
wenzelm@38486
   128
        }
wenzelm@38486
   129
      padding(parent.range.start, substream)
wenzelm@38486
   130
    }
wenzelm@38486
   131
    // FIXME handle disjoint range!?
wenzelm@38486
   132
    stream(Node(range, Nil), branches)
wenzelm@38486
   133
  }
wenzelm@38486
   134
wenzelm@38479
   135
  def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node => DefaultMutableTreeNode)
wenzelm@38479
   136
  {
wenzelm@38479
   137
    for ((_, (node, subtree)) <- branches) {
wenzelm@38479
   138
      val current = swing_node(node)
wenzelm@38479
   139
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   140
      parent.add(current)
immler@34514
   141
    }
immler@34514
   142
  }
wenzelm@34717
   143
}
immler@34514
   144