-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlaneText.scala
182 lines (152 loc) · 4.65 KB
/
PlaneText.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// PlaneText.scala
// Copyright (c) 2015 J. M. Spivey
/** An extension of Text that keeps track of the division of the
* text into lines. */
class PlaneText(init: Int) extends Text() {
/* For present purposes, we count each newline character as part of the
* line it terminates, so that every line has non-zero length. Let's
* also imagine that a special terminator character is added to the end of
* the text, so that the very last line also has non-zero length. For an
* ordinary text file that ends in a newline, this last line will be empty
* and be counted as having length 1, and the editor will count the
* file as having one more line than there are newline characters. */
private var nlines = 1
private var linelen = new Array[Int](init)
linelen(0) = 1
/* For efficiency, we keep track of the beginning of the most recent line
* that was accessed. This will help a lot if accesses are clustered.
* The invariant is linestart = sum linelen[0..curline) */
private var curline = 0
private var linestart = 0
def this() { this(1000) }
/** Return the number of lines, including the fictitious last line. */
def numLines: Int = nlines
/** Return the length of a line in the file */
def getLineLength(n: Int): Int = linelen(n)
/** Find the line number corresponding to a character index. */
def getRow(pos: Int): Int = {
findPos(pos); curline
}
/** Find the column number of a character index in its line. */
def getColumn(pos: Int): Int = {
findPos(pos); pos - linestart
}
// Augment the mutator methods of Text to maintain the line map
override def clear() {
super.clear()
nlines = 1
linelen(0) = 1
curline = 0
linestart = 0
}
override def insert(pos: Int, ch: Char) {
super.insert(pos, ch)
findPos(pos)
if (ch != '\n')
linelen(curline) += 1
else
mapLines()
}
override def insert(pos: Int, s: String) {
super.insert(pos, s)
mapLines()
}
override def insertRange(pos: Int, t: Text, start: Int, nchars: Int) {
super.insertRange(pos, t, start, nchars)
mapLines()
}
override def insertFile(pos: Int, in: java.io.Reader) {
try {
super.insertFile(pos, in)
} finally {
// Even if an IOException is thrown, we still update the line map
mapLines()
}
}
override def deleteChar(pos: Int) {
val ch = charAt(pos)
super.deleteChar(pos)
findPos(pos)
if (ch != '\n')
linelen(curline) -= 1
else
mapLines()
}
override def deleteRange(start: Int, len: Int) {
super.deleteRange(start, len)
findPos(start)
if (start + len < linestart + linelen(curline))
linelen(curline) -= len
else
mapLines()
}
/** Return the editing position closest to the specified coordinates */
def getPos(row: Int, col: Int): Int = {
val r = Math.min(Math.max(row, 0), nlines - 1)
findLine(r)
val c = Math.min(Math.max(col, 0), linelen(curline) - 1)
linestart + c
}
/** Fetch the text of line n, without the trailing newline */
def fetchLine(n: Int, buf: Text) {
findLine(n)
getRange(linestart, linelen(n) - 1, buf)
}
/** Refresh the line map by scanning the whole file.
* This is always a last resort if we choose not to update the
* line map in a faster way. */
private def mapLines() {
nlines = 0
var c = 0
for (i <- 0 until length) {
c += 1
if (charAt(i) == '\n') {
lineRoom()
linelen(nlines) = c
nlines += 1; c = 0
}
}
lineRoom()
linelen(nlines) = c + 1
nlines += 1
// Reset the cache
curline = 0; linestart = 0
}
/** Set curline to a specified line number. */
private def findLine(n: Int) {
assert(n >= 0 && n < nlines)
// Move forwards if necessary
while (n > curline) {
linestart += linelen(curline)
curline += 1
}
// Move backwards if necessary
while (n < curline) {
curline -= 1
linestart -= linelen(curline)
}
}
/** Set current line so that it contains a specified character index. */
private def findPos(j: Int) {
assert(j >= 0 && j <= length)
// Move forwards if necessary
while (j >= linestart + linelen(curline)) {
linestart += linelen(curline)
curline += 1
}
// Move backwards if necessary
while (j < linestart) {
curline -= 1
linestart -= linelen(curline)
}
assert(linestart <= j && j < linestart + linelen(curline))
}
/** Find room for one more line */
private def lineRoom() {
if (nlines >= linelen.length) {
val newlen = new Array[Int](2 * linelen.length)
Array.copy(linelen, 0, newlen, 0, nlines)
linelen = newlen
}
}
}