isac-java/src/java/isac/gui/mawen/syntax/isabelle/text.scala
author Walther Neuper <wneuper@ist.tugraz.at>
Wed, 28 Nov 2018 08:59:29 +0100
changeset 5232 34f18fdc3103
parent 5229 6bf0e95981e3
child 5239 b4e3883d7b66
permissions -rw-r--r--
update Scala 2.10 to 2.12.3
     1 /*  THIS IS A COPY OF ...
     2     Title:      Pure/PIDE/text.scala
     3     Author:     Fabian Immler, TU Munich
     4     Author:     Makarius
     5     ... WITH ADDITIONS AFTER "/---..---\"
     6 
     7 Basic operations on plain text.
     8 */
     9 
    10 package isac.gui.mawen.syntax.isabelle
    11 
    12 
    13 import scala.collection.mutable
    14 import scala.util.Sorting
    15 
    16 import info.hupel.isabelle.api.XML // + for Isabelle/Isac
    17 
    18 
    19 object Text
    20 {
    21   /* offset */
    22 
    23   type Offset = Int
    24 
    25 
    26   /* range -- with total quasi-ordering */
    27 
    28   object Range
    29   {
    30     def apply(start: Offset): Range = Range(start, start)
    31 
    32     val offside: Range = apply(-1)
    33 
    34     object Ordering extends scala.math.Ordering[Text.Range]
    35     {
    36       def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
    37     }
    38   }
    39 
    40   sealed case class Range(start: Offset, stop: Offset)
    41   {
    42     // denotation: {start} Un {i. start < i & i < stop}
    43     if (start > stop)
    44       Console.printf("Bad range: [" + start.toString + ":" + stop.toString + "]")
    45 
    46     override def toString: String = "[" + start.toString + ":" + stop.toString + "]"
    47 
    48     def length: Int = stop - start
    49 
    50     def map(f: Offset => Offset): Range = Range(f(start), f(stop))
    51     def +(i: Offset): Range = if (i == 0) this else map(_ + i)
    52     def -(i: Offset): Range = if (i == 0) this else map(_ - i)
    53 
    54     def is_singularity: Boolean = start == stop
    55     def inflate_singularity: Range = if (is_singularity) Range(start, start + 1) else this
    56 
    57     def touches(i: Offset): Boolean = start <= i && i <= stop
    58 
    59     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    60     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    61     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    62     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    63 
    64     def apart(that: Range): Boolean =
    65       (this.start max that.start) > (this.stop min that.stop)
    66 
    67     def restrict(that: Range): Range =
    68       Range(this.start max that.start, this.stop min that.stop)
    69 
    70     def try_restrict(that: Range): Option[Range] =
    71       if (this apart that) None
    72       else Some(restrict(that))
    73 
    74     def try_join(that: Range): Option[Range] =
    75       if (this apart that) None
    76       else Some(Range(this.start min that.start, this.stop max that.stop))
    77   }
    78 
    79 
    80   /* perspective */
    81 
    82   object Perspective
    83   {
    84     val empty: Perspective = Perspective(Nil)
    85 
    86     def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
    87 
    88     def apply(ranges: Seq[Range]): Perspective =
    89     {
    90       val result = new mutable.ListBuffer[Text.Range]
    91       var last: Option[Text.Range] = None
    92       def ship(next: Option[Range]) { result ++= last; last = next }
    93 
    94       for (range <- ranges.sortBy(_.start))
    95       {
    96         last match {
    97           case None => ship(Some(range))
    98           case Some(last_range) =>
    99             last_range.try_join(range) match {
   100               case None => ship(Some(range))
   101               case joined => last = joined
   102             }
   103         }
   104       }
   105       ship(None)
   106       new Perspective(result.toList)
   107     }
   108   }
   109 
   110   final class Perspective private(
   111     val ranges: List[Range]) // visible text partitioning in canonical order
   112   {
   113     def is_empty: Boolean = ranges.isEmpty
   114     def range: Range =
   115       if (is_empty) Range(0)
   116       else Range(ranges.head.start, ranges.last.stop)
   117 
   118     override def hashCode: Int = ranges.hashCode
   119     override def equals(that: Any): Boolean =
   120       that match {
   121         case other: Perspective => ranges == other.ranges
   122         case _ => false
   123       }
   124     override def toString: String = ranges.toString
   125   }
   126 
   127 
   128   /* information associated with text range */
   129 
   130   sealed case class Info[A](range: Text.Range, info: A)
   131   {
   132     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
   133     def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
   134   }
   135 
   136   type Markup = Info[XML.Elem]
   137 
   138 
   139   /* editing */
   140 
   141   object Edit
   142   {
   143     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   144     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   145   }
   146 
   147   final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   148   {
   149     override def toString: String =
   150       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   151 
   152 
   153     /* transform offsets */
   154 
   155     private def transform(do_insert: Boolean, i: Offset): Offset =
   156       if (i < start) i
   157       else if (do_insert) i + text.length
   158       else (i - text.length) max start
   159 
   160     def convert(i: Offset): Offset = transform(is_insert, i)
   161     def revert(i: Offset): Offset = transform(!is_insert, i)
   162 
   163 
   164     /* edit strings */
   165 
   166     private def insert(i: Offset, string: String): String =
   167       string.substring(0, i) + text + string.substring(i)
   168 
   169     private def remove(i: Offset, count: Int, string: String): String =
   170       string.substring(0, i) + string.substring(i + count)
   171 
   172     def can_edit(string: String, shift: Int): Boolean =
   173       shift <= start && start < shift + string.length
   174 
   175     def edit(string: String, shift: Int): (Option[Edit], String) =
   176       if (!can_edit(string, shift)) (Some(this), string)
   177       else if (is_insert) (None, insert(start - shift, string))
   178       else {
   179         val i = start - shift
   180         val count = text.length min (string.length - i)
   181         val rest =
   182           if (count == text.length) None
   183           else Some(Edit.remove(start, text.substring(count)))
   184         (rest, remove(i, count, string))
   185       }
   186   }
   187   
   188 //----------------- ADDITIONS TO Isabelle2016-1 FOR ISAC -----------------\\
   189   
   190 }