1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-08 02:53:12 +02:00

Fixed handling of trailing comments after compound statements.

This commit is contained in:
Sergey Prigogin 2012-02-15 16:21:26 -08:00
parent 3453fa6fcd
commit 476364f0a6
3 changed files with 21 additions and 31 deletions

View file

@ -8,6 +8,7 @@
*
* Contributors:
* Institute for Software - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
@ -90,30 +91,24 @@ public class StatementWriter extends NodeWriter {
newLine = false;
} else if (statement instanceof IASTExpressionStatement) {
writeExpressionStatement((IASTExpressionStatement) statement);
//usually newLine
} else if (statement instanceof IASTDeclarationStatement) {
writeDeclarationStatement((IASTDeclarationStatement) statement);
newLine = false;
} else if (statement instanceof IASTNullStatement) {
writeNullStatement((IASTNullStatement)statement);
// usually newLine
} else if (statement instanceof IASTReturnStatement) {
writeReturnStatement((IASTReturnStatement)statement);
// usually newLine
} else if (statement instanceof IASTGotoStatement) {
writeGotoStatement((IASTGotoStatement) statement);
// usually newLine
} else if (statement instanceof IASTLabelStatement) {
writeLabelStatement((IASTLabelStatement) statement);
newLine = false;
} else if (statement instanceof IASTCaseStatement) {
writeCaseStatement((IASTCaseStatement) statement);
// usually newLine
} else if (statement instanceof IASTDefaultStatement) {
writeDefaultStatement((IASTDefaultStatement)statement);
} else if (statement instanceof IASTContinueStatement) {
writeContinueStatement((IASTContinueStatement)statement);
// usually newLine
} else if (statement instanceof IASTCompoundStatement) {
writeCompoundStatement((IASTCompoundStatement) statement);
if (compoundNoNewLine) {
@ -122,7 +117,6 @@ public class StatementWriter extends NodeWriter {
}
} else if (statement instanceof IASTBreakStatement) {
writeBreakStatement((IASTBreakStatement) statement);
// usually newLine
} else if (statement instanceof IASTSwitchStatement) {
writeSwitchStatement((IASTSwitchStatement) statement);
newLine = false;
@ -160,7 +154,7 @@ public class StatementWriter extends NodeWriter {
nextCompoundNoNewLine();
scribe.print(DO);
writeBodyStatement(doStatement.getBody(), true);
writeBodyStatement(doStatement.getBody(), false);
scribe.print(DO_WHILE);
doStatement.getCondition().accept(visitor);
scribe.print(')');
@ -191,7 +185,7 @@ public class StatementWriter extends NodeWriter {
scribe.print(')');
scribe.newLines();
nextCompoundNoNewLine();
writeBodyStatement(forStatement.getBody(), false);
writeBodyStatement(forStatement.getBody(), true);
}
private void writeForStatement(ICPPASTRangeBasedForStatement forStatment) {
@ -203,7 +197,7 @@ public class StatementWriter extends NodeWriter {
scribe.print(')');
scribe.newLines();
nextCompoundNoNewLine();
writeBodyStatement(forStatment.getBody(), false);
writeBodyStatement(forStatment.getBody(), true);
}
private void writeIfStatement(IASTIfStatement ifStatement) {
@ -225,7 +219,7 @@ public class StatementWriter extends NodeWriter {
scribe.newLines();
nextCompoundNoNewLine();
IASTStatement elseClause = ifStatement.getElseClause();
writeBodyStatement(ifStatement.getThenClause(), elseClause != null);
writeBodyStatement(ifStatement.getThenClause(), elseClause == null);
if (elseClause != null) {
scribe.print(ELSE);
@ -297,7 +291,7 @@ public class StatementWriter extends NodeWriter {
scribe.newLines();
}
scribe.print(')');
writeBodyStatement(catchStatement.getCatchBody(), true);
writeBodyStatement(catchStatement.getCatchBody(), false);
}
private void writeTryBlockStatement(ICPPASTTryBlockStatement tryStatement) {
@ -324,7 +318,7 @@ public class StatementWriter extends NodeWriter {
scribe.print(')');
scribe.newLines();
nextCompoundNoNewLine();
writeBodyStatement(whileStatment.getBody(), false);
writeBodyStatement(whileStatment.getBody(), true);
}
private void writeCaseStatement(IASTCaseStatement caseStatement) {
@ -358,7 +352,7 @@ public class StatementWriter extends NodeWriter {
scribe.print(')');
scribe.newLines();
nextCompoundNoNewLine();
writeBodyStatement(switchStatement.getBody(), false);
writeBodyStatement(switchStatement.getBody(), true);
switchIsNew = false;
}
@ -396,12 +390,10 @@ public class StatementWriter extends NodeWriter {
return compoundStatement.getStatements();
}
// TODO(sprigogin): Rename second parameter
protected void writeBodyStatement(IASTStatement statement, boolean isDoStatement) {
protected void writeBodyStatement(IASTStatement statement, boolean newLineForCompound) {
if (statement instanceof IASTCompoundStatement) {
//TODO hsr existiert noch eine methode
statement.accept(visitor);
if (!isDoStatement) {
writeCompoundStatement((IASTCompoundStatement) statement);
if (newLineForCompound) {
scribe.newLine();
}
compoundNoNewLine = false;
@ -417,7 +409,7 @@ public class StatementWriter extends NodeWriter {
}
/**
* Write no new Line after the next Compound-Statement
* Write no new Line after the next compound statement
*/
protected void nextCompoundNoNewLine() {
compoundNoNewLine = true;

View file

@ -7,7 +7,7 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software - initial API and implementation
* Institute for Software - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
@ -28,9 +28,9 @@ public class ModifiedASTStatementWriter extends StatementWriter {
}
@Override
protected void writeBodyStatement(IASTStatement statement, boolean isDoStatement) {
protected void writeBodyStatement(IASTStatement statement, boolean newLineForCompound) {
IASTStatement replacementNode = modificationHelper.getNodeAfterReplacement(statement);
super.writeBodyStatement(replacementNode, isDoStatement);
super.writeBodyStatement(replacementNode, newLineForCompound);
}
@Override

View file

@ -2446,8 +2446,7 @@ public class ToogleRefactoringTest extends RefactoringTestBase {
// return;
//}
//catch (int e) {
//}
//// Trailing comment
//} // Trailing comment
public void testClassToHeaderTrailingCommentWithTryBlock() throws Exception {
assertRefactoringSuccess();
}
@ -2474,11 +2473,10 @@ public class ToogleRefactoringTest extends RefactoringTestBase {
//} /* one */
//catch (int i) {
// // zwaa
//}
///* two */catch (int j) {
//} /* two */
//catch (int j) {
// // draa
//}
///* three */
//} /* three */
public void testClassToHeaderTrailingMultipleCommentsInTryBlock() throws Exception {
assertRefactoringSuccess();
}
@ -2810,8 +2808,8 @@ public class ToogleRefactoringTest extends RefactoringTestBase {
// } /*1*/
// catch (int e) {
// /*2*/
// }
// /*3*/catch (int e) {
// } /*3*/
// catch (int e) {
// /*4*/
// }
//};