Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f73f23bc043f7628ecc3f28a9b90097720438c54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*******************************************************************************
 * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik  
 * Rapperswil, University of applied sciences and others
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html  
 *  
 * Contributors: 
 * Institute for Software - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
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.ASTModificationMap;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;

public class ModificationScopeStack {
	private LinkedList<List<ASTModification>> scopeStack;
	private ASTModificationStore modStore;

	public ModificationScopeStack(ASTModificationStore modificationStore) {
		scopeStack = new LinkedList<List<ASTModification>>();
		modStore = modificationStore;
		ArrayList<ASTModification> nullModList = new ArrayList<ASTModification>();
		nullModList.add(null);
		scopeStack.addFirst(nullModList);
	}
	
	public void pushScope(IASTNode node) {
		List<ASTModification> newMods = new ArrayList<ASTModification>();
		for(ASTModification peekMod : scopeStack.peek()) {
			ASTModificationMap nestedMods = modStore.getNestedModifications(peekMod);
			if (nestedMods != null) {
				newMods.addAll(nestedMods.getModificationsForNode(node));
			}
		}
		
		if (!newMods.isEmpty()) {
			scopeStack.addFirst(newMods);
		}
	}

	private List<ASTModification> getNestedModifikationsForNode(IASTNode node) {
		ASTModificationMap rootModifications = modStore.getRootModifications();
		if (rootModifications == null) {
			return Collections.emptyList();
		}
		return rootModifications.getModificationsForNode(node);
	}

	public void popScope(IASTNode node) {
		List<ASTModification> peek = scopeStack.peek();
		if (peek != null) {
			if (!peek.isEmpty() && peek.get(0).getTargetNode() == node) {
				scopeStack.removeFirst();
			}
		}
	}
	
	public Collection<IASTNode> getModifiedNodes() {
		List<ASTModification> aktModList = scopeStack.peek();
		if (aktModList == null) {
			return getNestedModifiedNodes();
		}
		Collection<IASTNode> nodes = new ArrayList<IASTNode>();
		for (ASTModification modification : aktModList) {
			ASTModificationMap nestedModifications = modStore.getNestedModifications(modification);
			if (nestedModifications != null) {
				nodes.addAll(nestedModifications.getModifiedNodes());
			}
		}
		return Collections.unmodifiableCollection(nodes);
	}

	private Collection<IASTNode> getNestedModifiedNodes() {
		ASTModificationMap rootModifications = modStore.getRootModifications();
		if(rootModifications == null) {
			return Collections.emptyList();
		}
		return rootModifications.getModifiedNodes();
	}

	public List<ASTModification> getModificationsForNode(IASTNode node) {
		List<ASTModification> aktModList = scopeStack.peek();
		if (aktModList == null) {
			return getNestedModifikationsForNode(node);
		}
		List<ASTModification> modForNodeList = new ArrayList<ASTModification>();
		for (ASTModification modification : aktModList) {
			modForNodeList.addAll(modStore.getNestedModifications(modification).getModificationsForNode(node));
		}
		return Collections.unmodifiableList(modForNodeList);
	}

}

Back to the top