src/Pure/PIDE/markup_tree.scala
author wenzelm
Sun Aug 22 19:33:01 2010 +0200 (2010-08-22)
changeset 38578 1ebc6b76e5ff
parent 38577 4e4d3ea3725a
child 38582 3a6ce43d99b1
permissions -rw-r--r--
misc tuning and simplification;
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@38578
    25
    type T = SortedMap[Text.Range, Entry]
wenzelm@38479
    26
wenzelm@38578
    27
    val empty = SortedMap.empty[Text.Range, Entry](new scala.math.Ordering[Text.Range]
wenzelm@38479
    28
      {
wenzelm@38578
    29
        def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
wenzelm@38479
    30
      })
wenzelm@38562
    31
wenzelm@38578
    32
    def update(branches: T, entry: Entry): T = branches + (entry._1.range -> entry)
wenzelm@38562
    33
wenzelm@38562
    34
    def overlapping(range: Text.Range, branches: T): T =
wenzelm@38566
    35
    {
wenzelm@38578
    36
      val start = Text.Range(range.start)
wenzelm@38578
    37
      val stop = Text.Range(range.stop)
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@38578
    62
    val new_range = new_info.range
wenzelm@38578
    63
    branches.get(new_range) match {
wenzelm@38482
    64
      case None =>
wenzelm@38577
    65
        new Markup_Tree(Branches.update(branches, new_info -> empty))
wenzelm@38577
    66
      case Some((info, subtree)) =>
wenzelm@38578
    67
        val range = info.range
wenzelm@38578
    68
        if (range != new_range && range.contains(new_range))
wenzelm@38577
    69
          new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
wenzelm@38578
    70
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@38577
    71
          new Markup_Tree(Branches.update(Branches.empty, (new_info -> this)))
wenzelm@38482
    72
        else {
wenzelm@38578
    73
          val body = Branches.overlapping(new_range, branches)
wenzelm@38578
    74
          if (body.forall(e => new_range.contains(e._1))) {
wenzelm@38562
    75
            val rest = (branches /: body) { case (bs, (e, _)) => bs - e }
wenzelm@38577
    76
            new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
wenzelm@38482
    77
          }
wenzelm@38482
    78
          else { // FIXME split markup!?
wenzelm@38577
    79
            System.err.println("Ignored overlapping markup information: " + new_info)
wenzelm@38482
    80
            this
wenzelm@38482
    81
          }
wenzelm@38482
    82
        }
wenzelm@34703
    83
    }
wenzelm@34703
    84
  }
wenzelm@34703
    85
wenzelm@38578
    86
  def select[A](root: Text.Info[A])
wenzelm@38578
    87
    (result: PartialFunction[Text.Info[Any], A]): Stream[Text.Info[A]] =
wenzelm@38486
    88
  {
wenzelm@38577
    89
    def stream(parent: Text.Info[A], bs: Branches.T): Stream[Text.Info[A]] =
wenzelm@38486
    90
    {
wenzelm@38578
    91
      val range = parent.range
wenzelm@38486
    92
      val substream =
wenzelm@38578
    93
        (for ((info_range, (info, subtree)) <- Branches.overlapping(range, bs).toStream) yield {
wenzelm@38578
    94
          if (result.isDefinedAt(info)) {
wenzelm@38578
    95
            val current = Text.Info(info_range.restrict(range), result(info))
wenzelm@38564
    96
            stream(current, subtree.branches)
wenzelm@38564
    97
          }
wenzelm@38578
    98
          else stream(parent.restrict(info_range), subtree.branches)
wenzelm@38486
    99
        }).flatten
wenzelm@38486
   100
wenzelm@38577
   101
      def padding(last: Text.Offset, s: Stream[Text.Info[A]]): Stream[Text.Info[A]] =
wenzelm@38486
   102
        s match {
wenzelm@38577
   103
          case info #:: rest =>
wenzelm@38577
   104
            val Text.Range(start, stop) = info.range
wenzelm@38562
   105
            if (last < start)
wenzelm@38577
   106
              parent.restrict(Text.Range(last, start)) #:: info #:: padding(stop, rest)
wenzelm@38577
   107
            else info #:: padding(stop, rest)
wenzelm@38564
   108
          case Stream.Empty =>
wenzelm@38578
   109
            if (last < range.stop)
wenzelm@38578
   110
              Stream(parent.restrict(Text.Range(last, range.stop)))
wenzelm@38486
   111
            else Stream.Empty
wenzelm@38486
   112
        }
wenzelm@38566
   113
      if (substream.isEmpty) Stream(parent)
wenzelm@38578
   114
      else padding(range.start, substream)
wenzelm@38486
   115
    }
wenzelm@38571
   116
    stream(root, branches)
wenzelm@38486
   117
  }
wenzelm@38486
   118
wenzelm@38577
   119
  def swing_tree(parent: DefaultMutableTreeNode)
wenzelm@38577
   120
    (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
wenzelm@38479
   121
  {
wenzelm@38577
   122
    for ((_, (info, subtree)) <- branches) {
wenzelm@38577
   123
      val current = swing_node(info)
wenzelm@38479
   124
      subtree.swing_tree(current)(swing_node)
wenzelm@38479
   125
      parent.add(current)
immler@34514
   126
    }
immler@34514
   127
  }
wenzelm@34717
   128
}
immler@34514
   129