Skip to content

Commit

Permalink
Optimization of findModule
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobc committed Feb 4, 2013
1 parent ec2d152 commit 53a8abc
Showing 1 changed file with 66 additions and 40 deletions.
106 changes: 66 additions & 40 deletions org.erlide.core/src/org/erlide/core/internal/model/root/ErlModel.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ private Collection<IErlModule> getAllIncludes(final IErlProject project,
} }


static void getAllModulesAux(final Collection<IErlModule> modules, static void getAllModulesAux(final Collection<IErlModule> modules,
final List<IErlModule> result, final Set<String> paths) { final Collection<IErlModule> result, final Set<String> paths) {
for (final IErlModule module : modules) { for (final IErlModule module : modules) {
final String path = module.getFilePath(); final String path = module.getFilePath();
if (path != null) { if (path != null) {
Expand All @@ -980,42 +980,6 @@ static void getAllModulesAux(final Collection<IErlModule> modules,
} }
} }


private Collection<IErlModule> getAllModules(final IErlProject project,
final boolean checkExternals, final IErlElementLocator.Scope scope)
throws ErlModelException {
final Set<IErlProject> projects = Sets.newHashSet();
final List<IErlModule> result = Lists.newArrayList();
final Set<String> paths = Sets.newHashSet();

if (project != null) {
projects.add(project);
if (scope == IErlElementLocator.Scope.REFERENCED_PROJECTS) {
projects.addAll(project.getReferencedProjects());
}
}

if (scope == IErlElementLocator.Scope.ALL_PROJECTS) {
projects.addAll(getErlangProjects());
}

for (final IErlProject project2 : projects) {
ErlModel.getAllModulesAux(project2.getModules(), result, paths);
}
if (checkExternals) {
if (project != null) {
ErlModel.getAllModulesAux(project.getExternalModules(), result,
paths);
}
if (scope == IErlElementLocator.Scope.ALL_PROJECTS) {
for (final IErlProject project2 : projects) {
ErlModel.getAllModulesAux(project2.getExternalModules(),
result, paths);
}
}
}
return result;
}

private IErlModule findIncludeFromProject(final IErlProject project, private IErlModule findIncludeFromProject(final IErlProject project,
final String includeName, final String includePath, final String includeName, final String includePath,
final boolean ignoreCase, final boolean checkExternals, final boolean ignoreCase, final boolean checkExternals,
Expand Down Expand Up @@ -1086,9 +1050,71 @@ public IErlModule findModuleFromProject(final IErlProject project,
return module; return module;
} }
} }
final Collection<IErlModule> modules = getAllModules(project, final List<IErlModule> allModules = Lists.newArrayList();
checkExternals, scope); final Set<String> paths = Sets.newHashSet();
ErlModelCache.getDefault().putModules(modules); try {
if (project != null) {
final IErlModule module = tryFindModule(
Sets.newHashSet(project), moduleName, modulePath,
ignoreCase, checkExternals, allModules);
if (module != null || scope == Scope.PROJECT_ONLY) {
return module;
}
}
if (scope == Scope.REFERENCED_PROJECTS
|| scope == Scope.ALL_PROJECTS) {
final Collection<IErlProject> projects = project
.getReferencedProjects();
final IErlModule module = tryFindModule(projects, moduleName,
modulePath, ignoreCase, checkExternals, allModules);
if (module != null || scope == Scope.REFERENCED_PROJECTS) {
return module;
}
}
if (scope == Scope.ALL_PROJECTS) {
final Collection<IErlProject> projects = getErlangProjects();
final IErlModule module = tryFindModule(projects, moduleName,
modulePath, ignoreCase, checkExternals, allModules);
if (module != null) {
return module;
}
}
return null;
} finally {
ErlModelCache.getDefault().putModules(allModules);
}
}

private IErlModule tryFindModule(final Collection<IErlProject> projects,
final String moduleName, final String modulePath,
final boolean ignoreCase, final boolean checkExternals,
final List<IErlModule> allModules) throws ErlModelException {
IErlModule module;
final Set<String> paths = Sets.newHashSet();
for (final IErlProject project : projects) {
final Collection<IErlModule> modules = Lists.newArrayList();
getAllModulesAux(project.getModules(), modules, paths);
allModules.addAll(modules);
module = findModule(modules, moduleName, modulePath, ignoreCase);
if (module != null) {
return module;
}
if (checkExternals) {
modules.clear();
getAllModulesAux(project.getExternalModules(), modules, paths);
allModules.addAll(modules);
module = findModule(modules, moduleName, modulePath, ignoreCase);
if (module != null) {
return module;
}
}
}
return null;
}

private IErlModule findModule(final Collection<IErlModule> modules,
final String moduleName, final String modulePath,
final boolean ignoreCase) {
if (modulePath != null) { if (modulePath != null) {
for (final IErlModule module2 : modules) { for (final IErlModule module2 : modules) {
final String path2 = module2.getFilePath(); final String path2 = module2.getFilePath();
Expand Down

0 comments on commit 53a8abc

Please sign in to comment.