mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Code cleanup.
This commit is contained in:
parent
b8ac132da4
commit
8b3ce7c39a
8 changed files with 103 additions and 350 deletions
|
@ -74,7 +74,6 @@ import org.eclipse.core.runtime.CoreException;
|
|||
* @author Guido Zgraggen IFS, Lukas Felber IFS
|
||||
*/
|
||||
public class CommentHandlingTest extends RewriteBaseTest {
|
||||
|
||||
private static final String ANY_CHAR_REGEXP = "(.*)"; //$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
|
||||
protected void runTest() throws Throwable {
|
||||
if (fileMap.size() == 0) {
|
||||
if (fileMap.isEmpty()) {
|
||||
fail("No file for testing"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
|
|
|
@ -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.internal.core.dom.parser.IASTAmbiguousStatement;
|
||||
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
|
||||
|
@ -434,11 +431,7 @@ public class StatementWriter extends NodeWriter {
|
|||
}
|
||||
|
||||
protected int writeMixedStatement(IASTStatement statement) {
|
||||
IFile file = FileHelper.getFileFromNode(statement);
|
||||
int offset = statement.getFileLocation().getNodeOffset();
|
||||
int length = statement.getFileLocation().getNodeLength();
|
||||
String code = FileContentHelper.getContent(file, offset, length);
|
||||
|
||||
String code = statement.getRawSignature();
|
||||
scribe.println(code);
|
||||
return ASTVisitor.PROCESS_SKIP;
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
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.IASTExpressionList;
|
||||
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.IASTName;
|
||||
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.IASTTranslationUnit;
|
||||
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.ICPPASTFunctionDefinition;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionWithTryBlock;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
|
||||
import org.eclipse.cdt.core.formatter.CodeFormatter;
|
||||
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.ProblemRuntimeException;
|
||||
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.resources.ResourceLookup;
|
||||
import org.eclipse.cdt.internal.formatter.CCodeFormatter;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.runtime.Assert;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
|
@ -133,7 +128,7 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
rootNode.accept(pathProvider);
|
||||
for (IFile currentFile : changes.keySet()) {
|
||||
MultiTextEdit edit = changes.get(currentFile);
|
||||
edit = formatChangedCode(edit, currentFile);
|
||||
edit = formatChangedCode(edit, rootNode.getTranslationUnit().getRawSignature(), currentFile.getProject());
|
||||
TextFileChange subchange= ASTRewriteAnalyzer.createCTextFileChange(currentFile);
|
||||
subchange.setEdit(edit);
|
||||
change.add(subchange);
|
||||
|
@ -325,21 +320,12 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
* Applies the C++ code formatter to the code affected 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
|
||||
* in case of errors.
|
||||
*/
|
||||
private MultiTextEdit formatChangedCode(MultiTextEdit multiEdit, IFile file) {
|
||||
String code;
|
||||
try {
|
||||
code = FileContentHelper.getContent(file, 0);
|
||||
} catch (IOException e) {
|
||||
CCorePlugin.log(e);
|
||||
return multiEdit;
|
||||
} catch (CoreException e) {
|
||||
CCorePlugin.log(e);
|
||||
return multiEdit;
|
||||
}
|
||||
private MultiTextEdit formatChangedCode(MultiTextEdit multiEdit, String code, IProject project) {
|
||||
IDocument document = new Document(code);
|
||||
try {
|
||||
// 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.
|
||||
ICProject project = CCorePlugin.getDefault().getCoreModel().create(file.getProject());
|
||||
Map<String, String> options = project.getOptions(true);
|
||||
ICProject proj = CCorePlugin.getDefault().getCoreModel().create(project);
|
||||
Map<String, String> options = proj.getOptions(true);
|
||||
// Allow all comments to be indented.
|
||||
options.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN,
|
||||
DefaultCodeFormatterConstants.FALSE);
|
||||
|
@ -599,8 +585,7 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
IASTDeclaration lastDecl = targetTu.getDeclarations()[targetTu.getDeclarations().length - 1];
|
||||
targetLocation = lastDecl.getFileLocation();
|
||||
}
|
||||
String lineDelimiter = FileHelper.determineLineDelimiter(
|
||||
FileHelper.getFileFromNode(targetNode));
|
||||
String lineDelimiter = FileHelper.determineLineDelimiter(tu.getRawSignature());
|
||||
edit.addChild(new InsertEdit(endOffset(targetLocation), lineDelimiter + lineDelimiter + code));
|
||||
}
|
||||
}
|
||||
|
@ -941,15 +926,15 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
}
|
||||
|
||||
private int getOffsetIncludingComments(IASTNode node) {
|
||||
int nodeOffset = node.getFileLocation().getNodeOffset();
|
||||
int nodeOffset = offset(node);
|
||||
|
||||
List<IASTComment> comments = commentMap.getAllCommentsForNode(node);
|
||||
if (!comments.isEmpty()) {
|
||||
int startOffset = nodeOffset;
|
||||
for (IASTComment comment : comments) {
|
||||
IASTFileLocation commentLocation = comment.getFileLocation();
|
||||
if (commentLocation.getNodeOffset() < startOffset) {
|
||||
startOffset = commentLocation.getNodeOffset();
|
||||
int commentOffset = offset(comment);
|
||||
if (commentOffset < startOffset) {
|
||||
startOffset = commentOffset;
|
||||
}
|
||||
}
|
||||
nodeOffset = startOffset;
|
||||
|
@ -958,44 +943,47 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
}
|
||||
|
||||
private int getEndOffsetIncludingComments(IASTNode node) {
|
||||
IASTFileLocation nodeLocation = node.getFileLocation();
|
||||
int endOffset = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
|
||||
|
||||
List<IASTComment> comments = commentMap.getAllCommentsForNode(node);
|
||||
if (!comments.isEmpty()) {
|
||||
for (IASTComment comment : comments) {
|
||||
int commentEndOffset = endOffset(comment.getFileLocation());
|
||||
if (commentEndOffset >= endOffset) {
|
||||
endOffset = commentEndOffset;
|
||||
int endOffset = 0;
|
||||
while (true) {
|
||||
IASTFileLocation fileLocation = node.getFileLocation();
|
||||
if (fileLocation != null)
|
||||
endOffset = Math.max(endOffset, endOffset(fileLocation));
|
||||
List<IASTComment> comments = commentMap.getAllCommentsForNode(node);
|
||||
if (!comments.isEmpty()) {
|
||||
for (IASTComment comment : comments) {
|
||||
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;
|
||||
}
|
||||
|
||||
private int getEndOffsetIncludingTrailingComments(IASTNode node) {
|
||||
IASTFileLocation nodeLocation = node.getFileLocation();
|
||||
int endOffset = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
|
||||
|
||||
List<IASTComment> comments = commentMap.getTrailingCommentsForNode(node);
|
||||
if (!comments.isEmpty()) {
|
||||
for (IASTComment comment : comments) {
|
||||
int commentEndOffset = endOffset(comment.getFileLocation());
|
||||
if (commentEndOffset >= endOffset) {
|
||||
endOffset = commentEndOffset;
|
||||
int endOffset = 0;
|
||||
while (true) {
|
||||
IASTFileLocation fileLocation = node.getFileLocation();
|
||||
if (fileLocation != null)
|
||||
endOffset = Math.max(endOffset, endOffset(fileLocation));
|
||||
List<IASTComment> comments = commentMap.getTrailingCommentsForNode(node);
|
||||
if (!comments.isEmpty()) {
|
||||
for (IASTComment comment : comments) {
|
||||
int commentEndOffset = endOffset(comment);
|
||||
if (commentEndOffset >= endOffset) {
|
||||
endOffset = commentEndOffset;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO(sprigogin): Remove when comments are always assigned to the outermost nodes.
|
||||
if (node instanceof ICPPASTFunctionWithTryBlock) {
|
||||
ICPPASTCatchHandler[] catchHandlers = ((ICPPASTFunctionWithTryBlock) node).getCatchHandlers();
|
||||
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()));
|
||||
IASTNode[] children = node.getChildren();
|
||||
if (children.length == 0)
|
||||
break;
|
||||
node = children[children.length - 1];
|
||||
}
|
||||
return endOffset;
|
||||
}
|
||||
|
|
|
@ -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.IASTDeclarator;
|
||||
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.IASTName;
|
||||
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.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
|
||||
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.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
|
@ -215,7 +215,7 @@ public class ASTCommenter {
|
|||
for (IASTPreprocessorStatement statement : preprocessorStatements) {
|
||||
if (isInWorkspace(statement)) {
|
||||
String fileName = statement.getFileLocation().getFileName();
|
||||
treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement), fileName);
|
||||
treeOfPreProcessorLines.put(statement.getFileLocation().getStartingLineNumber(), fileName);
|
||||
ArrayList<Integer> offsetList = ppOffsetForFiles.get(fileName);
|
||||
if (offsetList == null) {
|
||||
offsetList = new ArrayList<Integer>();
|
||||
|
@ -227,8 +227,9 @@ public class ASTCommenter {
|
|||
|
||||
ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>();
|
||||
for (IASTComment comment : comments) {
|
||||
int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment);
|
||||
String fileName = comment.getFileLocation().getFileName();
|
||||
IASTFileLocation commentFileLocation = comment.getFileLocation();
|
||||
int comStartLineNumber = commentFileLocation.getStartingLineNumber();
|
||||
String fileName = commentFileLocation.getFileName();
|
||||
if (treeOfPreProcessorLines.containsKey(comStartLineNumber)
|
||||
&& treeOfPreProcessorLines.get(comStartLineNumber).equals(fileName)) {
|
||||
continue;
|
||||
|
@ -248,7 +249,7 @@ public class ASTCommenter {
|
|||
return false;
|
||||
}
|
||||
|
||||
if (comment.getTranslationUnit()==null || comment.getTranslationUnit().getDeclarations().length < 1) {
|
||||
if (comment.getTranslationUnit() == null || comment.getTranslationUnit().getDeclarations().length < 1) {
|
||||
return true;
|
||||
}
|
||||
IASTDeclaration decl = comment.getTranslationUnit().getDeclarations()[0];
|
||||
|
|
|
@ -8,19 +8,20 @@
|
|||
*
|
||||
* Contributors:
|
||||
* Institute for Software - initial API and implementation
|
||||
* Sergey Prigogin (Google)
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTComment;
|
||||
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.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.ICPPASTTemplateDeclaration;
|
||||
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.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.CPPASTTemplateDeclaration;
|
||||
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
|
||||
|
@ -96,10 +92,10 @@ public class NodeCommenter {
|
|||
return false;
|
||||
}
|
||||
|
||||
int nodeLineNumber = OffsetHelper.getEndingLineNumber(node);
|
||||
int commentLineNumber= OffsetHelper.getStartingLineNumber(comment);
|
||||
int nodeLineNumber = getEndingLineNumber(node);
|
||||
int commentLineNumber= getStartingLineNumber(comment);
|
||||
|
||||
if (OffsetHelper.getNodeEndPoint(com) <= OffsetHelper.getNodeOffset(node)) {
|
||||
if (getNodeEndPoint(com) <= getNodeOffset(node)) {
|
||||
addLeadingCommentToMap(node, comment);
|
||||
return true;
|
||||
} else if (isTrailing(node, com, nodeLineNumber, commentLineNumber)) {
|
||||
|
@ -116,7 +112,7 @@ public class NodeCommenter {
|
|||
// MacroExpansions have no FileLocation
|
||||
return false;
|
||||
}
|
||||
if (OffsetHelper.getNodeEndPoint(com) <= OffsetHelper.getNodeEndPoint(node)) {
|
||||
if (getNodeEndPoint(com) <= getNodeEndPoint(node)) {
|
||||
addFreestandingCommentToMap(node, comment);
|
||||
return true;
|
||||
}
|
||||
|
@ -139,47 +135,23 @@ public class NodeCommenter {
|
|||
}
|
||||
|
||||
private boolean isTrailing(ASTNode node, ASTNode com, int nodeLineNumber, int commentLineNumber) {
|
||||
if (nodeLineNumber == commentLineNumber
|
||||
&& OffsetHelper.getNodeOffset(com) >= OffsetHelper.getNodeEndPoint(node)
|
||||
&& canNotBeAddedToParent(node,com)
|
||||
&& !mustBeAddToSubnodes(node)) {
|
||||
if (OffsetHelper.getNodeOffset(com) < OffsetHelper.getNodeEndPoint(node) + 2) {
|
||||
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 (nodeLineNumber != commentLineNumber ||
|
||||
getNodeOffset(com) < getNodeEndPoint(node) ||
|
||||
!canNotBeAddedToParent(node, com) ||
|
||||
mustBeAddedToSubnodes(node)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
if (getNodeOffset(com) < getNodeEndPoint(node) + 2) {
|
||||
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) {
|
||||
ASTNode parent = (ASTNode) node.getParent();
|
||||
|
||||
|
@ -194,10 +166,10 @@ public class NodeCommenter {
|
|||
} else if (parent instanceof ICPPASTBaseSpecifier) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -276,4 +248,25 @@ public class NodeCommenter {
|
|||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,21 +11,12 @@
|
|||
*******************************************************************************/
|
||||
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.internal.core.resources.ResourceLookup;
|
||||
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.Path;
|
||||
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;
|
||||
|
||||
public class FileHelper {
|
||||
|
@ -36,36 +27,13 @@ public class FileHelper {
|
|||
return ResourceLookup.selectFileForLocation(implPath, null);
|
||||
}
|
||||
|
||||
public static boolean isFirstWithinSecondLocation(IASTFileLocation loc1, IASTFileLocation loc2) {
|
||||
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 };
|
||||
}
|
||||
public static String determineLineDelimiter(String text) {
|
||||
String platformDefaultLineDelimiter = System.getProperty("line.separator", DEFAULT_LINE_DELIMITTER); //$NON-NLS-1$
|
||||
String defaultLineDelimiter = Platform.getPreferencesService().getString(Platform.PI_RUNTIME,
|
||||
Platform.PREF_LINE_SEPARATOR, platformDefaultLineDelimiter, scopeContext);
|
||||
return TextUtilities.determineLineDelimiter(fileContent.toString(), defaultLineDelimiter);
|
||||
Platform.PREF_LINE_SEPARATOR, platformDefaultLineDelimiter, null);
|
||||
if (text.isEmpty()) {
|
||||
return defaultLineDelimiter;
|
||||
}
|
||||
return TextUtilities.determineLineDelimiter(text, defaultLineDelimiter);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue