Skip to content

Commit

Permalink
Fixes #324 ansi colors cor repl #325 allow several lines in repl
Browse files Browse the repository at this point in the history
  • Loading branch information
javierfernandes committed Oct 15, 2015
1 parent 8eeaa6e commit e4d6406
Show file tree
Hide file tree
Showing 16 changed files with 777 additions and 61 deletions.
1 change: 1 addition & 0 deletions org.uqbar.project.wollok.launch/.classpath
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="lib" path="lib/jansi-1.11.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry exported="true" kind="lib" path="lib/lipermi-0-4-2.jar"/>
Expand Down
4 changes: 3 additions & 1 deletion org.uqbar.project.wollok.launch/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Require-Bundle: org.eclipse.ui,
org.uqbar.project.wollok.lib,
org.eclipse.xtext.ui.shared;bundle-version="2.7.3"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ClassPath: lib/lipermi-0-4-2.jar, .
Bundle-ClassPath: lib/lipermi-0-4-2.jar,
lib/jansi-1.11.jar,
.
Export-Package: org.uqbar.project.wollok.debugger,
org.uqbar.project.wollok.launch,
org.uqbar.project.wollok.launch.setup,
Expand Down
6 changes: 4 additions & 2 deletions org.uqbar.project.wollok.launch/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ source.. = src/,\
xtend-gen/
bin.includes = META-INF/,\
.,\
lib/
lib/,\
lib/jansi-1.11.jar
jars.compile.order = lib/lipermi-0-4-2.jar,\
.
jars.extra.classpath = lib/lipermi-0-4-2.jar
jars.extra.classpath = lib/lipermi-0-4-2.jar,\
lib/jansi-1.11.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package org.uqbar.project.wollok.launch.repl

import com.google.inject.Injector
import java.io.BufferedReader
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.InputStreamReader
import org.eclipse.emf.common.util.URI
import java.io.PrintStream
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.xtext.resource.XtextResourceSet
import org.eclipse.xtext.util.LazyStringInputStream
Expand All @@ -16,9 +17,21 @@ import org.uqbar.project.wollok.interpreter.stack.VoidObject
import org.uqbar.project.wollok.launch.WollokLauncher
import org.uqbar.project.wollok.wollokDsl.WFile

import static org.fusesource.jansi.Ansi.*
import static org.fusesource.jansi.Ansi.Color.*

import static extension org.uqbar.project.wollok.model.WollokModelExtensions.*

/**
*
* @author tesonep
* @author jfernandes
*/
class WollokRepl {
static val COLOR_RETURN_VALUE = BLUE
static val COLOR_ERROR = RED
static val COLOR_REPL_MESSAGE = CYAN

val Injector injector
val WollokLauncher launcher
val WollokInterpreter interpreter
Expand All @@ -39,28 +52,32 @@ class WollokRepl {
def void startRepl() {
var String input

println("Wollok interactive console (type \"quit\" to quit): ")
print(prompt)
println("Wollok interactive console (type \"quit\" to quit): ".importantMessageStyle)
printPrompt

while ((input = readInput) != "quit") {
executeInput(input)
print(prompt)
printPrompt
}
}

def String readInput(){
def printPrompt() {
print(prompt.messageStyle)
}

def String readInput() {
val input = reader.readLine.trim
if(input == ""){
print(prompt)
if (input == "") {
printPrompt
readInput
}
else
if(input.endsWith(";"))
if (input.endsWith(";"))
input + " " + readInput
else input
}

def executeInput(String input){
def executeInput(String input) {
try {
val returnValue = interpreter.interpret(
'''
Expand All @@ -73,93 +90,108 @@ class WollokRepl {
}
'''.parseRepl(mainFile),true)
printReturnValue(returnValue)
} catch (Exception e){
resetIndent()
}
catch (Exception e) {
resetIndent
handleException(e)
}
}

def printReturnValue(Object obj){
if(obj == null)
println(obj)
def printReturnValue(Object obj) {
if (obj == null)
println("null".returnStyle)
else
doPrintReturnValue(obj)
}

def dispatch doPrintReturnValue(Object obj){
println(obj?.toString)
println(obj?.toString.returnStyle)
}

def dispatch doPrintReturnValue(String obj){
println('"' + obj +'"')
println(('"' + obj + '"').returnStyle)
}

def dispatch doPrintReturnValue(VoidObject obj){}

def parseRepl(CharSequence content, File mainFile) {
val resourceSet = injector.getInstance(XtextResourceSet)
val resource = resourceSet.createResource(uriToUse(resourceSet));
val resource = resourceSet.createResource(uriToUse(resourceSet))
val in = new LazyStringInputStream(content.toString)

launcher.createClassPath(mainFile, resourceSet)

resourceSet.getResources().add(resource);
resourceSet.resources.add(resource)

resource.load(in, #{});
launcher.validate(injector, resource, [], [throw new ReplParserException(it)])
resource.getContents().get(0) as WFile;
resource.contents.get(0) as WFile
}

def uriToUse(ResourceSet resourceSet) {
var name = "__synthetic";
for (var i = 0; i < Integer.MAX_VALUE; i++) {
var syntheticUri = parsedMainFile.eResource.URI.trimFileExtension.trimSegments(1).appendSegment(name + i).appendFileExtension(WollokConstants.PROGRAM_EXTENSION)
if (resourceSet.getResource(syntheticUri, false) == null){
return syntheticUri;
if (resourceSet.getResource(syntheticUri, false) == null) {
return syntheticUri
}
}
throw new IllegalStateException();
throw new IllegalStateException
}

def <X> X printlnIdent(X obj){
def <X> X printlnIdent(X obj) {
print(whiteSpaces)
println(obj)
}

def indent(){
def indent() {
whiteSpaces = whiteSpaces + " "
}

def resetIndent(){
def resetIndent() {
whiteSpaces = ""
}

def dispatch void handleException(ReplParserException e){
def dispatch void handleException(ReplParserException e) {
e.issues.forEach [
printlnIdent("" + severity.name + ":" + message + "(line:" + (lineNumber - numberOfLinesBefore) + ")")
printlnIdent(errorStyle("" + severity.name + ":" + message + "(line: " + (lineNumber - numberOfLinesBefore) + ")"))
]
}

def dispatch void handleException(Throwable e){
e.printStackTrace

def dispatch void handleException(Throwable e) {
println(e.stackTraceAsString.errorStyle)
}

def stackTraceAsString(Throwable e) {
val s = new ByteArrayOutputStream()
e.printStackTrace(new PrintStream(s))
new String(s.toByteArray)
}

def dispatch void handleException(MessageNotUnderstood e){
printlnIdent(e.internalMessage)
def dispatch void handleException(MessageNotUnderstood e) {
printlnIdent(e.internalMessage.errorStyle)
}

def getNumberOfLinesBefore(){
2 + parsedMainFile.imports.size
}

def dispatch void handleException(WollokInterpreterException e){
if(e.lineNumber > numberOfLinesBefore){
printlnIdent('''Error in line (line:«e.lineNumber - numberOfLinesBefore»): «e.nodeText»:''')
def dispatch void handleException(WollokInterpreterException e) {
if (e.lineNumber > numberOfLinesBefore){
printlnIdent('''Error in line (line: «e.lineNumber - numberOfLinesBefore»): «e.nodeText»:'''.errorStyle)
}

if(e.cause != null){
indent()
if (e.cause != null) {
indent
handleException(e.cause)
}
}

// ********** STYLING

// applies styles for errors
def errorStyle(CharSequence msg) { ansi.fg(COLOR_ERROR).a(msg).reset }
def importantMessageStyle(CharSequence msg) { ansi.fg(COLOR_REPL_MESSAGE).bold.a(msg).reset }
def messageStyle(CharSequence msg) { ansi.fg(COLOR_REPL_MESSAGE).a(msg).reset }
def returnStyle(CharSequence msg) { ansi().fg(COLOR_RETURN_VALUE).a(msg).reset }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import org.uqbar.project.wollok.ui.launch.Activator
import static org.uqbar.project.wollok.ui.console.RunInBackground.*
import static org.uqbar.project.wollok.ui.console.RunInUI.*

/**
* @author tesonep
*/
class WollokReplConsole extends TextConsole {

IProcess process
Expand All @@ -40,7 +43,7 @@ class WollokReplConsole extends TextConsole {
val lastCommands = new OrderedBoundedSet<String>(10)

new() {
super(WollokReplConsole.consoleName, null, Activator.getDefault.getImageDescriptor("icons/w.png"),true);
super(WollokReplConsole.consoleName, null, Activator.getDefault.getImageDescriptor("icons/w.png"), true)
this.partitioner = new WollokReplConsolePartitioner(this)
this.document.documentPartitioner = this.partitioner
}
Expand All @@ -49,8 +52,8 @@ class WollokReplConsole extends TextConsole {
loadHistory
this.process = process
this.streamsProxy = process.streamsProxy
this.activate()
outputTextEnd = 0;
this.activate
outputTextEnd = 0


runInUI[
Expand Down Expand Up @@ -95,8 +98,8 @@ class WollokReplConsole extends TextConsole {
inputBuffer = this.document.get(outputTextEnd, this.document.length - outputTextEnd)
}

def addCommandToHistory(){
if(!inputBuffer.empty){
def addCommandToHistory() {
if(!inputBuffer.empty) {
lastCommands.remove(inputBuffer)
lastCommands.add(inputBuffer)
saveHistory()
Expand Down Expand Up @@ -135,9 +138,9 @@ class WollokReplConsole extends TextConsole {
this.page.viewer.textWidget.caretOffset = outputTextEnd
}

def numberOfHistories(){ lastCommands.size }
def numberOfHistories() { lastCommands.size }

def loadHistory(int pos){
def loadHistory(int pos) {
runInUI[
inputBuffer = if(lastCommands.size == 0) ""
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,57 @@ import org.eclipse.swt.events.KeyListener
import org.eclipse.swt.widgets.Composite
import org.eclipse.ui.console.IConsoleView
import org.eclipse.ui.console.TextConsolePage
import org.uqbar.project.wollok.ui.console.highlight.WollokReplConsolePageParticipant

/**
*
*
* @author tesonep
* @author jfernandes
*/
class WollokReplConsolePage extends TextConsolePage implements KeyListener {

val WollokReplConsole console
var int historyPosition = -1
WollokReplConsolePageParticipant participant

new(WollokReplConsole console, IConsoleView view) {
super(console, view)
this.console = console
}

override dispose() {
participant.dispose
super.dispose
}

override createControl(Composite parent) {
super.createControl(parent)
override createControl(Composite oldParent) {
super.createControl(oldParent)
viewer.textWidget.addKeyListener(this)

viewer.textWidget.addVerifyListener [ event |
event.doit = ! console.partitioner.isReadOnly(event.start)
]

// init participant (coloring)
this.participant = new WollokReplConsolePageParticipant
participant.init(this, console)
}

def increaseHistoryPosition(){
historyPosition++;
historyPosition++

if(historyPosition >= console.numberOfHistories)
if (historyPosition >= console.numberOfHistories)
historyPosition = 0

if(historyPosition <= 0 && console.numberOfHistories == 0){
if (historyPosition <= 0 && console.numberOfHistories == 0) {
historyPosition = -1
}
}

def decreaseHistoryPosition(){
historyPosition--;
historyPosition--

if(historyPosition < 0)
if (historyPosition < 0)
historyPosition = console.numberOfHistories - 1
}

Expand All @@ -58,15 +74,17 @@ class WollokReplConsolePage extends TextConsolePage implements KeyListener {
}


if (e.keyCode == 0x0d){
// return key pressed
if (e.keyCode == 0x0d && !e.controlPressed) {
console.sendInputBuffer
historyPosition = -1
}
else
console.updateInputBuffer
}

def isControlPressed(KeyEvent it) { stateMask.bitwiseAnd(SWT.CTRL) == SWT.CTRL }

override keyReleased(KeyEvent e) {
}
override keyReleased(KeyEvent e) { }

}
Loading

0 comments on commit e4d6406

Please sign in to comment.