mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-21 16:05:25 +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.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.IdentityHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKin
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
public class ASTModificationMap {
|
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.
|
* Adds a modification to this modification map.
|
||||||
|
@ -38,7 +38,7 @@ public class ASTModificationMap {
|
||||||
IASTNode targetNode = mod.getTargetNode();
|
IASTNode targetNode = mod.getTargetNode();
|
||||||
List<ASTModification> mods= fModifications.get(targetNode);
|
List<ASTModification> mods= fModifications.get(targetNode);
|
||||||
if (mods == null || mods.isEmpty()) {
|
if (mods == null || mods.isEmpty()) {
|
||||||
mods= new ArrayList<ASTModification>();
|
mods= new ArrayList<>();
|
||||||
mods.add(mod);
|
mods.add(mod);
|
||||||
fModifications.put(targetNode, mods);
|
fModifications.put(targetNode, mods);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -69,7 +69,7 @@ import org.eclipse.text.edits.TextEditGroup;
|
||||||
|
|
||||||
public class ChangeGenerator extends ASTVisitor {
|
public class ChangeGenerator extends ASTVisitor {
|
||||||
private final Map<IASTNode, Map<ModificationKind, List<ASTModification>>> classifiedModifications =
|
private final Map<IASTNode, Map<ModificationKind, List<ASTModification>>> classifiedModifications =
|
||||||
new HashMap<IASTNode, Map<ModificationKind, List<ASTModification>>>();
|
new HashMap<>();
|
||||||
private int processedOffset;
|
private int processedOffset;
|
||||||
private MultiTextEdit rootEdit;
|
private MultiTextEdit rootEdit;
|
||||||
private CompositeChange change;
|
private CompositeChange change;
|
||||||
|
@ -130,13 +130,13 @@ public class ChangeGenerator extends ASTVisitor {
|
||||||
for (ASTModification modification : modifications) {
|
for (ASTModification modification : modifications) {
|
||||||
Map<ModificationKind, List<ASTModification>> map = classifiedModifications.get(node);
|
Map<ModificationKind, List<ASTModification>> map = classifiedModifications.get(node);
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new TreeMap<ModificationKind, List<ASTModification>>();
|
map = new TreeMap<>();
|
||||||
classifiedModifications.put(node, map);
|
classifiedModifications.put(node, map);
|
||||||
}
|
}
|
||||||
ModificationKind kind = modification.getKind();
|
ModificationKind kind = modification.getKind();
|
||||||
List<ASTModification> list = map.get(kind);
|
List<ASTModification> list = map.get(kind);
|
||||||
if (list == null) {
|
if (list == null) {
|
||||||
list = new ArrayList<ASTModification>(2);
|
list = new ArrayList<>(2);
|
||||||
map.put(kind, list);
|
map.put(kind, list);
|
||||||
}
|
}
|
||||||
list.add(modification);
|
list.add(modification);
|
||||||
|
|
|
@ -11,32 +11,33 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
|
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.Deque;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
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;
|
||||||
|
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.ASTModificationMap;
|
||||||
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
|
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
|
||||||
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind;
|
|
||||||
|
|
||||||
public class ModificationScopeStack {
|
public class ModificationScopeStack {
|
||||||
private LinkedList<List<ASTModification>> scopeStack;
|
private final Deque<List<ASTModification>> scopeStack;
|
||||||
private ASTModificationStore modStore;
|
private final ASTModificationStore modStore;
|
||||||
|
|
||||||
public ModificationScopeStack(ASTModificationStore modificationStore) {
|
public ModificationScopeStack(ASTModificationStore modificationStore) {
|
||||||
scopeStack = new LinkedList<List<ASTModification>>();
|
scopeStack = new ArrayDeque<>();
|
||||||
modStore = modificationStore;
|
modStore = modificationStore;
|
||||||
ArrayList<ASTModification> nullModList = new ArrayList<ASTModification>();
|
ArrayList<ASTModification> nullModList = new ArrayList<>();
|
||||||
nullModList.add(null);
|
nullModList.add(null);
|
||||||
scopeStack.addFirst(nullModList);
|
scopeStack.addFirst(nullModList);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pushScope(IASTNode node) {
|
public void pushScope(IASTNode node) {
|
||||||
List<ASTModification> newMods = new ArrayList<ASTModification>();
|
List<ASTModification> newMods = new ArrayList<>();
|
||||||
for (ASTModification peekMod : scopeStack.peek()) {
|
for (ASTModification peekMod : scopeStack.peek()) {
|
||||||
ASTModificationMap nestedMods = modStore.getNestedModifications(peekMod);
|
ASTModificationMap nestedMods = modStore.getNestedModifications(peekMod);
|
||||||
if (nestedMods != null) {
|
if (nestedMods != null) {
|
||||||
|
@ -59,13 +60,13 @@ public class ModificationScopeStack {
|
||||||
|
|
||||||
public void popScope(IASTNode node) {
|
public void popScope(IASTNode node) {
|
||||||
List<ASTModification> peek = scopeStack.peek();
|
List<ASTModification> peek = scopeStack.peek();
|
||||||
if (peek != null) {
|
if (peek != null && !peek.isEmpty()) {
|
||||||
if (!peek.isEmpty() && peek.get(0)!=null) {
|
ASTModification modification = peek.get(0);
|
||||||
if( peek.get(0).getKind() == ModificationKind.REPLACE){
|
if (modification != null) {
|
||||||
if(peek.get(0).getTargetNode() == node)
|
if (modification.getKind() == ModificationKind.REPLACE) {
|
||||||
|
if (modification.getTargetNode() == node)
|
||||||
scopeStack.removeFirst();
|
scopeStack.removeFirst();
|
||||||
}
|
} else if (modification.getNewNode() == node) {
|
||||||
else if(peek.get(0).getNewNode() == node){
|
|
||||||
scopeStack.removeFirst();
|
scopeStack.removeFirst();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,7 +78,7 @@ public class ModificationScopeStack {
|
||||||
if (aktModList == null) {
|
if (aktModList == null) {
|
||||||
return getNestedModifiedNodes();
|
return getNestedModifiedNodes();
|
||||||
}
|
}
|
||||||
Collection<IASTNode> nodes = new ArrayList<IASTNode>();
|
Collection<IASTNode> nodes = new ArrayList<>();
|
||||||
for (ASTModification modification : aktModList) {
|
for (ASTModification modification : aktModList) {
|
||||||
ASTModificationMap nestedModifications = modStore.getNestedModifications(modification);
|
ASTModificationMap nestedModifications = modStore.getNestedModifications(modification);
|
||||||
if (nestedModifications != null) {
|
if (nestedModifications != null) {
|
||||||
|
@ -100,7 +101,7 @@ public class ModificationScopeStack {
|
||||||
if (aktModList == null) {
|
if (aktModList == null) {
|
||||||
return getNestedModifikationsForNode(node);
|
return getNestedModifikationsForNode(node);
|
||||||
}
|
}
|
||||||
List<ASTModification> modForNodeList = new ArrayList<ASTModification>();
|
List<ASTModification> modForNodeList = new ArrayList<>();
|
||||||
for (ASTModification modification : aktModList) {
|
for (ASTModification modification : aktModList) {
|
||||||
ASTModificationMap nestedModifications = modStore.getNestedModifications(modification);
|
ASTModificationMap nestedModifications = modStore.getNestedModifications(modification);
|
||||||
if (nestedModifications != null) {
|
if (nestedModifications != null) {
|
||||||
|
@ -122,19 +123,16 @@ public class ModificationScopeStack {
|
||||||
if (!nodeIsChildOfModifications(actualNode, scopeStack.getFirst())) {
|
if (!nodeIsChildOfModifications(actualNode, scopeStack.getFirst())) {
|
||||||
if (scopeStack.getFirst().get(0).getTargetNode().getTranslationUnit() == actualNode.getTranslationUnit()) {
|
if (scopeStack.getFirst().get(0).getTargetNode().getTranslationUnit() == actualNode.getTranslationUnit()) {
|
||||||
scopeStack.removeFirst();
|
scopeStack.removeFirst();
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean nodeIsChildOfModifications(IASTNode actualNode,
|
private boolean nodeIsChildOfModifications(IASTNode actualNode, List<ASTModification> modifications) {
|
||||||
List<ASTModification> modifications) {
|
|
||||||
for (ASTModification currentModification : modifications) {
|
for (ASTModification currentModification : modifications) {
|
||||||
if (currentModification != null && nodeIsChildOfModification(currentModification, actualNode)) {
|
if (currentModification != null && nodeIsChildOfModification(currentModification, actualNode)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -143,14 +141,12 @@ public class ModificationScopeStack {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean nodeIsChildOfModification(
|
private boolean nodeIsChildOfModification(ASTModification modification, IASTNode actualNode) {
|
||||||
ASTModification modification, IASTNode actualNode) {
|
|
||||||
IASTNode nodeToTest = actualNode;
|
IASTNode nodeToTest = actualNode;
|
||||||
while (nodeToTest != null) {
|
while (nodeToTest != null) {
|
||||||
if (modification.getNewNode() == nodeToTest) {
|
if (modification.getNewNode() == nodeToTest) {
|
||||||
return true;
|
return true;
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
nodeToTest = nodeToTest.getParent();
|
nodeToTest = nodeToTest.getParent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,7 +78,7 @@ public abstract class CRefactoring extends Refactoring {
|
||||||
this.project = project;
|
this.project = project;
|
||||||
this.initStatus= new RefactoringStatus();
|
this.initStatus= new RefactoringStatus();
|
||||||
if (!(element instanceof ISourceReference)) {
|
if (!(element instanceof ISourceReference)) {
|
||||||
this.tu = null;
|
tu = null;
|
||||||
initStatus.addFatalError(Messages.Refactoring_SelectionNotValid);
|
initStatus.addFatalError(Messages.Refactoring_SelectionNotValid);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -87,11 +87,11 @@ public abstract class CRefactoring extends Refactoring {
|
||||||
tu = CModelUtil.toWorkingCopy(sourceRef.getTranslationUnit());
|
tu = CModelUtil.toWorkingCopy(sourceRef.getTranslationUnit());
|
||||||
|
|
||||||
if (selection instanceof ITextSelection) {
|
if (selection instanceof ITextSelection) {
|
||||||
this.selectedRegion = SelectionHelper.getRegion(selection);
|
selectedRegion = SelectionHelper.getRegion(selection);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
ISourceRange sourceRange = sourceRef.getSourceRange();
|
ISourceRange sourceRange = sourceRef.getSourceRange();
|
||||||
this.selectedRegion = new Region(sourceRange.getIdStartPos(), sourceRange.getIdLength());
|
selectedRegion = new Region(sourceRange.getIdStartPos(), sourceRange.getIdLength());
|
||||||
} catch (CModelException e) {
|
} catch (CModelException e) {
|
||||||
CUIPlugin.log(e);
|
CUIPlugin.log(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,9 +37,8 @@ import org.eclipse.cdt.internal.ui.refactoring.changes.CreateFileChange;
|
||||||
public class ModificationCollector {
|
public class ModificationCollector {
|
||||||
private final IResourceChangeDescriptionFactory deltaFactory;
|
private final IResourceChangeDescriptionFactory deltaFactory;
|
||||||
|
|
||||||
// Each translation unit can have only one ASTRewrite
|
// Each translation unit can have only one ASTRewrite.
|
||||||
private final Map<IASTTranslationUnit, ASTRewrite> rewriters =
|
private final Map<IASTTranslationUnit, ASTRewrite> rewriters = new HashMap<>();
|
||||||
new HashMap<IASTTranslationUnit, ASTRewrite>();
|
|
||||||
|
|
||||||
private Collection<CreateFileChange> changes;
|
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.
|
// Creating new files doesn't concern the rewriter, the refactorings can add them here as needed.
|
||||||
public void addFileChange(CreateFileChange change) {
|
public void addFileChange(CreateFileChange change) {
|
||||||
if (changes == null) {
|
if (changes == null) {
|
||||||
changes = new ArrayList<CreateFileChange>();
|
changes = new ArrayList<>();
|
||||||
}
|
}
|
||||||
changes.add(change);
|
changes.add(change);
|
||||||
if (deltaFactory != null)
|
if (deltaFactory != null)
|
||||||
|
@ -92,6 +91,7 @@ public class ModificationCollector {
|
||||||
/**
|
/**
|
||||||
* If {@code change} is a CompositeChange, merges it into the {@code receiver}, otherwise
|
* If {@code change} is a CompositeChange, merges it into the {@code receiver}, otherwise
|
||||||
* adds it to the {@code receiver}.
|
* adds it to the {@code receiver}.
|
||||||
|
*
|
||||||
* @param change The change being added.
|
* @param change The change being added.
|
||||||
* @param receiver The composite change that receives the addition.
|
* @param receiver The composite change that receives the addition.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Reference in a new issue