mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-19 15:05:36 +02:00
Cosmetics and code streamlining.
Change-Id: I2c5479f4259cafe5d98cb203f3af2d4aac62cfbe
This commit is contained in:
parent
d72b4df3eb
commit
d3b62dd5ba
5 changed files with 46 additions and 50 deletions
|
@ -14,7 +14,7 @@ package org.eclipse.cdt.internal.core.dom.rewrite;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -29,7 +29,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKin
|
|||
* @since 5.0
|
||||
*/
|
||||
public class ASTModificationMap {
|
||||
private final Map<IASTNode, List<ASTModification>> fModifications= new HashMap<IASTNode, List<ASTModification>>();
|
||||
private final Map<IASTNode, List<ASTModification>> fModifications= new IdentityHashMap<>();
|
||||
|
||||
/**
|
||||
* Adds a modification to this modification map.
|
||||
|
@ -38,7 +38,7 @@ public class ASTModificationMap {
|
|||
IASTNode targetNode = mod.getTargetNode();
|
||||
List<ASTModification> mods= fModifications.get(targetNode);
|
||||
if (mods == null || mods.isEmpty()) {
|
||||
mods= new ArrayList<ASTModification>();
|
||||
mods= new ArrayList<>();
|
||||
mods.add(mod);
|
||||
fModifications.put(targetNode, mods);
|
||||
} else {
|
||||
|
|
|
@ -69,7 +69,7 @@ import org.eclipse.text.edits.TextEditGroup;
|
|||
|
||||
public class ChangeGenerator extends ASTVisitor {
|
||||
private final Map<IASTNode, Map<ModificationKind, List<ASTModification>>> classifiedModifications =
|
||||
new HashMap<IASTNode, Map<ModificationKind, List<ASTModification>>>();
|
||||
new HashMap<>();
|
||||
private int processedOffset;
|
||||
private MultiTextEdit rootEdit;
|
||||
private CompositeChange change;
|
||||
|
@ -130,13 +130,13 @@ public class ChangeGenerator extends ASTVisitor {
|
|||
for (ASTModification modification : modifications) {
|
||||
Map<ModificationKind, List<ASTModification>> map = classifiedModifications.get(node);
|
||||
if (map == null) {
|
||||
map = new TreeMap<ModificationKind, List<ASTModification>>();
|
||||
map = new TreeMap<>();
|
||||
classifiedModifications.put(node, map);
|
||||
}
|
||||
ModificationKind kind = modification.getKind();
|
||||
List<ASTModification> list = map.get(kind);
|
||||
if (list == null) {
|
||||
list = new ArrayList<ASTModification>(2);
|
||||
list = new ArrayList<>(2);
|
||||
map.put(kind, list);
|
||||
}
|
||||
list.add(modification);
|
||||
|
|
|
@ -7,37 +7,38 @@
|
|||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Institute for Software - initial API and implementation
|
||||
* Institute for Software - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
|
||||
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind;
|
||||
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationMap;
|
||||
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
|
||||
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind;
|
||||
|
||||
public class ModificationScopeStack {
|
||||
private LinkedList<List<ASTModification>> scopeStack;
|
||||
private ASTModificationStore modStore;
|
||||
private final Deque<List<ASTModification>> scopeStack;
|
||||
private final ASTModificationStore modStore;
|
||||
|
||||
public ModificationScopeStack(ASTModificationStore modificationStore) {
|
||||
scopeStack = new LinkedList<List<ASTModification>>();
|
||||
scopeStack = new ArrayDeque<>();
|
||||
modStore = modificationStore;
|
||||
ArrayList<ASTModification> nullModList = new ArrayList<ASTModification>();
|
||||
ArrayList<ASTModification> nullModList = new ArrayList<>();
|
||||
nullModList.add(null);
|
||||
scopeStack.addFirst(nullModList);
|
||||
}
|
||||
|
||||
public void pushScope(IASTNode node) {
|
||||
List<ASTModification> newMods = new ArrayList<ASTModification>();
|
||||
for(ASTModification peekMod : scopeStack.peek()) {
|
||||
List<ASTModification> newMods = new ArrayList<>();
|
||||
for (ASTModification peekMod : scopeStack.peek()) {
|
||||
ASTModificationMap nestedMods = modStore.getNestedModifications(peekMod);
|
||||
if (nestedMods != null) {
|
||||
newMods.addAll(nestedMods.getModificationsForNode(node));
|
||||
|
@ -59,13 +60,13 @@ public class ModificationScopeStack {
|
|||
|
||||
public void popScope(IASTNode node) {
|
||||
List<ASTModification> peek = scopeStack.peek();
|
||||
if (peek != null) {
|
||||
if (!peek.isEmpty() && peek.get(0)!=null) {
|
||||
if( peek.get(0).getKind() == ModificationKind.REPLACE){
|
||||
if(peek.get(0).getTargetNode() == node)
|
||||
if (peek != null && !peek.isEmpty()) {
|
||||
ASTModification modification = peek.get(0);
|
||||
if (modification != null) {
|
||||
if (modification.getKind() == ModificationKind.REPLACE) {
|
||||
if (modification.getTargetNode() == node)
|
||||
scopeStack.removeFirst();
|
||||
}
|
||||
else if(peek.get(0).getNewNode() == node){
|
||||
} else if (modification.getNewNode() == node) {
|
||||
scopeStack.removeFirst();
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +78,7 @@ public class ModificationScopeStack {
|
|||
if (aktModList == null) {
|
||||
return getNestedModifiedNodes();
|
||||
}
|
||||
Collection<IASTNode> nodes = new ArrayList<IASTNode>();
|
||||
Collection<IASTNode> nodes = new ArrayList<>();
|
||||
for (ASTModification modification : aktModList) {
|
||||
ASTModificationMap nestedModifications = modStore.getNestedModifications(modification);
|
||||
if (nestedModifications != null) {
|
||||
|
@ -89,7 +90,7 @@ public class ModificationScopeStack {
|
|||
|
||||
private Collection<IASTNode> getNestedModifiedNodes() {
|
||||
ASTModificationMap rootModifications = modStore.getRootModifications();
|
||||
if(rootModifications == null) {
|
||||
if (rootModifications == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return rootModifications.getModifiedNodes();
|
||||
|
@ -100,10 +101,10 @@ public class ModificationScopeStack {
|
|||
if (aktModList == null) {
|
||||
return getNestedModifikationsForNode(node);
|
||||
}
|
||||
List<ASTModification> modForNodeList = new ArrayList<ASTModification>();
|
||||
List<ASTModification> modForNodeList = new ArrayList<>();
|
||||
for (ASTModification modification : aktModList) {
|
||||
ASTModificationMap nestedModifications = modStore.getNestedModifications(modification);
|
||||
if(nestedModifications != null) {
|
||||
if (nestedModifications != null) {
|
||||
modForNodeList.addAll(nestedModifications.getModificationsForNode(node));
|
||||
}
|
||||
}
|
||||
|
@ -111,46 +112,41 @@ public class ModificationScopeStack {
|
|||
}
|
||||
|
||||
public void clean(IASTNode actualNode) {
|
||||
while(scopeStack.size() > 1){
|
||||
while (scopeStack.size() > 1) {
|
||||
for (IASTNode currentModifiedNode : getModifiedNodes()) {
|
||||
for (ASTModification currentMod : getModificationsForNode(currentModifiedNode)) {
|
||||
if(currentMod.getNewNode() == actualNode){
|
||||
if (currentMod.getNewNode() == actualNode) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!nodeIsChildOfModifications(actualNode, scopeStack.getFirst())){
|
||||
if(scopeStack.getFirst().get(0).getTargetNode().getTranslationUnit() == actualNode.getTranslationUnit()){
|
||||
if (!nodeIsChildOfModifications(actualNode, scopeStack.getFirst())) {
|
||||
if (scopeStack.getFirst().get(0).getTargetNode().getTranslationUnit() == actualNode.getTranslationUnit()) {
|
||||
scopeStack.removeFirst();
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean nodeIsChildOfModifications(IASTNode actualNode,
|
||||
List<ASTModification> modifications) {
|
||||
for(ASTModification currentModification : modifications){
|
||||
if(currentModification != null && nodeIsChildOfModification(currentModification, actualNode)){
|
||||
private boolean nodeIsChildOfModifications(IASTNode actualNode, List<ASTModification> modifications) {
|
||||
for (ASTModification currentModification : modifications) {
|
||||
if (currentModification != null && nodeIsChildOfModification(currentModification, actualNode)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean nodeIsChildOfModification(
|
||||
ASTModification modification, IASTNode actualNode) {
|
||||
private boolean nodeIsChildOfModification(ASTModification modification, IASTNode actualNode) {
|
||||
IASTNode nodeToTest = actualNode;
|
||||
while(nodeToTest != null){
|
||||
if(modification.getNewNode() == nodeToTest){
|
||||
while (nodeToTest != null) {
|
||||
if (modification.getNewNode() == nodeToTest) {
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
nodeToTest = nodeToTest.getParent();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -78,7 +78,7 @@ public abstract class CRefactoring extends Refactoring {
|
|||
this.project = project;
|
||||
this.initStatus= new RefactoringStatus();
|
||||
if (!(element instanceof ISourceReference)) {
|
||||
this.tu = null;
|
||||
tu = null;
|
||||
initStatus.addFatalError(Messages.Refactoring_SelectionNotValid);
|
||||
return;
|
||||
}
|
||||
|
@ -87,11 +87,11 @@ public abstract class CRefactoring extends Refactoring {
|
|||
tu = CModelUtil.toWorkingCopy(sourceRef.getTranslationUnit());
|
||||
|
||||
if (selection instanceof ITextSelection) {
|
||||
this.selectedRegion = SelectionHelper.getRegion(selection);
|
||||
selectedRegion = SelectionHelper.getRegion(selection);
|
||||
} else {
|
||||
try {
|
||||
ISourceRange sourceRange = sourceRef.getSourceRange();
|
||||
this.selectedRegion = new Region(sourceRange.getIdStartPos(), sourceRange.getIdLength());
|
||||
selectedRegion = new Region(sourceRange.getIdStartPos(), sourceRange.getIdLength());
|
||||
} catch (CModelException e) {
|
||||
CUIPlugin.log(e);
|
||||
}
|
||||
|
|
|
@ -37,9 +37,8 @@ import org.eclipse.cdt.internal.ui.refactoring.changes.CreateFileChange;
|
|||
public class ModificationCollector {
|
||||
private final IResourceChangeDescriptionFactory deltaFactory;
|
||||
|
||||
// Each translation unit can have only one ASTRewrite
|
||||
private final Map<IASTTranslationUnit, ASTRewrite> rewriters =
|
||||
new HashMap<IASTTranslationUnit, ASTRewrite>();
|
||||
// Each translation unit can have only one ASTRewrite.
|
||||
private final Map<IASTTranslationUnit, ASTRewrite> rewriters = new HashMap<>();
|
||||
|
||||
private Collection<CreateFileChange> changes;
|
||||
|
||||
|
@ -63,7 +62,7 @@ public class ModificationCollector {
|
|||
// Creating new files doesn't concern the rewriter, the refactorings can add them here as needed.
|
||||
public void addFileChange(CreateFileChange change) {
|
||||
if (changes == null) {
|
||||
changes = new ArrayList<CreateFileChange>();
|
||||
changes = new ArrayList<>();
|
||||
}
|
||||
changes.add(change);
|
||||
if (deltaFactory != null)
|
||||
|
@ -92,6 +91,7 @@ public class ModificationCollector {
|
|||
/**
|
||||
* If {@code change} is a CompositeChange, merges it into the {@code receiver}, otherwise
|
||||
* adds it to the {@code receiver}.
|
||||
*
|
||||
* @param change The change being added.
|
||||
* @param receiver The composite change that receives the addition.
|
||||
*/
|
||||
|
|
Loading…
Add table
Reference in a new issue