src/Pure/PIDE/markup_tree.scala
changeset 38577 4e4d3ea3725a
parent 38576 ce3eed2b16f7
child 38578 1ebc6b76e5ff
     1.1 --- a/src/Pure/PIDE/markup_tree.scala	Sun Aug 22 16:43:20 2010 +0200
     1.2 +++ b/src/Pure/PIDE/markup_tree.scala	Sun Aug 22 18:46:16 2010 +0200
     1.3 @@ -17,31 +17,24 @@
     1.4  
     1.5  object Markup_Tree
     1.6  {
     1.7 -  case class Node[A](val range: Text.Range, val info: A)
     1.8 -  {
     1.9 -    def contains[B](that: Node[B]): Boolean = this.range contains that.range
    1.10 -    def restrict(r: Text.Range): Node[A] = Node(range.restrict(r), info)
    1.11 -  }
    1.12 -
    1.13 -
    1.14    /* branches sorted by quasi-order -- overlapping ranges appear as equivalent */
    1.15  
    1.16    object Branches
    1.17    {
    1.18 -    type Entry = (Node[Any], Markup_Tree)
    1.19 -    type T = SortedMap[Node[Any], Entry]
    1.20 +    type Entry = (Text.Info[Any], Markup_Tree)
    1.21 +    type T = SortedMap[Text.Info[Any], Entry]
    1.22  
    1.23 -    val empty = SortedMap.empty[Node[Any], Entry](new scala.math.Ordering[Node[Any]]
    1.24 +    val empty = SortedMap.empty[Text.Info[Any], Entry](new scala.math.Ordering[Text.Info[Any]]
    1.25        {
    1.26 -        def compare(x: Node[Any], y: Node[Any]): Int = x.range compare y.range
    1.27 +        def compare(x: Text.Info[Any], y: Text.Info[Any]): Int = x.range compare y.range
    1.28        })
    1.29  
    1.30      def update(branches: T, entry: Entry): T = branches + (entry._1 -> entry)
    1.31  
    1.32      def overlapping(range: Text.Range, branches: T): T =
    1.33      {
    1.34 -      val start = Node[Any](Text.Range(range.start), Nil)
    1.35 -      val stop = Node[Any](Text.Range(range.stop), Nil)
    1.36 +      val start = Text.Info[Any](Text.Range(range.start), Nil)
    1.37 +      val stop = Text.Info[Any](Text.Range(range.stop), Nil)
    1.38        branches.get(stop) match {
    1.39          case Some(end) if range overlaps end._1.range =>
    1.40            update(branches.range(start, stop), end)
    1.41 @@ -64,49 +57,50 @@
    1.42        case list => list.mkString("Tree(", ",", ")")
    1.43      }
    1.44  
    1.45 -  def + (new_node: Node[Any]): Markup_Tree =
    1.46 +  def + (new_info: Text.Info[Any]): Markup_Tree =
    1.47    {
    1.48 -    branches.get(new_node) match {
    1.49 +    branches.get(new_info) match {
    1.50        case None =>
    1.51 -        new Markup_Tree(Branches.update(branches, new_node -> empty))
    1.52 -      case Some((node, subtree)) =>
    1.53 -        if (node.range != new_node.range && node.contains(new_node))
    1.54 -          new Markup_Tree(Branches.update(branches, node -> (subtree + new_node)))
    1.55 -        else if (new_node.contains(branches.head._1) && new_node.contains(branches.last._1))
    1.56 -          new Markup_Tree(Branches.update(Branches.empty, (new_node -> this)))
    1.57 +        new Markup_Tree(Branches.update(branches, new_info -> empty))
    1.58 +      case Some((info, subtree)) =>
    1.59 +        if (info.range != new_info.range && info.contains(new_info))
    1.60 +          new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
    1.61 +        else if (new_info.contains(branches.head._1) && new_info.contains(branches.last._1))
    1.62 +          new Markup_Tree(Branches.update(Branches.empty, (new_info -> this)))
    1.63          else {
    1.64 -          val body = Branches.overlapping(new_node.range, branches)
    1.65 -          if (body.forall(e => new_node.contains(e._1))) {
    1.66 +          val body = Branches.overlapping(new_info.range, branches)
    1.67 +          if (body.forall(e => new_info.contains(e._1))) {
    1.68              val rest = (branches /: body) { case (bs, (e, _)) => bs - e }
    1.69 -            new Markup_Tree(Branches.update(rest, new_node -> new Markup_Tree(body)))
    1.70 +            new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
    1.71            }
    1.72            else { // FIXME split markup!?
    1.73 -            System.err.println("Ignored overlapping markup: " + new_node)
    1.74 +            System.err.println("Ignored overlapping markup information: " + new_info)
    1.75              this
    1.76            }
    1.77          }
    1.78      }
    1.79    }
    1.80  
    1.81 -  def select[A](root: Node[A])(sel: PartialFunction[Any, A]): Stream[Node[A]] =
    1.82 +  def select[A](root: Text.Info[A])(sel: PartialFunction[Text.Info[Any], A]): Stream[Text.Info[A]] =
    1.83    {
    1.84 -    def stream(parent: Node[A], bs: Branches.T): Stream[Node[A]] =
    1.85 +    def stream(parent: Text.Info[A], bs: Branches.T): Stream[Text.Info[A]] =
    1.86      {
    1.87        val substream =
    1.88 -        (for ((_, (node, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
    1.89 -          if (sel.isDefinedAt(node.info)) {
    1.90 -            val current = Node(node.range.restrict(parent.range), sel(node.info))
    1.91 +        (for ((_, (info, subtree)) <- Branches.overlapping(parent.range, bs).toStream) yield {
    1.92 +          if (sel.isDefinedAt(info)) {
    1.93 +            val current = Text.Info(info.range.restrict(parent.range), sel(info))
    1.94              stream(current, subtree.branches)
    1.95            }
    1.96 -          else stream(parent.restrict(node.range), subtree.branches)
    1.97 +          else stream(parent.restrict(info.range), subtree.branches)
    1.98          }).flatten
    1.99  
   1.100 -      def padding(last: Text.Offset, s: Stream[Node[A]]): Stream[Node[A]] =
   1.101 +      def padding(last: Text.Offset, s: Stream[Text.Info[A]]): Stream[Text.Info[A]] =
   1.102          s match {
   1.103 -          case (node @ Node(Text.Range(start, stop), _)) #:: rest =>
   1.104 +          case info #:: rest =>
   1.105 +            val Text.Range(start, stop) = info.range
   1.106              if (last < start)
   1.107 -              parent.restrict(Text.Range(last, start)) #:: node #:: padding(stop, rest)
   1.108 -            else node #:: padding(stop, rest)
   1.109 +              parent.restrict(Text.Range(last, start)) #:: info #:: padding(stop, rest)
   1.110 +            else info #:: padding(stop, rest)
   1.111            case Stream.Empty =>
   1.112              if (last < parent.range.stop)
   1.113                Stream(parent.restrict(Text.Range(last, parent.range.stop)))
   1.114 @@ -118,10 +112,11 @@
   1.115      stream(root, branches)
   1.116    }
   1.117  
   1.118 -  def swing_tree(parent: DefaultMutableTreeNode)(swing_node: Node[Any] => DefaultMutableTreeNode)
   1.119 +  def swing_tree(parent: DefaultMutableTreeNode)
   1.120 +    (swing_node: Text.Info[Any] => DefaultMutableTreeNode)
   1.121    {
   1.122 -    for ((_, (node, subtree)) <- branches) {
   1.123 -      val current = swing_node(node)
   1.124 +    for ((_, (info, subtree)) <- branches) {
   1.125 +      val current = swing_node(info)
   1.126        subtree.swing_tree(current)(swing_node)
   1.127        parent.add(current)
   1.128      }