1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Improved handling of blank lines when appending to a translation unit.

This commit is contained in:
Sergey Prigogin 2011-12-29 18:15:35 -08:00
parent da95eb2605
commit e3bdead668
19 changed files with 144 additions and 186 deletions

View file

@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
@ -144,6 +146,24 @@ public class ASTWriter {
* @return <code>true</code> if the blank line between the nodes is needed.
*/
public static boolean requireBlankLineInBetween(IASTNode node1, IASTNode node2) {
if (node1 instanceof ContainerNode) {
List<IASTNode> nodes = ((ContainerNode) node1).getNodes();
if (!nodes.isEmpty()) {
node1 = nodes.get(nodes.size() - 1);
}
}
if (node2 instanceof ContainerNode) {
List<IASTNode> nodes = ((ContainerNode) node2).getNodes();
if (!nodes.isEmpty()) {
node2 = nodes.get(0);
}
}
while (node1 instanceof ICPPASTTemplateDeclaration) {
node1 = ((ICPPASTTemplateDeclaration) node1).getDeclaration();
}
while (node2 instanceof ICPPASTTemplateDeclaration) {
node2 = ((ICPPASTTemplateDeclaration) node2).getDeclaration();
}
if (node1 instanceof ICPPASTVisibilityLabel && node2 instanceof ICPPASTVisibilityLabel) {
return true;
}

View file

@ -132,6 +132,7 @@ public class ASTWriterVisitor extends ASTVisitor {
}
public void visit(ASTLiteralNode lit) {
insertBlankLineIfNeeded(lit);
scribe.print(lit.getRawSignature());
}

View file

@ -75,7 +75,6 @@ import org.eclipse.jface.text.TextUtilities;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
@ -559,26 +558,47 @@ public class ChangeGenerator extends ASTVisitor {
if (modifications.isEmpty())
return;
ASTWriter synthWriter = new ASTWriter();
synthWriter.setModificationStore(modificationStore);
IASTNode prevNode = null;
IASTDeclaration[] declarations = tu.getDeclarations();
if (declarations.length != 0) {
prevNode = declarations[declarations.length - 1];
} else {
IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
if (preprocessorStatements.length != 0) {
prevNode = preprocessorStatements[preprocessorStatements.length - 1];
}
}
int offset = prevNode != null ? getEndOffsetIncludingComments(prevNode) : 0;
String source = tu.getRawSignature();
int endOffset = skipTrailingBlankLines(source, offset);
IASTFileLocation targetLocation = tu.getFileLocation();
ChangeGeneratorWriterVisitor writer =
new ChangeGeneratorWriterVisitor(modificationStore, commentMap);
IASTNode newNode = null;
for (ASTModification modification : modifications) {
boolean first = newNode == null;
newNode = modification.getNewNode();
if (first) {
if (prevNode != null) {
writer.newLine();
if (ASTWriter.requireBlankLineInBetween(prevNode, newNode)) {
writer.newLine();
}
}
}
newNode.accept(writer);
}
if (prevNode != null) {
IASTNode nextNode = getNextSiblingOrPreprocessorNode(prevNode);
if (nextNode != null && ASTWriter.requireBlankLineInBetween(newNode, nextNode)) {
writer.newLine();
}
}
String code = writer.toString();
IFile file = FileHelper.getFileFromNode(tu);
MultiTextEdit parentEdit = getEdit(tu, file);
IASTDeclaration[] declarations = tu.getDeclarations();
for (ASTModification modification : modifications) {
String code = synthWriter.write(modification.getNewNode(), commentMap);
if (declarations.length > 0) {
IASTDeclaration lastDecl = declarations[declarations.length - 1];
targetLocation = lastDecl.getFileLocation();
}
String lineDelimiter = FileHelper.determineLineDelimiter(tu.getRawSignature());
parentEdit.addChild(new InsertEdit(endOffset(targetLocation),
lineDelimiter + lineDelimiter + code));
}
parentEdit.addChild(new ReplaceEdit(offset, endOffset - offset, code));
}
/**

View file

@ -1173,7 +1173,6 @@ private:
};
#endif /* A_H_ */
//=
#ifndef A_H_
#define A_H_
@ -1195,9 +1194,7 @@ inline void Person::setId(int id) {
this->id = id;
}
#endif /* A_H_ */
//!No Methods Separate Definition
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
//@.config
@ -1248,7 +1245,6 @@ inline void test::setI(int i) {
this->i = i;
}
#endif /* TEST_H_ */
//!Bug 323780 "Generate Getters and Setters..." crashes
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
@ -1349,7 +1345,6 @@ public:
//@Test.cxx
//=
int Test::getTestField() const {
return testField;
}
@ -1390,7 +1385,6 @@ public:
//@component_b/implementation/Test.cpp
//=
int Test::getTestField() const {
return testField;
}

View file

@ -17,8 +17,6 @@ public:
inline bool X::a(int int1) const {
}
//!Param const and reference and pointer two params
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -38,8 +36,6 @@ public:
inline bool X::xy(int int1, int i) const {
}
//!Test if TemplateMethod stays in header
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -62,15 +58,13 @@ public:
template<class T>
inline void A<T>::test() {
}
//@A.cpp
#include "A.h"
//=
#include "A.h"
//!class template member functions
//!Class template member functions
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
filename=A.h
@ -102,9 +96,7 @@ A<T>::A() {
template<class T>
inline void A<T>::test() {
}
//!member class
//!Member class
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
filename=A.h
@ -144,8 +136,6 @@ public:
inline void A::test() {
}
//!Method declared in otherwise empty class
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -165,7 +155,6 @@ public:
//@A.cpp
//=
void A::test() {
}
//!Implement in existing namespace
@ -205,7 +194,7 @@ void ClassInNamespace::test2() {
}
}
//!virtual method in the middle of con/destructor, without parameters and void return value
//!Virtual method in the middle of con/destructor, without parameters and void return value
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
filename=A.h
@ -287,8 +276,6 @@ A::A() {
void A::foo() {
}
//!Method at beginning, without parameters, void return value and const
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -348,8 +335,6 @@ A::A() {
int A::foo() {
}
//!Method with two int parameters
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -379,8 +364,6 @@ A::A() {
int A::foo(int param1, int param2) {
}
//!Method defined in header
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -408,8 +391,6 @@ A::A() {
inline void A::test() {
}
//!Implement a function at end of source file
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -426,7 +407,6 @@ void function_with_impl() {
void function() {
}
//!Implement with namespace
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -453,7 +433,6 @@ void Namespace::ClassInNamespace::other_test() {
void Namespace::ClassInNamespace::test() {
}
//!Implement function within namespace
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -524,7 +503,7 @@ int test2() {
}
}
//!class template member functions with multiple templates
//!Class template member functions with multiple templates
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
filename=A.h
@ -556,9 +535,7 @@ A<T,U>::A() {
template<class T, class U>
inline void A<T, U>::test() {
}
//!with default parameters
//!With default parameters
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
filename=A.h
@ -578,7 +555,7 @@ public:
void Class::test(int param1, int param2, int param3) {
}
//!static method
//!Static method
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
filename=A.h
@ -672,7 +649,6 @@ class TestClass {
inline void nspace::TestClass::testMethod() {
}
#endif /* TESTCLASS_H_ */
//!Bug 290110 Source-> Implement Method
@ -736,8 +712,6 @@ A::~A() {
void n1::n2::A::B::testmethod2() {
}
//!Bug 337040 - Insert definition in empty implementation file (.cxx)
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -753,7 +727,6 @@ public:
//@A.cxx
//=
void TestClass::foo() {
}
//!Bug 355006 - NPE implementing template function
@ -773,7 +746,6 @@ void func(T&);
template<typename T>
inline void func(T&) {
}
//!Bug 363111 - Remove explicit in constructor definition
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
//@.config
@ -789,6 +761,5 @@ public:
//@A.cpp
//=
TestClass::TestClass() {
}

View file

@ -18,7 +18,6 @@ inline void A::member() {
// return comment
return;
}
//!ClassToHeaderTopCommentOrder
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -41,7 +40,6 @@ class A {
inline void A::member() {
return;
}
//!ClassToHeaderCatchComment
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -68,7 +66,6 @@ try {
catch (int i) {
// catch comment
}
//!ClassToHeaderTopComment
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -89,7 +86,6 @@ class A {
inline void A::member() {
return;
}
//!ClassToHeaderTemplateTopComment
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -114,7 +110,6 @@ template<typename T>
inline T A::member() {
return T();
}
//!ClassToHeaderTrailingComment
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -133,7 +128,6 @@ class A {
inline void A::member() {
return;
} // Trailing comment
//!ClassToHeaderTrailingCommentWithTryBlock
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config

View file

@ -93,7 +93,6 @@ void /*$*/member/*$$*/() {
//@A.h
//=
void member() {
// body comment
return;
@ -115,7 +114,6 @@ void /*$*/member/*$$*/() {
//@A.h
//=
// Top comment
void member() {
// body comment

View file

@ -34,7 +34,6 @@ public:
inline A::A(int x, int y) :
a(x), b(y) {
}
//!TestConstructorToggleInHeaderToImplementation
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -85,7 +84,6 @@ int main() {
A::A(int x, int y) :
a(x), b(y) {
}
//!TestConstructorToggleInImplementationToClass
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -154,7 +152,6 @@ public:
inline A::~A() {
}
//!TestDestructorToggleInHeaderToImplementation
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -199,7 +196,6 @@ int main() {
A::~A() {
}
//!TestDestructorToggleInImplementationToClass
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config

View file

@ -20,7 +20,6 @@ class A {
inline void A::member(int a, int b) {
return;
}
//!TestDefaultParameterInitializerInHeaderToImplementation
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -57,7 +56,6 @@ int main() {
void A::member(int a, int b) {
return;
}
//!TestDefaultParameterInitializerInImplementationToClass
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config

View file

@ -54,7 +54,6 @@ int main() {
int freeFunction(int* a, int& b) {
return 42;
}
//!TestFreeFunctionToggleFromImplementationToHeaderWithDeclaration
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -172,7 +171,6 @@ try {
}
catch (std::exception& e) {
}
//!TestFreeFunction
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -260,4 +258,3 @@ void freefunction() {
}
}

View file

@ -1,4 +1,4 @@
//!TestSimpleNamespaceInClassToInHeader
//!Test simple namespace in class to in header
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -30,7 +30,7 @@ inline void A::foo() {
}
}
//!TestSimpleNamespaceInHeaderToImplementationWithinNSDefinition
//!Test simple namespace in header to implementation within namespace definition
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -80,8 +80,7 @@ void A::foo() {
}
}
//!TestSimpleNamespaceInHeaderToImplementationWithNSDefinitionInImpl
//!Test simple namespace in header to implementation with namespace definition in implementation
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -134,7 +133,7 @@ void A::foo() {
}
}
//!TestSimpleNamespaceInHeaderToImplementationWithNamespaceQualifiedName
//!Test simple namespace in header to implementation with namespace qualified name
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -184,7 +183,6 @@ void A::foo() {
}
}
//!TestSimpleNamespaceFromImplementationToInHeader
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config

View file

@ -24,7 +24,6 @@ class A {
inline void A::B::member(int a, int b) {
return;
}
//!TestNestedClassInHeaderToImplementation
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -65,7 +64,6 @@ int main() {
void A::B::member(int a, int b) {
return;
}
//!TestNestedClassInImplementationToClass
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config

View file

@ -1,4 +1,4 @@
//!TestZeroLengthSelection
//!Test zero length selection
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -16,8 +16,7 @@ class A {
inline void A::member() {
return;
}
//!TestSubstringSelection
//!Test substring selection
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -35,8 +34,7 @@ class A {
inline void A::member() {
return;
}
//!TestBodySelection
//!Test body selection
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -54,8 +52,7 @@ class A {
inline void A::member() {
return;
}
//!TestBodySelectionWithConfusingName
//!Test body selection with confusing name
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -75,8 +72,7 @@ inline void A::member() {
int abcd = 42;
return;
}
//!TestLeftBorderSelection
//!Test left border selection
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -94,8 +90,7 @@ class A {
inline void A::member() {
return;
}
//!TestRightBorderSelection
//!Test right border selection
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -113,8 +108,7 @@ class A {
inline void A::member() {
return;
}
//!TestOverlappingSelection
//!Test overlapping selection
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -132,4 +126,3 @@ class A {
inline void A::member() {
return;
}

View file

@ -1,4 +1,4 @@
//!TestSimpleFunctionInClassToInHeader
//!Test simple function in class to in header
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -28,8 +28,7 @@ private:
inline int A::function() {
return 0;
}
//!TestSimpleFunctionInHeaderToImplementation
//!Test simple function in header to implementation
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -73,7 +72,6 @@ int main() {
int A::function() {
return 0;
}
//!TestSimpleFunctionInImplementationToInClass
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config

View file

@ -1,4 +1,4 @@
//!TestTemplateFunctionInClassToInHeader
//!Test template function in class to in header
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -27,8 +27,7 @@ template<typename T, typename U>
inline T A<T, U>::B::member() {
return T();
}
//!TestTemplateFunctionInHeaderToInClass
//!Test template function in header to in class
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -57,7 +56,7 @@ class A {
}
};
};
//!TestTemplateFunctionInHeaderToInClassWithTemplateSelected
//!Test template function in header to in class with template selected
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -78,8 +77,7 @@ template<typename T>
inline T A::foo() {
return T();
}
//!TestComplexTemplateFunctionFromInClassToInheader
//!Test complex template function from in class to in header
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h
@ -105,8 +103,7 @@ template<typename U, typename V>
inline void A<T, S>::foo(const U& u, const V& v) {
return;
}
//!TestComplexTemplateFunctionFromInHeaderToInClass
//!Test complex template function from in header to in class
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
filename=A.h

View file

@ -30,7 +30,6 @@ try {
catch (std::exception& e1) {
return;
}
//!TestTryCatchFromInHeaderToImplementation
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -77,7 +76,6 @@ try {
catch (std::exception& e1) {
return;
}
//!TestTryCatchFromInImplementationToClass
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -160,7 +158,6 @@ catch (std::exception& e1) {
catch (std::exception& e2) {
return;
}
//!TestMultipleTryCatchFromInHeaderToImplementation
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config
@ -213,7 +210,6 @@ catch (std::exception& e1) {
catch (std::exception& e2) {
return;
}
//!TestMultipleTryCatchFromInImplementationToClass
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config

View file

@ -18,7 +18,6 @@ class A {
inline int A::foo() {
return 0;
}
//!TestVirtualSpecifierFromInHeaderToImplementation
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//%CPP
@ -52,7 +51,6 @@ int main() {
int A::foo() {
return 0;
}
//!TestVirtualSpecifierFromImplementationToHeader
//#org.eclipse.cdt.ui.tests.refactoring.togglefunction.ToggleRefactoringTest
//@.config

View file

@ -29,7 +29,6 @@ import org.eclipse.cdt.internal.ui.refactoring.Container;
import org.eclipse.cdt.internal.ui.refactoring.CreateFileChange;
public class ToggleFileCreator {
private static final String EMPTY_STRING = ""; //$NON-NLS-1$
private static final String H = ".h"; //$NON-NLS-1$
private ToggleRefactoringContext context;
@ -85,6 +84,7 @@ public class ToggleFileCreator {
}
final Container<Boolean> answer = new Container<Boolean>();
Runnable r = new Runnable() {
@Override
public void run() {
Shell shell = CUIPlugin.getDefault().getWorkbench().getWorkbenchWindows()[0].getShell();
String functionname;
@ -103,7 +103,7 @@ public class ToggleFileCreator {
}
public String getIncludeStatement() {
return "#include \"" + ToggleNodeHelper.getFilenameWithoutExtension(getNewFileName()) + ".h\""; //$NON-NLS-1$//$NON-NLS-2$
return "#include \"" + ToggleNodeHelper.getFilenameWithoutExtension(getNewFileName()) + ".h\"\n"; //$NON-NLS-1$//$NON-NLS-2$
}
private String getNewFileName() {

View file

@ -57,7 +57,6 @@ import org.eclipse.cdt.internal.ui.refactoring.Container;
import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector;
public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefactoringStrategy {
private IASTTranslationUnit impl_unit;
private ToggleRefactoringContext context;
private TextEditGroup infoText;
@ -68,6 +67,7 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact
this.context = context;
}
@Override
public void run(ModificationCollector collector) {
if (!newFileCheck()) {
return;
@ -76,8 +76,7 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact
ICPPASTFunctionDefinition newDefinition = getNewDefinition();
if (context.getDeclaration() != null) {
removeDefinitionFromHeader(collector);
}
else {
} else {
replaceDefinitionWithDeclaration(collector);
}
@ -97,8 +96,7 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact
implRewrite = implRewrite.insertBefore(impl_unit.getTranslationUnit(),
null, insertionParent, infoText);
}
}
else {
} else {
insertionParent = impl_unit.getTranslationUnit();
}
@ -115,25 +113,6 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact
private void copyCommentsToNewFile(ICPPASTFunctionDefinition newDefinition, final ASTRewrite newRewriter,
final ASTRewrite oldRewriter) {
newDefinition.accept(new ASTVisitor(true) {
private void copy(IASTNode node) {
copyComments(node, newRewriter, oldRewriter, CommentPosition.leading);
copyComments(node, newRewriter, oldRewriter, CommentPosition.trailing);
copyComments(node, newRewriter, oldRewriter, CommentPosition.freestanding);
}
private void copyComments(IASTNode node, ASTRewrite newRewriter, ASTRewrite oldRewriter,
CommentPosition pos) {
if (node.getNodeLocations().length > 0 && node.getNodeLocations()[0] instanceof IASTCopyLocation) {
IASTCopyLocation copyLoc = (IASTCopyLocation) node.getNodeLocations()[0];
List<IASTComment> comments = oldRewriter.getComments(copyLoc.getOriginalNode(), pos);
for (IASTComment comment : comments) {
newRewriter.addComment(node, comment, pos);
}
}
}
@Override
public int visit(IASTName name) {
copy(name);
@ -241,13 +220,29 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact
copy(designator);
return super.visit(designator);
}
});
private void copy(IASTNode node) {
copyComments(node, newRewriter, oldRewriter, CommentPosition.leading);
copyComments(node, newRewriter, oldRewriter, CommentPosition.trailing);
copyComments(node, newRewriter, oldRewriter, CommentPosition.freestanding);
}
private void copyComments(IASTNode node, ASTRewrite newRewriter, ASTRewrite oldRewriter,
CommentPosition pos) {
if (node.getNodeLocations().length > 0 && node.getNodeLocations()[0] instanceof IASTCopyLocation) {
IASTCopyLocation copyLoc = (IASTCopyLocation) node.getNodeLocations()[0];
List<IASTComment> comments = oldRewriter.getComments(copyLoc.getOriginalNode(), pos);
for (IASTComment comment : comments) {
newRewriter.addComment(node, comment, pos);
}
}
}
});
}
private boolean newFileCheck() {
impl_unit = context.getTUForSiblingFile();
if (this.impl_unit == null) {
if (impl_unit == null) {
ToggleFileCreator filecreator = new ToggleFileCreator(context, ".cpp"); //$NON-NLS-1$
if (filecreator.askUserForFileCreation(context)) {
filecreator.createNewFile();
@ -269,8 +264,7 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact
return ToggleNodeHelper.getAncestorOfType(toquery, ICPPASTNamespaceDefinition.class);
}
private IASTNode findInsertionPoint(IASTNode insertionParent,
IASTTranslationUnit unit) {
private IASTNode findInsertionPoint(IASTNode insertionParent, IASTTranslationUnit unit) {
IASTFunctionDeclarator declarator = context.getDeclaration();
if (unit == null) {
unit = context.getDefinitionUnit();
@ -283,8 +277,8 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact
return insertion_point;
}
private void restoreLeadingComments(
ICPPASTFunctionDefinition newDefinition, ASTRewrite newRewriter, ModificationCollector collector) {
private void restoreLeadingComments(ICPPASTFunctionDefinition newDefinition,
ASTRewrite newRewriter, ModificationCollector collector) {
ASTRewrite rw = collector.rewriterForTranslationUnit(context.getDefinitionUnit());
List<IASTComment>comments = rw.getComments(context.getDefinition(), CommentPosition.leading);
if (comments != null) {
@ -297,8 +291,7 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact
}
}
private void replaceDefinitionWithDeclaration(
ModificationCollector collector) {
private void replaceDefinitionWithDeclaration(ModificationCollector collector) {
IASTSimpleDeclaration newdeclarator =
ToggleNodeHelper.createDeclarationFromDefinition(context.getDefinition());
ASTRewrite rewrite = collector.rewriterForTranslationUnit(context.getDefinitionUnit());
@ -308,9 +301,9 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact
private ICPPASTFunctionDefinition getNewDefinition() {
ICPPASTFunctionDefinition newDefinition =
ToggleNodeHelper.createFunctionSignatureWithEmptyBody(
context.getDefinition().getDeclSpecifier().copy(CopyStyle.withLocations), context
.getDefinition().getDeclarator().copy(CopyStyle.withLocations), context
.getDefinition().copy(CopyStyle.withLocations));
context.getDefinition().getDeclSpecifier().copy(CopyStyle.withLocations),
context.getDefinition().getDeclarator().copy(CopyStyle.withLocations),
context.getDefinition().copy(CopyStyle.withLocations));
newDefinition.getDeclSpecifier().setInline(false);
newDefinition.setBody(context.getDefinition().getBody().copy(CopyStyle.withLocations));
if (newDefinition instanceof ICPPASTFunctionWithTryBlock) {
@ -346,11 +339,9 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact
}
}
private CPPASTNamespaceDefinition createNamespace(
ICPPASTNamespaceDefinition parent_namespace) {
private CPPASTNamespaceDefinition createNamespace(ICPPASTNamespaceDefinition parent_namespace) {
CPPASTNamespaceDefinition insertionParent = new CPPASTNamespaceDefinition(
parent_namespace.getName()
.copy(CopyStyle.withLocations));
parent_namespace.getName().copy(CopyStyle.withLocations));
insertionParent.setParent(impl_unit);
return insertionParent;
}
@ -363,11 +354,11 @@ parent_namespace.getName()
private IASTNode searchNamespaceInImplementation(final IASTName name) {
final Container<IASTNode> result = new Container<IASTNode>();
this.impl_unit.accept(
new ASTVisitor() {
this.impl_unit.accept(new ASTVisitor() {
{
shouldVisitNamespaces = true;
}
@Override
public int visit(ICPPASTNamespaceDefinition namespaceDefinition) {
if (name.toString().equals(namespaceDefinition.getName().toString())) {