|
7 | 7 |
|
8 | 8 | import spoon.reflect.cu.SourcePosition; |
9 | 9 | import spoon.reflect.declaration.CtElement; |
| 10 | +import spoon.reflect.declaration.CtExecutable; |
| 11 | +import spoon.reflect.declaration.CtMethod; |
10 | 12 |
|
11 | 13 | public class ContextHistory { |
12 | 14 | private static ContextHistory instance; |
13 | 15 |
|
14 | | - private Map<String, Set<RefinedVariable>> vars; // scope -> variables in scope |
| 16 | + private Map<String, Map<String, Set<RefinedVariable>>> vars; // file -> (scope -> variables in scope) |
15 | 17 | private Set<RefinedVariable> instanceVars; |
16 | 18 | private Set<RefinedVariable> globalVars; |
17 | 19 | private Set<GhostState> ghosts; |
@@ -43,16 +45,28 @@ public void saveContext(CtElement element, Context context) { |
43 | 45 | SourcePosition pos = element.getPosition(); |
44 | 46 | if (pos == null || pos.getFile() == null) |
45 | 47 | return; |
| 48 | + |
| 49 | + // add variables in scope for this position |
| 50 | + String file = pos.getFile().getAbsolutePath(); |
| 51 | + String scope = getScopePosition(element); |
| 52 | + System.out.println("Saving context for " + file + " in scope " + scope); |
| 53 | + vars.putIfAbsent(file, new HashMap<>()); |
| 54 | + vars.get(file).put(scope, new HashSet<>(context.getCtxVars())); |
46 | 55 |
|
47 | | - String scope = String.format("%s:%d:%d", pos.getFile().getName(), pos.getLine(), pos.getColumn()); |
48 | | - vars.put(scope, new HashSet<>(context.getCtxVars())); |
| 56 | + // add other elements in context |
49 | 57 | instanceVars.addAll(context.getCtxInstanceVars()); |
50 | 58 | globalVars.addAll(context.getCtxGlobalVars()); |
51 | 59 | ghosts.addAll(context.getGhostStates()); |
52 | 60 | aliases.addAll(context.getAliases()); |
53 | 61 | } |
54 | 62 |
|
55 | | - public Map<String, Set<RefinedVariable>> getVars() { |
| 63 | + public String getScopePosition(CtElement element) { |
| 64 | + SourcePosition pos = element.getPosition(); |
| 65 | + SourcePosition innerPosition = element instanceof CtExecutable ? ((CtExecutable<?>) element).getBody().getPosition() : pos; |
| 66 | + return String.format("%d:%d-%d:%d", innerPosition.getLine(), innerPosition.getColumn() + 1, pos.getEndLine(), pos.getEndColumn()); |
| 67 | + } |
| 68 | + |
| 69 | + public Map<String, Map<String, Set<RefinedVariable>>> getVars() { |
56 | 70 | return vars; |
57 | 71 | } |
58 | 72 |
|
|
0 commit comments