diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractExpression.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractExpression.rts index 879d75f3afd..30cc977b6a9 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractExpression.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractExpression.rts @@ -428,3 +428,261 @@ int main() { contains(has(c)); } +//!Extract string constant +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=greeting + +//@test.h +class Test +{ + void test(); +}; + + +//= +class Test +{ + void test(); + wchar_t greeting(); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + char* hi = /*$*/"hello"/*$$*/; +} + +//= +#include "test.h" + +wchar_t Test::greeting() +{ + return "hello"; +} + +void Test::test() +{ + char* hi = greeting(); +} + +//!Extract int constant +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=size + +//@test.h +class Test +{ + void test(); +}; + + +//= +class Test +{ + void test(); + int size(); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + int i = /*$*/42/*$$*/; +} + +//= +#include "test.h" + +int Test::size() +{ + return 42; +} + +void Test::test() +{ + int i = size(); +} + +//!Extract float constant +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=certainty + +//@test.h +class Test +{ + void test(); +}; + + +//= +class Test +{ + void test(); + float certainty(); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + float f = /*$*/0.42/*$$*/; +} + +//= +#include "test.h" + +float Test::certainty() +{ + return 0.42; +} + +void Test::test() +{ + float f = certainty(); +} + +//!Extract char constant +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=newline + +//@test.h +class Test +{ + void test(); +}; + + +//= +class Test +{ + void test(); + char newline(); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + char nl = /*$*/'\n'/*$$*/; +} + +//= +#include "test.h" + +char Test::newline() +{ + return '\n'; +} + +void Test::test() +{ + char nl = newline(); +} + +//!Extract boolean true constant +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=valid + +//@test.h +class Test +{ + void test(); +}; + + +//= +class Test +{ + void test(); + bool valid(); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + bool b = /*$*/true/*$$*/; +} + +//= +#include "test.h" + +bool Test::valid() +{ + return true; +} + +void Test::test() +{ + bool b = valid(); +} + +//!Extract boolean false constant +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=invalid + +//@test.h +class Test +{ + void test(); +}; + + +//= +class Test +{ + void test(); + bool invalid(); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + bool b = /*$*/false/*$$*/; +} + +//= +#include "test.h" + +bool Test::invalid() +{ + return false; +} + +void Test::test() +{ + bool b = invalid(); +} + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java index 64aa180231b..36f96b2f1d1 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java @@ -29,6 +29,7 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier; @@ -90,6 +91,10 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper { declSpecifier = handleFunctionCallExpression((IASTFunctionCallExpression) extractedNode); } + if (extractedNode instanceof IASTLiteralExpression) { + declSpecifier = handleLiteralExpression((IASTLiteralExpression) extractedNode); + } + if(declSpecifier == null) { return createSimpleDeclSpecifier(IASTSimpleDeclSpecifier.t_void); } @@ -97,7 +102,26 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper { return declSpecifier; } - private IASTDeclSpecifier handleNewExpression(ICPPASTNewExpression expression) { + private IASTDeclSpecifier handleLiteralExpression(IASTLiteralExpression extractedNode) { + switch(extractedNode.getKind()){ + case IASTLiteralExpression.lk_char_constant: + return createSimpleDeclSpecifier(IASTSimpleDeclSpecifier.t_char); + case IASTLiteralExpression.lk_float_constant: + return createSimpleDeclSpecifier(IASTSimpleDeclSpecifier.t_float); + case IASTLiteralExpression.lk_integer_constant: + return createSimpleDeclSpecifier(IASTSimpleDeclSpecifier.t_int); + case IASTLiteralExpression.lk_string_literal: + return createSimpleDeclSpecifier(ICPPASTSimpleDeclSpecifier.t_wchar_t); + case ICPPASTLiteralExpression.lk_false: + //Like lk_true a boolean type + case ICPPASTLiteralExpression.lk_true: + return createSimpleDeclSpecifier(ICPPASTSimpleDeclSpecifier.t_bool); + default: + return null; + } + } + + private IASTDeclSpecifier handleNewExpression(ICPPASTNewExpression expression) { return expression.getTypeId().getDeclSpecifier(); }