-
-
Notifications
You must be signed in to change notification settings - Fork 365
/
ApiImpls.scala
136 lines (116 loc) · 3.83 KB
/
ApiImpls.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
package ammonite.repl
import ammonite.ops.Internals
import ammonite.runtime._
import ammonite.util.Util._
import ammonite.util._
import java.awt.Toolkit
import java.awt.datatransfer.{DataFlavor, StringSelection}
import scala.collection.mutable
class SessionApiImpl(frames0: => StableRef[List[Frame]]) extends Session{
def frames = frames0()
val namedFrames = mutable.Map.empty[String, List[Frame]]
def childFrame(parent: Frame) = new Frame(
new SpecialClassLoader(
parent.classloader,
parent.classloader.classpathSignature,
parent.classloader.specialLocalClasses
),
new SpecialClassLoader(
parent.pluginClassloader,
parent.pluginClassloader.classpathSignature,
parent.pluginClassloader.specialLocalClasses
),
parent.imports,
parent.classpath,
parent.usedEarlierDefinitions
)
def save(name: String = "") = {
if (name != "") namedFrames(name) = frames
// freezing the frame will trigger the creation of a new one later on,
// so that the saved one won't change any more
frames.head.freeze()
frames0() = frames
}
def pop(num: Int = 1) = {
var next = frames
for(i <- 0 until num){
if (next.tail != Nil) next = next.tail
}
val out = SessionChanged.delta(frames.head, next.head)
// freezing the current frame, so that the result of the current command,
// that tangles with sessions, isn't added to it
next.head.freeze()
frames0() = next
out
}
def load(name: String = "") = {
val next = if (name == "") frames.tail else namedFrames(name)
val out = SessionChanged.delta(frames.head, next.head)
// freezing the current frame, so that the result of the current command,
// that tangles with sessions, isn't added to it
next.head.freeze()
frames0() = next
out
}
def delete(name: String) = {
namedFrames.remove(name)
}
}
trait ReplApiImpl extends FullReplAPI{
implicit def tprintColorsImplicit = pprint.TPrintColors(
typeColor = colors().`type`()
)
implicit val codeColorsImplicit = new CodeColors{
def comment = colors().comment()
def `type` = colors().`type`()
def literal = colors().literal()
def keyword = colors().keyword()
def ident = colors().ident()
}
implicit val pprinter: Ref[pprint.PPrinter] = Ref.live(() =>
pprint.PPrinter.Color.copy(
defaultHeight = height / 2,
defaultWidth = width,
colorLiteral = colors().literal(),
colorApplyPrefix = colors().prefix(),
additionalHandlers = PPrints.replPPrintHandlers
)
)
def show(t: Any) = show(t, null, 9999999, null)
def printer: Printer
override def show(t: Any,
width: Integer = null,
height: Integer = 9999999,
indent: Integer = null) = {
pprinter()
.tokenize(
t,
width = if (width == null) pprinter().defaultWidth else width,
height = if (height == null) pprinter().defaultHeight else height,
indent = if (indent == null) pprinter().defaultIndent else indent
)
.map(_.render)
.foreach(printer.outStream.print)
printer.outStream.print(newLine)
}
def sess: SessionApiImpl
override def clipboard: Clipboard = ClipboardImpl
}
object ClipboardImpl extends Clipboard {
private lazy val systemClipboard =
Toolkit.getDefaultToolkit.getSystemClipboard
override def read: String =
Option(systemClipboard.getContents(null)) collect {
case data if data.isDataFlavorSupported(DataFlavor.stringFlavor) =>
data.getTransferData(DataFlavor.stringFlavor)
} match {
case Some(str: String) => str
case _ => ""
}
override def write(data: Internals.Writable): Unit = {
val newContents = new StringSelection(
data.writeableData.map(new String(_)).mkString
)
systemClipboard.setContents(newContents, newContents)
}
}