src/Pure/PIDE/markup_tree.scala
changeset 45469 25306d92f4ad
parent 45468 33e946d3f449
child 45470 81b85d4ed269
     1.1 --- a/src/Pure/PIDE/markup_tree.scala	Sat Nov 12 12:21:42 2011 +0100
     1.2 +++ b/src/Pure/PIDE/markup_tree.scala	Sat Nov 12 17:01:58 2011 +0100
     1.3 @@ -20,20 +20,26 @@
     1.4  
     1.5    val empty: Markup_Tree = new Markup_Tree(Branches.empty)
     1.6  
     1.7 +  sealed case class Entry(range: Text.Range, rev_markup: List[XML.Tree], subtree: Markup_Tree)
     1.8 +  {
     1.9 +    def + (m: XML.Tree): Entry = copy(rev_markup = m :: rev_markup)
    1.10 +    def markup: List[XML.Tree] = rev_markup.reverse
    1.11 +  }
    1.12 +
    1.13    object Branches
    1.14    {
    1.15 -    type Entry = (Text.Markup, Markup_Tree)
    1.16      type T = SortedMap[Text.Range, Entry]
    1.17 -
    1.18      val empty: T = SortedMap.empty(Text.Range.Ordering)
    1.19 -    def update(branches: T, entry: Entry): T = branches + (entry._1.range -> entry)
    1.20 -    def single(entry: Entry): T = update(empty, entry)
    1.21    }
    1.22  }
    1.23  
    1.24  
    1.25  class Markup_Tree private(branches: Markup_Tree.Branches.T)
    1.26  {
    1.27 +  private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
    1.28 +    this(branches + (entry.range -> entry))
    1.29 +
    1.30 +
    1.31    import Markup_Tree._
    1.32  
    1.33    override def toString =
    1.34 @@ -42,42 +48,45 @@
    1.35        case list => list.mkString("Tree(", ",", ")")
    1.36      }
    1.37  
    1.38 -  private def overlapping(range: Text.Range): Branches.T =  // FIXME special cases!?
    1.39 +  private def overlapping(range: Text.Range): Branches.T =
    1.40    {
    1.41      val start = Text.Range(range.start)
    1.42      val stop = Text.Range(range.stop)
    1.43      val bs = branches.range(start, stop)
    1.44      // FIXME check after Scala 2.8.x
    1.45      branches.get(stop) match {
    1.46 -      case Some(end) if range overlaps end._1.range => Branches.update(bs, end)
    1.47 +      case Some(end) if range overlaps end.range => bs + (end.range -> end)
    1.48        case _ => bs
    1.49      }
    1.50    }
    1.51  
    1.52 -  def + (new_info: Text.Markup): Markup_Tree =
    1.53 +  def + (new_markup: Text.Markup): Markup_Tree =
    1.54    {
    1.55 -    val new_range = new_info.range
    1.56 +    val new_range = new_markup.range
    1.57 +    val new_info = new_markup.info
    1.58 +
    1.59      branches.get(new_range) match {
    1.60        case None =>
    1.61 -        new Markup_Tree(Branches.update(branches, new_info -> empty))
    1.62 -      case Some((info, subtree)) =>
    1.63 -        val range = info.range
    1.64 -        if (range.contains(new_range))
    1.65 -          new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
    1.66 +        new Markup_Tree(branches, Entry(new_range, List(new_info), empty))
    1.67 +      case Some(entry) =>
    1.68 +        if (entry.range == new_range)
    1.69 +          new Markup_Tree(branches, entry + new_info)
    1.70 +        else if (entry.range.contains(new_range))
    1.71 +          new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
    1.72          else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    1.73 -          new Markup_Tree(Branches.single(new_info -> this))
    1.74 +          new Markup_Tree(Branches.empty, Entry(new_range, List(new_info), this))
    1.75          else {
    1.76            val body = overlapping(new_range)
    1.77            if (body.forall(e => new_range.contains(e._1))) {
    1.78 -            val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
    1.79 +            val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME
    1.80                if (body.size > 1)
    1.81                  (Branches.empty /: branches)((rest, entry) =>
    1.82                    if (body.isDefinedAt(entry._1)) rest else rest + entry)
    1.83                else branches
    1.84 -            new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
    1.85 +            new Markup_Tree(rest, Entry(new_range, List(new_info), new Markup_Tree(body)))
    1.86            }
    1.87            else { // FIXME split markup!?
    1.88 -            System.err.println("Ignored overlapping markup information: " + new_info)
    1.89 +            System.err.println("Ignored overlapping markup information: " + new_markup)
    1.90              this
    1.91            }
    1.92          }
    1.93 @@ -87,26 +96,38 @@
    1.94    def cumulate[A](root_range: Text.Range)(body: Cumulate[A]): Stream[Text.Info[A]] =
    1.95    {
    1.96      val root_info = body.info
    1.97 -    val result = body.result
    1.98 +
    1.99 +    def result(x: A, entry: Entry): Option[A] =
   1.100 +    {
   1.101 +      val (y, changed) =
   1.102 +        (entry.markup :\ (x, false))((info, res) =>
   1.103 +          {
   1.104 +            val (y, changed) = res
   1.105 +            val arg = (x, Text.Info(entry.range, info))
   1.106 +            if (body.result.isDefinedAt(arg)) (body.result(arg), true)
   1.107 +            else res
   1.108 +          })
   1.109 +      if (changed) Some(y) else None
   1.110 +    }
   1.111  
   1.112      def stream(
   1.113        last: Text.Offset,
   1.114 -      stack: List[(Text.Info[A], Stream[(Text.Range, Branches.Entry)])]): Stream[Text.Info[A]] =
   1.115 +      stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
   1.116      {
   1.117        stack match {
   1.118 -        case (parent, (range, (info, tree)) #:: more) :: rest =>
   1.119 +        case (parent, (range, entry) #:: more) :: rest =>
   1.120            val subrange = range.restrict(root_range)
   1.121 -          val subtree = tree.overlapping(subrange).toStream
   1.122 +          val subtree = entry.subtree.overlapping(subrange).toStream
   1.123            val start = subrange.start
   1.124  
   1.125 -          val arg = (parent.info, info)
   1.126 -          if (result.isDefinedAt(arg)) {
   1.127 -            val next = Text.Info(subrange, result(arg))
   1.128 -            val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   1.129 -            if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   1.130 -            else nexts
   1.131 +          result(parent.info, entry) match {
   1.132 +            case Some(res) =>
   1.133 +              val next = Text.Info(subrange, res)
   1.134 +              val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   1.135 +              if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   1.136 +              else nexts
   1.137 +            case None => stream(last, (parent, subtree #::: more) :: rest)
   1.138            }
   1.139 -          else stream(last, (parent, subtree #::: more) :: rest)
   1.140  
   1.141          case (parent, Stream.Empty) :: rest =>
   1.142            val stop = parent.range.stop
   1.143 @@ -127,10 +148,14 @@
   1.144    def swing_tree(parent: DefaultMutableTreeNode)
   1.145      (swing_node: Text.Markup => DefaultMutableTreeNode)
   1.146    {
   1.147 -    for ((_, (info, subtree)) <- branches) {
   1.148 -      val current = swing_node(info)
   1.149 -      subtree.swing_tree(current)(swing_node)
   1.150 -      parent.add(current)
   1.151 +    for ((_, entry) <- branches) {
   1.152 +      var current = parent
   1.153 +      for (info <- entry.markup) {
   1.154 +        val node = swing_node(Text.Info(entry.range, info))
   1.155 +        current.add(node)
   1.156 +        current = node
   1.157 +      }
   1.158 +      entry.subtree.swing_tree(current)(swing_node)
   1.159      }
   1.160    }
   1.161  }