Permalink
Browse files

Optimization of findModule

  • Loading branch information...
1 parent ec2d152 commit 53a8abc8f142b83370dce7adfd191353b3918240 @jakobc jakobc committed Feb 4, 2013
Showing with 66 additions and 40 deletions.
  1. +66 −40 org.erlide.core/src/org/erlide/core/internal/model/root/ErlModel.java
@@ -967,7 +967,7 @@ private static IErlModule getModuleFromCacheByNameOrPath(
}
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) {
final String path = module.getFilePath();
if (path != null) {
@@ -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,
final String includeName, final String includePath,
final boolean ignoreCase, final boolean checkExternals,
@@ -1086,9 +1050,71 @@ public IErlModule findModuleFromProject(final IErlProject project,
return module;
}
}
- final Collection<IErlModule> modules = getAllModules(project,
- checkExternals, scope);
- ErlModelCache.getDefault().putModules(modules);
+ final List<IErlModule> allModules = Lists.newArrayList();
+ final Set<String> paths = Sets.newHashSet();
+ 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) {
for (final IErlModule module2 : modules) {
final String path2 = module2.getFilePath();

0 comments on commit 53a8abc

Please sign in to comment.