Skip to content

Commit

Permalink
Fixes #329 syntax color for REPL input text. Initial version with cus…
Browse files Browse the repository at this point in the history
…toms highlights. Keywords in progress
  • Loading branch information
javierfernandes committed Oct 17, 2015
1 parent e4d6406 commit 92ec4b9
Show file tree
Hide file tree
Showing 15 changed files with 258 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class WollokChecker {

def String formattedIssue(Issue it) {
// COLUMN: investigate how to calculate the column number from the offset !
'''«uriToProblem?.trimFragment.toFileString»:«lineNumber»:«if (offset == null) 1 else offset» «severity.name» «message»'''
'''[«severity»] «uriToProblem?.trimFragment.toFileString»:«lineNumber»:«if (offset == null) 1 else offset» «severity.name» «message»'''
}

def validate(Injector injector, Resource resource, Procedure1<? super Issue> issueHandler, Procedure1<Iterable<Issue>> after) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package org.uqbar.project.wollok.launch.repl

import org.eclipse.xtext.validation.Issue

/**
* @author tesonep
*/
class ReplParserException extends RuntimeException {
val Iterable<Issue> issues

new(Iterable<Issue> issues){
new(Iterable<Issue> issues) {
this.issues = issues
}

def getIssues(){
def getIssues() {
issues
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import static extension org.uqbar.project.wollok.model.WollokModelExtensions.*
* @author tesonep
* @author jfernandes
*/
// I18N
class WollokRepl {
static val COLOR_RETURN_VALUE = BLUE
static val COLOR_ERROR = RED
Expand All @@ -37,8 +38,8 @@ class WollokRepl {
val WollokInterpreter interpreter
val File mainFile
val reader = new BufferedReader(new InputStreamReader(System.in))
val prompt = ">>> "
var whiteSpaces = ""
val static prompt = ">>> "
var static whiteSpaces = ""
val WFile parsedMainFile

new(WollokLauncher launcher, Injector injector, WollokInterpreter interpreter, File mainFile, WFile parsedMainFile) {
Expand Down Expand Up @@ -177,8 +178,8 @@ class WollokRepl {
}

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

if (e.cause != null) {
Expand Down
3 changes: 2 additions & 1 deletion org.uqbar.project.wollok.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Require-Bundle: org.uqbar.project.wollok;visibility:=reexport,
org.eclipse.e4.core.di;bundle-version="1.4.0",
org.eclipse.core.filesystem;bundle-version="1.4.100",
org.eclipse.xtext.ui.shared;bundle-version="2.7.3",
org.eclipse.xtext.xbase.ui;bundle-version="2.7.3"
org.eclipse.xtext.xbase.ui;bundle-version="2.7.3",
org.uqbar.project.wollok.ui.launch;bundle-version="1.3.0"
Import-Package: org.apache.log4j,
org.junit;version="4.5.0",
org.junit.runner;version="4.5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,24 @@ abstract class AbstractWollokInterpreterTestCase extends Assert {
}

def void assertIsException(Throwable exception, Class<? extends Throwable> clazz) {
if(!clazz.isInstance(exception)){
if(exception.cause == null){
if (!clazz.isInstance(exception)) {
if (exception.cause == null) {
exception.printStackTrace
fail('''Expecting exception «clazz.name» but found «exception.class.name»''')
}else{
}
else{
assertIsException(exception.cause, clazz)
}
}

}

def getMessageOf(Throwable exception, Class<? extends Throwable> clazz) {
if(clazz.isInstance(exception)){
if (clazz.isInstance(exception)) {
exception.message
}else{
if(exception.cause == null)
}
else{
if (exception.cause == null)
fail('''Expecting exception «clazz.name» but found «exception.class.name»''')
else{
getMessageOf(exception.cause, clazz)
Expand Down
1 change: 1 addition & 0 deletions org.uqbar.project.wollok.ui.launch/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Export-Package: org.uqbar.project.wollok.debugger,
org.uqbar.project.wollok.debugger.client.source,
org.uqbar.project.wollok.debugger.model,
org.uqbar.project.wollok.ui.console,
org.uqbar.project.wollok.ui.console.highlight,
org.uqbar.project.wollok.ui.i18n,
org.uqbar.project.wollok.ui.launch,
org.uqbar.project.wollok.ui.launch.shortcut,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.uqbar.project.wollok.ui.console

import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.runtime.Path
import org.eclipse.debug.core.model.IProcess
Expand All @@ -24,6 +19,8 @@ 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.*

import static extension org.uqbar.project.wollok.utils.WEclipseUtils.*

/**
* @author tesonep
*/
Expand Down Expand Up @@ -58,8 +55,8 @@ class WollokReplConsole extends TextConsole {

runInUI[
this.clearConsole
DebugUIPlugin.getDefault().getPreferenceStore().setValue(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT, false)
DebugUIPlugin.getDefault().getPreferenceStore().setValue(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR, false)
DebugUIPlugin.getDefault.preferenceStore.setValue(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT, false)
DebugUIPlugin.getDefault.preferenceStore.setValue(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR, false)
]

streamsProxy.outputStreamMonitor.addListener [ text, monitor |
Expand Down Expand Up @@ -92,41 +89,46 @@ class WollokReplConsole extends TextConsole {
}

def updateInputBuffer(){
if(outputTextEnd > this.document.length){
outputTextEnd = this.document.length
if(outputTextEnd > document.length){
outputTextEnd = document.length
}
inputBuffer = this.document.get(outputTextEnd, this.document.length - outputTextEnd)
inputBuffer = document.get(outputTextEnd, this.document.length - outputTextEnd)
}

def addCommandToHistory() {
if(!inputBuffer.empty) {
lastCommands.remove(inputBuffer)
lastCommands.add(inputBuffer)
saveHistory()
lastCommands => [
remove(inputBuffer)
add(inputBuffer)
]
saveHistory
}
}

def saveHistory(){
runInBackground[
var file = ResourcesPlugin.workspace.root.location.append(new Path("repl.history"))
val objStream = new ObjectOutputStream(new FileOutputStream(file.toOSString))
objStream.writeObject(this.lastCommands)
runInBackground [
historyFilePath.asObjectStream.writeObject(this.lastCommands)
]
}

def loadHistory(){
runInBackground[
var file = ResourcesPlugin.workspace.root.location.append(new Path("repl.history"))
var javaFile = new File(file.toOSString)
var javaFile = historyFilePath.asJavaFile

if(javaFile.exists){
val objStream = new ObjectInputStream(new FileInputStream(javaFile))
this.lastCommands.clear
this.lastCommands.addAll(objStream.readObject as OrderedBoundedSet<String>)
if (javaFile.exists) {
val objStream = javaFile.asObjectInputStream
lastCommands => [
clear
addAll(objStream.readObject as OrderedBoundedSet<String>)
]
}
]
}

def historyFilePath() {
ResourcesPlugin.workspace.root.location.append(new Path("repl.history"))
}

def sendInputBuffer(){
val x = inputBuffer + "\n";

Expand All @@ -135,16 +137,16 @@ class WollokReplConsole extends TextConsole {
streamsProxy.write(x)
outputTextEnd += x.length
updateInputBuffer
this.page.viewer.textWidget.caretOffset = outputTextEnd
page.viewer.textWidget.caretOffset = outputTextEnd
}

def numberOfHistories() { lastCommands.size }

def loadHistory(int pos) {
runInUI[
inputBuffer = if(lastCommands.size == 0) ""
inputBuffer = if (lastCommands.size == 0) ""
else {
val ps = if(pos >= lastCommands.size) 0 else pos
val ps = if (pos >= lastCommands.size) 0 else pos
lastCommands.last(ps)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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
import org.uqbar.project.wollok.ui.console.highlight.WollokCodeHighLightLineStyleListener
import org.eclipse.swt.custom.StyledText

/**
*
Expand Down Expand Up @@ -40,6 +42,9 @@ class WollokReplConsolePage extends TextConsolePage implements KeyListener {
// init participant (coloring)
this.participant = new WollokReplConsolePageParticipant
participant.init(this, console)

// need to remove the listener later ?
(control as StyledText).addLineStyleListener(new WollokCodeHighLightLineStyleListener)
}

def increaseHistoryPosition(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import org.eclipse.swt.graphics.GlyphMetrics
import static org.uqbar.project.wollok.ui.console.highlight.AnsiCommands.*

/**
* A LineStyleListener that interprets ansi codes
* and provides the styles based on that standard (font style and colors).
*
* @author jfernandes
*/
class WollokReplLineStyleListener implements LineStyleListener {
class WollokAnsiColorLineStyleListener implements LineStyleListener {
var lastAttributes = new WollokConsoleAttributes
var currentAttributes = new WollokConsoleAttributes
static val Pattern pattern = Pattern.compile("\u001b\\[[\\d;]*[A-HJKSTfimnsu]")
public static val Pattern pattern = Pattern.compile("\u001b\\[[\\d;]*[A-HJKSTfimnsu]")
static val Character ESCAPE_SGR = 'm'

int lastRangeEnd = 0;
Expand Down
Loading

0 comments on commit 92ec4b9

Please sign in to comment.