From e74b7d575cf994a7f183c0715d1cf9f5cf06fca5 Mon Sep 17 00:00:00 2001 From: Manindra Moharana Date: Sun, 23 Jun 2013 22:34:06 +0530 Subject: [PATCH] Further work on ASTGen --- pdex/Todo, GSoC 2013.txt | 2 + .../mode/experimental/ASTGenerator.java | 95 +++++++++++++++---- .../mode/experimental/ASTNodeWrapper.java | 25 ++++- 3 files changed, 97 insertions(+), 25 deletions(-) diff --git a/pdex/Todo, GSoC 2013.txt b/pdex/Todo, GSoC 2013.txt index 1a0187c76..0abbb121f 100644 --- a/pdex/Todo, GSoC 2013.txt +++ b/pdex/Todo, GSoC 2013.txt @@ -51,4 +51,6 @@ x Local Vars x Local Methods x Local Classes x Recursive lookup, a.b().c() +x Now highlihgting the declaration name, rather than the whole declaration. +*+ A silly bug where the name of the first field declaration isn't highlighted correctly. diff --git a/pdex/src/processing/mode/experimental/ASTGenerator.java b/pdex/src/processing/mode/experimental/ASTGenerator.java index e19ab9f58..b8cc56a2e 100644 --- a/pdex/src/processing/mode/experimental/ASTGenerator.java +++ b/pdex/src/processing/mode/experimental/ASTGenerator.java @@ -1010,16 +1010,20 @@ public class ASTGenerator { System.out.println("+> " + lineNode); ASTNode decl = null; + String nameOfNode = null; // The node name which is to be scrolled to if (lineNode != null) { ASTNodeWrapper lineNodeWrap = new ASTNodeWrapper(lineNode); + int altOff = offset; int ret[][] = lineNodeWrap.getOffsetMapping(errorCheckerService); - int javaCodeMap[] = ret[0], pdeCodeMap[] = ret[1]; - int altOff = 0; - for (; altOff < javaCodeMap.length; altOff++) { - if(javaCodeMap[altOff] == pdeCodeMap[offset]){ - //altOff = javaCodeMap[altOff]; - break; - } + if(ret != null){ + altOff = 0; + int javaCodeMap[] = ret[0], pdeCodeMap[] = ret[1]; + + for (; altOff < javaCodeMap.length; altOff++) { + if (javaCodeMap[altOff] == pdeCodeMap[offset]) { + break; + } + } } System.out.println("FLON2: " + lineNumber + " LN spos " + lineNode.getStartPosition() + " off " + offset + " alt off" + altOff); @@ -1032,7 +1036,7 @@ public class ASTGenerator { Iterator it = parLineNode .structuralPropertiesForType().iterator(); boolean flag = true; - int lineStartPos = lineNode.getStartPosition(), offAdjust = 0; + int offAdjust = 0; while (it.hasNext() && flag) { StructuralPropertyDescriptor prop = (StructuralPropertyDescriptor) it .next(); @@ -1062,6 +1066,7 @@ public class ASTGenerator { lineNode.getStartPosition(), name); System.out.println("+++> " + simpName); if (simpName instanceof SimpleName) { + nameOfNode = simpName.toString(); System.out.println(getNodeAsString(simpName)); decl = findDeclaration((SimpleName) simpName); if (decl != null) { @@ -1077,20 +1082,64 @@ public class ASTGenerator { // // retStr = ""; if (decl != null && scrollOnly) { - System.err.println("FINAL String label: " + getNodeAsString(decl)); - - DefaultProblem dpr = new DefaultProblem(null, null, -1, null, -1, - decl.getStartPosition(), - decl.getStartPosition(), - getLineNumber(decl) + 1, 0); - int[] position = errorCheckerService.calculateTabIndexAndLineNumber(dpr); - System.out.println("Tab " + position[0] + ", Line: " + (position[1])); - Problem p = new Problem(dpr, position[0], position[1]); - errorCheckerService.scrollToErrorLine(p); + ASTNode simpName2 = getNodeName(decl,nameOfNode); + System.err.println("FINAL String decl: " + getNodeAsString(decl)); + System.err.println("FINAL String label: " + getNodeAsString(simpName2)); + ASTNodeWrapper awrap = new ASTNodeWrapper(simpName2); + int pdeoffsets[] = awrap .getPDECodeOffsets(errorCheckerService); + int javaoffsets[] = awrap.getJavaCodeOffsets(errorCheckerService); + ErrorCheckerService.scrollToErrorLine(editor, pdeoffsets[0], + pdeoffsets[1],javaoffsets[1], + javaoffsets[2]); + // Now highlight the SimpleName + + + +// DefaultProblem dpr = new DefaultProblem(null, null, -1, null, -1, +// decl.getStartPosition(), +// decl.getStartPosition(), +// getLineNumber(decl) + 1, 0); +// int[] position = errorCheckerService.calculateTabIndexAndLineNumber(dpr); +// System.out.println("Tab " + position[0] + ", Line: " + (position[1])); +// Problem p = new Problem(dpr, position[0], position[1]); +// errorCheckerService.scrollToErrorLine(p); } // uncomment this one, it works return null; } + + private ASTNode getNodeName(ASTNode node, String name){ + List vdfs = null; + switch (node.getNodeType()) { + case ASTNode.TYPE_DECLARATION: + return ((TypeDeclaration) node).getName(); + case ASTNode.METHOD_DECLARATION: + return ((MethodDeclaration) node).getName(); + case ASTNode.SINGLE_VARIABLE_DECLARATION: + return ((SingleVariableDeclaration) node).getName(); + case ASTNode.FIELD_DECLARATION: + vdfs = ((FieldDeclaration) node).fragments(); + break; + case ASTNode.VARIABLE_DECLARATION_STATEMENT: + vdfs = ((VariableDeclarationStatement) node).fragments(); + break; + case ASTNode.VARIABLE_DECLARATION_EXPRESSION: + vdfs = ((VariableDeclarationExpression) node).fragments(); + break; + default: + break; + } + + if (vdfs != null) { + for (VariableDeclarationFragment vdf : vdfs) { + if (vdf.getName().toString().equals(name)) { + return vdf.getName(); + } + } + + } + return null; + } private static int getLineNumber(ASTNode node) { return ((CompilationUnit) node.getRoot()).getLineNumber(node @@ -1330,15 +1379,19 @@ public class ASTGenerator { if (node instanceof SimpleName) { SimpleName sn = (SimpleName) node; + System.out.println(offset+ "off,pol " + getNodeAsString(sn)); if ((lineStartOffset + offset) >= sn.getStartPosition() && (lineStartOffset + offset) <= sn.getStartPosition() + sn.getLength()) { - if (sn.toString().equals(name)) + if (sn.toString().equals(name)) { return sn; - else + } + else { return null; - } else + } + } else { return null; + } } for (Object oprop : node.structuralPropertiesForType()) { StructuralPropertyDescriptor prop = (StructuralPropertyDescriptor) oprop; diff --git a/pdex/src/processing/mode/experimental/ASTNodeWrapper.java b/pdex/src/processing/mode/experimental/ASTNodeWrapper.java index 66affac5c..7f9779421 100644 --- a/pdex/src/processing/mode/experimental/ASTNodeWrapper.java +++ b/pdex/src/processing/mode/experimental/ASTNodeWrapper.java @@ -110,18 +110,32 @@ public class ASTNodeWrapper { } } } - // System.out.println("Altspos " + altStartPos); + System.out.println("Altspos " + altStartPos); int pdeoffsets[] = getPDECodeOffsets(ecs); String pdeCode = ecs.getPDECode(pdeoffsets[1] - 1).trim(); int vals[] = createOffsetMapping(pdeCode,nodeOffset - altStartPos,nodeLength); - return new int[] { - lineNumber, nodeOffset + vals[0] - altStartPos, vals[1]}; + if(vals != null) + return new int[] { + lineNumber, nodeOffset + vals[0] - altStartPos, vals[1] }; + else + return new int[] { lineNumber, nodeOffset - altStartPos, nodeLength }; } + /** + * + * @param source + * @param inpOffset + * @param nodeLen + * @return int[0] - difference in start offset, int[1] - node length + */ private int[] createOffsetMapping(String source, int inpOffset, int nodeLen) { int ret[][] = getOffsetMapping(source); + if(ret == null){ + // no offset mapping needed + return null; + } int javaCodeMap[] = ret[0]; int pdeCodeMap[] = ret[1]; int pi = 1, pj = 1; @@ -234,7 +248,10 @@ public class ASTNodeWrapper { + "("); } - + if(offsetmap.isEmpty()){ + System.out.println("No offset matching needed."); + return null; + } // replace with 0xff[webcolor] and others webMatcher = webPattern.matcher(sourceAlt); while (webMatcher.find()) {