src/Pure/PIDE/line.scala
author wenzelm
Wed, 21 Dec 2016 22:49:53 +0100
changeset 64651 ea5620f7b0d8
parent 64650 011629dda937
child 64666 f6c6e25ef782
permissions -rw-r--r--
clarified signature;
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
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
    34
    def advance(text: String, length: Length = Length): Position =
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
e3d9a31281f3 proper reset of column (amending 01e50039edc9);
wenzelm
parents: 64617
diff changeset
    39
        val c = (if (l == line) column else 0) + length(Library.trim_line(lines.last))
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
  {
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    49
    val zero: Range = Range(Position.zero, Position.zero)
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    50
  }
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    51
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    52
  sealed case class Range(start: Position, stop: Position)
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
    if (start.compare(stop) > 0)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    55
      error("Bad line range: " + start.print + ".." + stop.print)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    56
64647
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    57
    def print: String =
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    58
      if (start == stop) start.print
bbfcef118acb clarified border cases;
wenzelm
parents: 64619
diff changeset
    59
      else start.print + ".." + stop.print
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    60
  }
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
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    63
  /* positions within document node */
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    64
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64650
diff changeset
    65
  sealed case class Node_Position(name: String, pos: Position = Position.zero)
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    66
  {
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    67
    def line: Int = pos.line
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    68
    def column: Int = pos.column
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    69
  }
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    70
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64650
diff changeset
    71
  sealed case class Node_Range(name: String, range: Range = Range.zero)
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    72
  {
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    73
    def start: Position = range.start
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    74
    def stop: Position = range.stop
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
    75
  }
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    76
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    77
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    78
  /* document with newline as separator (not terminator) */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    79
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    80
  object Document
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    81
  {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    82
    val empty: Document = new Document("", Nil)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    83
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    84
    def apply(lines: List[Line]): Document =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    85
      if (lines.isEmpty) empty
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    86
      else new Document(lines.mkString("", "\n", ""), lines)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    87
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    88
    def apply(text: String): Document =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    89
      if (text.contains('\r'))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    90
        apply(Library.cat_lines(Library.split_lines(text).map(Library.trim_line(_))))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    91
      else if (text == "") Document.empty
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    92
      else new Document(text, Library.split_lines(text).map(Line(_)))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    93
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    94
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    95
  final class Document private(val text: String, val lines: List[Line])
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    96
  {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    97
    override def toString: String = text
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    98
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    99
    override def equals(that: Any): Boolean =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   100
      that match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   101
        case other: Document => lines == other.lines
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   102
        case _ => false
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   103
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   104
    override def hashCode(): Int = lines.hashCode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   105
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   106
    def position(offset: Text.Offset, length: Length = Length): Position =
64605
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
      @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
   109
      {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   110
        lines_rest match {
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
   111
          case Nil => require(i == 0); Position(lines_count)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   112
          case line :: ls =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   113
            val n = line.text.length
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   114
            if (ls.isEmpty || i <= n)
64650
011629dda937 tuned signature;
wenzelm
parents: 64649
diff changeset
   115
              Position(lines_count).advance(line.text.substring(n - i), length)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   116
            else move(i - (n + 1), lines_count + 1, ls)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   117
        }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   118
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   119
      move(offset, 0, lines)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   120
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   121
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   122
    def offset(pos: Position, length: Length = Length): Option[Text.Offset] =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   123
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   124
      val l = pos.line
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   125
      val c = pos.column
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   126
      if (0 <= l && l < lines.length) {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   127
        val line_offset =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   128
          if (l == 0) 0
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   129
          else (0 /: lines.iterator.take(l - 1))({ case (n, line) => n + length(line.text) + 1 })
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   130
        length.offset(lines(l).text, c).map(line_offset + _)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   131
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   132
      else None
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   133
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   134
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   135
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   136
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   137
  /* line text */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   138
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   139
  val empty: Line = new Line("")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   140
  def apply(text: String): Line = if (text == "") empty else new Line(text)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   141
}
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
final class Line private(val text: String)
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
  require(text.forall(c => c != '\r' && c != '\n'))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   146
64610
1b89608974e9 clarified modules;
wenzelm
parents: 64605
diff changeset
   147
  lazy val length_codepoints: Int = Codepoint.iterator(text).length
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   148
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   149
  override def equals(that: Any): Boolean =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   150
    that match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   151
      case other: Line => text == other.text
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   152
      case _ => false
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
  override def hashCode(): Int = text.hashCode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   155
  override def toString: String = text
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   156
}