package com.reason.ide.search;

import com.intellij.lang.HelpID;
import com.intellij.lang.cacheBuilder.DefaultWordsScanner;
import com.intellij.lang.cacheBuilder.WordsScanner;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.TokenSet;
import com.reason.lang.LexerAdapter;
import com.reason.lang.core.psi.*;
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 LexerAdapter(RmlTypes.INSTANCE),
                TokenSet.create(RmlTypes.INSTANCE.UIDENT, RmlTypes.INSTANCE.LIDENT), TokenSet.EMPTY, TokenSet.EMPTY);
    }

    @Override
    public boolean canFindUsagesFor(@NotNull PsiElement element) {
        return element instanceof PsiUpperSymbol || element instanceof PsiLowerSymbol;
    }

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

    @NotNull
    @Override
    public String getType(@NotNull PsiElement element) {
        if (element instanceof PsiUpperSymbol || element instanceof PsiModule) {
            return "module";
        }
        if (element instanceof PsiLowerSymbol) {
            return "symbol";
        }
        if (element instanceof PsiLet) {
            return "let";
        }
        if (element instanceof PsiExternal) {
            return "external";
        }
        if (element instanceof PsiType) {
            return "type";
        }

        return "unknown type";
    }

    @NotNull
    @Override
    public String getDescriptiveName(@NotNull PsiElement element) {
        if (element instanceof PsiNamedElement) {
            String name = ((PsiNamedElement) element).getName();
            return name == null ? "" : name;
        }

        return "desc name of element ";
    }

    @NotNull
    @Override
    public String getNodeText(@NotNull PsiElement element, boolean useFullName) {
        if (element instanceof PsiNamedElement) {
            String name = ((PsiNamedElement) element).getName();
            return name == null ? "" : name;
        }

        return "";
    }
}

@@ -205,16 +205,66 @@
 lement;%0A
+import com.intellij.psi.PsiQualifiedNamedElement;%0A
 import c
@@ -2196,32 +2196,41 @@
 t instanceof Psi
+Qualified
 NamedElement) %7B%0A
@@ -2240,33 +2240,42 @@
          String 
-n
+qualifiedN
 ame = ((PsiNamed
@@ -2261,32 +2261,41 @@
 fiedName = ((Psi
+Qualified
 NamedElement) el
@@ -2296,32 +2296,41 @@
 nt) element).get
+Qualified
 Name();%0A        
@@ -2337,40 +2337,288 @@
     
-return name == null ? %22%22 : name;
+if (qualifiedName != null) %7B%0A                return qualifiedName;%0A            %7D%0A        %7D%0A        if (element instanceof PsiNamedElement) %7B%0A            String name = ((PsiNamedElement) element).getName();%0A            if (name != null) %7B%0A                return name;%0A            %7D
 %0A   

use qname