Skip to content

Commit

Permalink
RefactorWollokReplStyledText + fix arrow up in console
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Jul 10, 2017
1 parent 201569d commit cf588a6
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package org.uqbar.project.wollok.launch.repl

import org.fusesource.jansi.Ansi
import org.fusesource.jansi.AnsiConsole

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

/**
* An strategy to allow ANSI colors output
* or regular output without special characters
*
* https://github.com/fusesource/jansi
*
* @author jfernandes
*/
interface ReplOutputFormatter {
Expand Down Expand Up @@ -40,6 +44,9 @@ class AnsiColoredReplOutputFormatter implements ReplOutputFormatter {
override def importantMessageStyle(CharSequence msg) { ansi.fg(COLOR_REPL_MESSAGE).bold.a(msg).reset.toString() }
override def messageStyle(CharSequence msg) { ansi.fg(COLOR_REPL_MESSAGE).a(msg).reset.toString() }
override def returnStyle(CharSequence msg) { ansi().fg(COLOR_RETURN_VALUE).a(msg).reset.toString() }
override def linkStyle(CharSequence msg) { ansi.fg(COLOR_LINK_FILE).a(Ansi.Attribute.UNDERLINE).boldOff.a(msg).reset.a(Ansi.Attribute.UNDERLINE_OFF).bold.a("").toString() }
override def linkStyle(CharSequence msg) {
// https://mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console/
ansi.fg(COLOR_LINK_FILE).a(Ansi.Attribute.UNDERLINE).boldOff.a(msg).reset.a(Ansi.Attribute.UNDERLINE_OFF).bold.a(Ansi.Attribute.RESET).toString
}
override def normalStyle(CharSequence msg) { ansi.fg(COLOR_DEFAULT).a(msg).reset.toString() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class WollokRepl {

// TODO: should be WollokObject
def printReturnValue(Object obj) {
if (obj == null)
if (obj === null)
println("null".returnStyle)
else if (obj instanceof WollokObject && !(obj as WollokObject).isVoid)
doPrintReturnValue(obj)
Expand Down Expand Up @@ -194,7 +194,7 @@ class WollokRepl {
.toList
.reverse
.map [ stackDTO | stackDTO.toLinkForConsole ]
.join("")
.join(System.lineSeparator)

printlnIdent(errorLine.errorStyle)
}
Expand All @@ -204,15 +204,15 @@ class WollokRepl {
append(" ")
append("at")
append(" ")
if (st.contextDescription != null) {
if (st.contextDescription !== null) {
append(st.contextDescription)
append(" ")
}
]

var link = new StringBuffer
if (st.hasFileName) {
link.append("(" + st.fileName + ":" + st.lineNumber + ")\n")
link.append("(" + st.fileName + ":" + st.lineNumber + ")")
}

result.toString.errorStyle + link.toString.linkStyle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class WollokReplConsole extends TextConsole {
def sendInputBuffer() {
addCommandToHistory
sessionCommands += inputBuffer
streamsProxy.write(inputBuffer)
streamsProxy.write(inputBuffer)
outputTextEnd = page.viewer.textWidget.charCount
updateInputBuffer
page.viewer.textWidget.selection = outputTextEnd
Expand All @@ -202,7 +202,7 @@ class WollokReplConsole extends TextConsole {
lastCommands.last(ps)
}

page.viewer.textWidget.content.replaceTextRange(outputTextEnd, document.length - outputTextEnd, inputBuffer)
page.viewer.textWidget.content.replaceTextRange(outputTextEnd, document.length - outputTextEnd, inputBuffer.replace(System.lineSeparator, ""))
]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.uqbar.project.wollok.ui.console.editor

import java.lang.reflect.Field
import java.util.List
import org.eclipse.swt.SWTError
import org.eclipse.swt.custom.StyleRange
Expand All @@ -14,6 +13,7 @@ import org.eclipse.swt.widgets.Composite
import org.uqbar.project.wollok.ui.console.editor.rtf.WollokRTFWriter

import static extension org.uqbar.project.wollok.ui.console.highlight.AnsiUtils.*
import static extension org.uqbar.project.wollok.utils.ReflectionExtensions.*

/**
* Styled Text Wrapper for Wollok
Expand All @@ -40,15 +40,15 @@ class WollokReplStyledText extends StyledText {
def doCopySelection(int type) {
if(type != DND.CLIPBOARD && type != DND.SELECTION_CLIPBOARD) return false
try {
val blockXLocation = getFieldValue("blockXLocation") as Integer
val blockXLocation = this.getFieldValue("blockXLocation") as Integer
if (blockSelection && blockXLocation != -1) {
val text = getEscapedBlockText()
if (text.length() > 0) {
// TODO RTF support
val plainTextTransfer = TextTransfer.getInstance()
val Object[] data = #{text}
val Transfer[] types = #{plainTextTransfer}
val clipboard = getFieldValue("clipboard") as Clipboard
val clipboard = this.getFieldValue("clipboard") as Clipboard
clipboard.setContents(data, types, type)
return true
}
Expand All @@ -68,7 +68,7 @@ class WollokReplStyledText extends StyledText {
}

def void setClipboardContent(int start, int length, int clipboardType) throws SWTError {
val boolean isGtk = getFieldValue("IS_GTK") as Boolean
val boolean isGtk = this.getFieldValue("IS_GTK") as Boolean
if(clipboardType == DND.SELECTION_CLIPBOARD && !isGtk) return;

// Fix: when you select a line from start, it doesn't catch special characters
Expand All @@ -94,34 +94,18 @@ class WollokReplStyledText extends StyledText {
types = #[plainTextTransfer]
}
}
val clipboard = getFieldValue("clipboard") as Clipboard
val clipboard = this.getFieldValue("clipboard") as Clipboard
clipboard.setContents(data, types, clipboardType)
}

private def getEscapedBlockText() {
val text = executeMethod("getBlockSelectionText", #{System.getProperty("line.separator")}) as String
val text = this.executeMethod("getBlockSelectionText", #{System.getProperty("line.separator")}) as String
text.deleteAnsiCharacters
}

private def getField(String name) {
val Field field = typeof(StyledText).getDeclaredField(name) as Field
field.accessible = true
field
}

private def getFieldValue(String name) {
getField(name).get(this)
}

def executeMethod(String methodName, Object[] args) {
val method = typeof(StyledText).getDeclaredMethod(methodName, args.map[it.class])
method.accessible = true
method.invoke(this, args)
}

def void addStyle(int offset, List<StyleRange> styles) {
val line = content.getLineAtOffset(offset)
style.applyStyle(line, styles)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class WollokAnsiColorLineStyleListener implements LineStyleListener {
var currentAttributes = new WollokConsoleAttributes
public static val Character ESCAPE_SGR = 'm'

int lastRangeEnd = 0;
int lastRangeEnd = 0

override lineGetStyle(LineStyleEvent event) {
if (event == null || event.lineText == null || event.lineText.length() == 0)
if (event === null || event.lineText === null || event.lineText.length() == 0)
return

val currentPalette = WollokConsolePreferenceUtils.getString(WollokConsolePreferenceConstants.PREF_COLOR_PALETTE)
Expand Down Expand Up @@ -67,22 +67,23 @@ class WollokAnsiColorLineStyleListener implements LineStyleListener {
}

if (lastRangeEnd != start)
addRange(ranges, event.lineOffset + lastRangeEnd, start - lastRangeEnd, defStyle.foreground, false)
addRange(ranges, event.lineOffset + lastRangeEnd, start - lastRangeEnd, defStyle, false)
lastAttributes = currentAttributes.clone

addRange(ranges, event.lineOffset + start, end - start, defStyle.foreground, true)
addRange(ranges, event.lineOffset + start, end - start, defStyle, true)
}
if (lastRangeEnd != currentText.length)
addRange(ranges, event.lineOffset + lastRangeEnd, currentText.length - lastRangeEnd, defStyle.foreground, false)
addRange(ranges, event.lineOffset + lastRangeEnd, currentText.length - lastRangeEnd, defStyle, false)

lastAttributes = currentAttributes.clone

if (!ranges.empty)
event.styles = ranges
}

def void addRange(List<StyleRange> ranges, int start, int length, Color foreground, boolean isCode) {
val range = new StyleRange(start, length, foreground, null)
def void addRange(List<StyleRange> ranges, int start, int length, StyleRange defStyle, boolean isCode) {
val range = new StyleRange(start, length, defStyle.foreground, null)
range.underline = defStyle.underline
WollokConsoleAttributes.updateRangeStyle(range, lastAttributes)
if (isCode) {
val showEscapeCodes = WollokConsolePreferenceUtils.getBoolean(WollokConsolePreferenceConstants.PREF_SHOW_ESCAPES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class WollokCodeHighLightLineStyleListener implements LineStyleListener {
}

override lineGetStyle(LineStyleEvent event) {
if (event == null || event.lineText == null || event.lineText.length == 0 || !event.isCodeInputLine)
if (event === null || event.lineText === null || event.lineText.length == 0 || !event.isCodeInputLine)
return;

val originalText = (event.widget as StyledText).text
Expand All @@ -78,6 +78,7 @@ class WollokCodeHighLightLineStyleListener implements LineStyleListener {
val style = stylesProvider.getAttribute(styleId)

val s = new StyleRange(event.lineOffset + offset - headerLength, length, style.foreground, style.background)
// TODO: Ver underline s.underline = style.underline
s.data = styleId

if (s.start <= originalText.length && s.end <= originalText.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class WollokConsoleAttributes implements Cloneable {
// This function maps from the current attributes as "described" by escape sequences to real,
// Eclipse console specific attributes (resolving color palette, default colors, etc.)
def static updateRangeStyle(StyleRange range, WollokConsoleAttributes attribute) {
val useWindowsMapping = getBoolean(WollokConsolePreferenceConstants.PREF_WINDOWS_MAPPING);
val useWindowsMapping = getBoolean(WollokConsolePreferenceConstants.PREF_WINDOWS_MAPPING)
val WollokConsoleAttributes tempAttrib = attribute.clone as WollokConsoleAttributes

var hilite = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ class WollokStyleRangeListener implements LineStyleListener {
}
styledText.addStyle(event.lineOffset, styles)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,34 @@ import java.lang.reflect.Field
class ReflectionExtensions {

static def assign(Object receiver, String variableName, Object object) {
var Class<?> receiverClass = receiver.class
val Field f1 = getField(receiver.class, variableName)
f1.set(receiver, object)
}

static def getField(Class<?> c, String variableName) {
var Class<?> receiverClass = c
var mustCheck = true
while (mustCheck) {
try {
val Field f1 = receiverClass.getDeclaredField(variableName)
f1.accessible = true
f1.set(receiver, object)
mustCheck = false
return f1
} catch (NoSuchFieldException e) {
receiverClass = receiverClass.superclass
mustCheck = receiverClass !== null
}
}
return null
}

static def getFieldValue(Object o, String name) {
getField(o.class, name).get(o)
}

static def executeMethod(Object o, String methodName, Object[] args) {
val method = (o.class).getDeclaredMethod(methodName, args.map[it.class])
method.accessible = true
method.invoke(o, args)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class WEclipseUtils {
iPath.exists
else {
val s = URI.toFileString
s != null && new File(s).exists
s !== null && new File(s).exists
}
}
def static iPath(Resource it) { Path.fromOSString(URI.toPlatformString(true)) }
Expand Down Expand Up @@ -156,11 +156,11 @@ class WEclipseUtils {
def static openEditor(ITextEditor textEditor, String fileName, int lineNumber) {
try {
val IDocument document = textEditor.documentProvider.getDocument(textEditor.editorInput)
if(document == null) throw new RuntimeException("Could not open file " + fileName + " in editor")
if(document === null) throw new RuntimeException("Could not open file " + fileName + " in editor")
var IRegion lineInfo = null
// line count internaly starts with 0, and not with 1 like in GUI
lineInfo = document.getLineInformation(lineNumber - 1)
if (lineInfo != null) {
if (lineInfo !== null) {
textEditor.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength())
}
} catch (BadLocationException e) {
Expand Down

0 comments on commit cf588a6

Please sign in to comment.