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

Code cleanup.

This commit is contained in:
Sergey Prigogin 2011-12-14 00:48:19 -08:00
parent b8ac132da4
commit 8b3ce7c39a
8 changed files with 103 additions and 350 deletions

View file

@ -74,7 +74,6 @@ import org.eclipse.core.runtime.CoreException;
* @author Guido Zgraggen IFS, Lukas Felber IFS * @author Guido Zgraggen IFS, Lukas Felber IFS
*/ */
public class CommentHandlingTest extends RewriteBaseTest { public class CommentHandlingTest extends RewriteBaseTest {
private static final String ANY_CHAR_REGEXP = "(.*)"; //$NON-NLS-1$ private static final String ANY_CHAR_REGEXP = "(.*)"; //$NON-NLS-1$
private static final String SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$ private static final String SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
@ -92,7 +91,7 @@ public class CommentHandlingTest extends RewriteBaseTest {
@Override @Override
protected void runTest() throws Throwable { protected void runTest() throws Throwable {
if (fileMap.size() == 0) { if (fileMap.isEmpty()) {
fail("No file for testing"); //$NON-NLS-1$ fail("No file for testing"); //$NON-NLS-1$
} }

View file

@ -41,9 +41,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTWhileStatement; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTWhileStatement;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousStatement; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousStatement;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
import org.eclipse.cdt.internal.core.dom.rewrite.util.FileContentHelper;
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 * Generates source code of statement nodes. The actual string operations are delegated
@ -434,11 +431,7 @@ public class StatementWriter extends NodeWriter {
} }
protected int writeMixedStatement(IASTStatement statement) { protected int writeMixedStatement(IASTStatement statement) {
IFile file = FileHelper.getFileFromNode(statement); String code = statement.getRawSignature();
int offset = statement.getFileLocation().getNodeOffset();
int length = statement.getFileLocation().getNodeLength();
String code = FileContentHelper.getContent(file, offset, length);
scribe.println(code); scribe.println(code);
return ASTVisitor.PROCESS_SKIP; return ASTVisitor.PROCESS_SKIP;
} }

View file

@ -13,7 +13,6 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator; package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -36,7 +35,6 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList; import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTInitializer; import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
@ -48,10 +46,8 @@ import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId; import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionWithTryBlock;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.formatter.CodeFormatter; import org.eclipse.cdt.core.formatter.CodeFormatter;
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
@ -64,13 +60,12 @@ import org.eclipse.cdt.internal.core.dom.rewrite.ASTRewriteAnalyzer;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter; import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ProblemRuntimeException; import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ProblemRuntimeException;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
import org.eclipse.cdt.internal.core.dom.rewrite.util.FileContentHelper;
import org.eclipse.cdt.internal.core.dom.rewrite.util.FileHelper; import org.eclipse.cdt.internal.core.dom.rewrite.util.FileHelper;
import org.eclipse.cdt.internal.core.resources.ResourceLookup; import org.eclipse.cdt.internal.core.resources.ResourceLookup;
import org.eclipse.cdt.internal.formatter.CCodeFormatter; import org.eclipse.cdt.internal.formatter.CCodeFormatter;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
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.jface.text.BadLocationException; import org.eclipse.jface.text.BadLocationException;
@ -133,7 +128,7 @@ public class ChangeGenerator extends ASTVisitor {
rootNode.accept(pathProvider); rootNode.accept(pathProvider);
for (IFile currentFile : changes.keySet()) { for (IFile currentFile : changes.keySet()) {
MultiTextEdit edit = changes.get(currentFile); MultiTextEdit edit = changes.get(currentFile);
edit = formatChangedCode(edit, currentFile); edit = formatChangedCode(edit, rootNode.getTranslationUnit().getRawSignature(), currentFile.getProject());
TextFileChange subchange= ASTRewriteAnalyzer.createCTextFileChange(currentFile); TextFileChange subchange= ASTRewriteAnalyzer.createCTextFileChange(currentFile);
subchange.setEdit(edit); subchange.setEdit(edit);
change.add(subchange); change.add(subchange);
@ -325,21 +320,12 @@ public class ChangeGenerator extends ASTVisitor {
* Applies the C++ code formatter to the code affected by refactoring. * Applies the C++ code formatter to the code affected by refactoring.
* *
* @param multiEdit The text edit produced by refactoring. * @param multiEdit The text edit produced by refactoring.
* @param file The file being modified. * @param code The code being modified.
* @param project The project containing the code.
* @return The text edit containing formatted refactoring changes, or the original text edit * @return The text edit containing formatted refactoring changes, or the original text edit
* in case of errors. * in case of errors.
*/ */
private MultiTextEdit formatChangedCode(MultiTextEdit multiEdit, IFile file) { private MultiTextEdit formatChangedCode(MultiTextEdit multiEdit, String code, IProject project) {
String code;
try {
code = FileContentHelper.getContent(file, 0);
} catch (IOException e) {
CCorePlugin.log(e);
return multiEdit;
} catch (CoreException e) {
CCorePlugin.log(e);
return multiEdit;
}
IDocument document = new Document(code); IDocument document = new Document(code);
try { try {
// Apply refactoring changes to a temporary document. // Apply refactoring changes to a temporary document.
@ -382,8 +368,8 @@ public class ChangeGenerator extends ASTVisitor {
} }
// Calculate formatting changes for the regions after the refactoring changes. // Calculate formatting changes for the regions after the refactoring changes.
ICProject project = CCorePlugin.getDefault().getCoreModel().create(file.getProject()); ICProject proj = CCorePlugin.getDefault().getCoreModel().create(project);
Map<String, String> options = project.getOptions(true); Map<String, String> options = proj.getOptions(true);
// Allow all comments to be indented. // Allow all comments to be indented.
options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN,
DefaultCodeFormatterConstants.FALSE); DefaultCodeFormatterConstants.FALSE);
@ -599,8 +585,7 @@ public class ChangeGenerator extends ASTVisitor {
IASTDeclaration lastDecl = targetTu.getDeclarations()[targetTu.getDeclarations().length - 1]; IASTDeclaration lastDecl = targetTu.getDeclarations()[targetTu.getDeclarations().length - 1];
targetLocation = lastDecl.getFileLocation(); targetLocation = lastDecl.getFileLocation();
} }
String lineDelimiter = FileHelper.determineLineDelimiter( String lineDelimiter = FileHelper.determineLineDelimiter(tu.getRawSignature());
FileHelper.getFileFromNode(targetNode));
edit.addChild(new InsertEdit(endOffset(targetLocation), lineDelimiter + lineDelimiter + code)); edit.addChild(new InsertEdit(endOffset(targetLocation), lineDelimiter + lineDelimiter + code));
} }
} }
@ -941,15 +926,15 @@ public class ChangeGenerator extends ASTVisitor {
} }
private int getOffsetIncludingComments(IASTNode node) { private int getOffsetIncludingComments(IASTNode node) {
int nodeOffset = node.getFileLocation().getNodeOffset(); int nodeOffset = offset(node);
List<IASTComment> comments = commentMap.getAllCommentsForNode(node); List<IASTComment> comments = commentMap.getAllCommentsForNode(node);
if (!comments.isEmpty()) { if (!comments.isEmpty()) {
int startOffset = nodeOffset; int startOffset = nodeOffset;
for (IASTComment comment : comments) { for (IASTComment comment : comments) {
IASTFileLocation commentLocation = comment.getFileLocation(); int commentOffset = offset(comment);
if (commentLocation.getNodeOffset() < startOffset) { if (commentOffset < startOffset) {
startOffset = commentLocation.getNodeOffset(); startOffset = commentOffset;
} }
} }
nodeOffset = startOffset; nodeOffset = startOffset;
@ -958,44 +943,47 @@ public class ChangeGenerator extends ASTVisitor {
} }
private int getEndOffsetIncludingComments(IASTNode node) { private int getEndOffsetIncludingComments(IASTNode node) {
IASTFileLocation nodeLocation = node.getFileLocation(); int endOffset = 0;
int endOffset = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength(); while (true) {
IASTFileLocation fileLocation = node.getFileLocation();
List<IASTComment> comments = commentMap.getAllCommentsForNode(node); if (fileLocation != null)
if (!comments.isEmpty()) { endOffset = Math.max(endOffset, endOffset(fileLocation));
for (IASTComment comment : comments) { List<IASTComment> comments = commentMap.getAllCommentsForNode(node);
int commentEndOffset = endOffset(comment.getFileLocation()); if (!comments.isEmpty()) {
if (commentEndOffset >= endOffset) { for (IASTComment comment : comments) {
endOffset = commentEndOffset; int commentEndOffset = endOffset(comment);
if (commentEndOffset >= endOffset) {
endOffset = commentEndOffset;
}
} }
} }
IASTNode[] children = node.getChildren();
if (children.length == 0)
break;
node = children[children.length - 1];
} }
return endOffset; return endOffset;
} }
private int getEndOffsetIncludingTrailingComments(IASTNode node) { private int getEndOffsetIncludingTrailingComments(IASTNode node) {
IASTFileLocation nodeLocation = node.getFileLocation(); int endOffset = 0;
int endOffset = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength(); while (true) {
IASTFileLocation fileLocation = node.getFileLocation();
List<IASTComment> comments = commentMap.getTrailingCommentsForNode(node); if (fileLocation != null)
if (!comments.isEmpty()) { endOffset = Math.max(endOffset, endOffset(fileLocation));
for (IASTComment comment : comments) { List<IASTComment> comments = commentMap.getTrailingCommentsForNode(node);
int commentEndOffset = endOffset(comment.getFileLocation()); if (!comments.isEmpty()) {
if (commentEndOffset >= endOffset) { for (IASTComment comment : comments) {
endOffset = commentEndOffset; int commentEndOffset = endOffset(comment);
if (commentEndOffset >= endOffset) {
endOffset = commentEndOffset;
}
} }
} }
} IASTNode[] children = node.getChildren();
// TODO(sprigogin): Remove when comments are always assigned to the outermost nodes. if (children.length == 0)
if (node instanceof ICPPASTFunctionWithTryBlock) { break;
ICPPASTCatchHandler[] catchHandlers = ((ICPPASTFunctionWithTryBlock) node).getCatchHandlers(); node = children[children.length - 1];
if (catchHandlers.length > 0) {
endOffset = Math.max(endOffset,
getEndOffsetIncludingTrailingComments(catchHandlers[catchHandlers.length - 1]));
}
} else if (node instanceof IASTFunctionDefinition) {
endOffset = Math.max(endOffset,
getEndOffsetIncludingTrailingComments(((IASTFunctionDefinition) node).getBody()));
} }
return endOffset; return endOffset;
} }

View file

@ -23,6 +23,7 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTInitializer; import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
@ -37,7 +38,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.rewrite.util.OffsetHelper;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
@ -215,7 +215,7 @@ public class ASTCommenter {
for (IASTPreprocessorStatement statement : preprocessorStatements) { for (IASTPreprocessorStatement statement : preprocessorStatements) {
if (isInWorkspace(statement)) { if (isInWorkspace(statement)) {
String fileName = statement.getFileLocation().getFileName(); String fileName = statement.getFileLocation().getFileName();
treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement), fileName); treeOfPreProcessorLines.put(statement.getFileLocation().getStartingLineNumber(), fileName);
ArrayList<Integer> offsetList = ppOffsetForFiles.get(fileName); ArrayList<Integer> offsetList = ppOffsetForFiles.get(fileName);
if (offsetList == null) { if (offsetList == null) {
offsetList = new ArrayList<Integer>(); offsetList = new ArrayList<Integer>();
@ -227,8 +227,9 @@ public class ASTCommenter {
ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>(); ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>();
for (IASTComment comment : comments) { for (IASTComment comment : comments) {
int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment); IASTFileLocation commentFileLocation = comment.getFileLocation();
String fileName = comment.getFileLocation().getFileName(); int comStartLineNumber = commentFileLocation.getStartingLineNumber();
String fileName = commentFileLocation.getFileName();
if (treeOfPreProcessorLines.containsKey(comStartLineNumber) if (treeOfPreProcessorLines.containsKey(comStartLineNumber)
&& treeOfPreProcessorLines.get(comStartLineNumber).equals(fileName)) { && treeOfPreProcessorLines.get(comStartLineNumber).equals(fileName)) {
continue; continue;
@ -248,7 +249,7 @@ public class ASTCommenter {
return false; return false;
} }
if (comment.getTranslationUnit()==null || comment.getTranslationUnit().getDeclarations().length < 1) { if (comment.getTranslationUnit() == null || comment.getTranslationUnit().getDeclarations().length < 1) {
return true; return true;
} }
IASTDeclaration decl = comment.getTranslationUnit().getDeclarations()[0]; IASTDeclaration decl = comment.getTranslationUnit().getDeclarations()[0];

View file

@ -8,19 +8,20 @@
* *
* 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;
import java.io.InputStream;
import java.util.Vector; import java.util.Vector;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
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.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarationStatement; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarationStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExplicitTemplateInstantiation; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExplicitTemplateInstantiation;
@ -32,11 +33,6 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLinkageSpecification;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSwitchStatement; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSwitchStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateDeclaration; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTWhileStatement; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTWhileStatement;
import org.eclipse.cdt.internal.core.dom.rewrite.util.OffsetHelper;
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/** /**
* The NodeCommenter contains all the logic that is needed for the ASTCommentVisitor to assign * The NodeCommenter contains all the logic that is needed for the ASTCommentVisitor to assign
@ -96,10 +92,10 @@ public class NodeCommenter {
return false; return false;
} }
int nodeLineNumber = OffsetHelper.getEndingLineNumber(node); int nodeLineNumber = getEndingLineNumber(node);
int commentLineNumber= OffsetHelper.getStartingLineNumber(comment); int commentLineNumber= getStartingLineNumber(comment);
if (OffsetHelper.getNodeEndPoint(com) <= OffsetHelper.getNodeOffset(node)) { if (getNodeEndPoint(com) <= getNodeOffset(node)) {
addLeadingCommentToMap(node, comment); addLeadingCommentToMap(node, comment);
return true; return true;
} else if (isTrailing(node, com, nodeLineNumber, commentLineNumber)) { } else if (isTrailing(node, com, nodeLineNumber, commentLineNumber)) {
@ -116,7 +112,7 @@ public class NodeCommenter {
// MacroExpansions have no FileLocation // MacroExpansions have no FileLocation
return false; return false;
} }
if (OffsetHelper.getNodeEndPoint(com) <= OffsetHelper.getNodeEndPoint(node)) { if (getNodeEndPoint(com) <= getNodeEndPoint(node)) {
addFreestandingCommentToMap(node, comment); addFreestandingCommentToMap(node, comment);
return true; return true;
} }
@ -139,47 +135,23 @@ public class NodeCommenter {
} }
private boolean isTrailing(ASTNode node, ASTNode com, int nodeLineNumber, int commentLineNumber) { private boolean isTrailing(ASTNode node, ASTNode com, int nodeLineNumber, int commentLineNumber) {
if (nodeLineNumber == commentLineNumber if (nodeLineNumber != commentLineNumber ||
&& OffsetHelper.getNodeOffset(com) >= OffsetHelper.getNodeEndPoint(node) getNodeOffset(com) < getNodeEndPoint(node) ||
&& canNotBeAddedToParent(node,com) !canNotBeAddedToParent(node, com) ||
&& !mustBeAddToSubnodes(node)) { mustBeAddedToSubnodes(node)) {
if (OffsetHelper.getNodeOffset(com) < OffsetHelper.getNodeEndPoint(node) + 2) { return false;
return true;
}
IPath path = new Path(node.getContainingFilename());
IFile file = ResourceLookup.selectFileForLocation(path, null); // NPE thrown below, like original behavior
//XXX HSR Guido: Possible Performance Issue (File access)
try {
InputStream is = file.getContents();
int length = OffsetHelper.getNodeOffset(com) - OffsetHelper.getNodeEndPoint(node);
byte[] b = new byte[length];
long count = is.skip(OffsetHelper.getEndOffsetWithoutComments(node));
if (count < OffsetHelper.getEndOffsetWithoutComments(node)) {
return false;
}
if (is.read(b, 0, length) == -1) {
return false;
}
for (byte bb : b) {
if (!Character.isWhitespace(bb)) {
is.close();
return false;
}
}
is.close();
return true;
} catch (Exception e) {
return false;
}
} }
if (getNodeOffset(com) < getNodeEndPoint(node) + 2) {
return false; return true;
}
String code = node.getTranslationUnit().getRawSignature();
int commentOffset = getNodeOffset(com) - getNodeEndPoint(node) + getNodeEndOffset(node);
for (int offset = getNodeEndOffset(node); offset < commentOffset; offset++) {
if (!Character.isWhitespace(code.charAt(offset)))
return false;
}
return true;
} }
private boolean canNotBeAddedToParent(ASTNode node, ASTNode com) { private boolean canNotBeAddedToParent(ASTNode node, ASTNode com) {
ASTNode parent = (ASTNode) node.getParent(); ASTNode parent = (ASTNode) node.getParent();
@ -194,10 +166,10 @@ public class NodeCommenter {
} else if (parent instanceof ICPPASTBaseSpecifier) { } else if (parent instanceof ICPPASTBaseSpecifier) {
parent = (ASTNode) parent.getParent(); parent = (ASTNode) parent.getParent();
} }
return !(OffsetHelper.getNodeOffset(com) >= OffsetHelper.getNodeEndPoint(parent)); return getNodeOffset(com) < getNodeEndPoint(parent);
} }
private boolean mustBeAddToSubnodes(ASTNode node) { private boolean mustBeAddedToSubnodes(ASTNode node) {
return hasNodeSameEndingAsSubnode(node); return hasNodeSameEndingAsSubnode(node);
} }
@ -276,4 +248,25 @@ public class NodeCommenter {
} }
return !node.getFileLocation().getFileName().equals(comment.getFileLocation().getFileName()); return !node.getFileLocation().getFileName().equals(comment.getFileLocation().getFileName());
} }
private static int getNodeEndOffset(IASTNode node) {
IASTFileLocation fileLocation = node.getFileLocation();
return fileLocation.getNodeOffset() + fileLocation.getNodeLength();
}
private static int getNodeOffset(ASTNode node) {
return node.getOffset();
}
private static int getNodeEndPoint(ASTNode node) {
return node.getOffset() + node.getLength();
}
private static int getStartingLineNumber(IASTNode node) {
return node.getFileLocation().getStartingLineNumber();
}
private static int getEndingLineNumber(IASTNode node) {
return node.getFileLocation().getEndingLineNumber();
}
} }

View file

@ -1,94 +0,0 @@
/*******************************************************************************
* Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software - initial API and implementation
******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
/**
* @author Emanuel Graf IFS
*/
public class FileContentHelper {
private static final int BUFFER_SIZE = 2048;
public static String getContent(IFile file, int start) throws CoreException, IOException {
InputStreamReader reader = getReaderForFile(file);
skip(start, reader);
final String rest = readRest(reader);
reader.close();
return rest;
}
public static String getContent(IFile file, int start, int length) {
try {
InputStreamReader r = getReaderForFile(file);
char[] chars = new char[length];
skip(start, r);
read(length, r, chars);
r.close();
return new String(chars);
} catch (IOException e) {
CCorePlugin.log(e);
} catch (CoreException e) {
CCorePlugin.log(e);
}
return ""; //$NON-NLS-1$
}
private static InputStreamReader getReaderForFile(IFile file)
throws CoreException, UnsupportedEncodingException {
InputStream contents = file.getContents();
return new InputStreamReader(contents, file.getCharset());
}
private static String readRest(InputStreamReader reader) throws IOException {
StringBuilder content = new StringBuilder();
char[] buffer = new char[BUFFER_SIZE];
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) >= 0) {
content.append(buffer, 0, bytesRead);
}
return content.toString();
}
private static void read(int length, InputStreamReader r, char[] bytes) throws IOException {
int bufferOffset = 0;
int charactersRead = 0;
while (charactersRead >= 0 && length > 0) {
charactersRead = r.read(bytes, bufferOffset, length);
if (charactersRead > 0) {
bufferOffset += charactersRead;
length -= charactersRead;
}
}
}
private static void skip(int start, InputStreamReader r) throws IOException {
long skipped = 0;
while (skipped >= 0 && start > 0 && r.ready()) {
skipped = r.skip(start);
if (skipped > 0) {
start -= skipped;
}
}
}
}

View file

@ -11,21 +11,12 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.util; package org.eclipse.cdt.internal.core.dom.rewrite.util;
import java.io.IOException;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.internal.core.resources.ResourceLookup; import org.eclipse.cdt.internal.core.resources.ResourceLookup;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
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;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.text.TextUtilities; import org.eclipse.jface.text.TextUtilities;
public class FileHelper { public class FileHelper {
@ -36,36 +27,13 @@ public class FileHelper {
return ResourceLookup.selectFileForLocation(implPath, null); return ResourceLookup.selectFileForLocation(implPath, null);
} }
public static boolean isFirstWithinSecondLocation(IASTFileLocation loc1, IASTFileLocation loc2) { public static String determineLineDelimiter(String text) {
boolean isEquals = true;
isEquals &= loc1.getFileName().equals(loc2.getFileName());
isEquals &= loc1.getNodeOffset() >= loc2.getNodeOffset();
isEquals &= loc1.getNodeOffset() + loc1.getNodeLength() <= loc2.getNodeOffset()
+ loc2.getNodeLength();
return isEquals;
}
public static String determineLineDelimiter(IFile file) {
String fileContent = ""; //$NON-NLS-1$
try {
fileContent = FileContentHelper.getContent(file, 0);
} catch (CoreException e) {
} catch (IOException e) {
}
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject();
IScopeContext[] scopeContext;
if (project != null) {
scopeContext = new IScopeContext[] { new ProjectScope(project) };
} else {
scopeContext = new IScopeContext[] { InstanceScope.INSTANCE };
}
String platformDefaultLineDelimiter = System.getProperty("line.separator", DEFAULT_LINE_DELIMITTER); //$NON-NLS-1$ String platformDefaultLineDelimiter = System.getProperty("line.separator", DEFAULT_LINE_DELIMITTER); //$NON-NLS-1$
String defaultLineDelimiter = Platform.getPreferencesService().getString(Platform.PI_RUNTIME, String defaultLineDelimiter = Platform.getPreferencesService().getString(Platform.PI_RUNTIME,
Platform.PREF_LINE_SEPARATOR, platformDefaultLineDelimiter, scopeContext); Platform.PREF_LINE_SEPARATOR, platformDefaultLineDelimiter, null);
return TextUtilities.determineLineDelimiter(fileContent.toString(), defaultLineDelimiter); if (text.isEmpty()) {
return defaultLineDelimiter;
}
return TextUtilities.determineLineDelimiter(text, defaultLineDelimiter);
} }
} }

View file

@ -1,95 +0,0 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software - initial API and implementation
******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.util;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
/**
* @author Emanuel Graf IFS
*/
public class OffsetHelper {
public static int getOffsetIncludingComment(IASTNode node) {
int nodeStart = Integer.MAX_VALUE;
IASTNodeLocation[] nodeLocations = node.getNodeLocations();
if (nodeLocations.length != 1) {
int offset;
for (IASTNodeLocation location : nodeLocations) {
if (location instanceof IASTMacroExpansionLocation) {
IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location;
offset = macroLoc.asFileLocation().getNodeOffset();
} else {
offset = location.asFileLocation().getNodeOffset();
}
if (offset < nodeStart)
nodeStart = offset;
}
} else {
nodeStart = node.getFileLocation().getNodeOffset();
}
return nodeStart;
}
public static int getEndOffsetIncludingComments(IASTNode node) {
int fileOffset = 0;
int length = 0;
IASTNodeLocation[] nodeLocations = node.getNodeLocations();
if (nodeLocations.length != 1) {
for (IASTNodeLocation location : nodeLocations) {
if (location instanceof IASTMacroExpansionLocation) {
IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location;
fileOffset = macroLoc.asFileLocation().getNodeOffset();
length = macroLoc.asFileLocation().getNodeLength();
} else {
fileOffset = location.asFileLocation().getNodeOffset();
length = location.asFileLocation().getNodeLength();
}
}
} else {
IASTFileLocation loc = node.getFileLocation();
fileOffset = loc.getNodeOffset();
length = loc.getNodeLength();
}
return fileOffset + length;
}
public static int getEndOffsetWithoutComments(IASTNode node) {
return node.getFileLocation().getNodeOffset() + node.getFileLocation().getNodeLength();
}
public static int getLengthIncludingComment(IASTNode node) {
return getEndOffsetIncludingComments(node) - getOffsetIncludingComment(node);
}
public static int getNodeOffset(ASTNode node) {
return node.getOffset();
}
public static int getNodeEndPoint(ASTNode node) {
return node.getOffset() + node.getLength();
}
public static int getStartingLineNumber(IASTNode node) {
return node.getFileLocation().getStartingLineNumber();
}
public static int getEndingLineNumber(IASTNode node) {
return node.getFileLocation().getEndingLineNumber();
}
}