src/Pure/PIDE/line.scala
author wenzelm
Wed, 28 Dec 2016 17:02:38 +0100
changeset 64681 642b6105e6f4
parent 64679 b2bf280b7e13
child 64682 7e119f32276a
permissions -rw-r--r--
clarified signature: explicit Length to avoid implicit mistakes;
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
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     4
Line-oriented text.
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
{
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    15
  /* position */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    16
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    17
  object Position
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    18
  {
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    19
    val zero: Position = Position()
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    20
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    21
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    22
  sealed case class Position(line: Int = 0, column: Int = 0)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    23
  {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    24
    def line1: Int = line + 1
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    25
    def column1: Int = column + 1
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    26
    def print: String = line1.toString + ":" + column1.toString
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    27
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    28
    def compare(that: Position): Int =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    29
      line compare that.line match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    30
        case 0 => column compare that.column
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    31
        case i => i
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
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
    34
    def advance(text: String, text_length: Length): Position =
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
    35
      if (text.isEmpty) this
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
    36
      else {
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
    37
        val lines = Library.split_lines(text)
64619
e3d9a31281f3 proper reset of column (amending 01e50039edc9);
wenzelm
parents: 64617
diff changeset
    38
        val l = line + lines.length - 1
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
    39
        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
    40
        Position(l, c)
64605
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
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    43
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    44
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    45
  /* range (right-open interval) */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    46
64647
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    47
  object Range
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    48
  {
64666
wenzelm
parents: 64651
diff changeset
    49
    def apply(start: Position): Range = Range(start, start)
wenzelm
parents: 64651
diff changeset
    50
    val zero: Range = Range(Position.zero)
64647
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    51
  }
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    52
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    53
  sealed case class Range(start: Position, stop: Position)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    54
  {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    55
    if (start.compare(stop) > 0)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    56
      error("Bad line range: " + start.print + ".." + stop.print)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    57
64647
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    58
    def print: String =
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    59
      if (start == stop) start.print
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    60
      else start.print + ".." + stop.print
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    61
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    62
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    63
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    64
  /* positions within document node */
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    65
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64650
diff changeset
    66
  sealed case class Node_Position(name: String, pos: Position = Position.zero)
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    67
  {
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    68
    def line: Int = pos.line
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    69
    def column: Int = pos.column
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    70
  }
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    71
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64650
diff changeset
    72
  sealed case class Node_Range(name: String, range: Range = Range.zero)
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    73
  {
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    74
    def start: Position = range.start
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    75
    def stop: Position = range.stop
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    76
  }
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    77
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    78
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    79
  /* document with newline as separator (not terminator) */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    80
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    81
  object Document
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    82
  {
64672
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
    83
    val empty: Document = new Document(Nil)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    84
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    85
    def apply(lines: List[Line]): Document =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    86
      if (lines.isEmpty) empty
64672
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
    87
      else new Document(lines)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    88
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    89
    def apply(text: String): Document =
64672
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
    90
      if (text == "") empty
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
    91
      else if (text.contains('\r'))
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
    92
        new Document(Library.split_lines(text).map(s => Line(Library.trim_line(s))))
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
    93
      else
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
    94
        new Document(Library.split_lines(text).map(s => Line(s)))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    95
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    96
64672
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
    97
  final class Document private(val lines: List[Line])
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    98
  {
64672
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
    99
    def make_text: String = lines.mkString("", "\n", "")
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
   100
d8e0619abb60 clarified document: no stored text;
wenzelm
parents: 64666
diff changeset
   101
    override def toString: String = make_text
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   102
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   103
    override def equals(that: Any): Boolean =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   104
      that match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   105
        case other: Document => lines == other.lines
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   106
        case _ => false
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   107
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   108
    override def hashCode(): Int = lines.hashCode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   109
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   110
    def position(text_offset: Text.Offset, text_length: Length): Position =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   111
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   112
      @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
   113
      {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   114
        lines_rest match {
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
   115
          case Nil => require(i == 0); Position(lines_count)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   116
          case line :: ls =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   117
            val n = line.text.length
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   118
            if (ls.isEmpty || i <= n)
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   119
              Position(lines_count).advance(line.text.substring(n - i), text_length)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   120
            else move(i - (n + 1), lines_count + 1, ls)
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
      }
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   123
      move(text_offset, 0, lines)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   124
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   125
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   126
    def range(text_range: Text.Range, text_length: Length): Range =
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64672
diff changeset
   127
      Range(
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   128
        position(text_range.start, text_length),
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   129
        position(text_range.stop, text_length))
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64672
diff changeset
   130
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   131
    def offset(pos: Position, text_length: Length): Option[Text.Offset] =
64605
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
      val l = pos.line
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   134
      val c = pos.column
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   135
      if (0 <= l && l < lines.length) {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   136
        val line_offset =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   137
          if (l == 0) 0
64681
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   138
          else (0 /: lines.iterator.take(l - 1)) { case (n, line) => n + text_length(line.text) + 1 }
642b6105e6f4 clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents: 64679
diff changeset
   139
        text_length.offset(lines(l).text, c).map(line_offset + _)
64605
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
      else None
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   142
    }
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
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   145
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   146
  /* line text */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   147
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   148
  val empty: Line = new Line("")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   149
  def apply(text: String): Line = if (text == "") empty else new Line(text)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   150
}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   151
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   152
final class Line private(val text: String)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   153
{
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   154
  require(text.forall(c => c != '\r' && c != '\n'))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   155
64610
1b89608974e9 clarified modules;
wenzelm
parents: 64605
diff changeset
   156
  lazy val length_codepoints: Int = Codepoint.iterator(text).length
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   157
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   158
  override def equals(that: Any): Boolean =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   159
    that match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   160
      case other: Line => text == other.text
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   161
      case _ => false
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   162
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   163
  override def hashCode(): Int = text.hashCode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   164
  override def toString: String = text
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   165
}