mirror of
https://github.com/processing/processing4.git
synced 2026-02-13 10:30:44 +01:00
Ctrl+Click with offset handling. Also moved stuff around.
This commit is contained in:
@@ -1011,9 +1011,16 @@ public class ASTGenerator {
|
||||
ASTNode decl = null;
|
||||
if (lineNode != null) {
|
||||
ASTNodeWrapper lineNodeWrap = new ASTNodeWrapper(lineNode);
|
||||
int ret[][] = lineNodeWrap.getOffsetMapping(errorCheckerService);
|
||||
int javaCodeMap[] = ret[0];
|
||||
int altOff = 0;
|
||||
for (; altOff < javaCodeMap.length; altOff++) {
|
||||
if(javaCodeMap[altOff] == offset)
|
||||
break;
|
||||
}
|
||||
System.out.println("FLON2: " + lineNumber + " LN spos "
|
||||
+ lineNode.getStartPosition() + " off " + offset);
|
||||
ASTNode simpName = pinpointOnLine(lineNode, offset,
|
||||
+ lineNode.getStartPosition() + " off " + offset + " alt off" + altOff);
|
||||
ASTNode simpName = pinpointOnLine(lineNode, altOff,
|
||||
lineNode.getStartPosition(), name);
|
||||
System.out.println("+++> " + simpName);
|
||||
if (simpName instanceof SimpleName) {
|
||||
|
||||
@@ -120,163 +120,7 @@ public class ASTNodeWrapper {
|
||||
|
||||
|
||||
private int[] createOffsetMapping(String source, int inpOffset, int nodeLen) {
|
||||
|
||||
/*
|
||||
* This is some tricky shiz. So detailed explanation follows:
|
||||
*
|
||||
* The main issue here is that pde enhancements like color vars, # literals
|
||||
* and int() type casting deviate from standard java. But I need to exact
|
||||
* index matching for pde and java versions of snippets.For ex:
|
||||
* "color col = #ffaadd;" <-PDE version
|
||||
* "int col = 0xffffaadd;" <-Converted to Java
|
||||
*
|
||||
* For exact index mapping, I need to know at which indices either is
|
||||
* deviating from the other and by what amount. Turns out, it isn't quite
|
||||
* easy.(1) First I take the pde version of the code as an argument(pde
|
||||
* version fetched from the editor directly). I then find all instances
|
||||
* which need to be converted to pure java, marking those indices and the
|
||||
* index correction needed. (2) Now all java conversions are applied after
|
||||
* marking the offsets. This ensures that the index order isn't disturbed by
|
||||
* one at a time conversions as done in preprocessCode() in ECS. Took me
|
||||
* sometime to figure out this was a bug. (3) Next I create a tables(two
|
||||
* separate arrays) which allows me to look it up for matching any index
|
||||
* between pde or java version of the snippet. This also lets me find out
|
||||
* any difference in length between both versions.
|
||||
*
|
||||
* Keep in mind though, dark magic was involved in creating the final lookup
|
||||
* table.
|
||||
*
|
||||
* TODO: This is a work in progress. There may be more bugs here in hiding.
|
||||
*/
|
||||
|
||||
/*
|
||||
System.out.println("Src:" + source + "\ninpoff" + inpOffset + " nodelen "
|
||||
+ nodeLen);
|
||||
String sourceAlt = new String(source);
|
||||
TreeMap<Integer, Integer> offsetmap = new TreeMap<Integer, Integer>();
|
||||
|
||||
// Find all #[web color]
|
||||
// Should be 6 digits only.
|
||||
final String webColorRegexp = "#{1}[A-F|a-f|0-9]{6}\\W";
|
||||
Pattern webPattern = Pattern.compile(webColorRegexp);
|
||||
Matcher webMatcher = webPattern.matcher(sourceAlt);
|
||||
while (webMatcher.find()) {
|
||||
// System.out.println("Found at: " + webMatcher.start());
|
||||
// System.out.println("-> " + found);
|
||||
offsetmap.put(webMatcher.end() - 1, 3);
|
||||
}
|
||||
|
||||
// Find all color data types
|
||||
final String colorTypeRegex = "color(?![a-zA-Z0-9_])(?=\\[*)(?!(\\s*\\())";
|
||||
Pattern colorPattern = Pattern.compile(colorTypeRegex);
|
||||
Matcher colorMatcher = colorPattern.matcher(sourceAlt);
|
||||
while (colorMatcher.find()) {
|
||||
// System.out.print("Start index: " + colorMatcher.start());
|
||||
// System.out.println(" End index: " + colorMatcher.end() + " ");
|
||||
// System.out.println("-->" + colorMatcher.group() + "<--");
|
||||
offsetmap.put(colorMatcher.end() - 1, -2);
|
||||
}
|
||||
|
||||
// Find all int(), char()
|
||||
String dataTypeFunc[] = { "int", "char", "float", "boolean", "byte" };
|
||||
|
||||
for (String dataType : dataTypeFunc) {
|
||||
String dataTypeRegexp = "\\b" + dataType + "\\s*\\(";
|
||||
Pattern pattern = Pattern.compile(dataTypeRegexp);
|
||||
Matcher matcher = pattern.matcher(sourceAlt);
|
||||
|
||||
while (matcher.find()) {
|
||||
// System.out.print("Start index: " + matcher.start());
|
||||
// System.out.println(" End index: " + matcher.end() + " ");
|
||||
// System.out.println("-->" + matcher.group() + "<--");
|
||||
offsetmap.put(matcher.end() - 1, ("PApplet.parse").length());
|
||||
}
|
||||
matcher.reset();
|
||||
sourceAlt = matcher.replaceAll("PApplet.parse"
|
||||
+ Character.toUpperCase(dataType.charAt(0)) + dataType.substring(1)
|
||||
+ "(");
|
||||
|
||||
}
|
||||
|
||||
// replace with 0xff[webcolor] and others
|
||||
webMatcher = webPattern.matcher(sourceAlt);
|
||||
while (webMatcher.find()) {
|
||||
// System.out.println("Found at: " + webMatcher.start());
|
||||
String found = sourceAlt.substring(webMatcher.start(), webMatcher.end());
|
||||
// System.out.println("-> " + found);
|
||||
sourceAlt = webMatcher.replaceFirst("0xff" + found.substring(1));
|
||||
webMatcher = webPattern.matcher(sourceAlt);
|
||||
}
|
||||
|
||||
colorMatcher = colorPattern.matcher(sourceAlt);
|
||||
sourceAlt = colorMatcher.replaceAll("int");
|
||||
|
||||
System.out.println(sourceAlt);
|
||||
|
||||
// Create code map. Beware! Dark magic ahead.
|
||||
int javaCodeMap[] = new int[source.length() * 2];
|
||||
int pdeCodeMap[] = new int[source.length() * 2];
|
||||
int pi = 1, pj = 1;
|
||||
int keySum = 0;
|
||||
for (Integer key : offsetmap.keySet()) {
|
||||
for (; pi < key +keySum; pi++) {
|
||||
javaCodeMap[pi] = javaCodeMap[pi - 1] + 1;
|
||||
}
|
||||
for (; pj < key+keySum; pj++) {
|
||||
pdeCodeMap[pj] = pdeCodeMap[pj - 1] + 1;
|
||||
}
|
||||
|
||||
System.out.println(key + ":" + offsetmap.get(key));
|
||||
|
||||
int kval = offsetmap.get(key);
|
||||
if (kval > 0) {
|
||||
// repeat java offsets
|
||||
pi--;
|
||||
pj--;
|
||||
for (int i = 0; i < kval; i++, pi++, pj++) {
|
||||
javaCodeMap[pi] = javaCodeMap[pi - 1];
|
||||
pdeCodeMap[pj] = pdeCodeMap[pj - 1] + 1;
|
||||
}
|
||||
} else {
|
||||
// repeat pde offsets
|
||||
pi--;
|
||||
pj--;
|
||||
for (int i = 0; i < -kval; i++, pi++, pj++) {
|
||||
javaCodeMap[pi] = javaCodeMap[pi - 1] + 1;
|
||||
pdeCodeMap[pj] = pdeCodeMap[pj - 1];
|
||||
}
|
||||
}
|
||||
|
||||
// after each adjustment, the key values need to keep
|
||||
// up with changed offset
|
||||
keySum += kval;
|
||||
}
|
||||
|
||||
javaCodeMap[pi] = javaCodeMap[pi - 1] + 1;
|
||||
pdeCodeMap[pj] = pdeCodeMap[pj - 1] + 1;
|
||||
|
||||
while (pi < sourceAlt.length()) {
|
||||
javaCodeMap[pi] = javaCodeMap[pi - 1] + 1;
|
||||
pi++;
|
||||
}
|
||||
while (pj < source.length()) {
|
||||
pdeCodeMap[pj] = pdeCodeMap[pj - 1] + 1;
|
||||
pj++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < pdeCodeMap.length; i++) {
|
||||
if (pdeCodeMap[i] > 0 || javaCodeMap[i] > 0 || i == 0) {
|
||||
if (i < source.length())
|
||||
System.out.print(source.charAt(i));
|
||||
System.out.print(pdeCodeMap[i] + " - " + javaCodeMap[i]);
|
||||
if (i < sourceAlt.length())
|
||||
System.out.print(sourceAlt.charAt(i));
|
||||
System.out.print(" <-[" + i + "]");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
*/
|
||||
|
||||
int ret[][] = getOffsetMapping(source);
|
||||
int javaCodeMap[] = ret[0];
|
||||
int pdeCodeMap[] = ret[1];
|
||||
@@ -315,6 +159,35 @@ public class ASTNodeWrapper {
|
||||
* @return int[0] - java code offsets, int[1] = pde code offsets
|
||||
*/
|
||||
public int[][] getOffsetMapping(String source){
|
||||
|
||||
/*
|
||||
* This is some tricky shiz. So detailed explanation follows:
|
||||
*
|
||||
* The main issue here is that pde enhancements like color vars, # literals
|
||||
* and int() type casting deviate from standard java. But I need to exact
|
||||
* index matching for pde and java versions of snippets.For ex:
|
||||
* "color col = #ffaadd;" <-PDE version
|
||||
* "int col = 0xffffaadd;" <-Converted to Java
|
||||
*
|
||||
* For exact index mapping, I need to know at which indices either is
|
||||
* deviating from the other and by what amount. Turns out, it isn't quite
|
||||
* easy.(1) First I take the pde version of the code as an argument(pde
|
||||
* version fetched from the editor directly). I then find all instances
|
||||
* which need to be converted to pure java, marking those indices and the
|
||||
* index correction needed. (2) Now all java conversions are applied after
|
||||
* marking the offsets. This ensures that the index order isn't disturbed by
|
||||
* one at a time conversions as done in preprocessCode() in ECS. Took me
|
||||
* sometime to figure out this was a bug. (3) Next I create a tables(two
|
||||
* separate arrays) which allows me to look it up for matching any index
|
||||
* between pde or java version of the snippet. This also lets me find out
|
||||
* any difference in length between both versions.
|
||||
*
|
||||
* Keep in mind though, dark magic was involved in creating the final lookup
|
||||
* table.
|
||||
*
|
||||
* TODO: This is a work in progress. There may be more bugs here in hiding.
|
||||
*/
|
||||
|
||||
System.out.println("Src:" + source);
|
||||
String sourceAlt = new String(source);
|
||||
TreeMap<Integer, Integer> offsetmap = new TreeMap<Integer, Integer>();
|
||||
@@ -444,6 +317,12 @@ public class ASTNodeWrapper {
|
||||
|
||||
return new int[][]{javaCodeMap,pdeCodeMap};
|
||||
}
|
||||
|
||||
public int[][] getOffsetMapping(ErrorCheckerService ecs){
|
||||
int pdeoffsets[] = getPDECodeOffsets(ecs);
|
||||
String pdeCode = ecs.getPDECode(pdeoffsets[1] - 1).trim();
|
||||
return getOffsetMapping(pdeCode);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user