aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/repl/AmmoniteReader.scala
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2016-04-22 16:15:03 +0200
committerFelix Mulder <felix.mulder@gmail.com>2016-04-28 11:00:39 +0200
commit53bd25f7e2082a787936ae833b14f873a07ff22c (patch)
tree6ecb2d392eb12a3d82cffc45c5a1b23f3c85c39d /src/dotty/tools/dotc/repl/AmmoniteReader.scala
parent96fcdd9da51e1febe9e320f774424b5ac3f8ff3d (diff)
downloaddotty-53bd25f7e2082a787936ae833b14f873a07ff22c.tar.gz
dotty-53bd25f7e2082a787936ae833b14f873a07ff22c.tar.bz2
dotty-53bd25f7e2082a787936ae833b14f873a07ff22c.zip
Initial implementation featuring two different highlighters
One was implemted by hand and the other by using dotty's parser. The one built by hand is shorter, and behaves correctly. The scanner one is unfortunately not ready for testing - there are too many things that are workarounds for it to be a good solution as of now The code added from Ammonite is licensed under MIT, not sure where to put the license - but will add it once I know.
Diffstat (limited to 'src/dotty/tools/dotc/repl/AmmoniteReader.scala')
-rw-r--r--src/dotty/tools/dotc/repl/AmmoniteReader.scala71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/dotty/tools/dotc/repl/AmmoniteReader.scala b/src/dotty/tools/dotc/repl/AmmoniteReader.scala
new file mode 100644
index 000000000..0c4dd80ee
--- /dev/null
+++ b/src/dotty/tools/dotc/repl/AmmoniteReader.scala
@@ -0,0 +1,71 @@
+package dotty.tools
+package dotc
+package repl
+
+import core.Contexts._
+import ammonite.terminal._
+import LazyList._
+import Ansi.Color
+import filters._
+import BasicFilters._
+import GUILikeFilters._
+import util.SourceFile
+
+class AmmoniteReader extends InteractiveReader {
+ val interactive = true
+
+ val reader = new java.io.InputStreamReader(System.in)
+ val writer = new java.io.OutputStreamWriter(System.out)
+ val cutPasteFilter = ReadlineFilters.CutPasteFilter()
+ var history = List.empty[String]
+ val selectionFilter = GUILikeFilters.SelectionFilter(indent = 2)
+ val multilineFilter: Filter = Filter("multilineFilter") {
+ case TermState(lb ~: rest, b, c, _)
+ if (lb == 10 || lb == 13) => // Enter
+
+ BasicFilters.injectNewLine(b, c, rest)
+ }
+ def readLine(prompt: String)(implicit ctx: Context): String = {
+ val historyFilter = new HistoryFilter(
+ () => history.toVector,
+ Console.BLUE,
+ AnsiNav.resetForegroundColor
+ )
+
+ val allFilters = Filter.merge(
+ UndoFilter(),
+ historyFilter,
+ selectionFilter,
+ GUILikeFilters.altFilter,
+ GUILikeFilters.fnFilter,
+ ReadlineFilters.navFilter,
+ //autocompleteFilter,
+ cutPasteFilter,
+ //multilineFilter,
+ BasicFilters.all
+ )
+
+ Terminal.readLine(
+ Console.BLUE + prompt + Console.RESET,
+ reader,
+ writer,
+ allFilters,
+ displayTransform = (buffer, cursor) => {
+ val ansiBuffer = Ansi.Str.parse(SyntaxHighlighting(buffer))
+ //val ansiBuffer = Ansi.Str.parse(SyntaxHighlighting(new SourceFile("<console>", buffer)))
+ val (newBuffer, cursorOffset) = SelectionFilter.mangleBuffer(
+ selectionFilter, ansiBuffer, cursor, Ansi.Reversed.On
+ )
+ val newNewBuffer = HistoryFilter.mangleBuffer(
+ historyFilter, newBuffer, cursor,
+ Ansi.Color.Green
+ )
+
+ (newNewBuffer, cursorOffset)
+ }
+ ) match {
+ case Some(s) => history = s :: history; s
+ case None => ":q"
+ }
+ }
+}