adding experiment result

This commit is contained in:
Manindra Moharana
2013-03-30 02:23:46 +05:30
parent 9f0e4b7276
commit cb3f60ed7f
10 changed files with 2471 additions and 582 deletions

View File

@@ -10,6 +10,16 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
<dictionary>
<key>LaunchConfigHandle</key>
<value>&lt;project&gt;/.externalToolBuilders/processing-experimental build.xml [Builder] (1).launch</value>
</dictionary>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>

View File

@@ -56,4 +56,10 @@
<target name="build" depends="compile" description="Build Experimental mode">
<jar basedir="bin" destfile="mode/experimental.jar" />
</target>
<target name="install" depends="build">
<copy todir="../app/modes/experimental/mode">
<fileset file="mode/experimental.jar" />
</copy>
</target>
</project>

File diff suppressed because it is too large Load Diff

View File

@@ -179,7 +179,9 @@ public class DebugEditor extends JavaEditor implements ActionListener {
checkForJavaTabs();
initializeErrorChecker();
ta.setECSandThemeforTextArea(errorCheckerService, dmode);
addXQModeUI();
addXQModeUI();
//TODO: Remove this later
setBounds(160, 400, getWidth(), getHeight());
}
private void addXQModeUI(){

View File

@@ -187,11 +187,12 @@ public class ErrorBar extends JPanel {
// Each problem.getSourceLine() will have an extra line added
// because of
// class declaration in the beginning
// class declaration in the beginning as well as default imports
for (Problem problem : problems) {
if (problem.tabIndex == currentTab) {
// Ratio of error line to total lines
float y = problem.lineNumber / ((float) totalLines);
float y = (problem.lineNumber - errorCheckerService.defaultImportsOffset)
/ ((float) totalLines);
// Ratio multiplied by height of the error bar
y *= fheight - 15; // -15 is just a vertical offset
errorPoints.add(new ErrorMarker(problem, (int) y,

View File

@@ -28,6 +28,7 @@ import processing.app.Base;
import processing.app.Library;
import processing.app.SketchCode;
import processing.core.PApplet;
import processing.mode.java.preproc.PdePreprocessor;
public class ErrorCheckerService implements Runnable{
@@ -87,6 +88,11 @@ public class ErrorCheckerService implements Runnable{
*/
public int mainClassOffset;
/**
* Fixed p5 offsets for all sketches
*/
public int defaultImportsOffset;
/**
* Is the sketch running in static mode or active mode?
*/
@@ -173,6 +179,9 @@ public class ErrorCheckerService implements Runnable{
initParser();
initializeErrorWindow();
xqpreproc = new XQPreprocessor();
PdePreprocessor pdePrepoc = new PdePreprocessor(null);
defaultImportsOffset = pdePrepoc.getCoreImports().length +
pdePrepoc.getDefaultImports().length + 1;
}
/**
@@ -222,11 +231,6 @@ public class ErrorCheckerService implements Runnable{
});
}
/**
* checkCode() only on text area update
*/
protected AtomicInteger textModified = new AtomicInteger(0);
public void run() {
stopThread = false;
@@ -239,62 +243,62 @@ public class ErrorCheckerService implements Runnable{
System.out.println("Oops! [ErrorCheckerThreaded]: " + e);
// e.printStackTrace();
}
updatePaintedThingys();
if (pauseThread)
continue;
updatePaintedThingy();
if(textModified.get() == 0)
continue;
// Check every x seconds
checkCode();
}
}
ASTGenerator astGenerator = new ASTGenerator(this);
AtomicInteger textModified = new AtomicInteger();
private boolean checkCode() {
System.out.println("checkCode() " + textModified.get() );
lastTimeStamp = System.currentTimeMillis();
try {
sourceCode = preprocessCode(editor.getSketch().getMainProgram());
syntaxCheck();
System.err.println(editor.getSketch().getName()+ " MCO " + mainClassOffset);
// No syntax errors, proceed for compilation check, Stage 2.
if (problems.length == 0 && editor.compilationCheckEnabled) {
astGenerator.buildAST();
sourceCode = xqpreproc.doYourThing(sourceCode, programImports);
prepareCompilerClasspath();
mainClassOffset = xqpreproc.mainClassOffset; // tiny, but
// significant
if (staticMode) {
mainClassOffset++; // Extra line for setup() decl.
}
// mainClassOffset = xqpreproc.mainClassOffset; // tiny, but
// // significant
// if (staticMode) {
// mainClassOffset++; // Extra line for setup() decl.
// }
// System.out.println(sourceCode);
// System.out.println("--------------------------");
compileCheck();
}
updateErrorTable();
editor.updateErrorBar(problemsList);
updateEditorStatus();
// updatePaintedThingy();
updatePaintedThingys();
int x = textModified.get();
//System.out.println("TM " + x);
if(x>=3){
textModified.set(3);
x = 3;
textModified.set(3);
x = 3;
}
if(x>0)
textModified.set(x - 1);
textModified.set(x - 1);
else
textModified.set(0);
textModified.set(0);
return true;
} catch (Exception e) {
@@ -675,14 +679,15 @@ public class ErrorCheckerService implements Runnable{
/**
* Repaints the textarea if required
*/
public void updatePaintedThingy() {
public void updatePaintedThingys() {
editor.getTextArea().repaint();
updateEditorStatus();
currentTab = editor.getSketch().getCodeIndex(
editor.getSketch().getCurrentCode());
//System.out.println("awesome! " + currentTab + " LT " + lastTab);
if (currentTab != lastTab) {
textModified.incrementAndGet();
lastTab = currentTab;
editor.updateErrorBar(problemsList);
return;
}
@@ -725,7 +730,7 @@ public class ErrorCheckerService implements Runnable{
// String[] lines = {};// = PApplet.split(sourceString, '\n');
int codeIndex = 0;
int x = problem.getSourceLineNumber() - mainClassOffset;
int x = problem.getSourceLineNumber() - mainClassOffset + 1;
if (x < 0) {
// System.out.println("Negative line number "
// + problem.getSourceLineNumber() + " , offset "
@@ -900,23 +905,31 @@ public class ErrorCheckerService implements Runnable{
className = (editor == null) ? "DefaultClass" : editor.getSketch()
.getName();
// Check whether the code is being written in STATIC mode(no function
// declarations) - append class declaration and void setup() declaration
Matcher matcher = FUNCTION_DECL.matcher(sourceAlt);
if (!matcher.find()) {
sourceAlt = "public class " + className + " extends PApplet {\n"
sourceAlt = xqpreproc.prepareImports(programImports) + "public class " + className + " extends PApplet {\n"
+ "public void setup() {\n" + sourceAlt
+ "\nnoLoop();\n}\n" + "\n}\n";
staticMode = true;
mainClassOffset = 2;
staticMode = true;
} else {
sourceAlt = "public class " + className + " extends PApplet {\n"
sourceAlt = xqpreproc.prepareImports(programImports) + "public class " + className + " extends PApplet {\n"
+ sourceAlt + "\n}";
staticMode = false;
mainClassOffset = 1;
staticMode = false;
}
int position = sourceAlt.indexOf("{") + 1;
mainClassOffset = 1;
for (int i = 0; i <= position; i++) {
if (sourceAlt.charAt(i) == '\n') {
mainClassOffset++;
}
}
if(staticMode) mainClassOffset++;
//mainClassOffset += 2;
// Handle unicode characters
sourceAlt = substituteUnicode(sourceAlt);
@@ -963,6 +976,36 @@ public class ErrorCheckerService implements Runnable{
}
}
public void scrollToErrorLine(Problem p) {
if (editor == null) {
return;
}
if(p==null)
return;
try {
editor.toFront();
editor.getSketch().setCurrentCode(p.tabIndex);
editor.setSelection(editor.getTextArea()
.getLineStartNonWhiteSpaceOffset(p.lineNumber - 1)
+ editor.getTextArea().getLineText(p.lineNumber - 1)
.trim().length(), editor.getTextArea()
.getLineStartNonWhiteSpaceOffset(p.lineNumber - 1));
editor.getTextArea().scrollTo(p.lineNumber - 1, 0);
editor.repaint();
} catch (Exception e) {
System.err
.println(e
+ " : Error while selecting text in scrollToErrorLine()");
e.printStackTrace();
}
// System.out.println("---");
}
/**
* Checks if import statements in the sketch have changed. If they have,
* compiler classpath needs to be updated.
@@ -1000,7 +1043,7 @@ public class ErrorCheckerService implements Runnable{
* @return String - Tab code with imports replaced with white spaces
*/
private String scrapImportStatements(String tabProgram, int tabNumber) {
//TODO: Commented out imports are still detected as main imports.
String tabSource = new String(tabProgram);
do {
// System.out.println("-->\n" + sourceAlt + "\n<--");
@@ -1025,7 +1068,7 @@ public class ErrorCheckerService implements Runnable{
programImports.add(new ImportStatement(piece, tabNumber, Base
.countLines(tabSource.substring(0, idx))));
// Remove the import from the main program
// Substitue with white spaces
// Substitute with white spaces
String whiteSpace = "";
for (int j = 0; j < piece.length(); j++) {
whiteSpace += " ";

2
pdex/src/processing/mode/experimental/Problem.java Executable file → Normal file
View File

@@ -130,7 +130,7 @@ public class Problem {
public static String process(String message) {
// Remove all instances of token
// "Syntax error on token 'blah', delete this token"
if(message == null) return null;
pattern = Pattern.compile(tokenRegExp);
matcher = pattern.matcher(message);
message = matcher.replaceAll("");

706
pdex/src/processing/mode/experimental/TextArea.java Executable file → Normal file
View File

@@ -33,318 +33,462 @@ import processing.app.syntax.TextAreaDefaults;
/**
* Customized text area. Adds support for line background colors.
*
*
* @author Martin Leopold <m@martinleopold.com>
*/
public class TextArea extends JEditTextArea {
protected MouseListener[] mouseListeners; // cached mouselisteners, these are wrapped by MouseHandler
protected DebugEditor editor; // the editor
// line properties
protected Map<Integer, Color> lineColors = new HashMap(); // contains line background colors
// left-hand gutter properties
protected int gutterPadding = 3; // [px] space added to the left and right of gutter chars
protected Color gutterBgColor = new Color(252, 252, 252); // gutter background color
protected Color gutterLineColor = new Color(233, 233, 233); // color of vertical separation line
protected String breakpointMarker = "<>"; // the text marker for highlighting breakpoints in the gutter
protected String currentLineMarker = "->"; // the text marker for highlighting the current line in the gutter
protected Map<Integer, String> gutterText = new HashMap(); // maps line index to gutter text
protected Map<Integer, Color> gutterTextColors = new HashMap(); // maps line index to gutter text color
protected TextAreaPainter customPainter;
protected ErrorCheckerService errorCheckerService;
public TextArea(TextAreaDefaults defaults, DebugEditor editor) {
super(defaults);
this.editor = editor;
protected MouseListener[] mouseListeners; // cached mouselisteners, these are wrapped by MouseHandler
// replace the painter:
// first save listeners, these are package-private in JEditTextArea, so not accessible
ComponentListener[] componentListeners = painter.getComponentListeners();
mouseListeners = painter.getMouseListeners();
MouseMotionListener[] mouseMotionListeners = painter.getMouseMotionListeners();
protected DebugEditor editor; // the editor
remove(painter);
// line properties
protected Map<Integer, Color> lineColors = new HashMap(); // contains line background colors
// set new painter
customPainter = new TextAreaPainter(this, defaults);
painter = customPainter;
// set listeners
for (ComponentListener cl : componentListeners) {
painter.addComponentListener(cl);
}
// left-hand gutter properties
protected int gutterPadding = 3; // [px] space added to the left and right of gutter chars
for (MouseMotionListener mml : mouseMotionListeners) {
painter.addMouseMotionListener(mml);
}
protected Color gutterBgColor = new Color(252, 252, 252); // gutter background color
// use a custom mouse handler instead of directly using mouseListeners
MouseHandler mouseHandler = new MouseHandler();
painter.addMouseListener(mouseHandler);
painter.addMouseMotionListener(mouseHandler);
protected Color gutterLineColor = new Color(233, 233, 233); // color of vertical separation line
add(CENTER, painter);
protected String breakpointMarker = "<>"; // the text marker for highlighting breakpoints in the gutter
// load settings from theme.txt
ExperimentalMode theme = (ExperimentalMode) editor.getMode();
gutterBgColor = theme.getThemeColor("gutter.bgcolor", gutterBgColor);
gutterLineColor = theme.getThemeColor("gutter.linecolor", gutterLineColor);
gutterPadding = theme.getInteger("gutter.padding");
breakpointMarker = theme.loadThemeString("breakpoint.marker", breakpointMarker);
currentLineMarker = theme.loadThemeString("currentline.marker", currentLineMarker);
protected String currentLineMarker = "->"; // the text marker for highlighting the current line in the gutter
protected Map<Integer, String> gutterText = new HashMap(); // maps line index to gutter text
protected Map<Integer, Color> gutterTextColors = new HashMap(); // maps line index to gutter text color
protected TextAreaPainter customPainter;
protected ErrorCheckerService errorCheckerService;
public TextArea(TextAreaDefaults defaults, DebugEditor editor) {
super(defaults);
this.editor = editor;
// replace the painter:
// first save listeners, these are package-private in JEditTextArea, so not accessible
ComponentListener[] componentListeners = painter.getComponentListeners();
mouseListeners = painter.getMouseListeners();
MouseMotionListener[] mouseMotionListeners = painter
.getMouseMotionListeners();
remove(painter);
// set new painter
customPainter = new TextAreaPainter(this, defaults);
painter = customPainter;
// set listeners
for (ComponentListener cl : componentListeners) {
painter.addComponentListener(cl);
}
/**
* Sets ErrorCheckerService and loads theme for TextArea(XQMode)
* @param ecs
* @param mode
for (MouseMotionListener mml : mouseMotionListeners) {
painter.addMouseMotionListener(mml);
}
// use a custom mouse handler instead of directly using mouseListeners
MouseHandler mouseHandler = new MouseHandler();
painter.addMouseListener(mouseHandler);
painter.addMouseMotionListener(mouseHandler);
add(CENTER, painter);
// load settings from theme.txt
ExperimentalMode theme = (ExperimentalMode) editor.getMode();
gutterBgColor = theme.getThemeColor("gutter.bgcolor", gutterBgColor);
gutterLineColor = theme.getThemeColor("gutter.linecolor", gutterLineColor);
gutterPadding = theme.getInteger("gutter.padding");
breakpointMarker = theme.loadThemeString("breakpoint.marker",
breakpointMarker);
currentLineMarker = theme.loadThemeString("currentline.marker",
currentLineMarker);
}
/**
* Sets ErrorCheckerService and loads theme for TextArea(XQMode)
*
* @param ecs
* @param mode
*/
public void setECSandThemeforTextArea(ErrorCheckerService ecs,
ExperimentalMode mode) {
errorCheckerService = ecs;
customPainter.setECSandTheme(ecs, mode);
}
public void processKeyEvent(KeyEvent evt) {
super.processKeyEvent(evt);
if (evt.getID() == KeyEvent.KEY_TYPED) {
errorCheckerService.textModified.incrementAndGet();
System.out.println(" Typing: " + fetchPhrase(evt) + " "
+ (evt.getKeyChar() == KeyEvent.VK_BACK_SPACE));
}
}
private String fetchPhrase(KeyEvent evt) {
int off = getCaretPosition();
System.out.print("off " + off);
if (off < 0)
return null;
int line = getCaretLine();
if (line < 0)
return null;
String s = getLineText(line);
System.out.print("lin " + line);
/*
* if (s == null) return null; else if (s.length() == 0) return null;
*/
public void setECSandThemeforTextArea(ErrorCheckerService ecs, ExperimentalMode mode)
{
errorCheckerService = ecs;
customPainter.setECSandTheme(ecs, mode);
}
public void processKeyEvent(KeyEvent evt) {
super.processKeyEvent(evt);
if(evt.getID() == KeyEvent.KEY_TYPED){
errorCheckerService.textModified.incrementAndGet();
}
}
// else {
//System.out.print(s + " len " + s.length());
/**
* Retrieve the total width of the gutter area.
*
* @return gutter width in pixels
*/
protected int getGutterWidth() {
FontMetrics fm = painter.getFontMetrics();
int x = getCaretPosition() - getLineStartOffset(line) - 1, x2 = x + 1, x1 = x - 1;
System.out.print(" x char: " + s.charAt(x));
//int xLS = off - getLineStartNonWhiteSpaceOffset(line);
char keyChar = evt.getKeyChar();
if (keyChar == KeyEvent.VK_BACK_SPACE || keyChar == KeyEvent.VK_DELETE)
; // accepted these keys
else if (keyChar == KeyEvent.CHAR_UNDEFINED)
return null;
String word = (x < s.length() ? s.charAt(x) : "") + "";
if (s.trim().length() == 1) {
// word = ""
// + (keyChar == KeyEvent.CHAR_UNDEFINED ? s.charAt(x - 1) : keyChar);
//word = (x < s.length()?s.charAt(x):"") + "";
word = word.trim();
if (word.endsWith("."))
word = word.substring(0, word.length() - 1);
errorCheckerService.astGenerator.updatePredictions(word, line
+ errorCheckerService.mainClassOffset);
return word;
}
// if (keyChar == KeyEvent.VK_BACK_SPACE || keyChar == KeyEvent.VK_DELETE)
// ; // accepted these keys
// else if (!(Character.isLetterOrDigit(keyChar) || keyChar == '_' || keyChar == '$'))
// return null;
int i = 0;
int closeB = 0;
while (true) {
i++;
//TODO: currently works on single line only. "a. <new line> b()" won't be detected
if (x1 >= 0) {
// if (s.charAt(x1) != ';' && s.charAt(x1) != ',' && s.charAt(x1) != '(')
if (Character.isLetterOrDigit(s.charAt(x1)) || s.charAt(x1) == '_'
|| s.charAt(x1) == '.' || s.charAt(x1) == ')')
{
if (s.charAt(x1) == ')') {
word = s.charAt(x1--) + word;
closeB++;
while (x1 >= 0 && closeB > 0) {
word = s.charAt(x1) + word;
if (s.charAt(x1) == '(')
closeB--;
if (s.charAt(x1) == ')')
closeB++;
x1--;
}
} else {
word = s.charAt(x1--) + word;
}
} else {
break;
}
} else {
break;
}
// if (x2 >= 0 && x2 < s.length()) {
// if (Character.isLetterOrDigit(s.charAt(x2)) || s.charAt(x2) == '_'
// || s.charAt(x2) == '$')
// word = word + s.charAt(x2++);
// else
// x2 = -1;
// } else
// x2 = -1;
// if (x1 < 0 )//&& x2 < 0
// break;
if (i > 200) {
// time out!
System.err.println("Whoopsy! :P");
break;
}
}
// if (keyChar != KeyEvent.CHAR_UNDEFINED)
if (Character.isDigit(word.charAt(0)))
return null;
word = word.trim();
if (word.endsWith("."))
word = word.substring(0, word.length() - 1);
errorCheckerService.astGenerator.updatePredictions(word, line
+ errorCheckerService.mainClassOffset);
return word;
//}
}
/**
* Retrieve the total width of the gutter area.
*
* @return gutter width in pixels
*/
protected int getGutterWidth() {
FontMetrics fm = painter.getFontMetrics();
// System.out.println("fm: " + (fm == null));
// System.out.println("editor: " + (editor == null));
//System.out.println("BPBPBPBPB: " + (editor.breakpointMarker == null));
//System.out.println("BPBPBPBPB: " + (editor.breakpointMarker == null));
int textWidth = Math.max(fm.stringWidth(breakpointMarker), fm.stringWidth(currentLineMarker));
return textWidth + 2 * gutterPadding;
int textWidth = Math.max(fm.stringWidth(breakpointMarker),
fm.stringWidth(currentLineMarker));
return textWidth + 2 * gutterPadding;
}
/**
* Retrieve the width of margins applied to the left and right of the gutter
* text.
*
* @return margins in pixels
*/
protected int getGutterMargins() {
return gutterPadding;
}
/**
* Set the gutter text of a specific line.
*
* @param lineIdx
* the line index (0-based)
* @param text
* the text
*/
public void setGutterText(int lineIdx, String text) {
gutterText.put(lineIdx, text);
painter.invalidateLine(lineIdx);
}
/**
* Set the gutter text and color of a specific line.
*
* @param lineIdx
* the line index (0-based)
* @param text
* the text
* @param textColor
* the text color
*/
public void setGutterText(int lineIdx, String text, Color textColor) {
gutterTextColors.put(lineIdx, textColor);
setGutterText(lineIdx, text);
}
/**
* Clear the gutter text of a specific line.
*
* @param lineIdx
* the line index (0-based)
*/
public void clearGutterText(int lineIdx) {
gutterText.remove(lineIdx);
painter.invalidateLine(lineIdx);
}
/**
* Clear all gutter text.
*/
public void clearGutterText() {
for (int lineIdx : gutterText.keySet()) {
painter.invalidateLine(lineIdx);
}
gutterText.clear();
}
/**
* Retrieve the width of margins applied to the left and right of the gutter
* text.
*
* @return margins in pixels
*/
protected int getGutterMargins() {
return gutterPadding;
/**
* Retrieve the gutter text of a specific line.
*
* @param lineIdx
* the line index (0-based)
* @return the gutter text
*/
public String getGutterText(int lineIdx) {
return gutterText.get(lineIdx);
}
/**
* Retrieve the gutter text color for a specific line.
*
* @param lineIdx
* the line index
* @return the gutter text color
*/
public Color getGutterTextColor(int lineIdx) {
return gutterTextColors.get(lineIdx);
}
/**
* Set the background color of a line.
*
* @param lineIdx
* 0-based line number
* @param col
* the background color to set
*/
public void setLineBgColor(int lineIdx, Color col) {
lineColors.put(lineIdx, col);
painter.invalidateLine(lineIdx);
}
/**
* Clear the background color of a line.
*
* @param lineIdx
* 0-based line number
*/
public void clearLineBgColor(int lineIdx) {
lineColors.remove(lineIdx);
painter.invalidateLine(lineIdx);
}
/**
* Clear all line background colors.
*/
public void clearLineBgColors() {
for (int lineIdx : lineColors.keySet()) {
painter.invalidateLine(lineIdx);
}
lineColors.clear();
}
/**
* Set the gutter text of a specific line.
*
* @param lineIdx the line index (0-based)
* @param text the text
*/
public void setGutterText(int lineIdx, String text) {
gutterText.put(lineIdx, text);
painter.invalidateLine(lineIdx);
}
/**
* Get a lines background color.
*
* @param lineIdx
* 0-based line number
* @return the color or null if no color was set for the specified line
*/
public Color getLineBgColor(int lineIdx) {
return lineColors.get(lineIdx);
}
/**
* Set the gutter text and color of a specific line.
*
* @param lineIdx the line index (0-based)
* @param text the text
* @param textColor the text color
*/
public void setGutterText(int lineIdx, String text, Color textColor) {
gutterTextColors.put(lineIdx, textColor);
setGutterText(lineIdx, text);
}
/**
* Convert a character offset to a horizontal pixel position inside the text
* area. Overridden to take gutter width into account.
*
* @param line
* the 0-based line number
* @param offset
* the character offset (0 is the first character on a line)
* @return the horizontal position
*/
@Override
public int _offsetToX(int line, int offset) {
return super._offsetToX(line, offset) + getGutterWidth();
}
/**
* Clear the gutter text of a specific line.
*
* @param lineIdx the line index (0-based)
*/
public void clearGutterText(int lineIdx) {
gutterText.remove(lineIdx);
painter.invalidateLine(lineIdx);
}
/**
* Convert a horizontal pixel position to a character offset. Overridden to
* take gutter width into account.
*
* @param line
* the 0-based line number
* @param x
* the horizontal pixel position
* @return he character offset (0 is the first character on a line)
*/
@Override
public int xToOffset(int line, int x) {
return super.xToOffset(line, x - getGutterWidth());
}
/**
* Clear all gutter text.
*/
public void clearGutterText() {
for (int lineIdx : gutterText.keySet()) {
painter.invalidateLine(lineIdx);
}
gutterText.clear();
}
/**
* Custom mouse handler. Implements double clicking in the gutter area to
* toggle breakpoints, sets default cursor (instead of text cursor) in the
* gutter area.
*/
protected class MouseHandler implements MouseListener, MouseMotionListener {
/**
* Retrieve the gutter text of a specific line.
*
* @param lineIdx the line index (0-based)
* @return the gutter text
*/
public String getGutterText(int lineIdx) {
return gutterText.get(lineIdx);
}
protected int lastX; // previous horizontal positon of the mouse cursor
/**
* Retrieve the gutter text color for a specific line.
*
* @param lineIdx the line index
* @return the gutter text color
*/
public Color getGutterTextColor(int lineIdx) {
return gutterTextColors.get(lineIdx);
}
/**
* Set the background color of a line.
*
* @param lineIdx 0-based line number
* @param col the background color to set
*/
public void setLineBgColor(int lineIdx, Color col) {
lineColors.put(lineIdx, col);
painter.invalidateLine(lineIdx);
}
/**
* Clear the background color of a line.
*
* @param lineIdx 0-based line number
*/
public void clearLineBgColor(int lineIdx) {
lineColors.remove(lineIdx);
painter.invalidateLine(lineIdx);
}
/**
* Clear all line background colors.
*/
public void clearLineBgColors() {
for (int lineIdx : lineColors.keySet()) {
painter.invalidateLine(lineIdx);
}
lineColors.clear();
}
/**
* Get a lines background color.
*
* @param lineIdx 0-based line number
* @return the color or null if no color was set for the specified line
*/
public Color getLineBgColor(int lineIdx) {
return lineColors.get(lineIdx);
}
/**
* Convert a character offset to a horizontal pixel position inside the text
* area. Overridden to take gutter width into account.
*
* @param line the 0-based line number
* @param offset the character offset (0 is the first character on a line)
* @return the horizontal position
*/
@Override
public int _offsetToX(int line, int offset) {
return super._offsetToX(line, offset) + getGutterWidth();
public void mouseClicked(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseClicked(me);
}
}
/**
* Convert a horizontal pixel position to a character offset. Overridden to
* take gutter width into account.
*
* @param line the 0-based line number
* @param x the horizontal pixel position
* @return he character offset (0 is the first character on a line)
*/
@Override
public int xToOffset(int line, int x) {
return super.xToOffset(line, x - getGutterWidth());
public void mousePressed(MouseEvent me) {
// check if this happened in the gutter area
if (me.getX() < getGutterWidth()) {
if (me.getButton() == MouseEvent.BUTTON1 && me.getClickCount() == 2) {
int line = me.getY() / painter.getFontMetrics().getHeight()
+ firstLine;
if (line >= 0 && line <= getLineCount() - 1) {
editor.gutterDblClicked(line);
}
}
} else {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mousePressed(me);
}
}
}
/**
* Custom mouse handler. Implements double clicking in the gutter area to
* toggle breakpoints, sets default cursor (instead of text cursor) in the
* gutter area.
*/
protected class MouseHandler implements MouseListener, MouseMotionListener {
protected int lastX; // previous horizontal positon of the mouse cursor
@Override
public void mouseClicked(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseClicked(me);
}
}
@Override
public void mousePressed(MouseEvent me) {
// check if this happened in the gutter area
if (me.getX() < getGutterWidth()) {
if (me.getButton() == MouseEvent.BUTTON1 && me.getClickCount() == 2) {
int line = me.getY() / painter.getFontMetrics().getHeight() + firstLine;
if (line >= 0 && line <= getLineCount() - 1) {
editor.gutterDblClicked(line);
}
}
} else {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mousePressed(me);
}
}
}
@Override
public void mouseReleased(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseReleased(me);
}
}
@Override
public void mouseEntered(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseEntered(me);
}
}
@Override
public void mouseExited(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseExited(me);
}
}
@Override
public void mouseDragged(MouseEvent me) {
// No need to forward since the standard MouseMotionListeners are called anyway
// nop
}
@Override
public void mouseMoved(MouseEvent me) {
// No need to forward since the standard MouseMotionListeners are called anyway
if (me.getX() < getGutterWidth()) {
if (lastX >= getGutterWidth()) {
painter.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
} else {
if (lastX < getGutterWidth()) {
painter.setCursor(new Cursor(Cursor.TEXT_CURSOR));
}
}
lastX = me.getX();
}
@Override
public void mouseReleased(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseReleased(me);
}
}
@Override
public void mouseEntered(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseEntered(me);
}
}
@Override
public void mouseExited(MouseEvent me) {
// forward to standard listeners
for (MouseListener ml : mouseListeners) {
ml.mouseExited(me);
}
}
@Override
public void mouseDragged(MouseEvent me) {
// No need to forward since the standard MouseMotionListeners are called anyway
// nop
}
@Override
public void mouseMoved(MouseEvent me) {
// No need to forward since the standard MouseMotionListeners are called anyway
if (me.getX() < getGutterWidth()) {
if (lastX >= getGutterWidth()) {
painter.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
} else {
if (lastX < getGutterWidth()) {
painter.setCursor(new Cursor(Cursor.TEXT_CURSOR));
}
}
lastX = me.getX();
}
}
}

View File

@@ -19,6 +19,13 @@ package processing.mode.experimental;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Segment;
@@ -26,293 +33,457 @@ import javax.swing.text.Utilities;
import processing.app.syntax.TextAreaDefaults;
import processing.app.syntax.TokenMarker;
import processing.mode.experimental.ASTGenerator.ASTNodeWrapper;
/**
* Customized line painter. Adds support for background colors, left hand gutter
* area with background color and text.
*
*
* @author Martin Leopold <m@martinleopold.com>
*/
public class TextAreaPainter extends processing.app.syntax.TextAreaPainter {
protected TextArea ta; // we need the subclassed textarea
protected ErrorCheckerService errorCheckerService;
/**
* Error line underline color
*/
public Color errorColor = new Color(0xED2630);
protected TextArea ta; // we need the subclassed textarea
/**
* Warning line underline color
*/
protected ErrorCheckerService errorCheckerService;
public Color warningColor = new Color(0xFFC30E);
/**
* Error line underline color
*/
public Color errorColor = new Color(0xED2630);
/**
* Color of Error Marker
*/
public Color errorMarkerColor = new Color(0xED2630);
/**
* Warning line underline color
*/
/**
* Color of Warning Marker
*/
public Color warningMarkerColor = new Color(0xFFC30E);
public TextAreaPainter(TextArea textArea, TextAreaDefaults defaults) {
super(textArea, defaults);
ta = textArea;
}
private void loadTheme(ExperimentalMode mode){
errorColor = mode.getThemeColor("editor.errorcolor", errorColor);
warningColor = mode.getThemeColor("editor.warningcolor",
warningColor);
errorMarkerColor = mode.getThemeColor("editor.errormarkercolor",
errorMarkerColor);
warningMarkerColor = mode.getThemeColor(
"editor.warningmarkercolor", warningMarkerColor);
}
public Color warningColor = new Color(0xFFC30E);
/**
* Paint a line. Paints the gutter (with background color and text) then the
* line (background color and text).
*
* @param gfx the graphics context
* @param tokenMarker
* @param line 0-based line number
* @param x horizontal position
*/
@Override
protected void paintLine(Graphics gfx, TokenMarker tokenMarker,
int line, int x) {
/**
* Color of Error Marker
*/
public Color errorMarkerColor = new Color(0xED2630);
// paint gutter
paintGutterBg(gfx, line, x);
/**
* Color of Warning Marker
*/
public Color warningMarkerColor = new Color(0xFFC30E);
paintLineBgColor(gfx, line, x + ta.getGutterWidth());
static int ctrlMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
paintGutterLine(gfx, line, x);
// paint gutter symbol
paintGutterText(gfx, line, x);
paintErrorLine(gfx, line, x);
super.paintLine(gfx, tokenMarker, line, x + ta.getGutterWidth());
}
/**
* Paint the gutter background (solid color).
*
* @param gfx the graphics context
* @param line 0-based line number
* @param x horizontal position
*/
protected void paintGutterBg(Graphics gfx, int line, int x) {
gfx.setColor(ta.gutterBgColor);
int y = ta.lineToY(line) + fm.getLeading() + fm.getMaxDescent();
gfx.fillRect(0, y, ta.getGutterWidth(), fm.getHeight());
}
/**
* Paint the vertical gutter separator line.
*
* @param gfx the graphics context
* @param line 0-based line number
* @param x horizontal position
*/
protected void paintGutterLine(Graphics gfx, int line, int x) {
int y = ta.lineToY(line) + fm.getLeading() + fm.getMaxDescent();
gfx.setColor(ta.gutterLineColor);
gfx.drawLine(ta.getGutterWidth(), y, ta.getGutterWidth(), y + fm.getHeight());
}
/**
* Paint the gutter text.
*
* @param gfx the graphics context
* @param line 0-based line number
* @param x horizontal position
*/
protected void paintGutterText(Graphics gfx, int line, int x) {
String text = ta.getGutterText(line);
if (text == null) {
return;
}
gfx.setFont(getFont());
Color textColor = ta.getGutterTextColor(line);
if (textColor == null) {
gfx.setColor(getForeground());
} else {
gfx.setColor(textColor);
}
int y = ta.lineToY(line) + fm.getHeight();
// draw 4 times to make it appear bold, displaced 1px to the right, to the bottom and bottom right.
//int len = text.length() > ta.gutterChars ? ta.gutterChars : text.length();
Utilities.drawTabbedText(new Segment(text.toCharArray(), 0, text.length()), ta.getGutterMargins(), y, gfx, this, 0);
Utilities.drawTabbedText(new Segment(text.toCharArray(), 0, text.length()), ta.getGutterMargins() + 1, y, gfx, this, 0);
Utilities.drawTabbedText(new Segment(text.toCharArray(), 0, text.length()), ta.getGutterMargins(), y + 1, gfx, this, 0);
Utilities.drawTabbedText(new Segment(text.toCharArray(), 0, text.length()), ta.getGutterMargins() + 1, y + 1, gfx, this, 0);
}
/**
* Paint the background color of a line.
*
* @param gfx the graphics context
* @param line 0-based line number
* @param x
*/
protected void paintLineBgColor(Graphics gfx, int line, int x) {
int y = ta.lineToY(line);
y += fm.getLeading() + fm.getMaxDescent();
int height = fm.getHeight();
// get the color
Color col = ta.getLineBgColor(line);
//System.out.print("bg line " + line + ": ");
// no need to paint anything
if (col == null) {
//System.out.println("none");
return;
}
// paint line background
gfx.setColor(col);
gfx.fillRect(0, y, getWidth(), height);
}
/**
* Paints the underline for an error/warning line
*
* @param gfx
* the graphics context
* @param tokenMarker
* @param line
* 0-based line number: NOTE
* @param x
*/
protected void paintErrorLine(Graphics gfx, int line, int x) {
if (errorCheckerService == null) {
return;
public TextAreaPainter(TextArea textArea, TextAreaDefaults defaults) {
super(textArea, defaults);
ta = textArea;
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
// System.out.println( " Meta,Ctrl "+ (evt.getModifiers() & ctrlMask));
if (evt.isControlDown())
handleCtrlClick(evt);
}
if (errorCheckerService.problemsList== null) {
return;
}
boolean notFound = true;
boolean isWarning = false;
});
// Check if current line contains an error. If it does, find if it's an
// error or warning
for (ErrorMarker emarker : errorCheckerService.getEditor().errorBar.errorPoints) {
if (emarker.problem.lineNumber == line + 1) {
notFound = false;
if (emarker.type == ErrorMarker.Warning) {
isWarning = true;
}
}
// public void processKeyEvent(KeyEvent evt) {
// System.out.println(evt);
// }
void handleCtrlClick(MouseEvent evt) {
System.out.println("--handleCtrlClick--");
int off = ta.xyToOffset(evt.getX(), evt.getY());
if (off < 0)
return;
int line = ta.getLineOfOffset(off);
if (line < 0)
return;
String s = ta.getLineText(line);
if (s == null)
return;
else if (s.length() == 0)
return;
else {
int x = ta.xToOffset(line, evt.getX()), x2 = x + 1, x1 = x - 1;
int xLS = off - ta.getLineStartNonWhiteSpaceOffset(line);
if (x < 0 || x >= s.length())
return;
String word = s.charAt(x) + "";
if (s.charAt(x) == ' ')
return;
if (!(Character.isLetterOrDigit(s.charAt(x)) || s.charAt(x) == '_' || s
.charAt(x) == '$'))
return;
int i = 0;
while (true) {
i++;
if (x1 >= 0 && x1 < s.length()) {
if (Character.isLetter(s.charAt(x1)) || s.charAt(x1) == '_') {
word = s.charAt(x1--) + word;
} else
x1 = -1;
} else
x1 = -1;
if (x2 >= 0 && x2 < s.length()) {
if (Character.isLetterOrDigit(s.charAt(x2)) || s.charAt(x2) == '_'
|| s.charAt(x2) == '$')
word = word + s.charAt(x2++);
else
x2 = -1;
} else
x2 = -1;
if (x1 < 0 && x2 < 0)
break;
if (i > 200) {
// time out!
System.err.println("Whoopsy! :P");
break;
}
}
if (notFound) {
if (Character.isDigit(word.charAt(0)))
return;
System.out.print(errorCheckerService.mainClassOffset + line);
System.out.print("|" + line + "| offset " + xLS + word + " <= \n");
errorCheckerService.astGenerator.scrollToDeclaration(line
+ errorCheckerService.mainClassOffset, word, xLS);
}
}
private void loadTheme(ExperimentalMode mode) {
errorColor = mode.getThemeColor("editor.errorcolor", errorColor);
warningColor = mode.getThemeColor("editor.warningcolor", warningColor);
errorMarkerColor = mode.getThemeColor("editor.errormarkercolor",
errorMarkerColor);
warningMarkerColor = mode.getThemeColor("editor.warningmarkercolor",
warningMarkerColor);
}
/**
* Paint a line. Paints the gutter (with background color and text) then the
* line (background color and text).
*
* @param gfx
* the graphics context
* @param tokenMarker
* @param line
* 0-based line number
* @param x
* horizontal position
*/
@Override
protected void paintLine(Graphics gfx, TokenMarker tokenMarker, int line,
int x) {
// paint gutter
paintGutterBg(gfx, line, x);
paintLineBgColor(gfx, line, x + ta.getGutterWidth());
paintGutterLine(gfx, line, x);
// paint gutter symbol
paintGutterText(gfx, line, x);
paintErrorLine(gfx, line, x);
super.paintLine(gfx, tokenMarker, line, x + ta.getGutterWidth());
}
/**
* Paint the gutter background (solid color).
*
* @param gfx
* the graphics context
* @param line
* 0-based line number
* @param x
* horizontal position
*/
protected void paintGutterBg(Graphics gfx, int line, int x) {
gfx.setColor(ta.gutterBgColor);
int y = ta.lineToY(line) + fm.getLeading() + fm.getMaxDescent();
gfx.fillRect(0, y, ta.getGutterWidth(), fm.getHeight());
}
/**
* Paint the vertical gutter separator line.
*
* @param gfx
* the graphics context
* @param line
* 0-based line number
* @param x
* horizontal position
*/
protected void paintGutterLine(Graphics gfx, int line, int x) {
int y = ta.lineToY(line) + fm.getLeading() + fm.getMaxDescent();
gfx.setColor(ta.gutterLineColor);
gfx.drawLine(ta.getGutterWidth(), y, ta.getGutterWidth(),
y + fm.getHeight());
}
/**
* Paint the gutter text.
*
* @param gfx
* the graphics context
* @param line
* 0-based line number
* @param x
* horizontal position
*/
protected void paintGutterText(Graphics gfx, int line, int x) {
String text = ta.getGutterText(line);
if (text == null) {
return;
}
gfx.setFont(getFont());
Color textColor = ta.getGutterTextColor(line);
if (textColor == null) {
gfx.setColor(getForeground());
} else {
gfx.setColor(textColor);
}
int y = ta.lineToY(line) + fm.getHeight();
// draw 4 times to make it appear bold, displaced 1px to the right, to the bottom and bottom right.
//int len = text.length() > ta.gutterChars ? ta.gutterChars : text.length();
Utilities.drawTabbedText(new Segment(text.toCharArray(), 0, text.length()),
ta.getGutterMargins(), y, gfx, this, 0);
Utilities.drawTabbedText(new Segment(text.toCharArray(), 0, text.length()),
ta.getGutterMargins() + 1, y, gfx, this, 0);
Utilities.drawTabbedText(new Segment(text.toCharArray(), 0, text.length()),
ta.getGutterMargins(), y + 1, gfx, this, 0);
Utilities.drawTabbedText(new Segment(text.toCharArray(), 0, text.length()),
ta.getGutterMargins() + 1, y + 1, gfx, this, 0);
}
/**
* Paint the background color of a line.
*
* @param gfx
* the graphics context
* @param line
* 0-based line number
* @param x
*/
protected void paintLineBgColor(Graphics gfx, int line, int x) {
int y = ta.lineToY(line);
y += fm.getLeading() + fm.getMaxDescent();
int height = fm.getHeight();
// get the color
Color col = ta.getLineBgColor(line);
//System.out.print("bg line " + line + ": ");
// no need to paint anything
if (col == null) {
//System.out.println("none");
return;
}
// paint line background
gfx.setColor(col);
gfx.fillRect(0, y, getWidth(), height);
}
/**
* Paints the underline for an error/warning line
*
* @param gfx
* the graphics context
* @param tokenMarker
* @param line
* 0-based line number: NOTE
* @param x
*/
protected void paintErrorLine(Graphics gfx, int line, int x) {
if (errorCheckerService == null) {
return;
}
if (errorCheckerService.problemsList == null) {
return;
}
boolean notFound = true;
boolean isWarning = false;
// Check if current line contains an error. If it does, find if it's an
// error or warning
for (ErrorMarker emarker : errorCheckerService.getEditor().errorBar.errorPoints) {
if (emarker.problem.lineNumber == line + 1) {
notFound = false;
if (emarker.type == ErrorMarker.Warning) {
isWarning = true;
}
break;
}
// Determine co-ordinates
// System.out.println("Hoff " + ta.getHorizontalOffset() + ", " +
// horizontalAdjustment);
int y = ta.lineToY(line);
y += fm.getLeading() + fm.getMaxDescent();
int height = fm.getHeight();
int start = ta.getLineStartOffset(line);
}
if (notFound) {
return;
}
// Determine co-ordinates
// System.out.println("Hoff " + ta.getHorizontalOffset() + ", " +
// horizontalAdjustment);
int y = ta.lineToY(line);
y += fm.getLeading() + fm.getMaxDescent();
int height = fm.getHeight();
int start = ta.getLineStartOffset(line);
try {
String linetext = null;
try {
String linetext = null;
try {
linetext = ta.getDocument().getText(start,
ta.getLineStopOffset(line) - start - 1);
} catch (BadLocationException bl) {
// Error in the import statements or end of code.
// System.out.print("BL caught. " + ta.getLineCount() + " ,"
// + line + " ,");
// System.out.println((ta.getLineStopOffset(line) - start - 1));
return;
}
// Take care of offsets
int aw = fm.stringWidth(trimRight(linetext))
+ ta.getHorizontalOffset(); // apparent width. Whitespaces
// to the left of line + text
// width
int rw = fm.stringWidth(linetext.trim()); // real width
int x1 = 0 + (aw - rw), y1 = y + fm.getHeight() - 2, x2 = x1 + rw;
// Adding offsets for the gutter
x1 += 20;
x2 += 20;
// gfx.fillRect(x1, y, rw, height);
// Let the painting begin!
gfx.setColor(errorMarkerColor);
if (isWarning) {
gfx.setColor(warningMarkerColor);
}
gfx.fillRect(1, y + 2, 3, height - 2);
gfx.setColor(errorColor);
if (isWarning) {
gfx.setColor(warningColor);
}
int xx = x1;
// Draw the jagged lines
while (xx < x2) {
gfx.drawLine(xx, y1, xx + 2, y1 + 1);
xx += 2;
gfx.drawLine(xx, y1 + 1, xx + 2, y1);
xx += 2;
}
} catch (Exception e) {
System.out
.println("Looks like I messed up! XQTextAreaPainter.paintLine() : "
+ e);
//e.printStackTrace();
linetext = ta.getDocument().getText(start,
ta.getLineStopOffset(line) - start
- 1);
} catch (BadLocationException bl) {
// Error in the import statements or end of code.
// System.out.print("BL caught. " + ta.getLineCount() + " ,"
// + line + " ,");
// System.out.println((ta.getLineStopOffset(line) - start - 1));
return;
}
// Won't highlight the line. Select the text instead.
// gfx.setColor(Color.RED);
// gfx.fillRect(2, y, 3, height);
// Take care of offsets
int aw = fm.stringWidth(trimRight(linetext)) + ta.getHorizontalOffset(); // apparent width. Whitespaces
// to the left of line + text
// width
int rw = fm.stringWidth(linetext.trim()); // real width
int x1 = 0 + (aw - rw), y1 = y + fm.getHeight() - 2, x2 = x1 + rw;
// Adding offsets for the gutter
x1 += 20;
x2 += 20;
// gfx.fillRect(x1, y, rw, height);
// Let the painting begin!
gfx.setColor(errorMarkerColor);
if (isWarning) {
gfx.setColor(warningMarkerColor);
}
gfx.fillRect(1, y + 2, 3, height - 2);
gfx.setColor(errorColor);
if (isWarning) {
gfx.setColor(warningColor);
}
int xx = x1;
// Draw the jagged lines
while (xx < x2) {
gfx.drawLine(xx, y1, xx + 2, y1 + 1);
xx += 2;
gfx.drawLine(xx, y1 + 1, xx + 2, y1);
xx += 2;
}
} catch (Exception e) {
System.out
.println("Looks like I messed up! XQTextAreaPainter.paintLine() : "
+ e);
//e.printStackTrace();
}
/**
* Trims out trailing whitespaces (to the right)
*
* @param string
* @return - String
*/
private String trimRight(String string) {
String newString = "";
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) != ' ') {
newString = string.substring(0, i) + string.trim();
// Won't highlight the line. Select the text instead.
// gfx.setColor(Color.RED);
// gfx.fillRect(2, y, 3, height);
}
/**
* Trims out trailing whitespaces (to the right)
*
* @param string
* @return - String
*/
private String trimRight(String string) {
String newString = "";
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) != ' ') {
newString = string.substring(0, i) + string.trim();
break;
}
}
return newString;
}
/**
* Sets ErrorCheckerService and loads theme for TextAreaPainter(XQMode)
*
* @param ecs
* @param mode
*/
public void setECSandTheme(ErrorCheckerService ecs, ExperimentalMode mode) {
this.errorCheckerService = ecs;
loadTheme(mode);
}
public String getToolTipText(java.awt.event.MouseEvent evt) {System.err.println("GET");
int off = ta.xyToOffset(evt.getX(), evt.getY());
if (off < 0)
return null;
int line = ta.getLineOfOffset(off);
if (line < 0)
return null;
String s = ta.getLineText(line);
if (s == null)
return evt.toString();
else if (s.length() == 0)
return null;
else {
int x = ta.xToOffset(line, evt.getX()), x2 = x + 1, x1 = x - 1;
int xLS = off - ta.getLineStartNonWhiteSpaceOffset(line);
if (x < 0 || x >= s.length())
return null;
String word = s.charAt(x) + "";
if (s.charAt(x) == ' ')
return null;
if (!(Character.isLetterOrDigit(s.charAt(x)) || s.charAt(x) == '_' || s
.charAt(x) == '$'))
return null;
int i = 0;
while (true) {
i++;
if (x1 >= 0 && x1 < s.length()) {
if (Character.isLetter(s.charAt(x1)) || s.charAt(x1) == '_') {
word = s.charAt(x1--) + word;
} else
x1 = -1;
} else
x1 = -1;
if (x2 >= 0 && x2 < s.length()) {
if (Character.isLetterOrDigit(s.charAt(x2)) || s.charAt(x2) == '_'
|| s.charAt(x2) == '$')
word = word + s.charAt(x2++);
else
x2 = -1;
} else
x2 = -1;
if (x1 < 0 && x2 < 0)
break;
if (i > 200) {
// time out!
System.err.println("Whoopsy! :P");
break;
}
}
return newString;
}
/**
* Sets ErrorCheckerService and loads theme for TextAreaPainter(XQMode)
* @param ecs
* @param mode
*/
public void setECSandTheme(ErrorCheckerService ecs, ExperimentalMode mode){
this.errorCheckerService = ecs;
loadTheme(mode);
if (Character.isDigit(word.charAt(0)))
return null;
String tooltipText = errorCheckerService.astGenerator
.getLabelForASTNode(line + errorCheckerService.mainClassOffset, word,
xLS);
System.out.print(errorCheckerService.mainClassOffset + " MCO ");
System.out.print("|" + line + "| offset " + xLS + word + " <= offf: "+off+ "\n");
if (tooltipText != null)
return tooltipText;
return word;
}
}
}

View File

@@ -55,7 +55,6 @@ import processing.mode.java.preproc.PdePreprocessor;
public class XQPreprocessor {
private ASTRewrite rewrite = null;
public int mainClassOffset = 0;
private ArrayList<String> imports;
private ArrayList<ImportStatement> extraImports;
@@ -76,7 +75,7 @@ public class XQPreprocessor {
public String doYourThing(String source,
ArrayList<ImportStatement> programImports) {
this.extraImports = programImports;
source = prepareImports() + source;
//source = prepareImports() + source;
Document doc = new Document(source);
ASTParser parser = ASTParser.newParser(AST.JLS4);
@@ -117,7 +116,6 @@ public class XQPreprocessor {
}
lines += 2;
// System.out.println("Lines: " + lines);
mainClassOffset = lines;
return doc.get();
}
@@ -146,6 +144,11 @@ public class XQPreprocessor {
totalImports += "\n";
return totalImports;
}
public String prepareImports(ArrayList<ImportStatement> programImports) {
this.extraImports = programImports;
return prepareImports();
}
/**
* Visitor implementation that does all the substitution dirty work. <br>