mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-09-03 13:43:31 +02:00
Cosmetics.
This commit is contained in:
parent
78f658d473
commit
89e15ab2d8
8 changed files with 346 additions and 433 deletions
|
@ -18,23 +18,18 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter;
|
|||
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
|
||||
|
||||
/**
|
||||
*
|
||||
* ASTWriter main class. Generates source code from <code>IASTNode</code>. Uses a
|
||||
* <codeC>hangeGeneratorWriterVisitor</code> to generate the code for the given
|
||||
* nodes.
|
||||
* ASTWriter main class. Generates source code from <code>IASTNode</code>.
|
||||
* Uses a <code>hangeGeneratorWriterVisitor</code> to generate the code for the given nodes.
|
||||
*
|
||||
* @see ChangeGeneratorWriterVisitor
|
||||
*
|
||||
* @author Emanuel Graf
|
||||
*
|
||||
*/
|
||||
public class ASTWriter {
|
||||
|
||||
private ChangeGeneratorWriterVisitor transformationVisitor;
|
||||
private ASTModificationStore modificationStore = new ASTModificationStore();
|
||||
private String givenIndentation = ""; //$NON-NLS-1$
|
||||
|
||||
|
||||
/**
|
||||
* Creates a <code>ASTWriter</code>.
|
||||
*/
|
||||
|
@ -53,8 +48,7 @@ public class ASTWriter {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Genereates the source code representing this node.
|
||||
* Generates the source code representing this node.
|
||||
*
|
||||
* @param rootNode Node to write.
|
||||
* @return A <code>String</code> representing the source code for the node.
|
||||
|
@ -69,19 +63,21 @@ public class ASTWriter {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Generates the source code representing this node including comments.
|
||||
*
|
||||
* @param rootNode Node to write.
|
||||
* @param fileScope
|
||||
* @param commentMap Node Comment Map <code>ASTCommenter</code>
|
||||
* @return A <code>String</code> representing the source code for the node.
|
||||
* @throws ProblemRuntimeException if the node or one of it's children is a <code>IASTProblemNode</code>.
|
||||
* @throws ProblemRuntimeException if the node or one of it's children is
|
||||
* an <code>IASTProblemNode</code>.
|
||||
*
|
||||
* @see ASTCommenter#getCommentedNodeMap(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)
|
||||
*/
|
||||
public String write(IASTNode rootNode, String fileScope, NodeCommentMap commentMap) throws ProblemRuntimeException {
|
||||
transformationVisitor = new ChangeGeneratorWriterVisitor(modificationStore, givenIndentation, fileScope, commentMap);
|
||||
public String write(IASTNode rootNode, String fileScope, NodeCommentMap commentMap)
|
||||
throws ProblemRuntimeException {
|
||||
transformationVisitor = new ChangeGeneratorWriterVisitor(modificationStore, givenIndentation,
|
||||
fileScope, commentMap);
|
||||
if (rootNode != null) {
|
||||
rootNode.accept(transformationVisitor);
|
||||
}
|
||||
|
@ -90,9 +86,7 @@ public class ASTWriter {
|
|||
return str;
|
||||
}
|
||||
|
||||
|
||||
public void setModificationStore(ASTModificationStore modificationStore) {
|
||||
this.modificationStore = modificationStore;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -83,7 +83,6 @@ public class ASTWriterVisitor extends ASTVisitor {
|
|||
scribe.setGivenIndentation(givenIndentation);
|
||||
init(commentMap);
|
||||
this.commentMap = commentMap;
|
||||
|
||||
}
|
||||
|
||||
private void init(NodeCommentMap commentMap) {
|
||||
|
@ -120,8 +119,6 @@ public class ASTWriterVisitor extends ASTVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private ArrayList<IASTComment> getLeadingComments(IASTNode node) {
|
||||
ArrayList<IASTComment> leadingComments = commentMap.getLeadingCommentsForNode(node);
|
||||
IASTNodeLocation[] locs = node.getNodeLocations();
|
||||
|
@ -157,7 +154,8 @@ public class ASTWriterVisitor extends ASTVisitor {
|
|||
writeLeadingComments(expression);
|
||||
if (!macroHandler.checkisMacroExpansionNode(expression)) {
|
||||
if (expression instanceof IGNUASTCompoundStatementExpression) {
|
||||
IGNUASTCompoundStatementExpression gnuCompStmtExp = (IGNUASTCompoundStatementExpression) expression;
|
||||
IGNUASTCompoundStatementExpression gnuCompStmtExp =
|
||||
(IGNUASTCompoundStatementExpression) expression;
|
||||
gnuCompStmtExp.getCompoundStatement().accept(this);
|
||||
} else {
|
||||
expWriter.writeExpression(expression);
|
||||
|
@ -169,7 +167,8 @@ public class ASTWriterVisitor extends ASTVisitor {
|
|||
@Override
|
||||
public int visit(IASTStatement statement) {
|
||||
writeLeadingComments(statement);
|
||||
if(macroHandler.isStatementWithMixedLocation(statement) && !(statement instanceof IASTCompoundStatement)){
|
||||
if (macroHandler.isStatementWithMixedLocation(statement) &&
|
||||
!(statement instanceof IASTCompoundStatement)) {
|
||||
return statementWriter.writeMixedStatement(statement);
|
||||
}
|
||||
if (macroHandler.checkisMacroExpansionNode(statement)) {
|
||||
|
@ -232,8 +231,7 @@ public class ASTWriterVisitor extends ASTVisitor {
|
|||
return declarator.getName();
|
||||
}
|
||||
|
||||
protected IASTDeclarator getParameterDeclarator(
|
||||
IASTParameterDeclaration parameterDeclaration) {
|
||||
protected IASTDeclarator getParameterDeclarator(IASTParameterDeclaration parameterDeclaration) {
|
||||
return parameterDeclaration.getDeclarator();
|
||||
}
|
||||
|
||||
|
|
|
@ -39,19 +39,15 @@ import org.eclipse.cdt.core.parser.Keywords;
|
|||
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
|
||||
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Generates source code of declaration nodes. The actual string operations are delegated
|
||||
* to the <code>Scribe</code> class.
|
||||
*
|
||||
* @see Scribe
|
||||
* @see IASTDeclaration
|
||||
* @author Emanuel Graf IFS
|
||||
*
|
||||
*/
|
||||
public class DeclarationWriter extends NodeWriter{
|
||||
|
||||
private static final String ASM_END = ")"; //$NON-NLS-1$
|
||||
private static final String ASM_START = "asm("; //$NON-NLS-1$
|
||||
private static final String TEMPLATE_DECLARATION = "template<"; //$NON-NLS-1$
|
||||
|
@ -300,8 +296,8 @@ public class DeclarationWriter extends NodeWriter{
|
|||
}
|
||||
}
|
||||
|
||||
protected void writeCtorChainInitializer(
|
||||
ICPPASTFunctionDefinition funcDec, ICPPASTConstructorChainInitializer[] ctorInitChain) {
|
||||
protected void writeCtorChainInitializer(ICPPASTFunctionDefinition funcDec,
|
||||
ICPPASTConstructorChainInitializer[] ctorInitChain) {
|
||||
if (ctorInitChain.length != 0) {
|
||||
scribe.newLine();
|
||||
scribe.print(':');
|
||||
|
@ -337,5 +333,4 @@ public class DeclarationWriter extends NodeWriter{
|
|||
|
||||
printSemicolon();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -35,14 +35,12 @@ import org.eclipse.cdt.internal.core.pdom.dom.PDOMMacroReferenceName;
|
|||
import org.eclipse.core.runtime.CoreException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Recognizes nodes that are the result of an macro expansion and replaces them
|
||||
* with a suitable macro call.
|
||||
* @author Emanuel Graf IFS
|
||||
*
|
||||
* @author Emanuel Graf IFS
|
||||
*/
|
||||
public class MacroExpansionHandler {
|
||||
|
||||
private int lastMacroExpOffset;
|
||||
private final Scribe scribe;
|
||||
private IASTTranslationUnit tu;
|
||||
|
@ -176,7 +174,8 @@ public class MacroExpansionHandler {
|
|||
for (IASTPreprocessorMacroDefinition iastPreprocessorMacroDefinition : md) {
|
||||
if (iastPreprocessorMacroDefinition.getExpansion().length() == 0) {
|
||||
try {
|
||||
IIndexMacro[] macroBinding = index.findMacros(iastPreprocessorMacroDefinition.getName().toCharArray(), IndexFilter.ALL, null);
|
||||
IIndexMacro[] macroBinding = index.findMacros(iastPreprocessorMacroDefinition.getName().toCharArray(),
|
||||
IndexFilter.ALL, null);
|
||||
if (macroBinding.length > 0) {
|
||||
IIndexName[] refs = index.findReferences(macroBinding[0]);
|
||||
for (IIndexName iIndexName : refs) {
|
||||
|
@ -205,5 +204,4 @@ public class MacroExpansionHandler {
|
|||
public void reset() {
|
||||
lastMacroExpOffset = -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -55,8 +55,6 @@ import org.eclipse.text.edits.ReplaceEdit;
|
|||
import org.eclipse.text.edits.TextEditGroup;
|
||||
|
||||
public class ChangeGenerator extends ASTVisitor {
|
||||
|
||||
|
||||
private final LinkedHashMap<String, Integer> sourceOffsets = new LinkedHashMap<String, Integer>();
|
||||
public LinkedHashMap<IASTNode, List<ASTModification>> modificationParent = new LinkedHashMap<IASTNode, List<ASTModification>>();
|
||||
private final LinkedHashMap<IFile, MultiTextEdit> changes = new LinkedHashMap<IFile, MultiTextEdit>();
|
||||
|
@ -67,37 +65,23 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
|
||||
{
|
||||
shouldVisitExpressions = true;
|
||||
|
||||
shouldVisitStatements = true;
|
||||
|
||||
shouldVisitNames = true;
|
||||
|
||||
shouldVisitDeclarations = true;
|
||||
|
||||
shouldVisitDeclSpecifiers = true;
|
||||
|
||||
shouldVisitDeclarators = true;
|
||||
|
||||
shouldVisitArrayModifiers= true;
|
||||
|
||||
shouldVisitInitializers = true;
|
||||
|
||||
shouldVisitBaseSpecifiers = true;
|
||||
|
||||
shouldVisitNamespaces = true;
|
||||
|
||||
shouldVisitTemplateParameters = true;
|
||||
|
||||
shouldVisitParameterDeclarations = true;
|
||||
|
||||
shouldVisitTranslationUnit = true;
|
||||
|
||||
}
|
||||
|
||||
public ChangeGenerator(ASTModificationStore modificationStore, NodeCommentMap commentMap) {
|
||||
this.modificationStore = modificationStore;
|
||||
this.commentMap = commentMap;
|
||||
|
||||
}
|
||||
|
||||
public void generateChange(IASTNode rootNode) throws ProblemRuntimeException {
|
||||
|
@ -110,7 +94,6 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
initParentModList();
|
||||
rootNode.accept(pathProvider);
|
||||
for (IFile currentFile : changes.keySet()) {
|
||||
|
||||
TextFileChange subchange= ASTRewriteAnalyzer.createCTextFileChange(currentFile);
|
||||
subchange.setEdit(changes.get(currentFile));
|
||||
change.add(subchange);
|
||||
|
@ -118,20 +101,19 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
}
|
||||
|
||||
private void initParentModList() {
|
||||
ASTModificationMap rootModifications = modificationStore
|
||||
.getRootModifications();
|
||||
ASTModificationMap rootModifications = modificationStore.getRootModifications();
|
||||
if (rootModifications != null) {
|
||||
for (IASTNode modifiedNode : rootModifications.getModifiedNodes()) {
|
||||
List<ASTModification> modificationsForNode = rootModifications
|
||||
.getModificationsForNode(modifiedNode);
|
||||
List<ASTModification> modificationsForNode = rootModifications.getModificationsForNode(modifiedNode);
|
||||
IASTNode modifiedNodeParent = determineParentToBeRewritten(modifiedNode, modificationsForNode);
|
||||
List<ASTModification> list = modificationParent.get(modifiedNodeParent != null ? modifiedNodeParent : modifiedNode);
|
||||
List<ASTModification> list = modificationParent.get(modifiedNodeParent != null ?
|
||||
modifiedNodeParent : modifiedNode);
|
||||
if (list != null) {
|
||||
list.addAll(modificationsForNode);
|
||||
} else {
|
||||
List<ASTModification> modifiableList = new ArrayList<ASTModification>(modificationsForNode);
|
||||
modificationParent.put(modifiedNodeParent != null ? modifiedNodeParent : modifiedNode,
|
||||
modifiableList);
|
||||
modificationParent.put(modifiedNodeParent != null ?
|
||||
modifiedNodeParent : modifiedNode, modifiableList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -152,21 +134,20 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
@Override
|
||||
public int visit(IASTTranslationUnit translationUnit) {
|
||||
if (hasChangedChild(translationUnit)) {
|
||||
|
||||
synthTreatment(translationUnit);
|
||||
}
|
||||
IASTFileLocation location = getFileLocationOfEmptyTranslationUnit(translationUnit);
|
||||
sourceOffsets.put(location.getFileName(),
|
||||
Integer.valueOf(location.getNodeOffset()));
|
||||
sourceOffsets.put(location.getFileName(), Integer.valueOf(location.getNodeOffset()));
|
||||
return super.visit(translationUnit);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a Workaround for a known but not jet solved Problem in IASTNode. If you get the FileFocation of a translationUnit
|
||||
* that was built on an empty file you will get null because there it explicitly returns null if the index and length is 0.
|
||||
* To get to the Filename and other information, the location is never the less needed.
|
||||
* This is a workaround for a known but not jet solved problem in IASTNode. If you get
|
||||
* the FileFocation of a translation unit that was built on an empty file you will get null
|
||||
* because there it explicitly returns null if the index and length is 0.
|
||||
* To get to the filename and other information, the location is never the less needed.
|
||||
* @param node
|
||||
* @return a hopefully "unnull" FileLocation
|
||||
* @return a hopefully non-{@code null} FileLocation
|
||||
*/
|
||||
public IASTFileLocation getFileLocationOfEmptyTranslationUnit(IASTNode node) {
|
||||
IASTFileLocation fileLocation = node.getFileLocation();
|
||||
|
@ -175,7 +156,7 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
if (lr != null) {
|
||||
fileLocation = lr.getMappedFileLocation(0, 0);
|
||||
} else {
|
||||
// support for old location map
|
||||
// Support for old location map
|
||||
fileLocation = node.getTranslationUnit().flattenLocationsToFile(node.getNodeLocations());
|
||||
}
|
||||
}
|
||||
|
@ -184,14 +165,11 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
|
||||
@Override
|
||||
public int leave(IASTTranslationUnit tu) {
|
||||
|
||||
return super.leave(tu);
|
||||
}
|
||||
|
||||
private int getOffsetForNodeFile(IASTNode rootNode) {
|
||||
|
||||
Integer offset = sourceOffsets.get(rootNode.getFileLocation()
|
||||
.getFileName());
|
||||
Integer offset = sourceOffsets.get(rootNode.getFileLocation().getFileName());
|
||||
return offset == null ? 0 : offset.intValue();
|
||||
}
|
||||
|
||||
|
@ -217,10 +195,8 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
|
||||
createChange(synthNode, synthSource);
|
||||
|
||||
int newOffset = synthNode.getFileLocation().getNodeOffset()
|
||||
+ synthNode.getFileLocation().getNodeLength();
|
||||
int newOffset = synthNode.getFileLocation().getNodeOffset() + synthNode.getFileLocation().getNodeLength();
|
||||
sourceOffsets.put(synthNode.getFileLocation().getFileName(), Integer.valueOf(newOffset));
|
||||
|
||||
}
|
||||
|
||||
private void synthTreatment(IASTTranslationUnit synthTU) {
|
||||
|
@ -228,9 +204,8 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
synthWriter.setModificationStore(modificationStore);
|
||||
|
||||
for (ASTModification modification : modificationParent.get(synthTU)) {
|
||||
IASTFileLocation targetLocation;
|
||||
|
||||
targetLocation = getFileLocationOfEmptyTranslationUnit(modification.getTargetNode());
|
||||
IASTFileLocation targetLocation =
|
||||
getFileLocationOfEmptyTranslationUnit(modification.getTargetNode());
|
||||
String currentFile = targetLocation.getFileName();
|
||||
IPath implPath = new Path(currentFile);
|
||||
IFile relevantFile= ResourceLookup.selectFileForLocation(implPath, null);
|
||||
|
@ -256,14 +231,16 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
newNodeCode));
|
||||
break;
|
||||
case APPEND_CHILD:
|
||||
if(modification.getTargetNode() instanceof IASTTranslationUnit && ((IASTTranslationUnit)modification.getTargetNode()).getDeclarations().length > 0) {
|
||||
if (modification.getTargetNode() instanceof IASTTranslationUnit &&
|
||||
((IASTTranslationUnit)modification.getTargetNode()).getDeclarations().length > 0) {
|
||||
IASTTranslationUnit tu = (IASTTranslationUnit)modification.getTargetNode();
|
||||
IASTDeclaration lastDecl = tu.getDeclarations()[tu.getDeclarations().length -1];
|
||||
targetLocation = lastDecl.getFileLocation();
|
||||
}
|
||||
String lineDelimiter = FileHelper.determineLineDelimiter(FileHelper.getIFilefromIASTNode(modification.getTargetNode()));
|
||||
edit.addChild(new InsertEdit(targetLocation.getNodeOffset()
|
||||
+ targetLocation.getNodeLength(),lineDelimiter + lineDelimiter + newNodeCode));
|
||||
String lineDelimiter = FileHelper.determineLineDelimiter(
|
||||
FileHelper.getIFilefromIASTNode(modification.getTargetNode()));
|
||||
edit.addChild(new InsertEdit(targetLocation.getNodeOffset() + targetLocation.getNodeLength(),
|
||||
lineDelimiter + lineDelimiter + newNodeCode));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -338,16 +315,13 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
|
||||
private String getIndent(IASTNode nextNode) {
|
||||
IASTFileLocation fileLocation = nextNode.getFileLocation();
|
||||
int length = fileLocation.getNodeOffset()
|
||||
- getOffsetForNodeFile(nextNode);
|
||||
int length = fileLocation.getNodeOffset() - getOffsetForNodeFile(nextNode);
|
||||
|
||||
String originalSource = FileContentHelper.getContent(FileHelper
|
||||
.getIFilefromIASTNode(nextNode),
|
||||
String originalSource = FileContentHelper.getContent(FileHelper.getIFilefromIASTNode(nextNode),
|
||||
getOffsetForNodeFile(nextNode), length);
|
||||
StringBuilder indent = new StringBuilder(originalSource);
|
||||
indent.reverse();
|
||||
String lastline = indent
|
||||
.substring(0, Math.max(indent.indexOf("\n"), 0)); //$NON-NLS-1$
|
||||
String lastline = indent.substring(0, Math.max(indent.indexOf("\n"), 0)); //$NON-NLS-1$
|
||||
if (lastline.trim().length() == 0) {
|
||||
return lastline;
|
||||
}
|
||||
|
@ -355,11 +329,9 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
}
|
||||
|
||||
private boolean hasChangedChild(IASTNode parent) {
|
||||
|
||||
return modificationParent.containsKey(parent);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int visit(IASTDeclarator declarator) {
|
||||
if (hasChangedChild(declarator)) {
|
||||
|
@ -378,7 +350,6 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
return super.visit(mod);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int visit(ICPPASTNamespaceDefinition namespaceDefinition) {
|
||||
if (hasChangedChild(namespaceDefinition)) {
|
||||
|
@ -443,7 +414,6 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
}
|
||||
|
||||
class CodeComparer {
|
||||
|
||||
private final StringBuilder originalCode;
|
||||
private final StringBuilder synthCode;
|
||||
private int lastCommonInSynthStart;
|
||||
|
@ -460,34 +430,34 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
private void calculatePositions() {
|
||||
lastCommonInSynthStart = calcLastCommonPositionInSynthCode();
|
||||
lastCommonInOriginalStart = calcLastCommonPositionInOriginalCode();
|
||||
firstCommonInSynthEnd = calcFirstPositionOfCommonEndInSynthCode(lastCommonInSynthStart, lastCommonInOriginalStart);
|
||||
firstCommonInOriginalEnd = calcFirstPositionOfCommonEndInOriginalCode(lastCommonInOriginalStart, lastCommonInSynthStart);
|
||||
firstCommonInSynthEnd =
|
||||
calcFirstPositionOfCommonEndInSynthCode(lastCommonInSynthStart, lastCommonInOriginalStart);
|
||||
firstCommonInOriginalEnd =
|
||||
calcFirstPositionOfCommonEndInOriginalCode(lastCommonInOriginalStart, lastCommonInSynthStart);
|
||||
trimTrailingNewlines();
|
||||
}
|
||||
|
||||
private void trimTrailingNewlines() {
|
||||
int prevOrigEnd = firstCommonInOriginalEnd - 1;
|
||||
while( prevOrigEnd > lastCommonInOriginalStart
|
||||
&& prevOrigEnd > -1
|
||||
&& isUninterresting(originalCode, prevOrigEnd)){
|
||||
|
||||
while (prevOrigEnd > lastCommonInOriginalStart && prevOrigEnd > -1 &&
|
||||
isUninterresting(originalCode, prevOrigEnd)) {
|
||||
firstCommonInOriginalEnd = prevOrigEnd;
|
||||
prevOrigEnd--;
|
||||
}
|
||||
|
||||
while(firstCommonInOriginalEnd > 0 && firstCommonInOriginalEnd +1 < originalCode.length() && (originalCode.charAt(firstCommonInOriginalEnd) == ' ' || originalCode.charAt(firstCommonInOriginalEnd) == '\t')){
|
||||
while (firstCommonInOriginalEnd > 0 && firstCommonInOriginalEnd + 1 < originalCode.length() &&
|
||||
(originalCode.charAt(firstCommonInOriginalEnd) == ' ' || originalCode.charAt(firstCommonInOriginalEnd) == '\t')) {
|
||||
firstCommonInOriginalEnd++;
|
||||
}
|
||||
|
||||
int prevSynthEnd = firstCommonInSynthEnd - 1;
|
||||
while( prevSynthEnd > lastCommonInSynthStart
|
||||
&& prevSynthEnd > -1
|
||||
&& isUninterresting(synthCode, prevSynthEnd)){
|
||||
|
||||
while (prevSynthEnd > lastCommonInSynthStart && prevSynthEnd > -1 &&
|
||||
isUninterresting(synthCode, prevSynthEnd)) {
|
||||
firstCommonInSynthEnd = prevSynthEnd;
|
||||
prevSynthEnd--;
|
||||
}
|
||||
while(firstCommonInSynthEnd > 0 && firstCommonInSynthEnd +1< synthCode.length() && (synthCode.charAt(firstCommonInSynthEnd) == ' ' || synthCode.charAt(firstCommonInSynthEnd) == '\t')){
|
||||
while (firstCommonInSynthEnd > 0 && firstCommonInSynthEnd + 1 < synthCode.length() &&
|
||||
(synthCode.charAt(firstCommonInSynthEnd) == ' ' || synthCode.charAt(firstCommonInSynthEnd) == '\t')) {
|
||||
firstCommonInSynthEnd++;
|
||||
}
|
||||
}
|
||||
|
@ -508,7 +478,6 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
return firstCommonInSynthEnd;
|
||||
}
|
||||
|
||||
|
||||
public int calcLastCommonPositionInSynthCode() {
|
||||
return findLastCommonPosition(synthCode, originalCode);
|
||||
}
|
||||
|
@ -517,18 +486,14 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
return findLastCommonPosition(originalCode, synthCode);
|
||||
}
|
||||
|
||||
|
||||
private int calcFirstPositionOfCommonEndInOriginalCode(int originalLimit, int synthLimit) {
|
||||
|
||||
StringBuilder reverseOriginalCode = new StringBuilder(originalCode)
|
||||
.reverse();
|
||||
StringBuilder reverseSynthCode = new StringBuilder(synthCode)
|
||||
.reverse();
|
||||
StringBuilder reverseOriginalCode = new StringBuilder(originalCode).reverse();
|
||||
StringBuilder reverseSynthCode = new StringBuilder(synthCode).reverse();
|
||||
int lastCommonPosition = findLastCommonPosition(reverseOriginalCode, reverseSynthCode,
|
||||
reverseOriginalCode.length() - originalLimit - 1, reverseSynthCode.length() - synthLimit - 1);
|
||||
reverseOriginalCode.length() - originalLimit - 1,
|
||||
reverseSynthCode.length() - synthLimit - 1);
|
||||
|
||||
if (lastCommonPosition < 0
|
||||
|| lastCommonPosition >= originalCode.length()) {
|
||||
if (lastCommonPosition < 0 || lastCommonPosition >= originalCode.length()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -536,29 +501,26 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
}
|
||||
|
||||
private int calcFirstPositionOfCommonEndInSynthCode(int synthLimit, int originalLimit) {
|
||||
StringBuilder reverseOriginalCode = new StringBuilder(originalCode)
|
||||
.reverse();
|
||||
StringBuilder reverseSynthCode = new StringBuilder(synthCode)
|
||||
.reverse();
|
||||
StringBuilder reverseOriginalCode = new StringBuilder(originalCode).reverse();
|
||||
StringBuilder reverseSynthCode = new StringBuilder(synthCode).reverse();
|
||||
|
||||
int lastCommonPosition = findLastCommonPosition(reverseSynthCode, reverseOriginalCode,
|
||||
reverseSynthCode.length() - synthLimit -1, reverseOriginalCode.length() - originalLimit -1);
|
||||
reverseSynthCode.length() - synthLimit - 1,
|
||||
reverseOriginalCode.length() - originalLimit - 1);
|
||||
|
||||
if (lastCommonPosition < 0
|
||||
|| lastCommonPosition >= synthCode.length()) {
|
||||
if (lastCommonPosition < 0 || lastCommonPosition >= synthCode.length()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return synthCode.length() - lastCommonPosition - 1;
|
||||
}
|
||||
|
||||
|
||||
private int findLastCommonPosition(StringBuilder first, StringBuilder second) {
|
||||
return findLastCommonPosition(first, second, first.length(), second.length());
|
||||
}
|
||||
|
||||
|
||||
private int findLastCommonPosition(StringBuilder first, StringBuilder second, int firstLimit, int secondLimit){
|
||||
private int findLastCommonPosition(StringBuilder first, StringBuilder second, int firstLimit,
|
||||
int secondLimit) {
|
||||
int firstIndex = -1;
|
||||
int secondIndex = -1;
|
||||
int lastCommonIndex = -1;
|
||||
|
@ -567,11 +529,8 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
lastCommonIndex = firstIndex;
|
||||
firstIndex = nextInterrestingPosition(first, firstIndex);
|
||||
secondIndex = nextInterrestingPosition(second, secondIndex);
|
||||
} while (firstIndex > -1
|
||||
&& firstIndex <= firstLimit
|
||||
&& secondIndex > -1
|
||||
&& secondIndex <= secondLimit
|
||||
&& first.charAt(firstIndex) == second.charAt(secondIndex));
|
||||
} while (firstIndex > -1 && firstIndex <= firstLimit && secondIndex > -1 &&
|
||||
secondIndex <= secondLimit && first.charAt(firstIndex) == second.charAt(secondIndex));
|
||||
return lastCommonIndex;
|
||||
}
|
||||
|
||||
|
@ -600,63 +559,52 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
|
||||
protected void createChange(MultiTextEdit edit, IASTNode changedNode) {
|
||||
int changeOffset = getOffsetIncludingComments(changedNode);
|
||||
|
||||
TextEditGroup editGroup = new TextEditGroup(Messages.ChangeGenerator_group);
|
||||
for (ASTModification currentModification : modificationParent
|
||||
.get(changedNode)) {
|
||||
for (ASTModification currentModification : modificationParent.get(changedNode)) {
|
||||
if (currentModification.getAssociatedEditGroup() != null) {
|
||||
editGroup = currentModification.getAssociatedEditGroup();
|
||||
edit.addChildren(editGroup.getTextEdits());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
createChange(edit, changeOffset);
|
||||
}
|
||||
|
||||
private void createChange(MultiTextEdit edit, int changeOffset) {
|
||||
|
||||
int i = (firstCommonInSynthEnd >= 0 ? firstCommonInOriginalEnd
|
||||
: originalCode.length())
|
||||
- lastCommonInOriginalStart;
|
||||
int i = (firstCommonInSynthEnd >= 0 ?
|
||||
firstCommonInOriginalEnd : originalCode.length()) - lastCommonInOriginalStart;
|
||||
if (i <= 0) {
|
||||
String insertCode = synthCode.substring(
|
||||
lastCommonInSynthStart, firstCommonInSynthEnd);
|
||||
InsertEdit iEdit = new InsertEdit(changeOffset
|
||||
+ lastCommonInOriginalStart, insertCode);
|
||||
String insertCode = synthCode.substring(lastCommonInSynthStart,
|
||||
firstCommonInSynthEnd);
|
||||
InsertEdit iEdit = new InsertEdit(changeOffset + lastCommonInOriginalStart,
|
||||
insertCode);
|
||||
edit.addChild(iEdit);
|
||||
} else if ((firstCommonInSynthEnd >= 0 ? firstCommonInSynthEnd
|
||||
: synthCode.length())
|
||||
- lastCommonInSynthStart <= 0) {
|
||||
} else if ((firstCommonInSynthEnd >= 0 ?
|
||||
firstCommonInSynthEnd : synthCode.length()) - lastCommonInSynthStart <= 0) {
|
||||
int correction = 0;
|
||||
if (lastCommonInSynthStart > firstCommonInSynthEnd) {
|
||||
correction = lastCommonInSynthStart
|
||||
- firstCommonInSynthEnd;
|
||||
correction = lastCommonInSynthStart - firstCommonInSynthEnd;
|
||||
}
|
||||
DeleteEdit dEdit = new DeleteEdit(changeOffset
|
||||
+ lastCommonInOriginalStart,
|
||||
firstCommonInOriginalEnd
|
||||
- lastCommonInOriginalStart + correction);
|
||||
DeleteEdit dEdit = new DeleteEdit(changeOffset + lastCommonInOriginalStart,
|
||||
firstCommonInOriginalEnd - lastCommonInOriginalStart + correction);
|
||||
edit.addChild(dEdit);
|
||||
} else {
|
||||
String replacementCode = getReplacementCode(
|
||||
lastCommonInSynthStart, firstCommonInSynthEnd);
|
||||
String replacementCode = getReplacementCode(lastCommonInSynthStart,
|
||||
firstCommonInSynthEnd);
|
||||
ReplaceEdit rEdit = new ReplaceEdit(
|
||||
changeOffset
|
||||
+ Math.max(lastCommonInOriginalStart, 0),
|
||||
(firstCommonInOriginalEnd >= 0 ? firstCommonInOriginalEnd
|
||||
: originalCode.length())
|
||||
- Math.max(lastCommonInOriginalStart, 0),
|
||||
changeOffset + Math.max(lastCommonInOriginalStart, 0),
|
||||
(firstCommonInOriginalEnd >= 0 ?
|
||||
firstCommonInOriginalEnd :
|
||||
originalCode.length()) - Math.max(lastCommonInOriginalStart, 0),
|
||||
replacementCode);
|
||||
edit.addChild(rEdit);
|
||||
}
|
||||
}
|
||||
|
||||
private String getReplacementCode(int lastCommonPositionInSynth,
|
||||
int firstOfCommonEndInSynth) {
|
||||
private String getReplacementCode(int lastCommonPositionInSynth, int firstOfCommonEndInSynth) {
|
||||
int replacementStart = Math.max(lastCommonPositionInSynth, 0);
|
||||
int replacementEnd = (firstOfCommonEndInSynth >= 0 ? firstOfCommonEndInSynth
|
||||
: synthCode.length());
|
||||
int replacementEnd = (firstOfCommonEndInSynth >= 0 ?
|
||||
firstOfCommonEndInSynth : synthCode.length());
|
||||
if (replacementStart < replacementEnd) {
|
||||
return synthCode.substring(replacementStart, replacementEnd);
|
||||
}
|
||||
|
|
|
@ -37,15 +37,12 @@ import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor;
|
|||
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
|
||||
|
||||
/**
|
||||
*
|
||||
* Visits the nodes in consideration of {@link ASTModification}s.
|
||||
*
|
||||
* @since 5.0
|
||||
* @author Emanuel Graf IFS
|
||||
*
|
||||
*/
|
||||
public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
|
||||
|
||||
private static final String DEFAULT_INDENTATION = ""; //$NON-NLS-1$
|
||||
private final ASTModificationStore modificationStore;
|
||||
private final String fileScope;
|
||||
|
@ -74,15 +71,14 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
|
|||
shouldVisitProblems = delegateVisitor.shouldVisitProblems;
|
||||
shouldVisitTypeIds = delegateVisitor.shouldVisitTypeIds;
|
||||
shouldVisitArrayModifiers= delegateVisitor.shouldVisitArrayModifiers;
|
||||
|
||||
}
|
||||
|
||||
public ChangeGeneratorWriterVisitor(ASTModificationStore modStore, NodeCommentMap nodeMap) {
|
||||
this(modStore, DEFAULT_INDENTATION, null, nodeMap);
|
||||
}
|
||||
|
||||
public ChangeGeneratorWriterVisitor(ASTModificationStore modStore,
|
||||
String givenIndentation, String fileScope, NodeCommentMap commentMap) {
|
||||
public ChangeGeneratorWriterVisitor(ASTModificationStore modStore, String givenIndentation,
|
||||
String fileScope, NodeCommentMap commentMap) {
|
||||
super(givenIndentation, commentMap);
|
||||
this.modificationStore = modStore;
|
||||
this.fileScope = fileScope;
|
||||
|
@ -98,13 +94,12 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected IASTDeclarator getParameterDeclarator(
|
||||
IASTParameterDeclaration parameterDeclaration) {
|
||||
|
||||
protected IASTDeclarator getParameterDeclarator(IASTParameterDeclaration parameterDeclaration) {
|
||||
IASTDeclarator newDecl = parameterDeclaration.getDeclarator();
|
||||
if (stack.getModifiedNodes().contains(newDecl)) {
|
||||
for (ASTModification currentModification : stack.getModificationsForNode(newDecl)) {
|
||||
if(currentModification.getKind() == ASTModification.ModificationKind.REPLACE && currentModification.getTargetNode() == parameterDeclaration){
|
||||
if (currentModification.getKind() == ASTModification.ModificationKind.REPLACE &&
|
||||
currentModification.getTargetNode() == parameterDeclaration) {
|
||||
newDecl = (IASTDeclarator) currentModification.getNewNode();
|
||||
}
|
||||
}
|
||||
|
@ -117,7 +112,8 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
|
|||
IASTName newName = declarator.getName();
|
||||
if (stack.getModifiedNodes().contains(newName)) {
|
||||
for (ASTModification currentModification : stack.getModificationsForNode(newName)) {
|
||||
if(currentModification.getKind() == ASTModification.ModificationKind.REPLACE && currentModification.getTargetNode() == newName){
|
||||
if (currentModification.getKind() == ASTModification.ModificationKind.REPLACE &&
|
||||
currentModification.getTargetNode() == newName) {
|
||||
newName = (IASTName) currentModification.getNewNode();
|
||||
}
|
||||
}
|
||||
|
@ -329,14 +325,12 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
|
|||
|
||||
@Override
|
||||
public int visit(IASTTranslationUnit tu) {
|
||||
ASTModificationHelper helper = new ASTModificationHelper(
|
||||
stack);
|
||||
|
||||
IASTDeclaration[] declarations = helper.createModifiedChildArray(tu, tu.getDeclarations(), IASTDeclaration.class, commentMap);
|
||||
ASTModificationHelper helper = new ASTModificationHelper(stack);
|
||||
IASTDeclaration[] declarations = helper.createModifiedChildArray(tu, tu.getDeclarations(),
|
||||
IASTDeclaration.class, commentMap);
|
||||
for (IASTDeclaration currentDeclaration : declarations) {
|
||||
currentDeclaration.accept(this);
|
||||
}
|
||||
|
||||
return PROCESS_SKIP;
|
||||
}
|
||||
|
||||
|
@ -357,7 +351,8 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
//Check all insert before and append modifications for the current node. If necessary put it onto the stack.
|
||||
// Check all insert before and append modifications for the current node.
|
||||
// If necessary put it onto the stack.
|
||||
for (IASTNode currentModifiedNode : stack.getModifiedNodes()) {
|
||||
for (ASTModification currentMod : stack.getModificationsForNode(currentModifiedNode)) {
|
||||
if (currentMod.getNewNode() == node) {
|
||||
|
@ -377,8 +372,7 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
|
|||
currentMod.getNewNode().accept(this);
|
||||
stack.popScope(node);
|
||||
return PROCESS_SKIP;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
return PROCESS_SKIP;
|
||||
}
|
||||
}
|
||||
|
@ -389,7 +383,6 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
|
|||
}
|
||||
|
||||
private String getCorrespondingFile(IASTNode node) {
|
||||
|
||||
if (node.getFileLocation() != null) {
|
||||
return node.getFileLocation().getFileName();
|
||||
}
|
||||
|
@ -398,11 +391,8 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
|
|||
return getCorrespondingFile(node.getParent());
|
||||
}
|
||||
|
||||
for (IASTNode modifiedNode : modificationStore.getRootModifications()
|
||||
.getModifiedNodes()) {
|
||||
for (ASTModification modification : modificationStore
|
||||
.getRootModifications().getModificationsForNode(
|
||||
modifiedNode)) {
|
||||
for (IASTNode modifiedNode : modificationStore.getRootModifications().getModifiedNodes()) {
|
||||
for (ASTModification modification : modificationStore.getRootModifications().getModificationsForNode(modifiedNode)) {
|
||||
if (modification.getNewNode() == node) {
|
||||
return getCorrespondingFile(modification.getTargetNode());
|
||||
}
|
||||
|
|
|
@ -30,17 +30,13 @@ import org.eclipse.osgi.util.NLS;
|
|||
* A Change for creating a new file with the given name, content and encoding at the specified path.
|
||||
*
|
||||
* @author Emanuel Graf
|
||||
*
|
||||
*/
|
||||
public class CreateFileChange extends Change {
|
||||
|
||||
private String name;
|
||||
private final IPath path;
|
||||
private final String source;
|
||||
private final String encoding;
|
||||
|
||||
|
||||
|
||||
public CreateFileChange(String name, IPath path, String source, String encoding) {
|
||||
super();
|
||||
this.name = name;
|
||||
|
@ -55,8 +51,7 @@ public class CreateFileChange extends Change {
|
|||
|
||||
@Override
|
||||
public Object getModifiedElement() {
|
||||
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
|
||||
return file;
|
||||
return ResourcesPlugin.getWorkspace().getRoot().getFile(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -72,8 +67,8 @@ public class CreateFileChange extends Change {
|
|||
}
|
||||
|
||||
@Override
|
||||
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException,
|
||||
OperationCanceledException {
|
||||
public RefactoringStatus isValid(IProgressMonitor pm)
|
||||
throws CoreException, OperationCanceledException {
|
||||
RefactoringStatus result= new RefactoringStatus();
|
||||
IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(path);
|
||||
|
||||
|
@ -84,8 +79,7 @@ public class CreateFileChange extends Change {
|
|||
}
|
||||
|
||||
if (file.exists()) {
|
||||
result.addFatalError( NLS.bind(Messages.CreateFileChange_FileExists,
|
||||
file.getFullPath().toString()));
|
||||
result.addFatalError(NLS.bind(Messages.CreateFileChange_FileExists, file.getFullPath().toString()));
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
|
@ -102,8 +96,6 @@ public class CreateFileChange extends Change {
|
|||
return new DeleteFileChange(file.getFullPath());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
|
@ -116,5 +108,4 @@ public class CreateFileChange extends Change {
|
|||
public String toString() {
|
||||
return getName();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -38,7 +38,6 @@ public class ModificationCollector {
|
|||
if (!rewriters.containsKey(ast)) {
|
||||
rewriters.put(ast, ASTRewrite.create(ast));
|
||||
}
|
||||
|
||||
return rewriters.get(ast);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue