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

Cosmetics.

This commit is contained in:
Sergey Prigogin 2011-11-01 15:45:38 -07:00
parent d8d335dbf8
commit 0177053e2b
9 changed files with 107 additions and 158 deletions

View file

@ -26,7 +26,6 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
* @author Emanuel Graf
*/
public class ASTWriter {
private ChangeGeneratorWriterVisitor transformationVisitor;
private ASTModificationStore modificationStore = new ASTModificationStore();
private String givenIndentation = ""; //$NON-NLS-1$
@ -76,14 +75,12 @@ public class ASTWriter {
*/
public String write(IASTNode rootNode, String fileScope, NodeCommentMap commentMap)
throws ProblemRuntimeException {
transformationVisitor = new ChangeGeneratorWriterVisitor(modificationStore, givenIndentation,
fileScope, commentMap);
ChangeGeneratorWriterVisitor transformationVisitor = new ChangeGeneratorWriterVisitor(
modificationStore, givenIndentation, fileScope, commentMap);
if (rootNode != null) {
rootNode.accept(transformationVisitor);
}
String str = transformationVisitor.toString();
transformationVisitor.cleanCache();
return str;
return transformationVisitor.toString();
}
public void setModificationStore(ASTModificationStore modificationStore) {

View file

@ -224,7 +224,6 @@ public class DeclSpecWriter extends NodeWriter {
}
}
scribe.print('}');
}
private void writeEnumerator(IASTEnumerator enumerator) {

View file

@ -32,19 +32,14 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
/**
*
* Generates source code of declarator nodes. The actual string operations are delegated
* to the <code>Scribe</code> class.
* to the {@link Scribe} class.
*
* @see Scribe
* @see IASTDeclarator
* @author Emanuel Graf IFS
*
*/
public class DeclaratorWriter extends NodeWriter {
private static final String AMPERSAND_SPACE = "& "; //$NON-NLS-1$
private static final String AMPERSAND__AMPERSAND_SPACE = "&& "; //$NON-NLS-1$
private static final String STAR_SPACE = "* "; //$NON-NLS-1$
@ -186,7 +181,6 @@ public class DeclaratorWriter extends NodeWriter {
scribe.print('*');
}
if (operator.isConst()) {
scribe.printStringSpace(CONST);
}
@ -267,12 +261,10 @@ public class DeclaratorWriter extends NodeWriter {
scribe.print(')');
scribe.newLine();
writeKnRParameterDeclarations(knrFunct, knrFunct.getParameterDeclarations());
}
protected void writeKnRParameterDeclarations(
ICASTKnRFunctionDeclarator knrFunct, IASTDeclaration[] knrDeclarations) {
protected void writeKnRParameterDeclarations(ICASTKnRFunctionDeclarator knrFunct,
IASTDeclaration[] knrDeclarations) {
for (int i = 0; i < knrDeclarations.length; ++i) {
scribe.noNewLines();
knrDeclarations[i].accept(visitor);

View file

@ -21,15 +21,12 @@ import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
/**
*
* Base class for node writers. This class contains methods and string constants
* used by multiple node writers.
*
* @author Emanuel Graf IFS
*
*/
public class NodeWriter {
protected Scribe scribe;
protected ASTVisitor visitor;
protected NodeCommentMap commentMap;
@ -86,29 +83,11 @@ public class NodeWriter {
}
}
protected void writeTrailingComments(IASTNode node) {
//default write newLine
// Default is to write a new line after trailing comments.
writeTrailingComments(node, true);
}
protected boolean hasTrailingComments(IASTNode node){
if(getTrailingComments(node).size()>0) {
return true;
}
return false;
}
private ArrayList<IASTComment> getTrailingComments(IASTNode node) {
ArrayList<IASTComment> trailingComments = commentMap.getTrailingCommentsForNode(node);
IASTNodeLocation[] locs = node.getNodeLocations();
if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) {
IASTCopyLocation loc = (IASTCopyLocation) locs[0];
trailingComments.addAll(commentMap.getTrailingCommentsForNode(loc.getOriginalNode()));
}
return trailingComments;
}
protected void writeTrailingComments(IASTNode node, boolean newLine) {
for (IASTComment comment : getTrailingComments(node)) {
scribe.printSpace();
@ -119,11 +98,22 @@ public class NodeWriter {
}
}
protected boolean hasFreestandingComments(IASTNode node){
if(getFreestandingComments(node).size()>0) {
return true;
protected boolean hasTrailingComments(IASTNode node) {
return !getTrailingComments(node).isEmpty();
}
return false;
private ArrayList<IASTComment> getTrailingComments(IASTNode node) {
ArrayList<IASTComment> trailingComments = commentMap.getTrailingCommentsForNode(node);
IASTNodeLocation[] locs = node.getNodeLocations();
if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) {
IASTCopyLocation loc = (IASTCopyLocation) locs[0];
trailingComments.addAll(commentMap.getTrailingCommentsForNode(loc.getOriginalNode()));
}
return trailingComments;
}
protected boolean hasFreestandingComments(IASTNode node) {
return !getFreestandingComments(node).isEmpty();
}
private ArrayList<IASTComment> getFreestandingComments(IASTNode node) {

View file

@ -46,17 +46,14 @@ import org.eclipse.cdt.internal.core.dom.rewrite.util.FileHelper;
import org.eclipse.core.resources.IFile;
/**
*
* Generates source code of statement nodes. The actual string operations are delegated
* to the <code>Scribe</code> class.
*
* @see Scribe
* @see IASTStatement
* @author Emanuel Graf IFS
*
*/
public class StatementWriter extends NodeWriter {
private static final String DEFAULT = "default:"; //$NON-NLS-1$
private static final String CASE = "case "; //$NON-NLS-1$
private static final String WHILE = "while("; //$NON-NLS-1$
@ -85,7 +82,7 @@ public class StatementWriter extends NodeWriter{
/**
*
* @param statement
* @param newLine if true print a newline if statment usually have one.
* @param newLine if true print a newline if statement usually have one.
* @return {@link ASTVisitor#PROCESS_SKIP}
*/
protected int writeStatement(IASTStatement statement, boolean newLine) {
@ -158,12 +155,9 @@ public class StatementWriter extends NodeWriter{
if (hasTrailingComments(statement)) {
writeTrailingComments(statement, newLine);
}
else{
if(newLine){
} else if (newLine) {
scribe.newLine();
}
}
return ASTVisitor.PROCESS_SKIP;
}
@ -192,7 +186,6 @@ public class StatementWriter extends NodeWriter{
} else {
cppConditionDeclaration.accept(visitor);
}
} else {
if (forStatment.getConditionExpression() != null) {
forStatment.getConditionExpression().accept(visitor);
@ -238,7 +231,7 @@ public class StatementWriter extends NodeWriter{
scribe.newLines();
nextCompoundNoNewLine();
IASTStatement elseClause = ifStatement.getElseClause();
writeBodyStatement(ifStatement.getThenClause(), elseClause != null ? true : false);
writeBodyStatement(ifStatement.getThenClause(), elseClause != null);
if (elseClause != null) {
scribe.print(ELSE);
@ -247,8 +240,7 @@ public class StatementWriter extends NodeWriter{
}
}
protected void writeDeclarationWithoutSemicolon(
IASTDeclaration declaration) {
protected void writeDeclarationWithoutSemicolon(IASTDeclaration declaration) {
declWriter.writeDeclaration(declaration, false);
}
@ -432,7 +424,6 @@ public class StatementWriter extends NodeWriter{
/**
* Write no new Line after the next Compound-Statement
*
*/
protected void nextCompoundNoNewLine() {
compoundNoNewLine = true;
@ -441,7 +432,6 @@ public class StatementWriter extends NodeWriter{
/**
* Indent one time more at the end (before the closing Brackets)
* of a Compound-Statement
*
*/
protected void nextCompoundIndentationLevelOneMore() {
decrementIndentationLevelOneMore = true;

View file

@ -28,14 +28,12 @@ import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ContainerNode;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
public class ASTModificationHelper {
private final ModificationScopeStack modificationStore;
public ASTModificationHelper(ModificationScopeStack stack) {
this.modificationStore = stack;
}
public <T extends IASTNode> T[] createModifiedChildArray(IASTNode parent, T[] unmodifiedChildren, Class<T> clazz, NodeCommentMap commentMap) {
ArrayList<T> modifiedChildren = new ArrayList<T>(Arrays.asList(unmodifiedChildren));
@ -151,21 +149,16 @@ public class ASTModificationHelper {
return null;
}
public List<ASTModification> modificationsForNode(
IASTNode targetNode) {
public List<ASTModification> modificationsForNode(IASTNode targetNode) {
List<ASTModification> modificationsForNode;
if (modificationStore.getModifiedNodes().contains(targetNode)) {
modificationsForNode = modificationStore.getModificationsForNode(targetNode);
}
else{
} else {
modificationsForNode = Collections.emptyList();
}
return modificationsForNode;
}
public IASTInitializer getInitializer(IASTDeclarator decl) {
IASTInitializer initializer = decl.getInitializer();
@ -179,8 +172,6 @@ public class ASTModificationHelper {
return null;
}
throw new UnhandledASTModificationException(childModification);
case INSERT_BEFORE:
throw new UnhandledASTModificationException(childModification);
@ -188,9 +179,7 @@ public class ASTModificationHelper {
throw new UnhandledASTModificationException(childModification);
}
}
}
else
{
} else {
for (ASTModification parentModification : modificationsForNode(decl)) {
if (parentModification.getKind() == ModificationKind.APPEND_CHILD) {
IASTNode newNode = parentModification.getNewNode();
@ -203,7 +192,6 @@ public class ASTModificationHelper {
return initializer;
}
@SuppressWarnings("unchecked")
public <T extends IASTNode> T getNodeAfterReplacement(T replacedNode) {
List<ASTModification> modifications = modificationsForNode(replacedNode);
@ -212,8 +200,7 @@ public class ASTModificationHelper {
if (currentModification.getKind() == ModificationKind.REPLACE) {
return (T) currentModification.getNewNode();
}
}
catch(ClassCastException e){
} catch (ClassCastException e) {
throw new UnhandledASTModificationException(currentModification);
}
}

View file

@ -84,10 +84,8 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
this.fileScope = fileScope;
this.shouldVisitTranslationUnit = true;
this.stack = new ModificationScopeStack(modificationStore);
declaratorWriter = new ModifiedASTDeclaratorWriter(scribe, this,
stack, commentMap);
expWriter = new ModifiedASTExpressionWriter(scribe, this, macroHandler,
stack, commentMap);
declaratorWriter = new ModifiedASTDeclaratorWriter(scribe, this, stack, commentMap);
expWriter = new ModifiedASTExpressionWriter(scribe, this, macroHandler, stack, commentMap);
statementWriter = new ModifiedASTStatementWriter(scribe, this, stack, commentMap);
declSpecWriter = new ModifiedASTDeclSpecWriter(scribe, this, stack, commentMap);
declarationWriter = new ModifiedASTDeclarationWriter(scribe, this, stack, commentMap);

View file

@ -41,10 +41,6 @@ public class ExtractFunctionRefactoringTest extends RefactoringTest {
private boolean virtual;
private static int nr = 1;
/**
* @param name
* @param files
*/
public ExtractFunctionRefactoringTest(String name, Collection<TestSourceFile> files) {
super(name, files);
}