Skip to content

Commit 3d37a7a

Browse files
committed
tweakmode: place tweak init function right after the size function
1 parent c9dc9a7 commit 3d37a7a

File tree

2 files changed

+133
-22
lines changed

2 files changed

+133
-22
lines changed

java/src/processing/mode/java/JavaEditor.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,8 +2898,8 @@ protected boolean automateSketch(Sketch sketch, SketchParser parser) {
28982898
return false;
28992899
}
29002900

2901-
int setupEndPos = SketchParser.getSetupEnd(baseCode[0]);
2902-
if (setupEndPos < 0) {
2901+
int afterSizePos = SketchParser.getAfterSizePos(baseCode[0]);
2902+
if (afterSizePos < 0) {
29032903
return false;
29042904
}
29052905

@@ -2992,12 +2992,14 @@ protected boolean automateSketch(Sketch sketch, SketchParser parser) {
29922992

29932993
// add call to our initAllVars and initOSC functions
29942994
// from the setup() function.
2995-
String addToSetup = "\n"+
2996-
" tweakmode_initAllVars();\n"+
2997-
" tweakmode_initCommunication();\n\n";
2998-
2999-
setupEndPos = SketchParser.getSetupEnd(c);
3000-
c = replaceString(c, setupEndPos, setupEndPos, addToSetup);
2995+
String addToSetup = "\n\n\n"+
2996+
" /* TWEAKMODE */\n"+
2997+
" tweakmode_initAllVars();\n"+
2998+
" tweakmode_initCommunication();\n"+
2999+
" /* TWEAKMODE */\n\n";
3000+
3001+
afterSizePos = SketchParser.getAfterSizePos(c);
3002+
c = replaceString(c, afterSizePos, afterSizePos, addToSetup);
30013003

30023004
// Server code defines a class, so it should go later in the sketch
30033005
String serverCode =

java/src/processing/mode/java/tweak/SketchParser.java

Lines changed: 123 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public class SketchParser {
3939

4040
List<List<Range>> scientificNotations;
4141

42-
Range setupFunction;
42+
// currently is used to ignore numbers in 'setup' and 'settings' functions
43+
List<Range> ignoreFunctions;
4344

4445
List<List<Range>> commentBlocks;
4546
List<int[]> curlyScopes;
@@ -57,8 +58,12 @@ public SketchParser(String[] codeTabs, boolean requiresComment) {
5758
commentBlocks.add(getCommentBlocks(code));
5859
}
5960

60-
// get setup function range (to ignore all numbers there)
61-
setupFunction = new Range(getSetupStart(codeTabs[0]), getSetupEnd(codeTabs[0]));
61+
// add 'settings' and 'setup' to ignore list (to ignore all numbers there)
62+
ignoreFunctions = new ArrayList<Range>();
63+
ignoreFunctions.add(new Range(getVoidFunctionStart(codeTabs[0], "settings"),
64+
getVoidFunctionEnd(codeTabs[0], "settings")));
65+
ignoreFunctions.add(new Range(getVoidFunctionStart(codeTabs[0], "setup"),
66+
getVoidFunctionEnd(codeTabs[0], "setup")));
6267

6368
// build curly scope for every character in the code
6469
curlyScopes = new ArrayList<>();
@@ -125,8 +130,8 @@ private void addAllDecimalNumbers() {
125130
continue;
126131
}
127132

128-
if (setupFunction.contains(start)) {
129-
// ignore numbers in setup
133+
if (isInRangeList(start, ignoreFunctions)) {
134+
// ignore numbers in predefined functions
130135
continue;
131136
}
132137

@@ -217,8 +222,8 @@ private void addAllHexNumbers() {
217222
continue;
218223
}
219224

220-
if (setupFunction.contains(start)) {
221-
// ignore number in setup
225+
if (isInRangeList(start, ignoreFunctions)) {
226+
// ignore numbers in predefined functions
222227
continue;
223228
}
224229

@@ -278,8 +283,8 @@ private void addAllWebColorNumbers() {
278283
continue;
279284
}
280285

281-
if (setupFunction.contains(start)) {
282-
// ignore number in setup
286+
if (isInRangeList(start, ignoreFunctions)) {
287+
// ignore numbers in predefined functions
283288
continue;
284289
}
285290

@@ -382,8 +387,8 @@ private void createColorBoxes() {
382387
continue;
383388
}
384389

385-
if (setupFunction.contains(m.start())) {
386-
// ignore number in setup
390+
if (isInRangeList(m.start(), ignoreFunctions)) {
391+
// ignore numbers in predefined functions
387392
continue;
388393
}
389394

@@ -466,8 +471,8 @@ private void createColorBoxesForLights() {
466471
continue;
467472
}
468473

469-
if (setupFunction.contains(m.start())) {
470-
// ignore number in setup
474+
if (isInRangeList(m.start(), ignoreFunctions)) {
475+
// ignore numbers in predefined functions
471476
continue;
472477
}
473478

@@ -827,6 +832,50 @@ else if (readObject) {
827832
return obj;
828833
}
829834

835+
static public int getVoidFunctionStart(String code, String functionName) {
836+
Pattern p = Pattern.compile("void[\\s\\t\\r\\n]*"+functionName+"[\\s\\t]*\\(\\)[\\s\\t\\r\\n]*\\{");
837+
Matcher m = p.matcher(code);
838+
839+
if (m.find()) {
840+
return m.end();
841+
}
842+
843+
return -1;
844+
}
845+
846+
static public int getVoidFunctionEnd(String code, String functionName) {
847+
List<Range> comments = getCommentBlocks(code);
848+
849+
int start = getVoidFunctionStart(code, functionName);
850+
if (start == -1) {
851+
return -1;
852+
}
853+
854+
// count brackets to look for setup end
855+
int bracketCount=1;
856+
int pos = start;
857+
while (bracketCount > 0 && pos < code.length()) {
858+
if (isInRangeList(pos, comments)) {
859+
// in a comment, ignore and move on
860+
pos++;
861+
continue;
862+
}
863+
864+
if (code.charAt(pos) == '{') {
865+
bracketCount++;
866+
}
867+
else if (code.charAt(pos) == '}') {
868+
bracketCount--;
869+
}
870+
pos++;
871+
}
872+
873+
if (bracketCount == 0) {
874+
return pos-1;
875+
}
876+
return -1;
877+
}
878+
830879

831880
static public int getSetupStart(String code) {
832881
Pattern p = Pattern.compile("void[\\s\\t\\r\\n]*setup[\\s\\t]*\\(\\)[\\s\\t\\r\\n]*\\{");
@@ -839,7 +888,6 @@ static public int getSetupStart(String code) {
839888
return -1;
840889
}
841890

842-
843891
static public int getSetupEnd(String code) {
844892
List<Range> comments = getCommentBlocks(code);
845893

@@ -873,6 +921,67 @@ else if (code.charAt(pos) == '}') {
873921
return -1;
874922
}
875923

924+
static public int getAfterSizePos(String code) {
925+
List<Range> comments = getCommentBlocks(code);
926+
927+
// find the size function
928+
Pattern p = Pattern.compile("size[\\s\\t]*\\(");
929+
Matcher m = p.matcher(code);
930+
931+
while (m.find()) {
932+
if (isInRangeList(m.start(), comments) ||
933+
isInRangeList(m.end(), comments)) {
934+
// this is a comment, next
935+
continue;
936+
}
937+
938+
// count brackets to look for size call end
939+
int bracketCount=1;
940+
int pos = m.end();
941+
while (bracketCount > 0 && pos < code.length()) {
942+
if (isInRangeList(pos, comments)) {
943+
// in a comment, ignore and move on
944+
pos++;
945+
continue;
946+
}
947+
948+
if (code.charAt(pos) == '(') {
949+
bracketCount++;
950+
}
951+
else if (code.charAt(pos) == ')') {
952+
bracketCount--;
953+
}
954+
pos++;
955+
}
956+
957+
if (bracketCount != 0) {
958+
// could not find closing ')'. next
959+
continue;
960+
}
961+
962+
// find ';' sign
963+
boolean found = false;
964+
while (pos < code.length()) {
965+
if (code.charAt(pos) == ';' &&
966+
!isInRangeList(pos, comments)) {
967+
found = true;
968+
break;
969+
}
970+
pos++;
971+
}
972+
973+
if (!found) {
974+
// didn't find the ';'. next
975+
continue;
976+
}
977+
978+
// success! we found the place
979+
return pos+1;
980+
}
981+
982+
// nothing was found
983+
return -1;
984+
}
876985

877986
static class Range {
878987
int start;

0 commit comments

Comments
 (0)