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 cbabba76867..d68aa090f12 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts @@ -2564,3 +2564,31 @@ int main() { return 0; } +//!Bug#262000 refactoring "extract function" misinterprets artificial blocks +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +filename=main.cpp +methodname=exp +//@main.cpp +int main(int argc, char **argv) { + + /*$*/int a = 0; + { + a++; + }/*$$*/ + +} + +//= +void exp() +{ + int a = 0; + { + a++; + } +} + +int main(int argc, char **argv) { + exp(); +} + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java index a11840e197d..f317fa34a0a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java @@ -714,8 +714,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { @Override public int visit(IASTStatement stmt) { - if (!(stmt instanceof IASTCompoundStatement) - && SelectionHelper.isSelectedFile(region, stmt, file)) { + if ( SelectionHelper.isSelectedFile(region, stmt, file)) { container.add(stmt); return PROCESS_SKIP; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/SelectionHelper.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/SelectionHelper.java index dbf85ac0e5b..b829d90236e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/SelectionHelper.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/SelectionHelper.java @@ -105,15 +105,25 @@ public class SelectionHelper { protected static Region createExpressionPosition(IASTNode expression) { - int start = 0; + int start = Integer.MAX_VALUE; int nodeLength = 0; IASTNodeLocation[] nodeLocations = expression.getNodeLocations(); if (nodeLocations.length != 1) { for (IASTNodeLocation location : nodeLocations) { if (location instanceof IASTMacroExpansionLocation) { IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location; - start = macroLoc.asFileLocation().getNodeOffset(); - nodeLength = macroLoc.asFileLocation().getNodeLength(); + int nodeOffset = macroLoc.asFileLocation().getNodeOffset(); + if(nodeOffset < start) { + start = nodeOffset; + } + nodeLength += macroLoc.asFileLocation().getNodeLength(); + }else { + IASTFileLocation loc = expression.getFileLocation(); + int nodeOffset = loc.getNodeOffset(); + if(nodeOffset < start) { + start = nodeOffset; + } + nodeLength = loc.getNodeLength(); } } } else {