Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Save Context Vars by File and Scope
  • Loading branch information
rcosta358 committed Feb 19, 2026
commit 803d1a5404f66ca51e1ce28af5226eae2f1f42f8
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtMethod;

public class ContextHistory {
private static ContextHistory instance;

private Map<String, Set<RefinedVariable>> vars; // scope -> variables in scope
private Map<String, Map<String, Set<RefinedVariable>>> vars; // file -> (scope -> variables in scope)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like in here this var could be fileScopeVars to indicate what they mean

private Set<RefinedVariable> instanceVars;
private Set<RefinedVariable> globalVars;
private Set<GhostState> ghosts;
Expand Down Expand Up @@ -43,16 +45,28 @@ public void saveContext(CtElement element, Context context) {
SourcePosition pos = element.getPosition();
if (pos == null || pos.getFile() == null)
return;

// add variables in scope for this position
String file = pos.getFile().getAbsolutePath();
String scope = getScopePosition(element);
System.out.println("Saving context for " + file + " in scope " + scope);
vars.putIfAbsent(file, new HashMap<>());
vars.get(file).put(scope, new HashSet<>(context.getCtxVars()));

String scope = String.format("%s:%d:%d", pos.getFile().getName(), pos.getLine(), pos.getColumn());
vars.put(scope, new HashSet<>(context.getCtxVars()));
// add other elements in context
instanceVars.addAll(context.getCtxInstanceVars());
globalVars.addAll(context.getCtxGlobalVars());
ghosts.addAll(context.getGhostStates());
aliases.addAll(context.getAliases());
}

public Map<String, Set<RefinedVariable>> getVars() {
public String getScopePosition(CtElement element) {
SourcePosition pos = element.getPosition();
SourcePosition innerPosition = element instanceof CtExecutable ? ((CtExecutable<?>) element).getBody().getPosition() : pos;
return String.format("%d:%d-%d:%d", innerPosition.getLine(), innerPosition.getColumn() + 1, pos.getEndLine(), pos.getEndColumn());
}

public Map<String, Map<String, Set<RefinedVariable>>> getVars() {
return vars;
}

Expand Down
Loading