src/Pure/PIDE/line.scala
author wenzelm
Mon Mar 13 21:37:35 2017 +0100 (2017-03-13 ago)
changeset 65214 a2ec0db555c7
parent 65203 314246c6eeaa
child 65234 1d6e9048cb62
permissions -rw-r--r--
clarified modules;
wenzelm@64612
     1
/*  Title:      Pure/PIDE/line.scala
wenzelm@64605
     2
    Author:     Makarius
wenzelm@64605
     3
wenzelm@65157
     4
Line-oriented text documents, with some bias towards VSCode.
wenzelm@64605
     5
*/
wenzelm@64605
     6
wenzelm@64612
     7
package isabelle
wenzelm@64605
     8
wenzelm@64605
     9
wenzelm@64605
    10
import scala.annotation.tailrec
wenzelm@64605
    11
wenzelm@64605
    12
wenzelm@64605
    13
object Line
wenzelm@64605
    14
{
wenzelm@64806
    15
  /* logical lines */
wenzelm@64806
    16
wenzelm@64806
    17
  def normalize(text: String): String =
wenzelm@64806
    18
    if (text.contains('\r')) text.replace("\r\n", "\n") else text
wenzelm@64806
    19
wenzelm@64806
    20
  def logical_lines(text: String): List[String] =
wenzelm@64806
    21
    Library.split_lines(normalize(text))
wenzelm@64806
    22
wenzelm@64806
    23
wenzelm@64605
    24
  /* position */
wenzelm@64605
    25
wenzelm@64605
    26
  object Position
wenzelm@64605
    27
  {
wenzelm@64650
    28
    val zero: Position = Position()
wenzelm@64605
    29
  }
wenzelm@64605
    30
wenzelm@64650
    31
  sealed case class Position(line: Int = 0, column: Int = 0)
wenzelm@64605
    32
  {
wenzelm@64605
    33
    def line1: Int = line + 1
wenzelm@64605
    34
    def column1: Int = column + 1
wenzelm@64605
    35
    def print: String = line1.toString + ":" + column1.toString
wenzelm@64605
    36
wenzelm@64605
    37
    def compare(that: Position): Int =
wenzelm@64605
    38
      line compare that.line match {
wenzelm@64605
    39
        case 0 => column compare that.column
wenzelm@64605
    40
        case i => i
wenzelm@64605
    41
      }
wenzelm@64605
    42
wenzelm@65196
    43
    def advance(text: String): Position =
wenzelm@64618
    44
      if (text.isEmpty) this
wenzelm@64618
    45
      else {
wenzelm@64806
    46
        val lines = logical_lines(text)
wenzelm@64620
    47
        val l = line + lines.length - 1
wenzelm@65196
    48
        val c = (if (l == line) column else 0) + Library.trim_line(lines.last).length
wenzelm@64620
    49
        Position(l, c)
wenzelm@64605
    50
      }
wenzelm@64605
    51
  }
wenzelm@64605
    52
wenzelm@64605
    53
wenzelm@64605
    54
  /* range (right-open interval) */
wenzelm@64605
    55
wenzelm@64647
    56
  object Range
wenzelm@64647
    57
  {
wenzelm@64666
    58
    def apply(start: Position): Range = Range(start, start)
wenzelm@64666
    59
    val zero: Range = Range(Position.zero)
wenzelm@64647
    60
  }
wenzelm@64647
    61
wenzelm@64605
    62
  sealed case class Range(start: Position, stop: Position)
wenzelm@64605
    63
  {
wenzelm@64605
    64
    if (start.compare(stop) > 0)
wenzelm@64605
    65
      error("Bad line range: " + start.print + ".." + stop.print)
wenzelm@64605
    66
wenzelm@64647
    67
    def print: String =
wenzelm@64647
    68
      if (start == stop) start.print
wenzelm@64647
    69
      else start.print + ".." + stop.print
wenzelm@64605
    70
  }
wenzelm@64605
    71
wenzelm@64605
    72
wenzelm@64649
    73
  /* positions within document node */
wenzelm@64649
    74
wenzelm@64651
    75
  sealed case class Node_Position(name: String, pos: Position = Position.zero)
wenzelm@64650
    76
  {
wenzelm@64650
    77
    def line: Int = pos.line
wenzelm@64650
    78
    def column: Int = pos.column
wenzelm@64650
    79
  }
wenzelm@64650
    80
wenzelm@64651
    81
  sealed case class Node_Range(name: String, range: Range = Range.zero)
wenzelm@64650
    82
  {
wenzelm@64650
    83
    def start: Position = range.start
wenzelm@64650
    84
    def stop: Position = range.stop
wenzelm@64650
    85
  }
wenzelm@64649
    86
wenzelm@64649
    87
wenzelm@64605
    88
  /* document with newline as separator (not terminator) */
wenzelm@64605
    89
wenzelm@64605
    90
  object Document
wenzelm@64605
    91
  {
wenzelm@65196
    92
    def apply(text: String): Document =
wenzelm@65196
    93
      Document(logical_lines(text).map(s => Line(Library.trim_substring(s))))
wenzelm@65196
    94
wenzelm@65196
    95
    val empty: Document = apply("")
wenzelm@65157
    96
wenzelm@65159
    97
    private def split(line_text: String): List[Line] =
wenzelm@65196
    98
      if (line_text == "") List(Line.empty) else apply(line_text).lines
wenzelm@65157
    99
wenzelm@65157
   100
    private def chop(lines: List[Line]): (String, List[Line]) =
wenzelm@65157
   101
      lines match {
wenzelm@65157
   102
        case Nil => ("", Nil)
wenzelm@65159
   103
        case line :: rest => (line.text, rest)
wenzelm@65157
   104
      }
wenzelm@65157
   105
wenzelm@65157
   106
    private def length(lines: List[Line]): Int =
wenzelm@65157
   107
      if (lines.isEmpty) 0
wenzelm@65157
   108
      else ((0 /: lines) { case (n, line) => n + line.text.length + 1 }) - 1
wenzelm@65157
   109
wenzelm@65157
   110
    def text(lines: List[Line]): String = lines.mkString("", "\n", "")
wenzelm@64605
   111
  }
wenzelm@64605
   112
wenzelm@65196
   113
  sealed case class Document(lines: List[Line])
wenzelm@64605
   114
  {
wenzelm@65197
   115
    lazy val text_length: Text.Offset = Document.length(lines)
wenzelm@65197
   116
    def text_range: Text.Range = Text.Range(0, text_length)
wenzelm@65197
   117
wenzelm@65157
   118
    lazy val text: String = Document.text(lines)
wenzelm@64672
   119
wenzelm@64877
   120
    def try_get_text(range: Text.Range): Option[String] =
wenzelm@64877
   121
      if (text_range.contains(range)) Some(text.substring(range.start, range.stop))
wenzelm@64877
   122
      else None
wenzelm@64877
   123
wenzelm@64821
   124
    override def toString: String = text
wenzelm@64605
   125
wenzelm@64605
   126
    override def equals(that: Any): Boolean =
wenzelm@64605
   127
      that match {
wenzelm@64605
   128
        case other: Document => lines == other.lines
wenzelm@64605
   129
        case _ => false
wenzelm@64605
   130
      }
wenzelm@64605
   131
    override def hashCode(): Int = lines.hashCode
wenzelm@64605
   132
wenzelm@64683
   133
    def position(text_offset: Text.Offset): Position =
wenzelm@64605
   134
    {
wenzelm@64605
   135
      @tailrec def move(i: Text.Offset, lines_count: Int, lines_rest: List[Line]): Position =
wenzelm@64605
   136
      {
wenzelm@64605
   137
        lines_rest match {
wenzelm@64650
   138
          case Nil => require(i == 0); Position(lines_count)
wenzelm@64605
   139
          case line :: ls =>
wenzelm@64605
   140
            val n = line.text.length
wenzelm@64618
   141
            if (ls.isEmpty || i <= n)
wenzelm@65196
   142
              Position(lines_count).advance(line.text.substring(n - i))
wenzelm@64605
   143
            else move(i - (n + 1), lines_count + 1, ls)
wenzelm@64605
   144
        }
wenzelm@64605
   145
      }
wenzelm@64681
   146
      move(text_offset, 0, lines)
wenzelm@64605
   147
    }
wenzelm@64605
   148
wenzelm@64683
   149
    def range(text_range: Text.Range): Range =
wenzelm@64683
   150
      Range(position(text_range.start), position(text_range.stop))
wenzelm@64679
   151
wenzelm@64683
   152
    def offset(pos: Position): Option[Text.Offset] =
wenzelm@64605
   153
    {
wenzelm@64605
   154
      val l = pos.line
wenzelm@64605
   155
      val c = pos.column
wenzelm@65157
   156
      val n = lines.length
wenzelm@65157
   157
      if (0 <= l && l < n) {
wenzelm@65196
   158
        if (0 <= c && c <= lines(l).text.length) {
wenzelm@65196
   159
          val line_offset =
wenzelm@65196
   160
            (0 /: lines.iterator.take(l)) { case (n, line) => n + line.text.length + 1 }
wenzelm@65196
   161
          Some(line_offset + c)
wenzelm@65196
   162
        }
wenzelm@65196
   163
        else None
wenzelm@64605
   164
      }
wenzelm@65197
   165
      else if (l == n && c == 0) Some(text_length)
wenzelm@64605
   166
      else None
wenzelm@64605
   167
    }
wenzelm@65157
   168
wenzelm@65157
   169
    def change(remove: Range, insert: String): Option[(List[Text.Edit], Document)] =
wenzelm@65157
   170
    {
wenzelm@65157
   171
      for {
wenzelm@65157
   172
        edit_start <- offset(remove.start)
wenzelm@65157
   173
        if remove.stop == remove.start || offset(remove.stop).isDefined
wenzelm@65157
   174
        l1 = remove.start.line
wenzelm@65157
   175
        l2 = remove.stop.line
wenzelm@65157
   176
        if l1 <= l2
wenzelm@65159
   177
        (removed_text, new_lines) <-
wenzelm@65157
   178
        {
wenzelm@65196
   179
          val c1 = remove.start.column
wenzelm@65196
   180
          val c2 = remove.stop.column
wenzelm@65196
   181
wenzelm@65157
   182
          val (prefix, lines1) = lines.splitAt(l1)
wenzelm@65157
   183
          val (s1, rest1) = Document.chop(lines1)
wenzelm@65157
   184
wenzelm@65157
   185
          if (l1 == l2) {
wenzelm@65196
   186
            if (0 <= c1 && c1 <= c2 && c2 <= s1.length) {
wenzelm@65196
   187
              Some(
wenzelm@65203
   188
                if (lines1.isEmpty) ("", prefix ::: Document.split(insert))
wenzelm@65196
   189
                else {
wenzelm@65196
   190
                  val removed_text = s1.substring(c1, c2)
wenzelm@65196
   191
                  val changed_text = s1.substring(0, c1) + insert + s1.substring(c2)
wenzelm@65196
   192
                  (removed_text, prefix ::: Document.split(changed_text) ::: rest1)
wenzelm@65196
   193
                })
wenzelm@65157
   194
            }
wenzelm@65196
   195
            else None
wenzelm@65157
   196
          }
wenzelm@65157
   197
          else {
wenzelm@65157
   198
            val (middle, lines2) = rest1.splitAt(l2 - l1 - 1)
wenzelm@65157
   199
            val (s2, rest2) = Document.chop(lines2)
wenzelm@65196
   200
            if (0 <= c1 && c1 <= s1.length && 0 <= c2 && c2 <= s2.length) {
wenzelm@65196
   201
              Some(
wenzelm@65203
   202
                if (lines1.isEmpty) ("", prefix ::: Document.split(insert))
wenzelm@65196
   203
                else {
wenzelm@65196
   204
                  val r1 = s1.substring(c1)
wenzelm@65196
   205
                  val r2 = s2.substring(0, c2)
wenzelm@65196
   206
                  val removed_text =
wenzelm@65196
   207
                    if (lines2.isEmpty) Document.text(Line(r1) :: middle)
wenzelm@65196
   208
                    else Document.text(Line(r1) :: middle ::: List(Line(r2)))
wenzelm@65196
   209
                  val changed_text = s1.substring(0, c1) + insert + s2.substring(c2)
wenzelm@65196
   210
                  (removed_text, prefix ::: Document.split(changed_text) ::: rest2)
wenzelm@65196
   211
                })
wenzelm@65157
   212
            }
wenzelm@65196
   213
            else None
wenzelm@65157
   214
          }
wenzelm@65157
   215
        }
wenzelm@65157
   216
      }
wenzelm@65157
   217
      yield
wenzelm@65157
   218
        (Text.Edit.removes(edit_start, removed_text) ::: Text.Edit.inserts(edit_start, insert),
wenzelm@65196
   219
          Document(new_lines))
wenzelm@65157
   220
    }
wenzelm@64605
   221
  }
wenzelm@64605
   222
wenzelm@64605
   223
wenzelm@64605
   224
  /* line text */
wenzelm@64605
   225
wenzelm@64605
   226
  val empty: Line = new Line("")
wenzelm@64605
   227
  def apply(text: String): Line = if (text == "") empty else new Line(text)
wenzelm@64605
   228
}
wenzelm@64605
   229
wenzelm@64605
   230
final class Line private(val text: String)
wenzelm@64605
   231
{
wenzelm@64605
   232
  require(text.forall(c => c != '\r' && c != '\n'))
wenzelm@64605
   233
wenzelm@64605
   234
  override def equals(that: Any): Boolean =
wenzelm@64605
   235
    that match {
wenzelm@64605
   236
      case other: Line => text == other.text
wenzelm@64605
   237
      case _ => false
wenzelm@64605
   238
    }
wenzelm@64605
   239
  override def hashCode(): Int = text.hashCode
wenzelm@64605
   240
  override def toString: String = text
wenzelm@64605
   241
}