src/Pure/PIDE/line.scala
changeset 64611 d72d63d05bdb
parent 64610 1b89608974e9
child 64614 88211daacf93
equal deleted inserted replaced
64610:1b89608974e9 64611:d72d63d05bdb
       
     1 /*  Title:      Pure/PIDE/line.scala
       
     2     Author:     Makarius
       
     3 
       
     4 Line-oriented text.
       
     5 */
       
     6 
       
     7 package isabelle
       
     8 
       
     9 
       
    10 import scala.annotation.tailrec
       
    11 
       
    12 
       
    13 object Line
       
    14 {
       
    15   /* position */
       
    16 
       
    17   object Position
       
    18   {
       
    19     val zero: Position = Position(0, 0)
       
    20   }
       
    21 
       
    22   sealed case class Position(line: Int, column: Int)
       
    23   {
       
    24     def line1: Int = line + 1
       
    25     def column1: Int = column + 1
       
    26     def print: String = line1.toString + ":" + column1.toString
       
    27 
       
    28     def compare(that: Position): Int =
       
    29       line compare that.line match {
       
    30         case 0 => column compare that.column
       
    31         case i => i
       
    32       }
       
    33 
       
    34     private def advance[A](iterator: Iterator[A], is_newline: A => Boolean): Position =
       
    35     {
       
    36       var l = line
       
    37       var c = column
       
    38       for (x <- iterator) {
       
    39         if (is_newline(x)) { l += 1; c = 0 } else c += 1
       
    40       }
       
    41       Position(l, c)
       
    42     }
       
    43 
       
    44     def advance(text: String): Position =
       
    45       if (text.isEmpty) this else advance[Char](text.iterator, _ == '\n')
       
    46 
       
    47     def advance_symbols(text: String): Position =
       
    48       if (text.isEmpty) this else advance[Symbol.Symbol](Symbol.iterator(text), Symbol.is_newline _)
       
    49 
       
    50     def advance_codepoints(text: String): Position =
       
    51       if (text.isEmpty) this else advance[Int](Codepoint.iterator(text), _ == 10)
       
    52   }
       
    53 
       
    54 
       
    55   /* range (right-open interval) */
       
    56 
       
    57   sealed case class Range(start: Position, stop: Position)
       
    58   {
       
    59     if (start.compare(stop) > 0)
       
    60       error("Bad line range: " + start.print + ".." + stop.print)
       
    61 
       
    62     def print: String = start.print + ".." + stop.print
       
    63   }
       
    64 
       
    65 
       
    66   /* document with newline as separator (not terminator) */
       
    67 
       
    68   object Document
       
    69   {
       
    70     val empty: Document = new Document("", Nil)
       
    71 
       
    72     def apply(lines: List[Line]): Document =
       
    73       if (lines.isEmpty) empty
       
    74       else new Document(lines.mkString("", "\n", ""), lines)
       
    75 
       
    76     def apply(text: String): Document =
       
    77       if (text.contains('\r'))
       
    78         apply(Library.cat_lines(Library.split_lines(text).map(Library.trim_line(_))))
       
    79       else if (text == "") Document.empty
       
    80       else new Document(text, Library.split_lines(text).map(Line(_)))
       
    81   }
       
    82 
       
    83   final class Document private(val text: String, val lines: List[Line])
       
    84   {
       
    85     override def toString: String = text
       
    86 
       
    87     override def equals(that: Any): Boolean =
       
    88       that match {
       
    89         case other: Document => lines == other.lines
       
    90         case _ => false
       
    91       }
       
    92     override def hashCode(): Int = lines.hashCode
       
    93 
       
    94     private def position(advance: (Position, String) => Position, offset: Text.Offset): Position =
       
    95     {
       
    96       @tailrec def move(i: Text.Offset, lines_count: Int, lines_rest: List[Line]): Position =
       
    97       {
       
    98         lines_rest match {
       
    99           case Nil => require(i == 0); Position(lines_count, 0)
       
   100           case line :: ls =>
       
   101             val n = line.text.length
       
   102             if (ls.isEmpty || i <= n) advance(Position(lines_count, 0), line.text.substring(n - i))
       
   103             else move(i - (n + 1), lines_count + 1, ls)
       
   104         }
       
   105       }
       
   106       move(offset, 0, lines)
       
   107     }
       
   108 
       
   109     def position(i: Text.Offset): Position = position(_.advance(_), i)
       
   110     def position_symbols(i: Text.Offset): Position = position(_.advance_symbols(_), i)
       
   111     def position_codepoints(i: Text.Offset): Position = position(_.advance_codepoints(_), i)
       
   112 
       
   113     // FIXME symbols, codepoints
       
   114     def offset(pos: Position): Option[Text.Offset] =
       
   115     {
       
   116       val l = pos.line
       
   117       val c = pos.column
       
   118       if (0 <= l && l < lines.length) {
       
   119         val line_offset =
       
   120           if (l == 0) 0
       
   121           else (0 /: lines.take(l - 1))({ case (n, line) => n + line.text.length + 1 })
       
   122         if (c <= lines(l).text.length) Some(line_offset + c) else None
       
   123       }
       
   124       else None
       
   125     }
       
   126   }
       
   127 
       
   128 
       
   129   /* line text */
       
   130 
       
   131   val empty: Line = new Line("")
       
   132   def apply(text: String): Line = if (text == "") empty else new Line(text)
       
   133 }
       
   134 
       
   135 final class Line private(val text: String)
       
   136 {
       
   137   require(text.forall(c => c != '\r' && c != '\n'))
       
   138 
       
   139   lazy val length_symbols: Int = Symbol.iterator(text).length
       
   140   lazy val length_codepoints: Int = Codepoint.iterator(text).length
       
   141 
       
   142   override def equals(that: Any): Boolean =
       
   143     that match {
       
   144       case other: Line => text == other.text
       
   145       case _ => false
       
   146     }
       
   147   override def hashCode(): Int = text.hashCode
       
   148   override def toString: String = text
       
   149 }