-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Decorators.scala
238 lines (210 loc) · 7.88 KB
/
Decorators.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package dotty.tools
package dotc
package core
import annotation.tailrec
import Symbols._
import Contexts._, Names._, Phases._, printing.Texts._, printing.Printer
import util.Spans.Span
import collection.mutable.ListBuffer
import dotty.tools.dotc.transform.MegaPhase
import ast.tpd._
import scala.language.implicitConversions
import printing.Formatting._
/** This object provides useful implicit decorators for types defined elsewhere */
object Decorators {
/** Extension methods for toType/TermName methods on strings.
* They are in an implicit object for now, so that we can import decorators
* with a normal wildcard. In the future, once #9255 is in trunk, replace with
* a simple collective extension.
*/
implicit object PreNamedString:
extension (pn: PreName) def toTypeName: TypeName = pn match
case s: String => typeName(s)
case n: Name => n.toTypeName
extension (pn: PreName) def toTermName: TermName = pn match
case s: String => termName(s)
case n: Name => n.toTermName
extension (s: String):
def splitWhere(f: Char => Boolean, doDropIndex: Boolean): Option[(String, String)] = {
def splitAt(idx: Int, doDropIndex: Boolean): Option[(String, String)] =
if (idx == -1) None
else Some((s.take(idx), s.drop(if (doDropIndex) idx + 1 else idx)))
splitAt(s.indexWhere(f), doDropIndex)
}
/** Implements a findSymbol method on iterators of Symbols that
* works like find but avoids Option, replacing None with NoSymbol.
*/
extension (it: Iterator[Symbol]):
final def findSymbol(p: Symbol => Boolean): Symbol = {
while (it.hasNext) {
val sym = it.next()
if (p(sym)) return sym
}
NoSymbol
}
final val MaxFilterRecursions = 1000
/** Implements filterConserve, zipWithConserve methods
* on lists that avoid duplication of list nodes where feasible.
*/
implicit class ListDecorator[T](val xs: List[T]) extends AnyVal {
final def mapconserve[U](f: T => U): List[U] = {
@tailrec
def loop(mapped: ListBuffer[U], unchanged: List[U], pending: List[T]): List[U] =
if (pending.isEmpty)
if (mapped eq null) unchanged
else mapped.prependToList(unchanged)
else {
val head0 = pending.head
val head1 = f(head0)
if (head1.asInstanceOf[AnyRef] eq head0.asInstanceOf[AnyRef])
loop(mapped, unchanged, pending.tail)
else {
val b = if (mapped eq null) new ListBuffer[U] else mapped
var xc = unchanged
while (xc ne pending) {
b += xc.head
xc = xc.tail
}
b += head1
val tail0 = pending.tail
loop(b, tail0.asInstanceOf[List[U]], tail0)
}
}
loop(null, xs.asInstanceOf[List[U]], xs)
}
/** Like `xs filter p` but returns list `xs` itself - instead of a copy -
* if `p` is true for all elements and `xs` is not longer
* than `MaxFilterRecursions`.
*/
def filterConserve(p: T => Boolean): List[T] = {
def loop(xs: List[T], nrec: Int): List[T] = xs match {
case Nil => xs
case x :: xs1 =>
if (nrec < MaxFilterRecursions) {
val ys1 = loop(xs1, nrec + 1)
if (p(x))
if (ys1 eq xs1) xs else x :: ys1
else
ys1
}
else xs filter p
}
loop(xs, 0)
}
/** Like `xs.lazyZip(ys).map(f)`, but returns list `xs` itself
* - instead of a copy - if function `f` maps all elements of
* `xs` to themselves. Also, it is required that `ys` is at least
* as long as `xs`.
*/
def zipWithConserve[U](ys: List[U])(f: (T, U) => T): List[T] =
if (xs.isEmpty || ys.isEmpty) Nil
else {
val x1 = f(xs.head, ys.head)
val xs1 = xs.tail.zipWithConserve(ys.tail)(f)
if ((x1.asInstanceOf[AnyRef] eq xs.head.asInstanceOf[AnyRef]) &&
(xs1 eq xs.tail)) xs
else x1 :: xs1
}
/** Like `xs.lazyZip(xs.indices).map(f)`, but returns list `xs` itself
* - instead of a copy - if function `f` maps all elements of
* `xs` to themselves.
*/
def mapWithIndexConserve[U <: T](f: (T, Int) => U): List[U] =
def recur(xs: List[T], idx: Int): List[U] =
if xs.isEmpty then Nil
else
val x1 = f(xs.head, idx)
val xs1 = recur(xs.tail, idx + 1)
if (x1.asInstanceOf[AnyRef] eq xs.head.asInstanceOf[AnyRef])
&& (xs1 eq xs.tail)
then xs.asInstanceOf[List[U]]
else x1 :: xs1
recur(xs, 0)
final def hasSameLengthAs[U](ys: List[U]): Boolean = {
@tailrec def loop(xs: List[T], ys: List[U]): Boolean =
if (xs.isEmpty) ys.isEmpty
else ys.nonEmpty && loop(xs.tail, ys.tail)
loop(xs, ys)
}
@tailrec final def eqElements(ys: List[AnyRef]): Boolean = xs match {
case x :: _ =>
ys match {
case y :: _ =>
x.asInstanceOf[AnyRef].eq(y) &&
xs.tail.eqElements(ys.tail)
case _ => false
}
case nil => ys.isEmpty
}
/** Union on lists seen as sets */
def | (ys: List[T]): List[T] = xs ::: (ys filterNot (xs contains _))
/** Intersection on lists seen as sets */
def & (ys: List[T]): List[T] = xs filter (ys contains _)
}
extension [T, U](xss: List[List[T]]):
def nestedMap(f: T => U): List[List[U]] =
xss.map(_.map(f))
def nestedMapConserve(f: T => U): List[List[U]] =
xss.mapconserve(_.mapconserve(f))
def nestedZipWithConserve(yss: List[List[U]])(f: (T, U) => T): List[List[T]] =
xss.zipWithConserve(yss)((xs, ys) => xs.zipWithConserve(ys)(f))
end extension
extension (text: Text):
def show(using Context): String = text.mkString(ctx.settings.pageWidth.value, ctx.settings.printLines.value)
/** Test whether a list of strings representing phases contains
* a given phase. See [[config.CompilerCommand#explainAdvanced]] for the
* exact meaning of "contains" here.
*/
extension (names: List[String]) {
def containsPhase(phase: Phase): Boolean =
names.nonEmpty && {
phase match {
case phase: MegaPhase => phase.miniPhases.exists(x => names.containsPhase(x))
case _ =>
names exists { name =>
name == "all" || {
val strippedName = name.stripSuffix("+")
val logNextPhase = name != strippedName
phase.phaseName.startsWith(strippedName) ||
(logNextPhase && phase.prev.phaseName.startsWith(strippedName))
}
}
}
}
}
extension [T](x: T) {
def reporting(
op: WrappedResult[T] ?=> String,
printer: config.Printers.Printer = config.Printers.default): T = {
printer.println(op(using WrappedResult(x)))
x
}
}
extension [T](x: T) {
def assertingErrorsReported(using Context): T = {
assert(ctx.reporter.errorsReported)
x
}
def assertingErrorsReported(msg: => String)(using Context): T = {
assert(ctx.reporter.errorsReported, msg)
x
}
}
extension (sc: StringContext) {
/** General purpose string formatting */
def i(args: Any*)(using Context): String =
new StringFormatter(sc).assemble(args)
/** Formatting for error messages: Like `i` but suppress follow-on
* error messages after the first one if some of their arguments are "non-sensical".
*/
def em(args: Any*)(using Context): String =
new ErrorMessageFormatter(sc).assemble(args)
/** Formatting with added explanations: Like `em`, but add explanations to
* give more info about type variables and to disambiguate where needed.
*/
def ex(args: Any*)(using Context): String =
explained(em(args: _*))
}
extension [T <: AnyRef](arr: Array[T]):
def binarySearch(x: T): Int = java.util.Arrays.binarySearch(arr.asInstanceOf[Array[Object]], x)
}