From efdeb84daeeae9bed07fb302a43570d7c91066fc Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Sun, 17 Apr 2016 00:32:28 +0200 Subject: [PATCH] ECS + ASTGen: fix minor mapping inaccuracies --- .../mode/java/pdex/ASTGenerator.java | 31 ++++++++++++------- .../mode/java/pdex/ErrorCheckerService.java | 16 +++++++--- .../mode/java/pdex/JavaTextArea.java | 5 ++- .../mode/java/pdex/JavaTextAreaPainter.java | 5 ++- .../mode/java/pdex/PreprocessedSketch.java | 4 +-- .../mode/java/pdex/SourceMapping.java | 6 ++-- .../mode/java/pdex/SourceUtils.java | 5 ++- 7 files changed, 48 insertions(+), 24 deletions(-) diff --git a/java/src/processing/mode/java/pdex/ASTGenerator.java b/java/src/processing/mode/java/pdex/ASTGenerator.java index c580dd57e..1b59e063c 100644 --- a/java/src/processing/mode/java/pdex/ASTGenerator.java +++ b/java/src/processing/mode/java/pdex/ASTGenerator.java @@ -1024,12 +1024,10 @@ public class ASTGenerator { * @param scrollOnly * @return */ - public ASTNode getASTNodeAt(int offset) { + public ASTNode getASTNodeAt(int javaOffset) { Messages.log("* getASTNodeAt"); PreprocessedSketch ps = errorCheckerService.latestResult; - int tabIndex = ps.sketch.getCodeIndex(editor.getCurrentTab()); - int javaOffset = ps.tabOffsetToJavaOffset(tabIndex, offset); ASTNode node = NodeFinder.perform(ps.compilationUnit, javaOffset, 0); if (node == null) { @@ -1235,10 +1233,11 @@ public class ASTGenerator { return lastClickedWord; } - public void setLastClickedWord(int offset, String lastClickedWord) { + public void setLastClickedWord(int tabIndex, int offset, String lastClickedWord) { Messages.log("* setLastClickedWord"); this.lastClickedWord = lastClickedWord; - lastClickedWordNode = getASTNodeAt(offset); + int javaOffset = errorCheckerService.latestResult.tabOffsetToJavaOffset(tabIndex, offset); + lastClickedWordNode = getASTNodeAt(javaOffset); // TODO: don't call this on EDT log("Last clicked node: " + lastClickedWordNode); } @@ -1256,9 +1255,13 @@ public class ASTGenerator { + (ta.getSelectionStart() - ta.getLineStartOffset(line)) + ", " + (ta.getSelectionStop() - ta.getLineStartOffset(line))); + int tabIndex = editor.getSketch().getCurrentCodeIndex(); + int offset = ta.getSelectionStart(); + int javaOffset = + errorCheckerService.latestResult.tabOffsetToJavaOffset(tabIndex, offset); ASTNode wnode; if (lastClickedWord == null || lastClickedWordNode == null) { - wnode = getASTNodeAt(ta.getSelectionStart()); + wnode = getASTNodeAt(javaOffset); } else{ wnode = lastClickedWordNode; @@ -2150,7 +2153,7 @@ public class ASTGenerator { value = ((MethodInvocation) node).getName().toString() + " | " + className; else if (node instanceof FieldDeclaration) - value = node.toString() + " FldDecl| "; + value = node.toString() + " FldDecl | "; else if (node instanceof SingleVariableDeclaration) value = ((SingleVariableDeclaration) node).getName() + " - " + ((SingleVariableDeclaration) node).getType() + " | SVD "; @@ -2165,7 +2168,7 @@ public class ASTGenerator { else if (className.startsWith("Variable")) value = node.toString() + " | " + className; else if (className.endsWith("Type")) - value = node.toString() + " |" + className; + value = node.toString() + " | " + className; value += " [" + node.getStartPosition() + "," + (node.getStartPosition() + node.getLength()) + "]"; value += " Line: " @@ -2698,16 +2701,22 @@ public class ASTGenerator { /// Editor stuff ------------------------------------------------------------- - public void scrollToDeclaration(int offset, String name) { + // Thread: EDT + public void scrollToDeclaration(int tabIndex, int offset, String name) { Messages.log("* scrollToDeclaration"); - ASTNode node = getASTNodeAt(offset); + // TODO: don't run the heavy lifting on EDT + + PreprocessedSketch ps = errorCheckerService.latestResult; + + int javaOffset = ps.tabOffsetToJavaOffset(tabIndex, offset); + + ASTNode node = getASTNodeAt(javaOffset); if (node == null) { return; } - PreprocessedSketch ps = errorCheckerService.latestResult; IBinding binding = resolveBinding(node); if (binding == null) { diff --git a/java/src/processing/mode/java/pdex/ErrorCheckerService.java b/java/src/processing/mode/java/pdex/ErrorCheckerService.java index f8deed939..7aa48bb33 100644 --- a/java/src/processing/mode/java/pdex/ErrorCheckerService.java +++ b/java/src/processing/mode/java/pdex/ErrorCheckerService.java @@ -1010,10 +1010,18 @@ public class ErrorCheckerService { int startPdeOffset = ps.javaOffsetToPdeOffset(startJavaOffset); - int stopPdeOffset = javaLength == 0 ? - startPdeOffset : - // Make the stop inclusive for the purpose of mapping - ps.javaOffsetToPdeOffset(stopJavaOffset - 1) + 1; + int stopPdeOffset; + + if (javaLength == 0) { + stopPdeOffset = startPdeOffset; + } else { + // Subtract one for inclusive end + stopPdeOffset = ps.javaOffsetToPdeOffset(stopJavaOffset - 1); + if (javaLength == 1 || stopPdeOffset > startPdeOffset) { + // Add one back for exclusive end + stopPdeOffset += 1; + } + } int tabIndex = ps.pdeOffsetToTabIndex(startPdeOffset); diff --git a/java/src/processing/mode/java/pdex/JavaTextArea.java b/java/src/processing/mode/java/pdex/JavaTextArea.java index a46e76c8a..74b39f85d 100644 --- a/java/src/processing/mode/java/pdex/JavaTextArea.java +++ b/java/src/processing/mode/java/pdex/JavaTextArea.java @@ -318,8 +318,11 @@ public class JavaTextArea extends JEditTextArea { } Messages.log("Mouse click, word: " + word.trim()); ASTGenerator astGenerator = editor.getErrorChecker().getASTGenerator(); + + int tabIndex = editor.getSketch().getCurrentCodeIndex(); + synchronized (astGenerator) { - astGenerator.setLastClickedWord(off, word); + astGenerator.setLastClickedWord(tabIndex, off, word); } return word.trim(); } diff --git a/java/src/processing/mode/java/pdex/JavaTextAreaPainter.java b/java/src/processing/mode/java/pdex/JavaTextAreaPainter.java index 1a81aa600..2654439fe 100644 --- a/java/src/processing/mode/java/pdex/JavaTextAreaPainter.java +++ b/java/src/processing/mode/java/pdex/JavaTextAreaPainter.java @@ -176,9 +176,12 @@ public class JavaTextAreaPainter extends TextAreaPainter return; Messages.log(line + "|" + line + "| offset " + xLS + word + " <= \n"); + + int tabIndex = getEditor().getSketch().getCurrentCodeIndex(); + ASTGenerator astGenerator = getJavaEditor().getErrorChecker().getASTGenerator(); synchronized (astGenerator) { - astGenerator.scrollToDeclaration(off, word); + astGenerator.scrollToDeclaration(tabIndex, off, word); } } } diff --git a/java/src/processing/mode/java/pdex/PreprocessedSketch.java b/java/src/processing/mode/java/pdex/PreprocessedSketch.java index 22050832c..d9ccb3ddf 100644 --- a/java/src/processing/mode/java/pdex/PreprocessedSketch.java +++ b/java/src/processing/mode/java/pdex/PreprocessedSketch.java @@ -44,9 +44,9 @@ public class PreprocessedSketch { public static int lineToOffset(String text, int line) { int lineOffset = 0; for (int i = 0; i < line && lineOffset >= 0; i++) { - lineOffset = text.indexOf('\n', lineOffset+1); + lineOffset = text.indexOf('\n', lineOffset) + 1; } - return lineOffset + 1; + return lineOffset; } diff --git a/java/src/processing/mode/java/pdex/SourceMapping.java b/java/src/processing/mode/java/pdex/SourceMapping.java index dc61a48bf..efac198f9 100644 --- a/java/src/processing/mode/java/pdex/SourceMapping.java +++ b/java/src/processing/mode/java/pdex/SourceMapping.java @@ -130,8 +130,9 @@ public class SourceMapping { int i = Collections.binarySearch(outMap, searchKey, OUTPUT_OFFSET_COMP); if (i < 0) { i = -(i + 1); + i -= 1; } - i = PApplet.constrain(i-1, 0, outMap.size()-1); + i = PApplet.constrain(i, 0, outMap.size()-1); Edit edit = outMap.get(i); int diff = outputOffset - edit.toOffset; return edit.fromOffset + Math.min(diff, Math.max(0, edit.fromLength - 1)); @@ -144,8 +145,9 @@ public class SourceMapping { int i = Collections.binarySearch(inMap, searchKey, INPUT_OFFSET_COMP); if (i < 0) { i = -(i + 1); + i -= 1; } - i = PApplet.constrain(i-1, 0, inMap.size()-1); + i = PApplet.constrain(i, 0, inMap.size()-1); Edit edit = inMap.get(i); int diff = inputOffset - edit.fromOffset; return edit.toOffset + Math.min(diff, Math.max(0, edit.toLength - 1)); diff --git a/java/src/processing/mode/java/pdex/SourceUtils.java b/java/src/processing/mode/java/pdex/SourceUtils.java index d49d99903..bc9cd7e97 100644 --- a/java/src/processing/mode/java/pdex/SourceUtils.java +++ b/java/src/processing/mode/java/pdex/SourceUtils.java @@ -62,9 +62,8 @@ public class SourceUtils { int offset = matcher.start(1); int length = match.length(); String replace = "PApplet.parse" - + Character.toUpperCase(match.charAt(0)) - + match.substring(1); - result.add(Edit.replace(offset, length, replace)); + + Character.toUpperCase(match.charAt(0)); + result.add(Edit.replace(offset, length - (match.length() - 1), replace)); } return result;