src/Pure/PIDE/line.scala
author wenzelm
Thu, 09 Mar 2017 11:34:24 +0100
changeset 65157 cd977a5bd928
parent 64877 31e9920a0dc1
child 65159 0ae97858d8b3
permissions -rw-r--r--
clarified Document.offset: including final position; support Document.change according to VSCode;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64611
d72d63d05bdb clarified modules;
wenzelm
parents: 64610
diff changeset
     1
/*  Title:      Pure/PIDE/line.scala
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     3
65157
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
     4
Line-oriented text documents, with some bias towards VSCode.
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     5
*/
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     6
64611
d72d63d05bdb clarified modules;
wenzelm
parents: 64610
diff changeset
     7
package isabelle
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     8
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     9
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    10
import scala.annotation.tailrec
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    11
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    12
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    13
object Line
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    14
{
64806
99f49258b02b more robust treatment of logical lines;
wenzelm
parents: 64800
diff changeset
    15
  /* logical lines */
99f49258b02b more robust treatment of logical lines;
wenzelm
parents: 64800
diff changeset
    16
99f49258b02b more robust treatment of logical lines;
wenzelm
parents: 64800
diff changeset
    17
  def normalize(text: String): String =
99f49258b02b more robust treatment of logical lines;
wenzelm
parents: 64800
diff changeset
    18
    if (text.contains('\r')) text.replace("\r\n", "\n") else text
99f49258b02b more robust treatment of logical lines;
wenzelm
parents: 64800
diff changeset
    19
99f49258b02b more robust treatment of logical lines;
wenzelm
parents: 64800
diff changeset
    20
  def logical_lines(text: String): List[String] =
99f49258b02b more robust treatment of logical lines;
wenzelm
parents: 64800
diff changeset
    21
    Library.split_lines(normalize(text))
99f49258b02b more robust treatment of logical lines;
wenzelm
parents: 64800
diff changeset
    22
99f49258b02b more robust treatment of logical lines;
wenzelm
parents: 64800
diff changeset
    23
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    24
  /* position */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    25
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    26
  object Position
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    27
  {
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    28
    val zero: Position = Position()
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    29
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    30
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    31
  sealed case class Position(line: Int = 0, column: Int = 0)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    32
  {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    33
    def line1: Int = line + 1
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    34
    def column1: Int = column + 1
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    35
    def print: String = line1.toString + ":" + column1.toString
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    36
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    37
    def compare(that: Position): Int =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    38
      line compare that.line match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    39
        case 0 => column compare that.column
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    40
        case i => i
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    41
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    42
64682
7e119f32276a clarified modules;
wenzelm
parents: 64681
diff changeset
    43
    def advance(text: String, text_length: Text.Length): Position =
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
    44
      if (text.isEmpty) this
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
    45
      else {
64806
99f49258b02b more robust treatment of logical lines;
wenzelm
parents: 64800
diff changeset
    46
        val lines = logical_lines(text)
64619
e3d9a31281f3 proper reset of column (amending 01e50039edc9);
wenzelm
parents: 64617
diff changeset
    47
        val l = line + lines.length - 1
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
    48
        val c = (if (l == line) column else 0) + text_length(Library.trim_line(lines.last))
64619
e3d9a31281f3 proper reset of column (amending 01e50039edc9);
wenzelm
parents: 64617
diff changeset
    49
        Position(l, c)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    50
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    51
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    52
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    53
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    54
  /* range (right-open interval) */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    55
64647
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    56
  object Range
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    57
  {
64666
wenzelm
parents: 64651
diff changeset
    58
    def apply(start: Position): Range = Range(start, start)
wenzelm
parents: 64651
diff changeset
    59
    val zero: Range = Range(Position.zero)
64647
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    60
  }
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    61
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    62
  sealed case class Range(start: Position, stop: Position)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    63
  {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    64
    if (start.compare(stop) > 0)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    65
      error("Bad line range: " + start.print + ".." + stop.print)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    66
64647
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    67
    def print: String =
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    68
      if (start == stop) start.print
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    69
      else start.print + ".." + stop.print
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    70
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    71
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    72
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    73
  /* positions within document node */
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    74
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64650
diff changeset
    75
  sealed case class Node_Position(name: String, pos: Position = Position.zero)
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    76
  {
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    77
    def line: Int = pos.line
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    78
    def column: Int = pos.column
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    79
  }
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    80
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64650
diff changeset
    81
  sealed case class Node_Range(name: String, range: Range = Range.zero)
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    82
  {
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    83
    def start: Position = range.start
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    84
    def stop: Position = range.stop
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    85
  }
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    86
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    87
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    88
  /* document with newline as separator (not terminator) */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    89
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    90
  object Document
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    91
  {
64683
c0c09b6dfbe0 clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents: 64682
diff changeset
    92
    def apply(text: String, text_length: Text.Length): Document =
64820
00488a8c042f Line.Document consists of independently allocated strings;
wenzelm
parents: 64806
diff changeset
    93
      Document(logical_lines(text).map(s => Line(Library.trim_substring(s))), text_length)
65157
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
    94
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
    95
    def lines(text: String): List[Line] = apply(text, Text.Length).lines
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
    96
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
    97
    private def chop(lines: List[Line]): (String, List[Line]) =
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
    98
      lines match {
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
    99
        case Nil => ("", Nil)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   100
        case List(line) => (line.text, Nil)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   101
        case line :: rest => (line.text + "\n", rest)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   102
      }
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   103
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   104
    private def length(lines: List[Line]): Int =
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   105
      if (lines.isEmpty) 0
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   106
      else ((0 /: lines) { case (n, line) => n + line.text.length + 1 }) - 1
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   107
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   108
    def text(lines: List[Line]): String = lines.mkString("", "\n", "")
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   109
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   110
64683
c0c09b6dfbe0 clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents: 64682
diff changeset
   111
  sealed case class Document(lines: List[Line], text_length: Text.Length)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   112
  {
65157
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   113
    lazy val text_range: Text.Range = Text.Range(0, Document.length(lines))
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   114
    lazy val text: String = Document.text(lines)
64672
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
   115
64877
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64830
diff changeset
   116
    def try_get_text(range: Text.Range): Option[String] =
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64830
diff changeset
   117
      if (text_range.contains(range)) Some(text.substring(range.start, range.stop))
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64830
diff changeset
   118
      else None
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64830
diff changeset
   119
64821
37bf7acf6a4b clarified lazy text content;
wenzelm
parents: 64820
diff changeset
   120
    override def toString: String = text
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   121
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   122
    override def equals(that: Any): Boolean =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   123
      that match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   124
        case other: Document => lines == other.lines
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   125
        case _ => false
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   126
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   127
    override def hashCode(): Int = lines.hashCode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   128
64683
c0c09b6dfbe0 clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents: 64682
diff changeset
   129
    def position(text_offset: Text.Offset): Position =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   130
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   131
      @tailrec def move(i: Text.Offset, lines_count: Int, lines_rest: List[Line]): Position =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   132
      {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   133
        lines_rest match {
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
   134
          case Nil => require(i == 0); Position(lines_count)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   135
          case line :: ls =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   136
            val n = line.text.length
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   137
            if (ls.isEmpty || i <= n)
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   138
              Position(lines_count).advance(line.text.substring(n - i), text_length)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   139
            else move(i - (n + 1), lines_count + 1, ls)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   140
        }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   141
      }
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   142
      move(text_offset, 0, lines)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   143
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   144
64683
c0c09b6dfbe0 clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents: 64682
diff changeset
   145
    def range(text_range: Text.Range): Range =
c0c09b6dfbe0 clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents: 64682
diff changeset
   146
      Range(position(text_range.start), position(text_range.stop))
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64672
diff changeset
   147
64683
c0c09b6dfbe0 clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents: 64682
diff changeset
   148
    def offset(pos: Position): Option[Text.Offset] =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   149
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   150
      val l = pos.line
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   151
      val c = pos.column
65157
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   152
      val n = lines.length
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   153
      if (0 <= l && l < n) {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   154
        val line_offset =
65157
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   155
          (0 /: lines.iterator.take(l)) { case (n, line) => n + line.text.length + 1 }
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   156
        text_length.offset(lines(l).text, c).map(line_offset + _)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   157
      }
65157
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   158
      else if (l == n && c == 0) Some(text_range.length)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   159
      else None
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   160
    }
65157
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   161
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   162
    def change(remove: Range, insert: String): Option[(List[Text.Edit], Document)] =
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   163
    {
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   164
      for {
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   165
        edit_start <- offset(remove.start)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   166
        if remove.stop == remove.start || offset(remove.stop).isDefined
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   167
        l1 = remove.start.line
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   168
        l2 = remove.stop.line
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   169
        if l1 <= l2
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   170
        (removed_text, changed_lines) <-
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   171
        {
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   172
          val (prefix, lines1) = lines.splitAt(l1)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   173
          val (s1, rest1) = Document.chop(lines1)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   174
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   175
          if (l1 == l2) {
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   176
            for {
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   177
              c1 <- text_length.offset(s1, remove.start.column)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   178
              c2 <- text_length.offset(s1, remove.stop.column)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   179
              if c1 <= c2
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   180
            }
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   181
            yield {
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   182
              val removed_text = s1.substring(c1, c2)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   183
              val changed_text = s1.substring(0, c1) + insert + Library.trim_line(s1.substring(c2))
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   184
              (removed_text, prefix ::: Document.lines(changed_text) ::: rest1)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   185
            }
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   186
          }
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   187
          else {
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   188
            val (middle, lines2) = rest1.splitAt(l2 - l1 - 1)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   189
            val (s2, rest2) = Document.chop(lines2)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   190
            for {
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   191
              c1 <- text_length.offset(s1, remove.start.column)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   192
              c2 <- text_length.offset(s2, remove.stop.column)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   193
            }
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   194
            yield {
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   195
              val r1 = Library.trim_line(s1.substring(c1))
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   196
              val r2 = s2.substring(0, c2)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   197
              val removed_text =
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   198
                if (lines1.isEmpty) ""
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   199
                else if (lines2.isEmpty) Document.text(Line(r1) :: middle)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   200
                else Document.text(Line(r1) :: middle ::: List(Line(r2)))
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   201
              val changed_text = s1.substring(0, c1) + insert + Library.trim_line(s2.substring(c2))
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   202
              (removed_text, prefix ::: Document.lines(changed_text) ::: rest2)
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   203
            }
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   204
          }
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   205
        }
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   206
      }
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   207
      yield
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   208
        (Text.Edit.removes(edit_start, removed_text) ::: Text.Edit.inserts(edit_start, insert),
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   209
          Document(changed_lines, text_length))
cd977a5bd928 clarified Document.offset: including final position;
wenzelm
parents: 64877
diff changeset
   210
    }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   211
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   212
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   213
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   214
  /* line text */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   215
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   216
  val empty: Line = new Line("")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   217
  def apply(text: String): Line = if (text == "") empty else new Line(text)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   218
}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   219
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   220
final class Line private(val text: String)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   221
{
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   222
  require(text.forall(c => c != '\r' && c != '\n'))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   223
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   224
  override def equals(that: Any): Boolean =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   225
    that match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   226
      case other: Line => text == other.text
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   227
      case _ => false
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   228
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   229
  override def hashCode(): Int = text.hashCode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   230
  override def toString: String = text
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   231
}