diff --git a/pdex/Todo, GSoC 2013.txt b/pdex/Todo, GSoC 2013.txt index 2d8b268c5..0663bd840 100644 --- a/pdex/Todo, GSoC 2013.txt +++ b/pdex/Todo, GSoC 2013.txt @@ -28,8 +28,8 @@ Finer details * Reflection API - getMethods vs getDeclaredMethods. * Need to add offset correction to ASTGenerator and its lookup methods. Or leave it for later? -Offset Mapping & Refactoring -============================ +Offset Mapping +============== First major hurdle is offset mapping *! pde<->java code offset : precise conversion needed x for the above, I've decide to first implement a sketch outline like feature, which would highlight an AST element precisely in the pde code. This would ensure I've got the mapping working properly. And may lead to a future feature. @@ -38,12 +38,18 @@ x Edge case - multiple statements in a single line x PDE specific enhancements will also have to be tackled like int(), # literals. The length of the node returned needs to be modified to make up for extra chars added like PApplet.parseFloat, etc. Also the 2nd or futher pde enhancements in the same line means even the beginning offset would need adjustment. Meh. * The above is almost working. There are some offset issues when multiple pde statements are in the same line, but I guess it's good enough for now to proceed ahead. Will keep a close watch for potential bugs. +Refactoring +=========== Refactoring would work only when code is compiler error free. I plan to do a find replace type op on the compile ready code. 1. First identify the declaration of the variable in the AST. We'll then make a list of all its occurrences. 2. DFS through the AST, for each (SimpleName)instance of the word in code, find if the matched word is the same one whose declaration we found. x Edge Case: For renaming a TypeDeclaration, the declaration of SimpleName instance of the TD and it's constructor(s) aren't added to the list generated by DFS. So for renaming TD, will have to manually add the TD SimpleName and it's constructors' SimpleNames to the a list of declaration nodes that can be positively matched against. -3. Find corresponding PDE offsets of the SimpleNames, rename in each line taking displaced offsets into consideration. +x Renaming any constructor is equivalent to renaming the TD +3. Find corresponding PDE offsets of the SimpleNames, rename in each line. +* Edge Case: Need to take displaced offsets on a line, due to pde enhancements, into consideration. 4. All the changes in code would be made in a separate copy of the code(?). After all the renaming is done, allow it only if the new code compiles. Basically an undo should be possible in case of conflicts. +* Undo misbehaves here, handle carefully. +* Handle saving. If sketch closed after renaming w/o saving find bugs. Quick Navigation ================ diff --git a/pdex/src/processing/mode/experimental/ASTGenerator.java b/pdex/src/processing/mode/experimental/ASTGenerator.java index 4a91fcd7d..749571692 100644 --- a/pdex/src/processing/mode/experimental/ASTGenerator.java +++ b/pdex/src/processing/mode/experimental/ASTGenerator.java @@ -20,6 +20,7 @@ import java.lang.reflect.Modifier; import java.net.URL; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -178,7 +179,7 @@ public class ASTGenerator { //// loadJars(); //addCompletionPopupListner(); - addListenrs(); + addListeners(); } private DefaultMutableTreeNode buildAST(String source, CompilationUnit cu) { @@ -1248,7 +1249,7 @@ public class ASTGenerator { } } - private void addListenrs(){ + private void addListeners(){ jtree.addTreeSelectionListener(new TreeSelectionListener() { @Override @@ -1302,6 +1303,10 @@ public class ASTGenerator { DefaultMutableTreeNode defCU = findAllOccurrences(); renameTree.setModel(new DefaultTreeModel(defCU)); ((DefaultTreeModel) renameTree.getModel()).reload(); + int lineOffsetDisplacementConst = newName.length() + - editor.ta.getSelectedText().length(); + HashMap lineOffsetDisplacement = new HashMap(); + for (int i = 0; i < defCU.getChildCount(); i++) { ASTNodeWrapper awrap = (ASTNodeWrapper) ((DefaultMutableTreeNode) (defCU .getChildAt(i))).getUserObject(); @@ -1313,6 +1318,19 @@ public class ASTGenerator { javaoffsets[1], javaoffsets[2]); editor.ta.setSelectedText(newName); + if(lineOffsetDisplacement.get(javaoffsets[0]) != null){ + int off = lineOffsetDisplacement.get(javaoffsets[0]); + lineOffsetDisplacement.put(javaoffsets[0], + lineOffsetDisplacementConst + off); + } + else{ + lineOffsetDisplacement.put(javaoffsets[0], + lineOffsetDisplacementConst); + } + } + for (Integer lineNum : lineOffsetDisplacement.keySet()) { + System.out.println(lineNum + "line, disp" + + lineOffsetDisplacement.get(lineNum)); } editor.getSketch().setModified(true); } @@ -1400,6 +1418,26 @@ public class ASTGenerator { System.err.println("Gonna find all occurrences of " + getNodeAsString(wnode.getNode())); + //If wnode is a constructor, find the TD instead. + if (wnode.getNodeType() == ASTNode.METHOD_DECLARATION) { + MethodDeclaration md = (MethodDeclaration) wnode.getNode(); + ASTNode node = md.getParent(); + while (node != null) { + if (node instanceof TypeDeclaration) { + // System.out.println("Parent class " + getNodeAsString(node)); + break; + } + node = node.getParent(); + } + if(node != null && node instanceof TypeDeclaration){ + TypeDeclaration td = (TypeDeclaration) node; + if(td.getName().toString().equals(md.getName().toString())){ + System.err.println("Renaming constructor of " + getNodeAsString(td)); + wnode = new ASTNodeWrapper(td); + } + } + } + DefaultMutableTreeNode defCU = new DefaultMutableTreeNode(wnode); dfsNameOnly(defCU, wnode.getNode(), selText); System.out.println(wnode); @@ -1933,7 +1971,8 @@ public class ASTGenerator { // constrains.add(ASTNode.TYPE_DECLARATION); // constrains.add(ASTNode.METHOD_DECLARATION); // constrains.add(ASTNode.FIELD_DECLARATION); - } + } // TODO: in findDec, we also have a case where parent of type TD is handled. + // Figure out if needed here as well. System.out.println("Alternate parent: " + getNodeAsString(alternateParent)); while (alternateParent != null) { // System.out.println("findDeclaration2 -> "