/
TokenCollection.scala
198 lines (150 loc) · 5.74 KB
/
TokenCollection.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
Copyright 2013 Ilya Lakhin (Илья Александрович Лахин)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package name.lakhin.eliah.projects
package papacarlo.lexis
import name.lakhin.eliah.projects.papacarlo.utils.{Difference, Signal, Bounds}
import scala.util.control.Breaks._
final class TokenCollection(lineCutTokens: Set[String]) {
private[papacarlo] var descriptions = IndexedSeq(Token.lineBreak)
private[papacarlo] var references = IndexedSeq(new TokenReference(this, 0))
private[papacarlo] var head = new TokenReference(this, 0)
private[papacarlo] var last = new TokenReference(this, 0)
val onBeforeRewrite = new Signal[Bounds]
val onAfterRewrite = new Signal[Bounds]
def cursor(tokenIndex: Int) = {
var result = (1, 1)
var currentIndex = 0
breakable {
for (token <- descriptions) {
currentIndex += 1
if (currentIndex > tokenIndex) break()
if (token.kind == Token.LineBreakKind) result = (result._1 + 1, 1)
else result = (result._1, result._2 + token.value.length)
}
}
result
}
def range(range: Bounds) = {
var result = ""
if (range.defined) {
val begin = cursor(range.from)
result += "(" + begin._1 + ":" + begin._2 + ")"
if (range.length > 0) {
val end = cursor(range.until)
result += " - (" + end._1 + ":" + end._2 + ")"
}
}
result
}
def write(bounds: Bounds, replacement: Seq[Token]) {
val oldTokens = bounds.slice(descriptions)
var equal = false
if (oldTokens.length == replacement.length) {
val tokenPairs = oldTokens.zip(replacement)
if (tokenPairs.forall(pair => pair._1.sameAs(pair._2))) {
val references = bounds.slice(this.references)
if (references.forall(_.onUpdate.nonEmpty)) {
equal = true
descriptions = bounds.replace(descriptions, replacement)
for (((oldToken, newToken), reference) <- tokenPairs.zip(references))
if (oldToken.value != newToken.value)
reference.onUpdate.trigger(reference)
}
}
}
if (!equal) {
val diff = computeDifference(oldTokens, replacement)
val range = bounds.enlarge(-diff._1, -diff._2)
onBeforeRewrite.trigger(range)
val newTokens = replacement.slice(diff._1, replacement.size - diff._2)
val newRange = Bounds(range.from, range.from + newTokens.size)
for (removeTarget <- range.slice(references)) removeTarget.remove()
val rightHandOffset = newTokens.size - range.length
for (rightHand <- references.slice(range.until, references.size))
rightHand.index += rightHandOffset
last.index += rightHandOffset
descriptions = range.replace(descriptions, newTokens)
references = range.replace(
references,
(for (index <- newRange.iterator)
yield new TokenReference(this, index)).toSeq
)
onAfterRewrite.trigger(newRange)
}
}
def highlight(segment: Bounds, limit: Option[Int] = None): String =
highlightSegments(List((segment, "<<<", ">>>")), limit = limit)
def highlightSegments(segments: List[(Bounds, String, String)],
context: Boolean = false,
limit: Option[Int] = None) = {
var currentContext = ""
val tokens = descriptions.zipWithIndex.map {
case (token, index) =>
var view = token.value
if (context) {
val contextView = token.context.view
if (currentContext != contextView ||
segments.exists(segment => segment._1.from == index ||
segment._1.until - 1 == index)) {
currentContext = contextView
view += "~" + currentContext + "~"
}
}
for ((range, start, end) <- segments) {
if (range.length > 0) {
if (range.from == index) {
view = start + view
}
if (range.until - 1 == index) {
view = view + end
}
}
else if (range.from == index) {
view = start + end + view
}
}
view
}
limit match {
case Some(limit: Int) =>
val lines = tokens.mkString.split("\n", -1)
val starts = segments.map(_._2)
val ends = segments.map(_._3)
val firstLine =
(lines.indexWhere(line => starts.exists(line.contains)) -
limit) max 0
val lastLine =
(lines.lastIndexWhere(line => ends.exists(line.contains)) +
limit + 1) min lines.size
var result = lines.slice(firstLine, lastLine).mkString("\n")
if (firstLine > 0)
result = "..." + firstLine + "\n" + result
if (lastLine < lines.size)
result = result + "\n" + (lastLine + 1) + "..."
result
case None => tokens.mkString
}
}
private def computeDifference(first: Seq[Token], second: Seq[Token]) = {
val comparator =
(pair: Pair[Token, Token]) => pair._1.value == pair._2.value
if (first.exists(token => lineCutTokens(token.kind))
|| second.exists(token => lineCutTokens(token.kind)))
(
Difference.head[Token](first, second, comparator),
0
)
else
Difference.double[Token](first, second, comparator)
}
}