src/Pure/PIDE/line.scala
author wenzelm
Wed, 21 Dec 2016 22:27:38 +0100
changeset 64649 d67c3094a0c2
parent 64647 bbfcef118acb
child 64650 011629dda937
permissions -rw-r--r--
tuned signature -- more explicit types;
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
  {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    19
    val zero: Position = Position(0, 0)
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
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    22
  sealed case class Position(line: Int, column: Int)
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
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    65
  sealed case class Position_Node(pos: Position, name: String)
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    66
  sealed case class Range_Node(range: Range, name: String)
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    67
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64647
diff changeset
    68
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    69
  /* document with newline as separator (not terminator) */
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
  object Document
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    72
  {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    73
    val empty: Document = new Document("", Nil)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    74
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    75
    def apply(lines: List[Line]): Document =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    76
      if (lines.isEmpty) empty
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    77
      else new Document(lines.mkString("", "\n", ""), lines)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    78
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    79
    def apply(text: String): Document =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    80
      if (text.contains('\r'))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    81
        apply(Library.cat_lines(Library.split_lines(text).map(Library.trim_line(_))))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    82
      else if (text == "") Document.empty
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    83
      else new Document(text, Library.split_lines(text).map(Line(_)))
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
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    86
  final class Document private(val text: String, val lines: List[Line])
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
    override def toString: String = text
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
    override def equals(that: Any): Boolean =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    91
      that match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    92
        case other: Document => lines == other.lines
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    93
        case _ => false
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
    override def hashCode(): Int = lines.hashCode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    96
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
    97
    def position(offset: Text.Offset, length: Length = Length): Position =
64605
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
      @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
   100
      {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   101
        lines_rest match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   102
          case Nil => require(i == 0); Position(lines_count, 0)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   103
          case line :: ls =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   104
            val n = line.text.length
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   105
            if (ls.isEmpty || i <= n)
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   106
              Position(lines_count, 0).advance(line.text.substring(n - i), length)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   107
            else move(i - (n + 1), lines_count + 1, ls)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   108
        }
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
      move(offset, 0, lines)
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
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   113
    def offset(pos: Position, length: Length = Length): Option[Text.Offset] =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   114
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   115
      val l = pos.line
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   116
      val c = pos.column
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   117
      if (0 <= l && l < lines.length) {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   118
        val line_offset =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   119
          if (l == 0) 0
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64615
diff changeset
   120
          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
   121
        length.offset(lines(l).text, c).map(line_offset + _)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   122
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   123
      else None
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
  }
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
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   128
  /* line text */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   129
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   130
  val empty: Line = new Line("")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   131
  def apply(text: String): Line = if (text == "") empty else new Line(text)
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
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   134
final class Line private(val text: String)
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
  require(text.forall(c => c != '\r' && c != '\n'))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   137
64610
1b89608974e9 clarified modules;
wenzelm
parents: 64605
diff changeset
   138
  lazy val length_codepoints: Int = Codepoint.iterator(text).length
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   139
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   140
  override def equals(that: Any): Boolean =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   141
    that match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   142
      case other: Line => text == other.text
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   143
      case _ => false
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
  override def hashCode(): Int = text.hashCode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   146
  override def toString: String = text
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   147
}