From 8a00d5a774c7d1fcce0155be2e8759cdbbcfd220 Mon Sep 17 00:00:00 2001 From: Manindra Moharana Date: Wed, 19 Jun 2013 00:51:46 +0530 Subject: [PATCH] ironing out edge cases --- pdex/Todo, GSoC 2013.txt | 1 + .../mode/experimental/ASTGenerator.java | 11 ++++- .../mode/experimental/ASTNodeWrapper.java | 40 ++++++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/pdex/Todo, GSoC 2013.txt b/pdex/Todo, GSoC 2013.txt index c4c9b267e..e105e352d 100644 --- a/pdex/Todo, GSoC 2013.txt +++ b/pdex/Todo, GSoC 2013.txt @@ -33,6 +33,7 @@ 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. * This is precise upto a certain line. Once on a line, pde stuff have to be taken into consideration. +x Edge case - multiple staetments in a single line * PDE specific enhancements will also have to be tackled like int(), # literals. Refactoring would work only when code is compiler error free. I plan to do a find replace type op on the compile ready code. diff --git a/pdex/src/processing/mode/experimental/ASTGenerator.java b/pdex/src/processing/mode/experimental/ASTGenerator.java index 7c512ae14..becb60450 100644 --- a/pdex/src/processing/mode/experimental/ASTGenerator.java +++ b/pdex/src/processing/mode/experimental/ASTGenerator.java @@ -52,6 +52,7 @@ import org.eclipse.jdt.core.dom.QualifiedName; import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.SimpleType; import org.eclipse.jdt.core.dom.SingleVariableDeclaration; +import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jdt.core.dom.VariableDeclarationExpression; @@ -1760,7 +1761,15 @@ public class ASTGenerator { } public static boolean isAddableASTNode(ASTNode node) { - return true; + switch (node.getNodeType()) { +// case ASTNode.STRING_LITERAL: +// case ASTNode.NUMBER_LITERAL: +// case ASTNode.BOOLEAN_LITERAL: +// case ASTNode.NULL_LITERAL: +// return false; + default: + return true; + } } /** diff --git a/pdex/src/processing/mode/experimental/ASTNodeWrapper.java b/pdex/src/processing/mode/experimental/ASTNodeWrapper.java index 313f27400..620d2ca78 100644 --- a/pdex/src/processing/mode/experimental/ASTNodeWrapper.java +++ b/pdex/src/processing/mode/experimental/ASTNodeWrapper.java @@ -1,5 +1,8 @@ package processing.mode.experimental; +import java.util.Iterator; +import java.util.List; + import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.ExpressionStatement; @@ -9,6 +12,7 @@ import org.eclipse.jdt.core.dom.MethodInvocation; import org.eclipse.jdt.core.dom.QualifiedName; import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.SingleVariableDeclaration; +import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; import org.eclipse.jdt.core.dom.TypeDeclaration; public class ASTNodeWrapper { @@ -58,8 +62,40 @@ public class ASTNodeWrapper { break; } } - return new int[] { - lineNumber, thisNode.getStartPosition(), nodeOffset, nodeLength }; + /* + * There's an edge case here - multiple staetments in a single line. + * After identifying the statement with the line number, I'll have to + * look at previous tree nodes in the same level for same line number. + * The correct line start offset would be the line start offset of + * the first node with this line number. + * + * Using linear search for now. P.S: Eclipse AST iterators are messy. + * TODO: binary search might improve speed by 0.001%? + */ + + int altStartPos = thisNode.getStartPosition(); + thisNode = thisNode.getParent(); + + Iterator it = thisNode + .structuralPropertiesForType().iterator(); + + while (it.hasNext()) { + StructuralPropertyDescriptor prop = (StructuralPropertyDescriptor) it + .next(); + if (prop.isChildListProperty()) { + List nodelist = (List) thisNode + .getStructuralProperty(prop); + for (ASTNode cnode : nodelist) { + if (getLineNumber(cnode) == lineNumber) { + altStartPos = cnode.getStartPosition(); + // System.out.println("multi..."); + break; + } + } + } + } + // System.out.println("Altspos " + altStartPos); + return new int[] { lineNumber,altStartPos , nodeOffset, nodeLength }; } /**