Update of /cvsroot/cpptool/rfta/src/rfta In directory sc8-pr-cvs1:/tmp/cvs-serv17642 Modified Files: CodeModel.cpp CodeModelStatements.cpp CodeRewriter.cpp CodeRewriter.h CodeWriterTest.cpp CodeWriterTest.h UnitTesting.h rfta.dsp Log Message: * started adding possibility to move code element. Index: CodeModel.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/CodeModel.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CodeModel.cpp 6 Mar 2003 22:17:56 -0000 1.4 --- CodeModel.cpp 8 Mar 2003 13:11:42 -0000 1.5 *************** *** 71,75 **** CompoundStatementPtr compound( new CompoundStatement() ); // compound->setSourceRange( compoundNode->getRange() ); - compound->setOriginalText( ElementText( compoundNode ) ); for ( int index = 0; index < compoundNode->getChildCount(); ++index ) --- 71,74 ---- *************** *** 81,84 **** --- 80,84 ---- } + compound->setOriginalText( ElementText( compoundNode ) ); return compound; } Index: CodeModelStatements.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/CodeModelStatements.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** CodeModelStatements.cpp 6 Mar 2003 22:17:57 -0000 1.3 --- CodeModelStatements.cpp 8 Mar 2003 13:11:42 -0000 1.4 *************** *** 5,8 **** --- 5,9 ---- #include "stdafx.h" + #include <rfta/refactoring/CodeModelExpressions.h> #include <rfta/refactoring/CodeModelStatements.h> #include <rfta/refactoring/CodeModelVisitor.h> *************** *** 478,481 **** --- 479,493 ---- ReturnStatement::setValue( const ExpressionPtr &value ) { + if ( hasValue() && change_.type_ == Change::unmodified ) + { + change_.type_ = Change::replaced; + change_.oldRange_ = value_->getText().getOriginalSourceRange(); + } + else + change_.type_ = Change::added; + + if ( !value ) + change_.type_ = Change::removed; + value_ = value; } *************** *** 487,490 **** --- 499,516 ---- FlowControlStatement::accept( visitor ); visitor.visit( *this ); + } + + + void + ReturnStatement::originalTextSet() + { + change_.type_ = Change::unmodified; + } + + + Change + ReturnStatement::getValueChange() const + { + return change_; } Index: CodeRewriter.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/CodeRewriter.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** CodeRewriter.cpp 7 Mar 2003 20:12:39 -0000 1.6 --- CodeRewriter.cpp 8 Mar 2003 13:11:42 -0000 1.7 *************** *** 21,24 **** --- 21,25 ---- , currentInsertionPos_( 0 ) , indentManager_( originalSource ) + , mode_( updating ) { } *************** *** 69,72 **** --- 70,87 ---- + bool + CodeRewriter::isInserting() const + { + return mode_ == inserting; + } + + + bool + CodeRewriter::isUpdating() const + { + return mode_ == updating; + } + + void CodeRewriter::setPreviousStatementEnd( CompoundStatement &statement, *************** *** 95,98 **** --- 110,114 ---- { IndentLevelManager::IndentRestorer restorer( indentManager_ ); + ModeModifier mode( mode_, updating ); if ( statement.getText().hasOriginalSourceRange() ) *************** *** 118,121 **** --- 134,138 ---- deleteText( change.statement_->getText().getOriginalSourceRange() ); + ModeModifier mode( mode_, change.type_ == Change::unmodified ? updating : inserting ); change.statement_->accept( *this ); } *************** *** 209,218 **** { const ElementText &text = statement.getText(); ! if ( text.isOriginalText() ) ! return; ! if ( text.isModifiedText() ) ! replaceText( text.getOriginalSourceRange(), text.getOriginalText() ); ! else if ( text.isNewText() ) { insertText( "\n" + indentManager_.getIndentSpacer() + "return" ); --- 226,246 ---- { const ElementText &text = statement.getText(); ! if ( isUpdating() ) ! { ! Change change = statement.getValueChange(); ! if ( change.type_ == Change::replaced || change.type_ == Change::removed ) ! deleteText( change.oldRange_ ); ! if ( change.type_ == Change::removed || !statement.hasValue() ) ! return; ! ! ModeModifier mode( mode_, change.type_ == Change::unmodified ? updating : inserting ); ! ! if ( mode_ == inserting ) ! currentInsertionPos_ = statement.getText().getOriginalSourceRange().getEndIndex() -1; ! ! statement.getValue()->accept( *this ); ! } ! else if ( isInserting() ) { insertText( "\n" + indentManager_.getIndentSpacer() + "return" ); *************** *** 278,281 **** --- 306,319 ---- CodeRewriter::visit( GenericExpression &expression ) { + if ( isInserting() ) + insertText( expression.getText().getOriginalText() ); + else if ( expression.getText().isModifiedText() ) + { + replaceText( expression.getText().getOriginalSourceRange(), + expression.getText().getOriginalText() ); + } + + + #if 0 if ( expression.getText().isModifiedText() ) { *************** *** 287,290 **** --- 325,329 ---- insertText( expression.getText().getOriginalText() ); } + #endif } Index: CodeRewriter.h =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/CodeRewriter.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** CodeRewriter.h 7 Mar 2003 09:41:11 -0000 1.5 --- CodeRewriter.h 8 Mar 2003 13:11:42 -0000 1.6 *************** *** 70,73 **** --- 70,100 ---- private: + enum Mode + { + updating = 0, + inserting + }; + + class ModeModifier + { + public: + ModeModifier( Mode &mode, + Mode newMode ) + : oldMode_( mode ) + , mode_( mode ) + { + mode_ = newMode; + } + + ~ModeModifier() + { + mode_ = oldMode_; + } + + private: + Mode &mode_; + Mode oldMode_; + }; + SourceRange getCurrentInsertionRange() const; *************** *** 79,82 **** --- 106,113 ---- void insertText( const std::string &content ); + bool isInserting() const; + + bool isUpdating() const; + private: IndentLevelManager indentManager_; *************** *** 86,89 **** --- 117,121 ---- int currentInsertionPos_; int indentWidth_; + Mode mode_; }; Index: CodeWriterTest.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/CodeWriterTest.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** CodeWriterTest.cpp 7 Mar 2003 20:12:40 -0000 1.7 --- CodeWriterTest.cpp 8 Mar 2003 13:11:42 -0000 1.8 *************** *** 66,69 **** --- 66,76 ---- + CodeModel::ExpressionPtr + CodeWriterTest::makeTrueExpression() + { + return CodeModel::ExpressionPtr( new CodeModel::GenericExpression( "true" ) ); + } + + void CodeWriterTest::testRewriteStatement() *************** *** 79,85 **** CodeModel::IfStatement &ifStatement = ! static_cast<CodeModel::IfStatement &>( *compound_->getStatementAt(0) ); CodeModel::GenericExpression &condition = ! static_cast<CodeModel::GenericExpression &>( *ifStatement.getCondition() ); condition.setText( "false" ); --- 86,92 ---- CodeModel::IfStatement &ifStatement = ! dynamic_cast<CodeModel::IfStatement &>( *compound_->getStatementAt(0) ); CodeModel::GenericExpression &condition = ! dynamic_cast<CodeModel::GenericExpression &>( *ifStatement.getCondition() ); condition.setText( "false" ); *************** *** 158,165 **** ! CodeModel::ExpressionPtr ! CodeWriterTest::makeTrueExpression() { ! return CodeModel::ExpressionPtr( new CodeModel::GenericExpression( "true" ) ); } --- 165,204 ---- ! void ! CodeWriterTest::testMoveSubExpression() { ! source_ = "{\n" ! " int x = 3;\n" ! " return x;\n" ! "}"; ! ! generateCompound(); ! ! RFTA_ASSERT_EQUAL( 2, compound_->getStatementCount() ); ! ! ! CodeModel::DeclarationStatement &declStatement = ! dynamic_cast<CodeModel::DeclarationStatement &>( *compound_->getStatementAt(0) ); ! CodeModel::DeclaratorExpression &declExpression = *declStatement.getDeclaration(); ! RFTA_ASSERT_EQUAL( 1, declExpression.getDeclaratorCount() ); ! const CodeModel::Declarator &xDecl = declExpression.getDeclaratorAt(0); ! CPPUNIT_ASSERT( xDecl.hasInitializer() ); ! CodeModel::AssignInitializerExpression &xValueInitializer = ! dynamic_cast<CodeModel::AssignInitializerExpression &>( *xDecl.getInitializer() ); ! CodeModel::ExpressionPtr xValue = xValueInitializer.getValue(); ! ! compound_->removeStatementAt( 0 ); ! ! CodeModel::ReturnStatement &returnStatement = ! dynamic_cast<CodeModel::ReturnStatement &>( *compound_->getStatementAt(0) ); ! returnStatement.setValue( xValue ); ! ! rewriteSource(); ! ! std::string expectedSource = "{\n" ! " \n" ! " return 3;\n" // todo: there should be only one space between return and 3. ! "}"; ! RFTA_ASSERT_EQUAL( expectedSource, document_->getAllText() ); } Index: CodeWriterTest.h =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/CodeWriterTest.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** CodeWriterTest.h 7 Mar 2003 20:12:40 -0000 1.5 --- CodeWriterTest.h 8 Mar 2003 13:11:42 -0000 1.6 *************** *** 25,28 **** --- 25,29 ---- CPPUNIT_TEST( testInsertReturnValueStatement ); CPPUNIT_TEST( testRemoveStatement ); + CPPUNIT_TEST( testMoveSubExpression ); CPPUNIT_TEST_SUITE_END(); *************** *** 42,45 **** --- 43,47 ---- void testInsertReturnValueStatement(); void testRemoveStatement(); + void testMoveSubExpression(); private: Index: UnitTesting.h =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/UnitTesting.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** UnitTesting.h 10 Nov 2002 19:10:29 -0000 1.3 --- UnitTesting.h 8 Mar 2003 13:11:42 -0000 1.4 *************** *** 4,7 **** --- 4,8 ---- #include <cppunit/extensions/HelperMacros.h> #include <sstream> + #include <typeinfo> namespace Refactoring *************** *** 36,39 **** --- 37,59 ---- fixedMessage ); } + + + inline void checkType( const std::type_info &expectedType, + const std::type_info &actualType, + const std::string &expression, + const CppUnit::SourceLine &sourceLine ) + { + if ( expectedType == actualType ) + return; + + CppUnit::Message message( "expression is not of the expected type" ); + message.addDetail( std::string("Expected type: ") + expectedType.name() ); + message.addDetail( std::string("Actual type: ") + actualType.name() ); + if ( !expression.empty() ) + message.addDetail( "Expression: " + expression ); + + CppUnit::Asserter::fail( message, sourceLine ); + } + } } *************** *** 60,63 **** --- 80,89 ---- #define RFTA_TEST_SUITE_REGISTRATION( FixtureType ) \ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FixtureType, "Refactoring" ) + + #define RFTA_ASSERT_TYPE_IS( expectedType, expression ) \ + Refactoring::Testing::checkType( typeid(expectedType), \ + typeid(expression), \ + #expression, \ + CPPUNIT_SOURCELINE() ) #endif // RFTA_UNITTESTING_H Index: rfta.dsp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/rfta.dsp,v retrieving revision 1.37 retrieving revision 1.38 diff -C2 -d -r1.37 -r1.38 *** rfta.dsp 7 Mar 2003 07:58:31 -0000 1.37 --- rfta.dsp 8 Mar 2003 13:11:42 -0000 1.38 *************** *** 901,904 **** --- 901,912 ---- # Begin Source File + SOURCE=.\CodeModelGeneratorTest.cpp + # End Source File + # Begin Source File + + SOURCE=.\CodeModelGeneratorTest.h + # End Source File + # Begin Source File + SOURCE=.\CodeWriterTest.cpp |