1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 08:55:25 +02:00

Bug 337714: Fixed streamline ChangeGenerator

https://bugs.eclipse.org/bugs/show_bug.cgi?id=337714
This commit is contained in:
Emanuel Graf 2011-02-21 13:43:33 +00:00
parent 8d45a913ca
commit 496551af98
16 changed files with 552 additions and 211 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* 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
@ -16,10 +16,14 @@ import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.model.CoreModelUtil;
import org.eclipse.cdt.core.parser.tests.rewrite.TestHelper;
import org.eclipse.cdt.core.tests.BaseTestFramework;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
import org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.Document;
@ -43,23 +47,23 @@ public abstract class ChangeGeneratorTest extends BaseTestFramework {
}
@Override
public void runTest() {
public void runTest() throws Exception{
final ASTModificationStore modStore = new ASTModificationStore();
final ChangeGenerator changegenartor = new ChangeGenerator(modStore);
try {
importFile("source.h", source); //$NON-NLS-1$
IASTTranslationUnit unit = CDOM.getInstance().getTranslationUnit(
project.getFile(new Path("source.h")), //$NON-NLS-1$
CDOM.getInstance().getCodeReaderFactory(
CDOM.PARSE_SAVED_RESOURCES),
true);
IFile testFile = importFile("source.h", source); //$NON-NLS-1$
CPPASTVisitor visitor = createModificator(modStore);
CCorePlugin.getIndexManager().reindex(cproject);
ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
boolean joined = CCorePlugin.getIndexManager().joinIndexer(20000, new NullProgressMonitor());
assertTrue("The indexing operation of the test CProject has not finished jet. This should not happen...", joined);
IASTTranslationUnit unit = CoreModelUtil.findTranslationUnit(testFile).getAST();
unit.accept(visitor);
// assertEquals(expectedSource, changegenartor.write(unit));
changegenartor.generateChange(unit);
Document doc = new Document(source);
for(Change curChange : ((CompositeChange)changegenartor.getChange()).getChildren()){
@ -69,10 +73,6 @@ public abstract class ChangeGeneratorTest extends BaseTestFramework {
}
}
assertEquals(TestHelper.unifyNewLines(expectedSource), TestHelper.unifyNewLines(doc.get()));
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
}
protected abstract CPPASTVisitor createModificator(ASTModificationStore modStore);

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* 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
@ -32,7 +32,7 @@ public class ChangeGeneratorTestSuite{
suite.addTest(RemoveTestSuite.suite());
suite.addTest(InsertBeforeTestSuite.suite());
suite.addTest(AppendTestSuite.suite());
return suite;
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* 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
@ -69,27 +69,23 @@ public class AddDeclarationBug extends ChangeGeneratorTest {
returnTyp.setType(IASTSimpleDeclSpecifier.t_int);
newDecl.setDeclSpecifier(returnTyp);
IASTStandardFunctionDeclarator declarator = new CPPASTFunctionDeclarator(new CPPASTName("exp".toCharArray()));
IASTStandardFunctionDeclarator declarator = new CPPASTFunctionDeclarator(new CPPASTName("exp".toCharArray())); //$NON-NLS-1$
IASTSimpleDeclSpecifier paramTyp = new CPPASTSimpleDeclSpecifier();
paramTyp.setType(IASTSimpleDeclSpecifier.t_int);
IASTDeclarator decl = new CPPASTDeclarator(new CPPASTName("i".toCharArray()));
IASTDeclarator decl = new CPPASTDeclarator(new CPPASTName("i".toCharArray())); //$NON-NLS-1$
ICPPASTParameterDeclaration param = new CPPASTParameterDeclaration(paramTyp, decl);
declarator.addParameterDeclaration(param);
newDecl.addDeclarator(declarator);
ASTModification mod= new ASTModification(ModificationKind.APPEND_CHILD, classNode, newDecl, null);
ASTModification mod = new ASTModification(ModificationKind.APPEND_CHILD, classNode, newDecl, null);
modStore.storeModification(null, mod);
}
return PROCESS_CONTINUE;
}
};
}
public static Test suite() {
return new AddDeclarationBug();
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* 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
@ -31,6 +31,8 @@ public class InsertBeforeTestSuite{
suite.addTest(ExpressionTest.suite());
suite.addTest(ArraySizeExpressionTest.suite());
suite.addTest(AddDeclarationBug.suite());
suite.addTest(MultilineWhitespaceHandlingTest.suite());
suite.addTest(SelfInsertionTest.suite());
return suite;
}

View file

@ -0,0 +1,87 @@
/*******************************************************************************
* Copyright (c) 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.core.parser.tests.rewrite.changegenerator.insertbefore;
import junit.framework.Test;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPNodeFactory;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
public class MultilineWhitespaceHandlingTest extends ChangeGeneratorTest {
public MultilineWhitespaceHandlingTest(){
super("Multiline Whitespace Handling"); //$NON-NLS-1$
}
@Override
protected void setUp() throws Exception {
source = "void foo(){\r\n\r\n for(int i = 0; i < 10; i++){\r\n\r\n }\r\n}\r\n"; //$NON-NLS-1$
expectedSource = "void foo(){\r\n\r\n for(int i = 0; i < 10; i++){\r\n int i;\r\n int j;\r\n\r\n }\r\n}\r\n"; //$NON-NLS-1$
super.setUp();
}
public static Test suite() {
return new MultilineWhitespaceHandlingTest();
}
@Override
protected CPPASTVisitor createModificator(
final ASTModificationStore modStore) {
return new CPPASTVisitor() {
{
shouldVisitStatements = true;
}
@Override
public int visit(IASTStatement statement) {
if (statement instanceof IASTForStatement) {
IASTForStatement forStatement = (IASTForStatement) statement;
IASTCompoundStatement compoundStatement = (IASTCompoundStatement) forStatement.getBody();
addIntDeclaration(modStore, compoundStatement, "i"); //$NON-NLS-1$
addIntDeclaration(modStore, compoundStatement, "j"); //$NON-NLS-1$
}
return PROCESS_CONTINUE;
}
private void addIntDeclaration(final ASTModificationStore modStore,
IASTCompoundStatement compoundStatement, String variableName) {
CPPNodeFactory nf = CPPNodeFactory.getDefault();
ICPPASTSimpleDeclSpecifier newSimpleDeclSpecifier = nf.newSimpleDeclSpecifier();
newSimpleDeclSpecifier.setType(IASTSimpleDeclSpecifier.t_int);
IASTSimpleDeclaration newSimpleDeclaration = nf.newSimpleDeclaration(newSimpleDeclSpecifier);
newSimpleDeclaration.addDeclarator(nf.newDeclarator(nf.newName(variableName.toCharArray())));
IASTDeclarationStatement newDeclaration = nf.newDeclarationStatement(newSimpleDeclaration);
ASTModification modification = new ASTModification(ASTModification.ModificationKind.APPEND_CHILD, compoundStatement, newDeclaration, null);
modStore.storeModification(null, modification);
}
};
}
}

View file

@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 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.core.parser.tests.rewrite.changegenerator.insertbefore;
import junit.framework.Test;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
public class SelfInsertionTest extends ChangeGeneratorTest {
public SelfInsertionTest(){
super("Self Insertion Test"); //$NON-NLS-1$
}
@Override
protected void setUp() throws Exception {
source = "void foo(){\r\n\r\n for(int i = 0; i < 10; i++){\r\n }\r\n}\r\n"; //$NON-NLS-1$
expectedSource = "void foo(){\r\n\r\n for(int i = 0; i < 10; i++){\r\n for(int i = 0;i < 10;i++){\r\n }\r\n }\r\n}\r\n"; //$NON-NLS-1$
super.setUp();
}
public static Test suite() {
return new SelfInsertionTest();
}
@Override
protected CPPASTVisitor createModificator(
final ASTModificationStore modStore) {
return new CPPASTVisitor() {
{
shouldVisitStatements = true;
}
@Override
public int visit(IASTStatement statement) {
if (statement instanceof IASTForStatement) {
IASTForStatement forStatement = (IASTForStatement) statement;
IASTCompoundStatement compoundStatement = (IASTCompoundStatement) forStatement.getBody();
ASTModification modification = new ASTModification(ASTModification.ModificationKind.APPEND_CHILD, compoundStatement, forStatement, null);
modStore.storeModification(null, modification);
}
return PROCESS_CONTINUE;
}
};
}
}

View file

@ -0,0 +1,74 @@
/*******************************************************************************
* Copyright (c) 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.core.parser.tests.rewrite.changegenerator.replace;
import junit.framework.Test;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTForStatement;
import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPNodeFactory;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
public class MultilineWhitespaceHandlingTest extends ChangeGeneratorTest {
private boolean statementsInserted = false;
public MultilineWhitespaceHandlingTest(){
super("Whitespace Handling in Replace"); //$NON-NLS-1$
}
@Override
protected void setUp() throws Exception {
source = "void foo(){\r\n\r\n for(int i = 0; i < 10; i++){\r\n\r\n }\r\n\r\n"; //$NON-NLS-1$
expectedSource = "void foo(){\r\n\r\n for(int i = 0; i < 10; i++){ int i;\r\n int j;\r\n\r\n }\r\n\r\n"; //$NON-NLS-1$
statementsInserted = false;
super.setUp();
}
public static Test suite() {
return new MultilineWhitespaceHandlingTest();
}
@Override
protected CPPASTVisitor createModificator(
final ASTModificationStore modStore) {
return new CPPASTVisitor() {
{
shouldVisitStatements = true;
}
@Override
public int visit(IASTStatement statement) {
if (statement instanceof IASTCompoundStatement) {
IASTCompoundStatement compoundStatement = (IASTCompoundStatement) statement;
CPPNodeFactory nf = CPPNodeFactory.getDefault();
ASTModification modification = new ASTModification(ASTModification.ModificationKind.APPEND_CHILD, compoundStatement, null, null);
modStore.storeModification(null, modification);
}
return PROCESS_CONTINUE;
}
};
}
}

View file

@ -0,0 +1,77 @@
/*******************************************************************************
* Copyright (c) 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.core.parser.tests.rewrite.changegenerator.replace;
import junit.framework.Test;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTForStatement;
import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPNodeFactory;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
public class ReplaceForLoopBodyTest extends ChangeGeneratorTest {
private boolean forReplaced = false;
public ReplaceForLoopBodyTest(){
super("Replace For-Loop"); //$NON-NLS-1$
}
@Override
protected void setUp() throws Exception {
source = "void foo(){\r\n\r\n for(int i = 0; i < 10; i++){\r\n\r\n }\r\n\r\n for(int j = 0; j < 10; j++){\r\n\r\n }\r\n\r\n}"; //$NON-NLS-1$
expectedSource = "void foo(){\r\n\r\n for(;;);\r\n\r\n for(int j = 0; j < 10; j++){\r\n\r\n }\r\n\r\n}"; //$NON-NLS-1$
forReplaced = false;
super.setUp();
}
public static Test suite() {
return new ReplaceForLoopBodyTest();
}
@Override
protected CPPASTVisitor createModificator(
final ASTModificationStore modStore) {
return new CPPASTVisitor() {
{
shouldVisitStatements = true;
}
@Override
public int visit(IASTStatement statement) {
if (!forReplaced && statement instanceof ICPPASTForStatement) {
ICPPASTForStatement forStatement = (ICPPASTForStatement) statement;
CPPNodeFactory nf = CPPNodeFactory.getDefault();
ICPPASTForStatement newFor = nf.newForStatement();
newFor.setInitializerStatement(nf.newNullStatement());
newFor.setBody(nf.newNullStatement());
ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, forStatement, newFor, null);
modStore.storeModification(null, modification);
forReplaced = true;
}
return PROCESS_CONTINUE;
}
};
}
}

View file

@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 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.core.parser.tests.rewrite.changegenerator.replace;
import junit.framework.Test;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTForStatement;
import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPNodeFactory;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
public class WhitespaceHandlingTest extends ChangeGeneratorTest {
private boolean forReplaced = false;
public WhitespaceHandlingTest(){
super("Whitespace Handling in Replace"); //$NON-NLS-1$
}
@Override
protected void setUp() throws Exception {
source = "void foo(){\r\n\r\n for(int i = 0; i < 10; i++){\r\n\r\n }\r\n\r\n for(int j = 0; j < 10; j++){\r\n\r\n }\r\n\r\n}"; //$NON-NLS-1$
expectedSource = "void foo(){\r\n\r\n for(int i = 0; i < 10; i++);\r\n\r\n for(int j = 0; j < 10; j++){\r\n\r\n }\r\n\r\n}"; //$NON-NLS-1$
forReplaced = false;
super.setUp();
}
public static Test suite() {
return new WhitespaceHandlingTest();
}
@Override
protected CPPASTVisitor createModificator(
final ASTModificationStore modStore) {
return new CPPASTVisitor() {
{
shouldVisitStatements = true;
}
@Override
public int visit(IASTStatement statement) {
if (!forReplaced && statement instanceof ICPPASTForStatement) {
ICPPASTForStatement forStatement = (ICPPASTForStatement) statement;
CPPNodeFactory nf = CPPNodeFactory.getDefault();
ICPPASTForStatement newFor = forStatement.copy();
newFor.setBody(nf.newNullStatement());
ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, forStatement, newFor, null);
modStore.storeModification(null, modification);
forReplaced = true;
}
return PROCESS_CONTINUE;
}
};
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2010 Institute for Software, HSR Hochschule fuer Technik
* 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
@ -30,7 +30,6 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationMap;
@ -56,7 +55,7 @@ import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEditGroup;
public class ChangeGenerator extends CPPASTVisitor {
public class ChangeGenerator extends ASTVisitor {
private final LinkedHashMap<String, Integer> sourceOffsets = new LinkedHashMap<String, Integer>();
@ -105,7 +104,7 @@ public class ChangeGenerator extends CPPASTVisitor {
generateChange(rootNode, this);
}
public void generateChange(IASTNode rootNode, CPPASTVisitor pathProvider)
public void generateChange(IASTNode rootNode, ASTVisitor pathProvider)
throws ProblemRuntimeException {
change = new CompositeChange(Messages.ChangeGenerator_compositeChange);
initParentModList();
@ -217,7 +216,7 @@ public class ChangeGenerator extends CPPASTVisitor {
String synthSource = synthWriter.write(synthNode, fileScope, commentMap);
reformatSynthCode(synthNode, synthSource); /*XXX resultat wird nicht verwendet?*/
createChange(synthNode, synthSource);
int newOffset = synthNode.getFileLocation().getNodeOffset()
+ synthNode.getFileLocation().getNodeLength();
@ -271,43 +270,12 @@ public class ChangeGenerator extends CPPASTVisitor {
}
}
private String reformatSynthCode(IASTNode synthNode, String synthSource) {
private void createChange(IASTNode synthNode, String synthSource) {
IFile relevantFile = FileHelper.getIFilefromIASTNode(synthNode);
StringBuilder formattedCode = new StringBuilder();
String originalCode = originalCodeOfNode(synthNode);
CodeComparer codeComparer = new CodeComparer(originalCode, synthSource);
int lastCommonPositionInOriginalCode = codeComparer
.getLastCommonPositionInOriginalCode();
if (lastCommonPositionInOriginalCode > -1) {
formattedCode.append(originalCode.substring(0,
lastCommonPositionInOriginalCode + 1));
}
int lastCommonPositionInSynthCode = codeComparer
.getLastCommonPositionInSynthCode();
int firstPositionOfCommonEndInSynthCode = codeComparer
.getFirstPositionOfCommonEndInSynthCode(lastCommonPositionInSynthCode, lastCommonPositionInOriginalCode);
int firstPositionOfCommonEndInOriginalCode = codeComparer
.getFirstPositionOfCommonEndInOriginalCode(lastCommonPositionInOriginalCode, lastCommonPositionInSynthCode);
if (firstPositionOfCommonEndInSynthCode == -1) {
formattedCode.append(synthSource
.substring(lastCommonPositionInSynthCode + 1));
} else {
if (lastCommonPositionInSynthCode + 1 < firstPositionOfCommonEndInSynthCode) {
formattedCode.append(synthSource.substring(
lastCommonPositionInSynthCode + 1,
firstPositionOfCommonEndInSynthCode));
}
}
if (firstPositionOfCommonEndInOriginalCode > -1) {
formattedCode.append(originalCode
.substring(firstPositionOfCommonEndInOriginalCode));
}
MultiTextEdit edit;
if (changes.containsKey(relevantFile)) {
edit = changes.get(relevantFile);
@ -317,8 +285,6 @@ public class ChangeGenerator extends CPPASTVisitor {
}
codeComparer.createChange(edit, synthNode);
return formattedCode.toString();
}
public String originalCodeOfNode(IASTNode node) {
@ -481,130 +447,133 @@ public class ChangeGenerator extends CPPASTVisitor {
private final StringBuilder originalCode;
private final StringBuilder synthCode;
private int lastCommonInSynthStart;
private int lastCommonInOriginalStart;
private int firstCommonInSynthEnd;
private int firstCommonInOriginalEnd;
public CodeComparer(String originalCode, String synthCode) {
this.originalCode = new StringBuilder(originalCode);
this.synthCode = new StringBuilder(synthCode);
calculatePositions();
}
private void calculatePositions(){
lastCommonInSynthStart = calcLastCommonPositionInSynthCode();
lastCommonInOriginalStart = calcLastCommonPositionInOriginalCode();
firstCommonInSynthEnd = calcFirstPositionOfCommonEndInSynthCode(lastCommonInSynthStart, lastCommonInOriginalStart);
firstCommonInOriginalEnd = calcFirstPositionOfCommonEndInOriginalCode(lastCommonInOriginalStart, lastCommonInSynthStart);
trimTrailingNewlines();
}
private void trimTrailingNewlines() {
int prevOrigEnd = firstCommonInOriginalEnd - 1;
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')){
firstCommonInOriginalEnd++;
}
int prevSynthEnd = firstCommonInSynthEnd - 1;
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')){
firstCommonInSynthEnd++;
}
}
public int getLastCommonPositionInSynthCode() {
int lastCommonPosition = -1;
int originalCodePosition = -1;
int synthCodePosition = -1;
do {
lastCommonPosition = synthCodePosition;
originalCodePosition = nextInterrestingPosition(originalCode,
originalCodePosition);
synthCodePosition = nextInterrestingPosition(synthCode,
synthCodePosition);
} while (originalCodePosition > -1
&& synthCodePosition > -1
&& originalCode.charAt(originalCodePosition) == synthCode
.charAt(synthCodePosition));
return lastCommonPosition;
return lastCommonInSynthStart;
}
public int getLastCommonPositionInOriginalCode() {
int lastCommonPosition = -1;
int originalCodePosition = -1;
int synthCodePosition = -1;
do {
lastCommonPosition = originalCodePosition;
originalCodePosition = nextInterrestingPosition(originalCode,
originalCodePosition);
synthCodePosition = nextInterrestingPosition(synthCode,
synthCodePosition);
} while (originalCodePosition > -1
&& synthCodePosition > -1
&& originalCode.charAt(originalCodePosition) == synthCode
.charAt(synthCodePosition));
return lastCommonPosition;
return lastCommonInOriginalStart;
}
public int getFirstPositionOfCommonEndInOriginalCode() {
return firstCommonInOriginalEnd;
}
public int getFirstPositionOfCommonEndInOriginalCode(int originalLimit, int synthLimit) {
public int getFirstPositionOfCommonEndInSynthCode() {
return firstCommonInSynthEnd;
}
int lastCommonPosition = -1;
int originalCodePosition = -1;
int synthCodePosition = -1;
public int calcLastCommonPositionInSynthCode() {
return findLastCommonPosition(synthCode, originalCode);
}
public int calcLastCommonPositionInOriginalCode() {
return findLastCommonPosition(originalCode, synthCode);
}
private int calcFirstPositionOfCommonEndInOriginalCode(int originalLimit, int synthLimit) {
StringBuilder reverseOriginalCode = new StringBuilder(originalCode)
.reverse();
StringBuilder reverseSynthCode = new StringBuilder(synthCode)
.reverse();
do {
lastCommonPosition = originalCodePosition;
originalCodePosition = nextInterrestingPosition(
reverseOriginalCode, originalCodePosition);
synthCodePosition = nextInterrestingPosition(reverseSynthCode,
synthCodePosition);
} while (originalCodePosition > -1
&& originalCodePosition < originalCode.length() - originalLimit
&& synthCodePosition > -1
&& synthCodePosition < synthCode.length() - synthLimit
&& reverseOriginalCode.charAt(originalCodePosition) == reverseSynthCode
.charAt(synthCodePosition));
int lastCommonPosition = findLastCommonPosition(reverseOriginalCode, reverseSynthCode,
reverseOriginalCode.length() - originalLimit - 1, reverseSynthCode.length() - synthLimit - 1);
if (lastCommonPosition < 0
|| lastCommonPosition >= originalCode.length()) {
return -1;
}
return originalCode.length() - lastCommonPosition;
return originalCode.length() - lastCommonPosition -1;
}
public int getFirstPositionOfCommonEndInSynthCode(int limmit, int lastCommonPositionInOriginal) {
int lastCommonPosition = 0;
int originalCodePosition = -1;
int synthCodePosition = -1;
int korOffset = 0;
private int calcFirstPositionOfCommonEndInSynthCode(int synthLimit, int originalLimit) {
StringBuilder reverseOriginalCode = new StringBuilder(originalCode)
.reverse();
StringBuilder reverseSynthCode = new StringBuilder(synthCode)
.reverse();
do {
if (lastCommonPosition >= 0
&& lastCommonPositionInOriginal >= 0
&& lastCommonPositionInOriginal - korOffset >= 0
&& originalCode.charAt(lastCommonPositionInOriginal
- korOffset) == reverseSynthCode
.charAt(lastCommonPosition)) {
++korOffset;
} else {
korOffset = 0;
}
lastCommonPosition = synthCodePosition;
originalCodePosition = nextInterrestingPosition(
reverseOriginalCode, originalCodePosition);
synthCodePosition = nextInterrestingPosition(reverseSynthCode,
synthCodePosition);
} while (originalCodePosition > -1
&& originalCodePosition < originalCode.length() - lastCommonPositionInOriginal
&& synthCodePosition > -1
&& synthCodePosition < synthCode.length() - limmit
&& reverseOriginalCode.charAt(originalCodePosition) == reverseSynthCode
.charAt(synthCodePosition));
int lastCommonPosition = findLastCommonPosition(reverseSynthCode, reverseOriginalCode,
reverseSynthCode.length() - synthLimit -1, reverseOriginalCode.length() - originalLimit -1);
if (lastCommonPosition < 0
|| lastCommonPosition >= synthCode.length()) {
return -1;
}
if (korOffset > 0) {
--korOffset;
}
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){
int firstIndex = -1;
int secondIndex = -1;
int lastCommonIndex = -1;
return synthCode.length() - lastCommonPosition + korOffset;
do {
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));
return lastCommonIndex;
}
private int nextInterrestingPosition(StringBuilder code, int position) {
@ -648,43 +617,37 @@ public class ChangeGenerator extends CPPASTVisitor {
private void createChange(MultiTextEdit edit, int changeOffset) {
int lastCommonPositionInOriginal = getLastCommonPositionInOriginalCode();
int lastCommonPositionInSynth = getLastCommonPositionInSynthCode();
int firstOfCommonEndInOriginal = getFirstPositionOfCommonEndInOriginalCode(lastCommonPositionInOriginal, lastCommonPositionInSynth);
int firstOfCommonEndInSynth = getFirstPositionOfCommonEndInSynthCode(
lastCommonPositionInSynth, lastCommonPositionInOriginal);
int i = (firstOfCommonEndInSynth >= 0 ? firstOfCommonEndInOriginal
int i = (firstCommonInSynthEnd >= 0 ? firstCommonInOriginalEnd
: originalCode.length())
- lastCommonPositionInOriginal;
- lastCommonInOriginalStart;
if (i <= 0) {
String insertCode = synthCode.substring(
lastCommonPositionInSynth, firstOfCommonEndInSynth);
lastCommonInSynthStart, firstCommonInSynthEnd);
InsertEdit iEdit = new InsertEdit(changeOffset
+ lastCommonPositionInOriginal, insertCode);
+ lastCommonInOriginalStart, insertCode);
edit.addChild(iEdit);
} else if ((firstOfCommonEndInSynth >= 0 ? firstOfCommonEndInSynth
} else if ((firstCommonInSynthEnd >= 0 ? firstCommonInSynthEnd
: synthCode.length())
- lastCommonPositionInSynth <= 0) {
- lastCommonInSynthStart <= 0) {
int correction = 0;
if (lastCommonPositionInSynth > firstOfCommonEndInSynth) {
correction = lastCommonPositionInSynth
- firstOfCommonEndInSynth;
if (lastCommonInSynthStart > firstCommonInSynthEnd) {
correction = lastCommonInSynthStart
- firstCommonInSynthEnd;
}
DeleteEdit dEdit = new DeleteEdit(changeOffset
+ lastCommonPositionInOriginal,
firstOfCommonEndInOriginal
- lastCommonPositionInOriginal + correction);
+ lastCommonInOriginalStart,
firstCommonInOriginalEnd
- lastCommonInOriginalStart + correction);
edit.addChild(dEdit);
} else {
} else {
String replacementCode = getReplacementCode(
lastCommonPositionInSynth, firstOfCommonEndInSynth);
lastCommonInSynthStart, firstCommonInSynthEnd);
ReplaceEdit rEdit = new ReplaceEdit(
changeOffset
+ Math.max(lastCommonPositionInOriginal, 0),
(firstOfCommonEndInOriginal >= 0 ? firstOfCommonEndInOriginal
+ Math.max(lastCommonInOriginalStart, 0),
(firstCommonInOriginalEnd >= 0 ? firstCommonInOriginalEnd
: originalCode.length())
- Math.max(lastCommonPositionInOriginal, 0),
- Math.max(lastCommonInOriginalStart, 0),
replacementCode);
edit.addChild(rEdit);
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* 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
@ -12,10 +12,12 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
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.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName;
@ -25,14 +27,12 @@ import org.eclipse.cdt.core.dom.ast.IASTProblem;
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.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
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.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
@ -51,7 +51,7 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
private final String fileScope;
private ModificationScopeStack stack;
public ChangeGeneratorWriterVisitor(CPPASTVisitor delegateVisitor,
public ChangeGeneratorWriterVisitor(ASTVisitor delegateVisitor,
ASTModificationStore modificationStore, String fileScope, NodeCommentMap commentMap) {
super(commentMap);

View file

@ -236,7 +236,7 @@ int main(){
int i;
exp(i);
return i;
return i;
}
//@.config
@ -2590,6 +2590,7 @@ void exp()
int main(int argc, char **argv) {
exp();
}
//!Bug#264712 Refactor extract function deletes comments in header

View file

@ -250,7 +250,7 @@ int main(){
int i;
exp(i);
return i;
return i;
}
//@refScript.xml

View file

@ -99,8 +99,8 @@ public:
{
return name;
}
int getSystemId(){
int getSystemId(){
return systemId;
}
@ -217,8 +217,8 @@ namespace Personal {
{
return name;
}
int getSystemId(){
int getSystemId(){
return systemId;
}
@ -334,8 +334,8 @@ public:
{
this->name = name;
}
int getSystemId(){
int getSystemId(){
return systemId;
}
@ -456,8 +456,8 @@ public:
{
this->name = name;
}
int getSystemId(){
int getSystemId(){
return systemId;
}
@ -568,7 +568,8 @@ public:
this->systemId = systemId;
}
Person(int socSecNo); // contructor
Person(int socSecNo); // contructor
~Person(); // destructor
@ -691,7 +692,6 @@ public:
{
this->id = id;
}
};
#endif /*C_H_*/
@ -744,8 +744,7 @@ public:
{
this->i = i;
}
//comment3
//comment3
};
#endif /* TEST_H_ */
@ -934,7 +933,7 @@ namespace Personal {
return name;
}
int Person::SocSecNo() {
int Person::SocSecNo() {
return socSecNo;
}
@ -1473,8 +1472,8 @@ namespace foo
{
this->testField = testField;
}
void Test::foo()
void Test::foo()
{
}

View file

@ -850,6 +850,7 @@ public:
private:
void methode2();
};
#endif /* HIDEMETHOD_H_ */
@ -1219,7 +1220,7 @@ struct A{
struct A{
private:
void method2();
void method2();
};
#endif /*A_H_*/
@ -1263,7 +1264,7 @@ public:
private:
void set(bool b)
{
}
}
};
//!HideMethod CheckIfPrivateBug 2
@ -1305,7 +1306,7 @@ public:
private:
void set(bool b)
{
}
}
};
//!HideMethod CheckIfPrivateBug 3
@ -1334,7 +1335,7 @@ public:
struct other
{
private:
bool value() {return true;}
bool value() {return true;}
};
class Klass
@ -1381,7 +1382,7 @@ public:
void set(bool b){}
private:
void test()
void test()
{
other o;
this->set(o.value());

View file

@ -221,8 +221,7 @@ namespace NameSpace
}
void ClassInNamespace::test2()
{
}
{}
}
//!virtual method in the middle of con/destructor, without parameters and void return value
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
@ -542,8 +541,7 @@ namespace OuterSpace {
}
int NameSpace::test2()
{
}
{}
}
//!implement function within namespaces
//#org.eclipse.cdt.ui.tests.refactoring.implementmethod.ImplementMethodRefactoringTest
@ -576,8 +574,7 @@ namespace OuterSpace {
}
int test2()
{
}
{}
}
}
//!class template member functions with multiple templates