<commit_before>package nl.hannahsten.texifyidea.reference

import com.intellij.psi.*
import com.intellij.util.containers.toArray
import nl.hannahsten.texifyidea.index.LatexDefinitionIndex
import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.util.Magic
import nl.hannahsten.texifyidea.util.childrenOfType
import nl.hannahsten.texifyidea.util.projectSearchScope

/**
 * Command reference. When resolved, points to the command definition.
 *
 * @author <NAME>
 */
class CommandDefinitionReference(element: LatexCommands) : PsiReferenceBase<LatexCommands>(element), PsiPolyVariantReference {
    init {
        rangeInElement = ElementManipulators.getValueTextRange(element)
    }

    // Find all command definitions and redefinitions which define the current element
    override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
        return LatexDefinitionIndex.getCommandsByNames(Magic.Command.commandDefinitions, element.project, element.project.projectSearchScope)
                .filter { it.requiredParameters.firstOrNull() == element.name }
                .mapNotNull {newcommand ->
                    // Find the command being defined, e.g. \hi in case of \newcommand{\hi}{}
                    // We should resolve to \hi, not to \newcommand, because otherwise the find usages will try to find references to the \hi definition and won't find anything because the references point to the \newcommand
                    val definedCommand = newcommand.childrenOfType<LatexCommands>().firstOrNull()

                    // Don't resolve to itself
                    if (definedCommand == null || definedCommand == element) {
                        null
                    }
                    else {
                        PsiElementResolveResult(definedCommand as PsiElement)
                    }
                }
                .toArray(emptyArray())<commit_msg>Take renew commands into account<commit_after>package nl.hannahsten.texifyidea.reference

import com.intellij.psi.*
import com.intellij.util.containers.toArray
import nl.hannahsten.texifyidea.index.LatexDefinitionIndex
import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.util.Magic
import nl.hannahsten.texifyidea.util.childrenOfType
import nl.hannahsten.texifyidea.util.parentsOfType
import nl.hannahsten.texifyidea.util.projectSearchScope

/**
 * Command reference. When resolved, points to the command definition.
 *
 * @author <NAME>
 */
class CommandDefinitionReference(element: LatexCommands) : PsiReferenceBase<LatexCommands>(element), PsiPolyVariantReference {
    init {
        rangeInElement = ElementManipulators.getValueTextRange(element)
    }

    // Find all command definitions and redefinitions which define the current element
    override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
        val definitionsAndRedefinitions = Magic.Command.commandDefinitions + Magic.Command.redefinitions

        // Don't resolve to a definition when you are in a \newcommand
        if (element.parentsOfType<LatexCommands>().any { it.name in definitionsAndRedefinitions }) {
            return emptyArray()
        }
        else {
            return LatexDefinitionIndex.getCommandsByNames(definitionsAndRedefinitions, element.project, element.project.projectSearchScope)
                    .filter { it.requiredParameters.firstOrNull() == element.name }
                    .mapNotNull { newcommand ->
                        // Find the command being defined, e.g. \hi in case of \newcommand{\hi}{}
                        // We should resolve to \hi, not to \newcommand, because otherwise the find usages will try to find references to the \hi definition and won't find anything because the references point to the \newcommand
                        val definedCommand = newcommand.childrenOfType<LatexCommands>().firstOrNull()

                        if (definedCommand == null) {
                            null
                        } else {
                            PsiElementResolveResult(definedCommand as PsiElement)
                        }
                    }
                    .toArray(emptyArray())
        }
{'fraction_non_alphanumeric': 0.07624911200568317, 'fraction_numerical': 0.0, 'mean_word_length': 2.6102564102564103, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, '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': '5528201', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1038, 'n_words': 266}