src/Pure/PIDE/markup_tree.scala
author wenzelm
Sun Aug 22 18:46:16 2010 +0200 (2010-08-22)
changeset 38577 4e4d3ea3725a
parent 38576 ce3eed2b16f7
child 38578 1ebc6b76e5ff
permissions -rw-r--r--
renamed Markup_Tree.Node to Text.Info;
Markup_Tree.select: body may depend on full Text.Info, including range;
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@38566
    20
  /* branches sorted by quasi-order -- overlapping ranges appear as equivalent */
wenzelm@38479
    21
wenzelm@38479
    22
  object Branches
wenzelm@38479
    23
  {
wenzelm@38577
    24
    type Entry = (Text.Info[Any], Markup_Tree)
wenzelm@38577
    25
    type T = SortedMap[Text.Info[Any], Entry]
wenzelm@38479
    26
wenzelm@38577
    27
    val empty = SortedMap.empty[Text.Info[Any], Entry](new scala.math.Ordering[Text.Info[Any]]
wenzelm@38479
    28
      {
wenzelm@38577
    29
        def compare(x: Text.Info[Any], y: Text.Info[Any]): Int = x.range compare y.range
wenzelm@38479
    30
      })
wenzelm@38562
    31
wenzelm@38562
    32
    def update(branches: T, entry: Entry): T = branches + (entry._1 -> entry)
wenzelm@38562
    33
wenzelm@38562
    34
    def overlapping(range: Text.Range, branches: T): T =
wenzelm@38566
    35
    {
wenzelm@38577
    36
      val start = Text.Info[Any](Text.Range(range.start), Nil)
wenzelm@38577
    37
      val stop = Text.Info[Any](Text.Range(range.stop), Nil)
wenzelm@38566
    38
      branches.get(stop) match {
wenzelm@38566
    39
        case Some(end) if range overlaps end._1.range =>
wenzelm@38566
    40
          update(branches.range(start, stop), end)
wenzelm@38566
    41
        case _ => branches.range(start, stop)
wenzelm@38566
    42
      }
wenzelm@38566
    43
    }
wenzelm@38479
    44
  }
wenzelm@38479
    45
wenzelm@38479
    46
  val empty = new Markup_Tree(Branches.empty)
wenzelm@38479
    47
}
immler@34554
    48
immler@34393
    49
wenzelm@38479
    50
case class Markup_Tree(val branches: Markup_Tree.Branches.T)
wenzelm@34717
    51
{
wenzelm@38479
    52
  import Markup_Tree._
immler@34557
    53
wenzelm@38563
    54
  override def toString =
wenzelm@38563
    55
    branches.toList.map(_._2) match {
wenzelm@38563
    56
      case Nil => "Empty"
wenzelm@38563
    57
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
    58
    }
wenzelm@38563
    59
wenzelm@38577
    60
  def + (new_info: Text.Info[Any]): Markup_Tree =
wenzelm@34703
    61
  {
wenzelm@38577
    62
    branches.get(new_info) match {
wenzelm@38482
    63
      case None =>
wenzelm@38577
    64
        new Markup_Tree(Branches.update(branches, new_info -> empty))
wenzelm@38577
    65
      case Some((info, subtree)) =>
wenzelm@38577
    66
        if (info.range != new_info.range && info.contains(new_info))
wenzelm@38577
    67
          new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
wenzelm@38577
    68
        else if (new_info.contains(branches.head._1) && new_info.contains(branches.last._1))
wenzelm@38577
    69
          new Markup_Tree(Branches.update(Branches.empty, (new_info -> this)))
wenzelm@38482
    70
        else {
wenzelm@38577
    71
          val body = Branches.overlapping(new_info.range, branches)
wenzelm@38577
    72
          if (body.forall(e => new_info.contains(e._1))) {
wenzelm@38562
    73
            val rest = (branches /: body) { case (bs, (e, _)) => bs - e }
wenzelm@38577
    74
            new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
wenzelm@38482
    75
          }
wenzelm@38482
    76
          else { // FIXME split markup!?
wenzelm@38577
    77
            System.err.println("Ignored overlapping markup information: " + new_info)
wenzelm@38482
    78
            this
wenzelm@38482
    79
          }
wenzelm@38482
    80
        }
wenzelm@34703
    81
    }
wenzelm@34703
    82
  }
wenzelm@34703
    83
wenzelm@38577
    84
  def select[A](root: Text.Info[A])(sel: PartialFunction[Text.Info[Any], A]): Stream[Text.Info[A]] =
wenzelm@38486
    85
  {
wenzelm@38577
    86
    def stream(parent: Text.Info[A], bs: Branches.T): Stream[Text.Info[A]] =
wenzelm@38486
    87
    {
wenzelm@38486
    88
      val substream =
wenzelm@38577
    89
        (for ((_, (info, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
wenzelm@38577
    90
          if (sel.isDefinedAt(info)) {
wenzelm@38577
    91
            val current = Text.Info(info.range.restrict(parent.range), sel(info))
wenzelm@38564
    92
            stream(current, subtree.branches)
wenzelm@38564
    93
          }
wenzelm@38577
    94
          else stream(parent.restrict(info.range), subtree.branches)
wenzelm@38486
    95
        }).flatten
wenzelm@38486
    96
wenzelm@38577
    97
      def padding(last: Text.Offset, s: Stream[Text.Info[A]]): Stream[Text.Info[A]] =
wenzelm@38486
    98
        s match {
wenzelm@38577
    99
          case info #:: rest =>
wenzelm@38577
   100
            val Text.Range(start, stop) = info.range
wenzelm@38562
   101
            if (last < start)
wenzelm@38577
   102
              parent.restrict(Text.Range(last, start)) #:: info #:: padding(stop, rest)
wenzelm@38577
   103
            else info #:: padding(stop, rest)
wenzelm@38564
   104
          case Stream.Empty =>
wenzelm@38486
   105
            if (last < parent.range.stop)
wenzelm@38562
   106
              Stream(parent.restrict(Text.Range(last, parent.range.stop)))
wenzelm@38486
   107
            else Stream.Empty
wenzelm@38486
   108
        }
wenzelm@38566
   109
      if (substream.isEmpty) Stream(parent)
wenzelm@38566
   110
      else padding(parent.range.start, substream)
wenzelm@38486
   111
    }
wenzelm@38571
   112
    stream(root, branches)
wenzelm@38486
   113
  }
wenzelm@38486
   114
wenzelm@38577
   115
  def swing_tree(parent: DefaultMutableTreeNode)
wenzelm@38577
   116
    (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
wenzelm@38479
   117
  {
wenzelm@38577
   118
    for ((_, (info, subtree)) <- branches) {
wenzelm@38577
   119
      val current = swing_node(info)
wenzelm@38479
   120
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   121
      parent.add(current)
immler@34514
   122
    }
immler@34514
   123
  }
wenzelm@34717
   124
}
immler@34514
   125