mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-01 06:05:24 +02:00
Comment handling for refactoring by Emanuel Graf, bug 228009.
This commit is contained in:
parent
7686c3bdbc
commit
e4ecf40136
4 changed files with 45 additions and 12 deletions
|
@ -11,18 +11,21 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
|
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
|
||||||
|
|
||||||
import java.util.Vector;
|
import java.util.ArrayList;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTComment;
|
import org.eclipse.cdt.core.dom.ast.IASTComment;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
|
import org.eclipse.cdt.internal.core.dom.rewrite.util.OffsetHelper;
|
||||||
import org.eclipse.core.runtime.IPath;
|
import org.eclipse.core.runtime.IPath;
|
||||||
import org.eclipse.core.runtime.Path;
|
import org.eclipse.core.runtime.Path;
|
||||||
import org.eclipse.core.runtime.Platform;
|
import org.eclipse.core.runtime.Platform;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the startpoint of the whole comment handling process. The creation of the
|
* This is the starting point of the entire comment handling process. The creation of the
|
||||||
* NodeCommentMap is based on the IASTTranslationUnit. From this TranslationUnit the comments
|
* NodeCommentMap is based on the IASTTranslationUnit. From this TranslationUnit the comments
|
||||||
* are extracted and skipped if they belong not to the same workspace. An ASTCommenterVisitor
|
* are extracted and skipped if they belong not to the same workspace. An ASTCommenterVisitor
|
||||||
* is initialized with this collection of comments. And the visit process can start.
|
* is initialized with this collection of comments. And the visit process can start.
|
||||||
|
@ -45,16 +48,25 @@ public class ASTCommenter {
|
||||||
if(transUnit== null) {
|
if(transUnit== null) {
|
||||||
return new NodeCommentMap();
|
return new NodeCommentMap();
|
||||||
}
|
}
|
||||||
Vector<IASTComment> comments = getCommentsInWorkspace(transUnit);
|
ArrayList<IASTComment> comments = removeNotNeededComments(transUnit);
|
||||||
if(comments == null || comments.size() == 0) {
|
if(comments == null || comments.size() == 0) {
|
||||||
return new NodeCommentMap();
|
return new NodeCommentMap();
|
||||||
}
|
}
|
||||||
return addCommentsToCommentMap(transUnit, comments);
|
return addCommentsToCommentMap(transUnit, comments);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Vector<IASTComment> getCommentsInWorkspace(IASTTranslationUnit tu) {
|
private static ArrayList<IASTComment> removeNotNeededComments(IASTTranslationUnit transUnit) {
|
||||||
|
ArrayList<IASTComment> comments = getCommentsInWorkspace(transUnit);
|
||||||
|
if (comments == null || comments.size() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
ArrayList<IASTComment> com = removeAllPreprocessorComments(transUnit, comments);
|
||||||
|
return com;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ArrayList<IASTComment> getCommentsInWorkspace(IASTTranslationUnit tu) {
|
||||||
IASTComment[] comments = tu.getComments();
|
IASTComment[] comments = tu.getComments();
|
||||||
Vector<IASTComment> commentsInWorksapce = new Vector<IASTComment>();
|
ArrayList<IASTComment> commentsInWorksapce = new ArrayList<IASTComment>();
|
||||||
|
|
||||||
if (comments == null || comments.length == 0) {
|
if (comments == null || comments.length == 0) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -68,6 +80,27 @@ public class ASTCommenter {
|
||||||
return commentsInWorksapce;
|
return commentsInWorksapce;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ArrayList<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList<IASTComment> comments) {
|
||||||
|
IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
|
||||||
|
TreeMap<Integer,Object> treeOfPreProcessorLines = new TreeMap<Integer,Object>();
|
||||||
|
|
||||||
|
for (IASTPreprocessorStatement statement : preprocessorStatements) {
|
||||||
|
if (isInWorkspace(statement)) {
|
||||||
|
treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement),null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>();
|
||||||
|
for (IASTComment comment : comments) {
|
||||||
|
int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment);
|
||||||
|
if (treeOfPreProcessorLines.containsKey(comStartLineNumber)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
commentsInCode.add(comment);
|
||||||
|
}
|
||||||
|
return commentsInCode;
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean isInWorkspace(IASTNode node) {
|
private static boolean isInWorkspace(IASTNode node) {
|
||||||
IPath workspacePath = Platform.getLocation();
|
IPath workspacePath = Platform.getLocation();
|
||||||
IPath nodePath = new Path(node.getContainingFilename());
|
IPath nodePath = new Path(node.getContainingFilename());
|
||||||
|
@ -75,7 +108,7 @@ public class ASTCommenter {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static NodeCommentMap addCommentsToCommentMap(IASTTranslationUnit rootNode, Vector<IASTComment> comments){
|
private static NodeCommentMap addCommentsToCommentMap(IASTTranslationUnit rootNode, ArrayList<IASTComment> comments){
|
||||||
NodeCommentMap commentMap = new NodeCommentMap();
|
NodeCommentMap commentMap = new NodeCommentMap();
|
||||||
CommentHandler commHandler = new CommentHandler(comments);
|
CommentHandler commHandler = new CommentHandler(comments);
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
|
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
|
||||||
|
|
||||||
import java.util.Vector;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTComment;
|
import org.eclipse.cdt.core.dom.ast.IASTComment;
|
||||||
|
|
||||||
|
@ -24,9 +24,9 @@ import org.eclipse.cdt.core.dom.ast.IASTComment;
|
||||||
*/
|
*/
|
||||||
public class CommentHandler {
|
public class CommentHandler {
|
||||||
|
|
||||||
private final Vector<IASTComment> comments;
|
private final ArrayList<IASTComment> comments;
|
||||||
|
|
||||||
public CommentHandler(Vector<IASTComment> comments) {
|
public CommentHandler(ArrayList<IASTComment> comments) {
|
||||||
super();
|
super();
|
||||||
this.comments = comments;
|
this.comments = comments;
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,6 @@ public class CommentHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
public IASTComment getFirst() {
|
public IASTComment getFirst() {
|
||||||
return comments.firstElement();
|
return comments.get(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -159,7 +159,7 @@ Refactoring.menu.label= Refac&tor
|
||||||
Refactoring.renameAction.label=Re&name...
|
Refactoring.renameAction.label=Re&name...
|
||||||
Refactoring.extractConstant.label=Extr&act Constant...
|
Refactoring.extractConstant.label=Extr&act Constant...
|
||||||
Refactoring.extractFunction.label=Extract &Function... (work in progress)
|
Refactoring.extractFunction.label=Extract &Function... (work in progress)
|
||||||
Refactoring.hideMethod.label=Hide Member Function... (work in progress)
|
Refactoring.hideMethod.label=Hide Method...
|
||||||
Refactoring.implementMethod.label=Impl&ement Method... (work in progress)
|
Refactoring.implementMethod.label=Impl&ement Method... (work in progress)
|
||||||
Refactoring.gettersAndSetters.label=Generate Getters and Setters...
|
Refactoring.gettersAndSetters.label=Generate Getters and Setters...
|
||||||
|
|
||||||
|
|
|
@ -13,5 +13,5 @@ CRenameAction_label=Rename...
|
||||||
ExtractConstantAction_label=Extract Constant...
|
ExtractConstantAction_label=Extract Constant...
|
||||||
GettersAndSetters_label=Generate Getters and Setters...
|
GettersAndSetters_label=Generate Getters and Setters...
|
||||||
ImplementMethodAction_label=Implement Method... (work in progress)
|
ImplementMethodAction_label=Implement Method... (work in progress)
|
||||||
HideMethodAction_label=Hide Member Function... (work in progress)
|
HideMethodAction_label=Hide Method...
|
||||||
ExtractFunctionAction_label=Extract Function... (work in progress)
|
ExtractFunctionAction_label=Extract Function... (work in progress)
|
||||||
|
|
Loading…
Add table
Reference in a new issue