change up how extra statements from size() are handled (fixes #3531)

This commit is contained in:
Ben Fry
2015-08-05 08:33:34 -04:00
parent 5315c6c869
commit 98a5650fc3
5 changed files with 94 additions and 36 deletions

View File

@@ -257,13 +257,22 @@ public class JavaBuild {
return null;
}
//System.out.format("size() is '%s'%n", info[0]);
// Remove the size() statement (will be added back by writeFooter())
// Remove the entries being moved to settings(). They will be re-inserted
// by writeFooter() when it emits the settings() method.
if (sizeInfo != null) {
String sizeStatement = sizeInfo.getStatement();
if (sizeStatement != null) {
int index = bigCode.indexOf(sizeStatement);
bigCode.delete(index, index + sizeStatement.length());
// String sizeStatement = sizeInfo.getStatement();
for (String stmt : sizeInfo.getStatements()) {
// if (sizeStatement != null) {
//System.out.format("size stmt is '%s'%n", sizeStatement);
int index = bigCode.indexOf(stmt);
if (index != -1) {
bigCode.delete(index, index + stmt.length());
} else {
// TODO remove once we hit final; but prevent an exception like in
// https://github.com/processing/processing/issues/3531
System.err.format("Error removing '%s' from the code.", stmt);
}
}
}

View File

@@ -357,7 +357,8 @@ public class PdePreprocessor {
if (sizeContents != null) {
StringList args = breakCommas(sizeContents[1]);
SurfaceInfo info = new SurfaceInfo();
info.statement = sizeContents[0];
// info.statement = sizeContents[0];
info.addStatement(sizeContents[0]);
info.width = args.get(0).trim();
info.height = args.get(1).trim();
info.renderer = (args.size() >= 3) ? args.get(2).trim() : null;
@@ -384,9 +385,7 @@ public class PdePreprocessor {
throw new SketchException("Please fix the size() line to continue.", false);
}
if (extraStatements.size() != 0) {
info.statement += extraStatements.join(" ");
}
info.addStatements(extraStatements);
info.checkEmpty();
return info;
//return new String[] { contents[0], width, height, renderer, path };
@@ -395,7 +394,8 @@ public class PdePreprocessor {
//contents = PApplet.match(searchArea, FULL_SCREEN_CONTENTS_REGEX);
if (fullContents != null) {
SurfaceInfo info = new SurfaceInfo();
info.statement = fullContents[0];
// info.statement = fullContents[0];
info.addStatement(fullContents[0]);
StringList args = breakCommas(fullContents[1]);
if (args.size() > 0) { // might have no args
String args0 = args.get(0).trim();
@@ -416,9 +416,10 @@ public class PdePreprocessor {
}
info.width = "displayWidth";
info.height = "displayHeight";
if (extraStatements.size() != 0) {
info.statement += extraStatements.join(" ");
}
// if (extraStatements.size() != 0) {
// info.statement += extraStatements.join(" ");
// }
info.addStatements(extraStatements);
info.checkEmpty();
return info;
}
@@ -427,7 +428,8 @@ public class PdePreprocessor {
// need to pull out the noSmooth() and smooth(N) methods.
if (extraStatements.size() != 0) {
SurfaceInfo info = new SurfaceInfo();
info.statement = extraStatements.join(" ");
// info.statement = extraStatements.join(" ");
info.addStatements(extraStatements);
return info;
}
@@ -1094,10 +1096,10 @@ public class PdePreprocessor {
}
if ((mode == Mode.STATIC) || (mode == Mode.ACTIVE)) {
// doesn't remove the oriiginal size() method, but calling size()
// doesn't remove the original size() method, but calling size()
// again in setup() is harmless.
if (!hasMethod("settings") && sizeInfo.statement != null) {
out.println(indent + "public void settings() { " + sizeInfo.statement + " }");
if (!hasMethod("settings") && sizeInfo.hasSettings()) {
out.println(indent + "public void settings() { " + sizeInfo.getSettings() + " }");
// out.println(indent + "public void settings() {");
// out.println(indent + indent + sizeStatement);
// out.println(indent + "}");

View File

@@ -25,10 +25,12 @@ package processing.mode.java.preproc;
import processing.app.Base;
import processing.core.PApplet;
import processing.data.StringList;
public class SurfaceInfo {
String statement;
// String statement;
StringList statements;
String width;
String height;
String renderer;
@@ -101,7 +103,42 @@ public class SurfaceInfo {
}
public String getStatement() {
return statement;
// public String getStatements() {
// return statements.join(" ");
// }
public StringList getStatements() {
return statements;
}
/**
* Add an item that will be moved from size() into the settings() method.
* This needs to be the exact version of the statement so that it can be
* matched against and removed from the size() method in the code.
*/
public void addStatement(String stmt) {
if (statements == null) {
statements = new StringList();
}
statements.append(stmt);
}
public void addStatements(StringList list) {
statements.append(list);
}
/** @return true if there's code to be inserted for a settings() method. */
public boolean hasSettings() {
return statements != null;
}
/** @return the contents of the settings() method to be inserted */
public String getSettings() {
return statements.join(" ");
}
}