-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathContextHistory.java
More file actions
99 lines (81 loc) · 2.74 KB
/
ContextHistory.java
File metadata and controls
99 lines (81 loc) · 2.74 KB
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
package liquidjava.processor.context;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import liquidjava.api.CommandLineLauncher;
import liquidjava.utils.Utils;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtElement;
public class ContextHistory {
private static ContextHistory instance;
private Map<String, Set<String>> fileScopes;
private Set<RefinedVariable> localVars;
private Set<GhostState> ghosts;
private Set<AliasWrapper> aliases;
private Set<RefinedVariable> globalVars;
private Set<RefinedFunction> methods;
private ContextHistory() {
fileScopes = new HashMap<>();
localVars = new HashSet<>();
globalVars = new HashSet<>();
ghosts = new HashSet<>();
aliases = new HashSet<>();
methods = new HashSet<>();
}
public static ContextHistory getInstance() {
if (instance == null)
instance = new ContextHistory();
return instance;
}
public void clearHistory() {
fileScopes.clear();
localVars.clear();
globalVars.clear();
ghosts.clear();
aliases.clear();
methods.clear();
}
public void saveContext(CtElement element, Context context) {
if (!CommandLineLauncher.cmdArgs.lspMode)
return;
String file = Utils.getFile(element);
if (file == null)
return;
// add scope
String scope = getScopePosition(element);
fileScopes.putIfAbsent(file, new HashSet<>());
fileScopes.get(file).add(scope);
// add variables, ghosts and aliases
localVars.addAll(context.getCtxVars());
localVars.addAll(context.getCtxInstanceVars());
globalVars.addAll(context.getCtxGlobalVars());
ghosts.addAll(context.getGhostStates());
aliases.addAll(context.getAliases());
methods.addAll(context.getCtxFunctions());
}
private String getScopePosition(CtElement element) {
SourcePosition startPos = Utils.getRealPosition(element);
SourcePosition endPos = element.getPosition();
return String.format("%d:%d-%d:%d", startPos.getLine(), startPos.getColumn(), endPos.getEndLine(),
endPos.getEndColumn() - 1);
}
public Set<RefinedVariable> getLocalVars() {
return localVars;
}
public Set<RefinedVariable> getGlobalVars() {
return globalVars;
}
public Set<GhostState> getGhosts() {
return ghosts;
}
public Set<AliasWrapper> getAliases() {
return aliases;
}
public Set<RefinedFunction> getMethods() {
return methods;
}
public Map<String, Set<String>> getFileScopes() {
return fileScopes;
}
}