<commit_before>package com.reason.ide.search;

import com.intellij.lang.HelpID;
import com.intellij.lang.cacheBuilder.WordOccurrence;
import com.intellij.lang.cacheBuilder.WordsScanner;
import com.intellij.lexer.LexerBase;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.ElementDescriptionUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.usageView.UsageViewLongNameLocation;
import com.intellij.usageView.UsageViewNodeTextLocation;
import com.intellij.usageView.UsageViewTypeLocation;
import com.reason.lang.LexerAdapter;
import com.reason.lang.core.psi.PsiModuleName;
import com.reason.lang.core.psi.PsiTypeName;
import com.reason.lang.core.psi.PsiVarName;
import com.reason.lang.reason.RmlTypes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class RmlFindUsagesProvider implements com.intellij.lang.findUsages.FindUsagesProvider {
    @Nullable
    @Override
    public WordsScanner getWordsScanner() {
        //return new DefaultWordsScanner(new RmlLexerAdapter(), TokenSet.create(RmlTypes.UIDENT, RmlTypes.VALUE_NAME), TokenSet.EMPTY, TokenSet.EMPTY);
        return (fileText, processor) -> {
            RmlTypes types = RmlTypes.INSTANCE;
            LexerBase lexer = new LexerAdapter(types);
            lexer.start(fileText);
            IElementType tokenType;
            while ((tokenType = lexer.getTokenType()) != null) {
                //TODO process occurrences in string literals and comments
                if (tokenType == types.LIDENT || tokenType == types.UIDENT || tokenType == types.MODULE_NAME || tokenType == types.VALUE_NAME) {
                    int tokenStart = lexer.getTokenStart();
                    for (TextRange wordRange : StringUtil.getWordIndicesIn(lexer.getTokenText())) {
                        int start = tokenStart + wordRange.getStartOffset();
                        int end = tokenStart + wordRange.getEndOffset();
//                        System.out.println("scan: " + start + "," + end + " -> " + lexer.getTokenText());
                        processor.process(new WordOccurrence(fileText, start, end, WordOccurrence.Kind.CODE));
                    }
                }
                lexer.advance();
            }
        };
    }

    @Override
    public boolean canFindUsagesFor(@NotNull PsiElement element) {
        return element instanceof PsiModuleName || element instanceof PsiTypeName || element instanceof PsiVarName;
    }

    @Nullable
    @Override
    public String getHelpId(@NotNull PsiElement psiElement) {
        return HelpID.FIND_OTHER_USAGES;
    }

    @NotNull
    @Override
    public String getType(@NotNull PsiElement element) {
        return ElementDescriptionUtil.getElementDescription(element, UsageViewTypeLocation.INSTANCE);
    }

    @NotNull
    @Override
    public String getDescriptiveName(@NotNull PsiElement element) {
        return ElementDescriptionUtil.getElementDescription(element, UsageViewLongNameLocation.INSTANCE);
    }

    @NotNull
    @Override
    public String getNodeText(@NotNull PsiElement element, boolean useFullName) {<commit_msg>Revert 'Find usages' for let until scope is implemented<commit_after>package com.reason.ide.search;

import com.intellij.lang.HelpID;
import com.intellij.lang.cacheBuilder.WordOccurrence;
import com.intellij.lang.cacheBuilder.WordsScanner;
import com.intellij.lexer.LexerBase;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.ElementDescriptionUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.usageView.UsageViewLongNameLocation;
import com.intellij.usageView.UsageViewNodeTextLocation;
import com.intellij.usageView.UsageViewTypeLocation;
import com.reason.lang.LexerAdapter;
import com.reason.lang.core.psi.PsiModuleName;
import com.reason.lang.core.psi.PsiTypeName;
import com.reason.lang.reason.RmlTypes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class RmlFindUsagesProvider implements com.intellij.lang.findUsages.FindUsagesProvider {
    @Nullable
    @Override
    public WordsScanner getWordsScanner() {
        //return new DefaultWordsScanner(new RmlLexerAdapter(), TokenSet.create(RmlTypes.UIDENT, RmlTypes.VALUE_NAME), TokenSet.EMPTY, TokenSet.EMPTY);
        return (fileText, processor) -> {
            RmlTypes types = RmlTypes.INSTANCE;
            LexerBase lexer = new LexerAdapter(types);
            lexer.start(fileText);
            IElementType tokenType;
            while ((tokenType = lexer.getTokenType()) != null) {
                //TODO process occurrences in string literals and comments
                if (tokenType == types.LIDENT || tokenType == types.UIDENT || tokenType == types.MODULE_NAME || tokenType == types.VALUE_NAME) {
                    int tokenStart = lexer.getTokenStart();
                    for (TextRange wordRange : StringUtil.getWordIndicesIn(lexer.getTokenText())) {
                        int start = tokenStart + wordRange.getStartOffset();
                        int end = tokenStart + wordRange.getEndOffset();
//                        System.out.println("scan: " + start + "," + end + " -> " + lexer.getTokenText());
                        processor.process(new WordOccurrence(fileText, start, end, WordOccurrence.Kind.CODE));
                    }
                }
                lexer.advance();
            }
        };
    }

    @Override
    public boolean canFindUsagesFor(@NotNull PsiElement element) {
        return element instanceof PsiModuleName || element instanceof PsiTypeName /*|| element instanceof PsiVarName*/;
    }

    @Nullable
    @Override
    public String getHelpId(@NotNull PsiElement psiElement) {
        return HelpID.FIND_OTHER_USAGES;
    }

    @NotNull
    @Override
    public String getType(@NotNull PsiElement element) {
        return ElementDescriptionUtil.getElementDescription(element, UsageViewTypeLocation.INSTANCE);
    }

    @NotNull
    @Override
    public String getDescriptiveName(@NotNull PsiElement element) {
        return ElementDescriptionUtil.getElementDescription(element, UsageViewLongNameLocation.INSTANCE);
    }

    @NotNull
    @Override
    public String getNodeText(@NotNull PsiElement element, boolean useFullName) {
{'fraction_non_alphanumeric': 0.09352740786621246, 'fraction_numerical': 0.0, 'mean_word_length': 3.145699614890886, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4891874', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1779, 'n_words': 348}