<commit_before>/*
 * Use of this source code is governed by the MIT license that can be
 * found in the LICENSE file.
 */

package org.rust.lang.core.stubs.index

import com.intellij.openapi.util.io.FileUtil
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 com.intellij.util.PathUtil
import com.intellij.util.SmartList
import org.rust.ide.search.RsWithMacrosProjectScope
import org.rust.lang.RsConstants
import org.rust.lang.core.macros.macroExpansionManagerIfCreated
import org.rust.lang.core.psi.RsFile
import org.rust.lang.core.psi.RsModDeclItem
import org.rust.lang.core.psi.ext.pathAttribute
import org.rust.lang.core.psi.isValidProjectMember
import org.rust.lang.core.stubs.RsFileStub
import org.rust.lang.core.stubs.RsModDeclItemStub
import org.rust.openapiext.checkCommitIsNotInProgress

class RsModulesIndex : StringStubIndexExtension<RsModDeclItem>() {
    override fun getVersion(): Int = RsFileStub.Type.stubVersion
    override fun getKey(): StubIndexKey<String, RsModDeclItem> = KEY

    companion object {
        fun getDeclarationsFor(mod: RsFile): List<RsModDeclItem> {
            val key = key(mod) ?: return emptyList()
            val project = mod.project
            checkCommitIsNotInProgress(project)

            val result = SmartList<RsModDeclItem>()

            val scope = project.macroExpansionManagerIfCreated?.expansionState?.expandedSearchScope
                ?: RsWithMacrosProjectScope(project)
            StubIndex.getInstance().processElements(
                KEY, key, project, scope, RsModDeclItem::class.java
            ) { modDecl ->
                if (modDecl.reference.resolve() == mod) {
                    result += modDecl
                }
                true
            }

            return result.filter { it.isValidProjectMember }.takeIf { it.isNotEmpty() } ?: result
        }


        fun index(stub: RsModDeclItemStub, indexSink: IndexSink) {
            val key = key(stub.psi)
            if (key != null) {
                indexSink.occurrence(KEY, key)
            }
        }
<commit_msg>PERF: slightly optimize `RsModulesIndex`<commit_after>/*
 * Use of this source code is governed by the MIT license that can be
 * found in the LICENSE file.
 */

package org.rust.lang.core.stubs.index

import com.intellij.openapi.util.io.FileUtil
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 com.intellij.util.PathUtil
import com.intellij.util.SmartList
import org.rust.ide.search.RsWithMacrosProjectScope
import org.rust.lang.RsConstants
import org.rust.lang.core.macros.macroExpansionManagerIfCreated
import org.rust.lang.core.psi.RsFile
import org.rust.lang.core.psi.RsModDeclItem
import org.rust.lang.core.psi.ext.pathAttribute
import org.rust.lang.core.psi.isValidProjectMember
import org.rust.lang.core.stubs.RsFileStub
import org.rust.lang.core.stubs.RsModDeclItemStub
import org.rust.openapiext.checkCommitIsNotInProgress
import org.rust.stdext.singleOrFilter

class RsModulesIndex : StringStubIndexExtension<RsModDeclItem>() {
    override fun getVersion(): Int = RsFileStub.Type.stubVersion
    override fun getKey(): StubIndexKey<String, RsModDeclItem> = KEY

    companion object {
        fun getDeclarationsFor(mod: RsFile): List<RsModDeclItem> {
            val key = key(mod) ?: return emptyList()
            val project = mod.project
            checkCommitIsNotInProgress(project)

            val result = SmartList<RsModDeclItem>()

            val scope = project.macroExpansionManagerIfCreated?.expansionState?.expandedSearchScope
                ?: RsWithMacrosProjectScope(project)
            StubIndex.getInstance().processElements(
                KEY, key, project, scope, RsModDeclItem::class.java
            ) { modDecl ->
                if (modDecl.reference.resolve() == mod) {
                    result += modDecl
                }
                true
            }

            return result.singleOrFilter { it.isValidProjectMember }.takeIf { it.isNotEmpty() } ?: result
        }


        fun index(stub: RsModDeclItemStub, indexSink: IndexSink) {
            val key = key(stub.psi)
            if (key != null) {
                indexSink.occurrence(KEY, key)
            }
        }

{'fraction_non_alphanumeric': 0.09276271566211068, 'fraction_numerical': 0.0, 'mean_word_length': 2.864935064935065, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3402281', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1325, 'n_words': 253}