<commit_before>package com.chrisfolger.needsmoredojo.core.amd.importing;

import com.chrisfolger.needsmoredojo.core.amd.filesystem.SourceLibrary;
import com.chrisfolger.needsmoredojo.core.amd.filesystem.SourcesLocator;
import com.chrisfolger.needsmoredojo.core.amd.naming.NameResolver;
import com.chrisfolger.needsmoredojo.core.util.FileUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public class ImportResolver
{
    private static final Map<String, Integer> libraryScores = new HashMap<String, Integer>();

    static
    {
        libraryScores.put("dojo/tests", 0);
        libraryScores.put("dojo/", 5);
        libraryScores.put("dijit/", 4);
        libraryScores.put("dgrid/", 2);
        libraryScores.put("dojox/", 1);
        libraryScores.put("doh/", 1);
        libraryScores.put("build/", 1);
    }

    private int getScore(String item)
    {
        int baseScore = 0;

        for(String key : libraryScores.keySet().toArray(new String[0]))
        {
            if(item.indexOf(key) != -1)
            {
                return libraryScores.get(key);
            }
        }

        return 0;
    }

    public @NotNull String[] getChoicesFromFiles(@NotNull PsiFile[] filesArray, @NotNull SourceLibrary[] libraries, @NotNull String module, @Nullable PsiFile originalModule)
    {
        return getChoicesFromFiles(filesArray, libraries, module, originalModule, false);
    }

    public @NotNull LinkedHashMap<String, PsiFile> getChoicesFromFiles(@NotNull PsiFile[] filesArray, @NotNull SourceLibrary[] libraries, @NotNull String module, @Nullable PsiFile originalModule, boolean prioritizeRelativePaths, boolean getMap)
    {
        Map<String, PsiFile> moduleFileMap = new HashMap<String, PsiFile>();

        SourcesLocator locator = new SourcesLocator();
        List<String> choices = new ArrayList<String>();

        for(int i=0;i<filesArray.length;i++)
        {
            PsiFile file = filesArray[i];

            PsiDirectory directory = file.getContainingDirectory();
            String result = directory.getVirtualFile().getCanonicalPath();

            SourceLibrary firstLibrary = locator.getFirstLibraryThatIncludesFile(result, libraries);

            if(firstLibrary != null)
            {
                if(!firstLibrary.getPath().equals(""))
                {
                    result = firstLibrary.getName() + result.substring(result.indexOf(firstLibrary.getPath()) + firstLibrary.getPath().length());
                }
                result = result.substring(result.indexOf(firstLibrary.getName()));
                result = result.replace('\\', '/') + '/' + file.getName().substring(0, file.getName().indexOf('.'));

                String originalModulePath = null;
                String relativePathOption = null;
                String absolutePathOption = null;

                if(originalModule != null)
                {
                    originalModulePath = originalModule.getContainingDirectory().getVirtualFile().getCanonicalPath();
                    SourceLibrary originalModuleLibrary = locator.getFirstLibraryThatIncludesFile(originalModulePath, libraries);
                    originalModulePath = originalModuleLibrary.getName() + originalModulePath.substring(originalModulePath.indexOf(originalModuleLibrary.getPath()) + originalModuleLibrary.getPath().length());

                    String relativePath = FileUtil.convertToRelativePath(originalModulePath, result);

                    if(relativePath != null)
                    {
                        // need to use dojo syntax when two files are in the same directory
                        if(relativePath.equals("."))
                        {
                            relativePath = "./";
                        }
                        else if (relativePath.charAt(0) != '.' && relativePath.charAt(0) != '/')
                        {
                            // top level module
                            relativePath = "./" + relativePath;
                        }

                        relativePathOption = relativePath;
                    }
                }

                absolutePathOption = result;
                String pluginPostFix = NameResolver.getAMDPluginResourceIfPossible(module, true);

                if(prioritizeRelativePaths && relativePathOption != null)
                {
                    choices.add(relativePathOption + pluginPostFix);
                    moduleFileMap.put(relativePathOption + pluginPostFix, file);
                    choices.add(absolutePathOption + pluginPostFix);
                    moduleFileMap.put(absolutePathOption + pluginPostFix, file);
                }
                else
                {
                    choices.add(absolutePathOption + pluginPostFix);
                    moduleFileMap.put(absolutePathOption + pluginPostFix, file);

                    if(relativePathOption != null)
                    {
                        choices.add(relativePathOption + pluginPostFix);
                        moduleFileMap.put(relativePathOption + pluginPostFix, file);
                    }
                }
            }
        }

        Collections.sort(choices, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return getScore(o2) - getScore(o1);
            }
        });

        // sort the map based on score.
        LinkedHashMap<String, PsiFile> finalMapResults = new LinkedHashMap<String, PsiFile>();
        for(String choice : choices)
        {
            for(Map.Entry<String, PsiFile> entry : moduleFileMap.entrySet())
            {
                if(choice.equals(entry.getKey()))
                {
                    finalMapResults.put(entry.getKey(), entry.getValue());
                    break;
                }
            }
        }

        choices.add(module);<commit_msg>compare modules against each other when scoring in case one is an nls module<commit_after>package com.chrisfolger.needsmoredojo.core.amd.importing;

import com.chrisfolger.needsmoredojo.core.amd.filesystem.SourceLibrary;
import com.chrisfolger.needsmoredojo.core.amd.filesystem.SourcesLocator;
import com.chrisfolger.needsmoredojo.core.amd.naming.NameResolver;
import com.chrisfolger.needsmoredojo.core.util.FileUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public class ImportResolver
{
    private static final Map<String, Integer> libraryScores = new HashMap<String, Integer>();

    static
    {
        libraryScores.put("dojo/tests", 0);
        libraryScores.put("dojo/", 5);
        libraryScores.put("dijit/", 4);
        libraryScores.put("dgrid/", 2);
        libraryScores.put("dojox/", 1);
        libraryScores.put("doh/", 1);
        libraryScores.put("build/", 1);
    }

    private int getScore(String item, String secondItem)
    {
        int baseScore = 0;

        // one non-nls module vs another one should be scored lower
        if(item.contains("nls") && !secondItem.contains("nls"))
        {
            baseScore -= 1;
        }
        else if (!item.contains("nls") && secondItem.contains("nls"))
        {
            baseScore += 1;
        }

        for(String key : libraryScores.keySet().toArray(new String[0]))
        {
            if(item.indexOf(key) != -1)
            {
                return libraryScores.get(key) + baseScore;
            }
        }

        return 0;
    }

    public @NotNull String[] getChoicesFromFiles(@NotNull PsiFile[] filesArray, @NotNull SourceLibrary[] libraries, @NotNull String module, @Nullable PsiFile originalModule)
    {
        return getChoicesFromFiles(filesArray, libraries, module, originalModule, false);
    }

    public @NotNull LinkedHashMap<String, PsiFile> getChoicesFromFiles(@NotNull PsiFile[] filesArray, @NotNull SourceLibrary[] libraries, @NotNull String module, @Nullable PsiFile originalModule, boolean prioritizeRelativePaths, boolean getMap)
    {
        Map<String, PsiFile> moduleFileMap = new HashMap<String, PsiFile>();

        SourcesLocator locator = new SourcesLocator();
        List<String> choices = new ArrayList<String>();

        for(int i=0;i<filesArray.length;i++)
        {
            PsiFile file = filesArray[i];

            PsiDirectory directory = file.getContainingDirectory();
            String result = directory.getVirtualFile().getCanonicalPath();

            SourceLibrary firstLibrary = locator.getFirstLibraryThatIncludesFile(result, libraries);

            if(firstLibrary != null)
            {
                if(!firstLibrary.getPath().equals(""))
                {
                    result = firstLibrary.getName() + result.substring(result.indexOf(firstLibrary.getPath()) + firstLibrary.getPath().length());
                }
                result = result.substring(result.indexOf(firstLibrary.getName()));
                result = result.replace('\\', '/') + '/' + file.getName().substring(0, file.getName().indexOf('.'));

                String originalModulePath = null;
                String relativePathOption = null;
                String absolutePathOption = null;

                if(originalModule != null)
                {
                    originalModulePath = originalModule.getContainingDirectory().getVirtualFile().getCanonicalPath();
                    SourceLibrary originalModuleLibrary = locator.getFirstLibraryThatIncludesFile(originalModulePath, libraries);
                    originalModulePath = originalModuleLibrary.getName() + originalModulePath.substring(originalModulePath.indexOf(originalModuleLibrary.getPath()) + originalModuleLibrary.getPath().length());

                    String relativePath = FileUtil.convertToRelativePath(originalModulePath, result);

                    if(relativePath != null)
                    {
                        // need to use dojo syntax when two files are in the same directory
                        if(relativePath.equals("."))
                        {
                            relativePath = "./";
                        }
                        else if (relativePath.charAt(0) != '.' && relativePath.charAt(0) != '/')
                        {
                            // top level module
                            relativePath = "./" + relativePath;
                        }

                        relativePathOption = relativePath;
                    }
                }

                absolutePathOption = result;
                String pluginPostFix = NameResolver.getAMDPluginResourceIfPossible(module, true);

                if(prioritizeRelativePaths && relativePathOption != null)
                {
                    choices.add(relativePathOption + pluginPostFix);
                    moduleFileMap.put(relativePathOption + pluginPostFix, file);
                    choices.add(absolutePathOption + pluginPostFix);
                    moduleFileMap.put(absolutePathOption + pluginPostFix, file);
                }
                else
                {
                    choices.add(absolutePathOption + pluginPostFix);
                    moduleFileMap.put(absolutePathOption + pluginPostFix, file);

                    if(relativePathOption != null)
                    {
                        choices.add(relativePathOption + pluginPostFix);
                        moduleFileMap.put(relativePathOption + pluginPostFix, file);
                    }
                }
            }
        }

        Collections.sort(choices, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return getScore(o2, o1) - getScore(o1, o2);
            }
        });

        // sort the map based on score.
        LinkedHashMap<String, PsiFile> finalMapResults = new LinkedHashMap<String, PsiFile>();
        for(String choice : choices)
        {
            for(Map.Entry<String, PsiFile> entry : moduleFileMap.entrySet())
            {
                if(choice.equals(entry.getKey()))
                {
                    finalMapResults.put(entry.getKey(), entry.getValue());
                    break;
                }
            }
        }

        choices.add(module);
{'fraction_non_alphanumeric': 0.10118347832902265, 'fraction_numerical': 0.003291794027745121, 'mean_word_length': 1.9496070272769301, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5045732', 'n_tokens_mistral': 3318, 'n_tokens_neox': 3120, 'n_words': 587}