mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-01 05:15:43 +02:00
Code streamlining.
This commit is contained in:
parent
1d9cb380ee
commit
aefd301b2e
1 changed files with 32 additions and 27 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik
|
* Copyright (c) 2008, 2013 Institute for Software, HSR Hochschule fuer Technik
|
||||||
* Rapperswil, University of applied sciences and others
|
* Rapperswil, University of applied sciences and others
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
@ -8,6 +8,7 @@
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* Institute for Software - initial API and implementation
|
* Institute for Software - initial API and implementation
|
||||||
|
* Sergey Prigogin (Google)
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
|
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
|
||||||
|
|
||||||
|
@ -82,8 +83,8 @@ public class ASTCommenter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isCommentOnSameLine(IASTNode node) {
|
private boolean isCommentOnSameLine(IASTNode node) {
|
||||||
return commentNodeLocation.getStartingLineNumber() == node.getFileLocation()
|
return commentNodeLocation.getStartingLineNumber() ==
|
||||||
.getEndingLineNumber();
|
node.getFileLocation().getEndingLineNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -183,26 +184,18 @@ public class ASTCommenter {
|
||||||
if (commentsArray == null) {
|
if (commentsArray == null) {
|
||||||
return commentMap;
|
return commentMap;
|
||||||
}
|
}
|
||||||
// Note that constructing a real ArrayList is required here, since in filterNonTuComments, the
|
List<IASTComment> comments = filterNonTuComments(commentsArray);
|
||||||
// remove-method will be invoked on the list's iterator. Calling it on the type Arrays$ArrayList (the
|
|
||||||
// resulting type of Arrays.asList() ) would throw a UnsupportedOperationException.
|
|
||||||
ArrayList<IASTComment> comments = new ArrayList<IASTComment>(Arrays.asList(commentsArray));
|
|
||||||
filterNonTuComments(comments);
|
|
||||||
return addCommentsToCommentMap(tu, comments);
|
return addCommentsToCommentMap(tu, comments);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static List<IASTComment> filterNonTuComments(IASTComment[] comments) {
|
||||||
* Note that passing an ArrayList (instead of just List or Collection) is required here, since this
|
List<IASTComment> filtered = new ArrayList<IASTComment>(comments.length);
|
||||||
* guarantees that the call to the remove-method on the list's iterator will not result in an
|
for (IASTComment comment : comments) {
|
||||||
* UnsupportedOperationException which might be the case for other Collection/List types.
|
if (comment.isPartOfTranslationUnitFile()) {
|
||||||
*/
|
filtered.add(comment);
|
||||||
private static void filterNonTuComments(ArrayList<IASTComment> comments) {
|
|
||||||
Iterator<IASTComment> iterator = comments.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
if (!iterator.next().isPartOfTranslationUnitFile()) {
|
|
||||||
iterator.remove();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return filtered;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isCommentDirectlyBeforePreprocessorStatement(IASTComment comment,
|
private static boolean isCommentDirectlyBeforePreprocessorStatement(IASTComment comment,
|
||||||
|
@ -224,8 +217,12 @@ public class ASTCommenter {
|
||||||
return node.isPartOfTranslationUnitFile();
|
return node.isPartOfTranslationUnitFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Puts leading and training comments to the returned map and removes them from
|
||||||
|
* the {@code comments} list.
|
||||||
|
*/
|
||||||
private static NodeCommentMap addCommentsToCommentMap(IASTTranslationUnit tu,
|
private static NodeCommentMap addCommentsToCommentMap(IASTTranslationUnit tu,
|
||||||
ArrayList<IASTComment> comments){
|
List<IASTComment> comments) {
|
||||||
NodeCommentMap commentMap = new NodeCommentMap();
|
NodeCommentMap commentMap = new NodeCommentMap();
|
||||||
CommentHandler commHandler = new CommentHandler(comments);
|
CommentHandler commHandler = new CommentHandler(comments);
|
||||||
|
|
||||||
|
@ -236,12 +233,11 @@ public class ASTCommenter {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Note that passing an ArrayList (instead of just List or Collection) is required here, since this
|
* Puts leading and training comments to {@code commentMap} and removes them from
|
||||||
* guarantees that the call to the remove-method on the list's iterator will not result in an
|
* the {@code comments} list.
|
||||||
* UnsupportedOperationException which might be the case for other Collection/List types.
|
|
||||||
*/
|
*/
|
||||||
private static void assignPreprocessorComments(NodeCommentMap commentMap,
|
private static void assignPreprocessorComments(NodeCommentMap commentMap,
|
||||||
ArrayList<IASTComment> comments, IASTTranslationUnit tu) {
|
List<IASTComment> comments, IASTTranslationUnit tu) {
|
||||||
IASTPreprocessorStatement[] preprocessorStatementsArray = tu.getAllPreprocessorStatements();
|
IASTPreprocessorStatement[] preprocessorStatementsArray = tu.getAllPreprocessorStatements();
|
||||||
if (preprocessorStatementsArray == null) {
|
if (preprocessorStatementsArray == null) {
|
||||||
return;
|
return;
|
||||||
|
@ -252,6 +248,7 @@ public class ASTCommenter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<IASTComment> freestandingComments = new ArrayList<IASTComment>(comments.size());
|
||||||
Iterator<IASTPreprocessorStatement> statementsIter = preprocessorStatements.iterator();
|
Iterator<IASTPreprocessorStatement> statementsIter = preprocessorStatements.iterator();
|
||||||
Iterator<IASTComment> commentIter = comments.iterator();
|
Iterator<IASTComment> commentIter = comments.iterator();
|
||||||
IASTPreprocessorStatement curStatement = getNextNodeInTu(statementsIter);
|
IASTPreprocessorStatement curStatement = getNextNodeInTu(statementsIter);
|
||||||
|
@ -261,18 +258,26 @@ public class ASTCommenter {
|
||||||
int commentLineNr = curComment.getFileLocation().getStartingLineNumber();
|
int commentLineNr = curComment.getFileLocation().getStartingLineNumber();
|
||||||
if (commentLineNr == statementLineNr) {
|
if (commentLineNr == statementLineNr) {
|
||||||
commentMap.addTrailingCommentToNode(curStatement, curComment);
|
commentMap.addTrailingCommentToNode(curStatement, curComment);
|
||||||
commentIter.remove();
|
|
||||||
curComment = getNextNodeInTu(commentIter);
|
curComment = getNextNodeInTu(commentIter);
|
||||||
} else if (commentLineNr > statementLineNr) {
|
} else if (commentLineNr > statementLineNr) {
|
||||||
curStatement = getNextNodeInTu(statementsIter);
|
curStatement = getNextNodeInTu(statementsIter);
|
||||||
} else if (isCommentDirectlyBeforePreprocessorStatement(curComment, curStatement, tu)) {
|
} else if (isCommentDirectlyBeforePreprocessorStatement(curComment, curStatement, tu)) {
|
||||||
commentMap.addLeadingCommentToNode(curStatement, curComment);
|
commentMap.addLeadingCommentToNode(curStatement, curComment);
|
||||||
commentIter.remove();
|
|
||||||
curComment = getNextNodeInTu(commentIter);
|
curComment = getNextNodeInTu(commentIter);
|
||||||
} else {
|
} else {
|
||||||
|
freestandingComments.add(curComment);
|
||||||
curComment = getNextNodeInTu(commentIter);
|
curComment = getNextNodeInTu(commentIter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
while (curComment != null) {
|
||||||
|
freestandingComments.add(curComment);
|
||||||
|
curComment = getNextNodeInTu(commentIter);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (freestandingComments.size() != comments.size()) {
|
||||||
|
comments.clear();
|
||||||
|
comments.addAll(freestandingComments);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T extends IASTNode> T getNextNodeInTu(Iterator<T> iter) {
|
private static <T extends IASTNode> T getNextNodeInTu(Iterator<T> iter) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue