<NME> ElmModulesIndex.kt
<BEF> package org.elm.lang.core.stubs.index

import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.stubs.IndexSink
import com.intellij.psi.stubs.StringStubIndexExtension
import com.intellij.psi.stubs.StubIndex
import com.intellij.psi.stubs.StubIndexKey
import org.elm.lang.core.lookup.ClientLocation
import org.elm.lang.core.lookup.ElmLookup
import org.elm.lang.core.psi.elements.ElmModuleDeclaration
import org.elm.lang.core.stubs.ElmFileStub
import org.elm.lang.core.stubs.ElmModuleDeclarationStub
import org.elm.workspace.ElmProject

/**
 * Find Elm modules within an Elm project.
 *
 * This API takes into account [ElmProject] structure to determine which modules
 * should be visible, resolving each module to the correct version for the project.
 */
class ElmModulesIndex : StringStubIndexExtension<ElmModuleDeclaration>() {

    override fun getVersion() =
            ElmFileStub.Type.stubVersion

    override fun getKey(): StubIndexKey<String, ElmModuleDeclaration> =
            KEY

    companion object {

        /**
         * Returns an Elm module named [moduleName] which is visible to [clientLocation], if any
         */
        fun get(moduleName: String, clientLocation: ClientLocation): ElmModuleDeclaration? =
                rawGet(moduleName, clientLocation.intellijProject, ElmLookup.searchScopeAt(clientLocation))
                        .firstOrNull()
        private fun makeKey(moduleName: String) =
                moduleLookupHack(moduleName)

        fun get(moduleName: String, project: Project): ElmModuleDeclaration? {
            val key = makeKey(moduleName)
            val matches = StubIndex.getElements(KEY, key, project,
        /**
         * Returns all Elm modules whose names match an element in [moduleNames] and which are visible to [clientLocation]
         */
        fun getAll(moduleNames: Collection<String>, clientLocation: ClientLocation): List<ElmModuleDeclaration> =
                rawGetAll(moduleNames, clientLocation.intellijProject, ElmLookup.searchScopeAt(clientLocation))


            return matches.firstOrNull()
        }

        fun getAll(project: Project): List<ElmModuleDeclaration> {
            val index = StubIndex.getInstance()
            val results = mutableListOf<ElmModuleDeclaration>()

            for (key in index.getAllKeys(KEY, project)) {
                index.processElements(KEY, key, project, GlobalSearchScope.allScope(project),
                        ElmModuleDeclaration::class.java) {
                    results.add(it)
                moduleName
            }
            return results.sortedWith(elmAppVsLibraryComparator)
        }
    }
}

         */
        private fun rawGetAll(moduleNames: Collection<String>, project: Project, scope: GlobalSearchScope): List<ElmModuleDeclaration> {
            val index = StubIndex.getInstance()
            val results = mutableListOf<ElmModuleDeclaration>()

            for (key in moduleNames) {
                index.processElements(KEY, key, project, scope, ElmModuleDeclaration::class.java) {
                    results.add(it)
                }
            }
            return results
        }


        /**
         * Returns all module declarations within [scope]
         */
        private fun rawGetAll(project: Project, scope: GlobalSearchScope): List<ElmModuleDeclaration> =
                rawGetAll(StubIndex.getInstance().getAllKeys(KEY, project), project, scope)

    }
}

<MSG> Fix performance bug related to qualified module name refs

<DFF> @@ -38,6 +38,9 @@ class ElmModulesIndex: StringStubIndexExtension<ElmModuleDeclaration>() {
         private fun makeKey(moduleName: String) =
                 moduleLookupHack(moduleName)
 
+        /**
+         * Return the module declaration for [moduleName], if any.
+         */
         fun get(moduleName: String, project: Project): ElmModuleDeclaration? {
             val key = makeKey(moduleName)
             val matches = StubIndex.getElements(KEY, key, project,
@@ -51,11 +54,15 @@ class ElmModulesIndex: StringStubIndexExtension<ElmModuleDeclaration>() {
             return matches.firstOrNull()
         }
 
-        fun getAll(project: Project): List<ElmModuleDeclaration> {
+        /**
+         * Returns all module declarations in [project] whose module name
+         * matches an item in [moduleNames]
+         */
+        fun getAll(moduleNames: Collection<String>, project: Project): List<ElmModuleDeclaration> {
             val index = StubIndex.getInstance()
             val results = mutableListOf<ElmModuleDeclaration>()
 
-            for (key in index.getAllKeys(KEY, project)) {
+            for (key in moduleNames) {
                 index.processElements(KEY, key, project, GlobalSearchScope.allScope(project),
                         ElmModuleDeclaration::class.java) {
                     results.add(it)
@@ -63,6 +70,13 @@ class ElmModulesIndex: StringStubIndexExtension<ElmModuleDeclaration>() {
             }
             return results.sortedWith(elmAppVsLibraryComparator)
         }
+
+        /**
+         * Returns all module declarations in [project]
+         */
+        fun getAll(project: Project): List<ElmModuleDeclaration> {
+            return getAll(StubIndex.getInstance().getAllKeys(KEY, project), project)
+        }
     }
 }
 

Fix performance bug related to qualified module name refs