From f92fff3e50f483475ccd13cec3d2908f825e5168 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Sun, 18 Dec 2011 12:09:43 -0800 Subject: [PATCH] Fixed MalformedTreeException. --- .../changegenerator/ChangeGenerator.java | 19 ++++---- .../resources/refactoring/ExtractMethod.rts | 46 +++++++++++++++++-- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java index cb9ced1447a..2f98b27c77d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java @@ -460,7 +460,7 @@ public class ChangeGenerator extends ASTVisitor { IFile file = FileHelper.getFileFromNode(anchorNode); MultiTextEdit parentEdit = getEdit(anchorNode, file); parentEdit.addChild(edit); - sourceOffsets.put(file.getName(), Integer.valueOf(edit.getOffset())); + sourceOffsets.put(file.getName(), edit.getOffset()); } private void handleReplace(IASTNode node) { @@ -469,10 +469,13 @@ public class ChangeGenerator extends ASTVisitor { TextEdit edit; ChangeGeneratorWriterVisitor writer = new ChangeGeneratorWriterVisitor(modificationStore, commentMap); + IASTFileLocation fileLocation = node.getFileLocation(); + Integer val = sourceOffsets.get(fileLocation.getFileName()); + int processedOffset = val != null ? val.intValue() : 0; if (modifications.size() == 1 && modifications.get(0).getNewNode() == null) { int offset = getOffsetIncludingComments(node); int endOffset = getEndOffsetIncludingComments(node); - offset = skipPrecedingBlankLines(source, offset); + offset = Math.max(skipPrecedingBlankLines(source, offset), processedOffset); endOffset = skipTrailingBlankLines(source, endOffset); IASTNode[] siblingsList = getContainingNodeList(node); if (siblingsList != null) { @@ -500,9 +503,8 @@ public class ChangeGenerator extends ASTVisitor { } else { node.accept(writer); String code = writer.toString(); - IASTFileLocation location = node.getFileLocation(); - int offset = location.getNodeOffset(); - int endOffset = offset + location.getNodeLength(); + int offset = fileLocation.getNodeOffset(); + int endOffset = offset + fileLocation.getNodeLength(); if (node instanceof IASTStatement || node instanceof IASTDeclaration) { // Include trailing comments in the area to be replaced. endOffset = Math.max(endOffset, getEndOffsetIncludingTrailingComments(node)); @@ -517,9 +519,7 @@ public class ChangeGenerator extends ASTVisitor { MultiTextEdit parentEdit = getEdit(node, file); parentEdit.addChild(edit); - IASTFileLocation fileLocation = node.getFileLocation(); - int newOffset = fileLocation.getNodeOffset() + fileLocation.getNodeLength(); - sourceOffsets.put(fileLocation.getFileName(), Integer.valueOf(newOffset)); + sourceOffsets.put(fileLocation.getFileName(), edit.getExclusiveEnd()); } private void handleAppends(IASTNode node) { @@ -553,8 +553,7 @@ public class ChangeGenerator extends ASTVisitor { code + anchor.getText()); parentEdit.addChild(edit); IASTFileLocation fileLocation = node.getFileLocation(); - int newOffset = fileLocation.getNodeOffset() + fileLocation.getNodeLength(); - sourceOffsets.put(fileLocation.getFileName(), Integer.valueOf(newOffset)); + sourceOffsets.put(fileLocation.getFileName(), endOffset(fileLocation)); } private void handleAppends(IASTTranslationUnit tu) { diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts index 06ad3de23a0..513d9d6d58d 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts @@ -2512,24 +2512,60 @@ visibility=public //#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest //@main.c int main() { - int a,b; - /*$*/a = b*2;/*$$*/ + int a, b; + /*$*/a = b * 2;/*$$*/ return a; } - //= void test(int* a, int b) { a = b * 2; } int main() { - int a,b; + int a, b; test(a, b); return a; } - //@.config filename=main.c methodname=test replaceduplicates=false returnvalue=false + +//!Handling of blank lines +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@main.c +int main() { + int f; + /*$*/int a = 0; + int b = 1; + + for (int i = 0; i < 10; ++i) { + int c = a + b; + a = b; + b = c; + }/*$$*/ + + f = b; +} +//= +int fib() { + int a = 0; + int b = 1; + for (int i = 0; i < 10; ++i) { + int c = a + b; + a = b; + b = c; + } + return b; +} + +int main() { + int f; + int b = fib(); + f = b; +} +//@.config +filename=main.c +methodname=fib +replaceduplicates=false