public IGoal[] init() {
GlobalVariableReferencesGoal typedGoal = (GlobalVariableReferencesGoal) goal;
IContext context = goal.getContext();
ISourceModuleContext sourceModuleContext = null;
IScriptProject scriptProject = null;
if (context instanceof ISourceModuleContext) {
sourceModuleContext = (ISourceModuleContext) context;
scriptProject = sourceModuleContext.getSourceModule()
.getScriptProject();
}
String variableName = typedGoal.getVariableName();
boolean exploreOtherFiles = true;
// Find all global variables from mixin
IDLTKSearchScope scope = SearchEngine.createSearchScope(scriptProject);
IField[] elements = PhpModelAccess.getDefault().findFields(
variableName, MatchRule.EXACT, Modifiers.AccGlobal,
Modifiers.AccConstant, scope, null);
// if no element found, return empty array.
if (elements == null) {
return new IGoal[] {};
}
Map<ISourceModule, SortedSet<ISourceRange>> offsets = new HashMap<ISourceModule, SortedSet<ISourceRange>>();
Comparator<ISourceRange> sourceRangeComparator = new Comparator<ISourceRange>() {
public int compare(ISourceRange o1, ISourceRange o2) {
return o1.getOffset() - o2.getOffset();
}
};
for (IModelElement element : elements) {
if (element instanceof SourceField) {
SourceField sourceField = (SourceField) element;
ISourceModule sourceModule = sourceField.getSourceModule();
if (!offsets.containsKey(sourceModule)) {
offsets.put(sourceModule, new TreeSet<ISourceRange>(
sourceRangeComparator));
}
try {
offsets.get(sourceModule).add(sourceField.getSourceRange());
} catch (ModelException e) {
if (DLTKCore.DEBUG) {
e.printStackTrace();
}
}
}
}
List<IGoal> subGoals = new LinkedList<IGoal>();
for (Entry<ISourceModule, SortedSet<ISourceRange>> entry : offsets
.entrySet()) {
ISourceModule sourceModule = entry.getKey();
if (exploreOtherFiles
|| (sourceModuleContext != null && sourceModuleContext
.getSourceModule().equals(sourceModule))) {
ModuleDeclaration moduleDeclaration = SourceParserUtil
.getModuleDeclaration(sourceModule);
SortedSet<ISourceRange> fileOffsets = entry.getValue();