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