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

import com.intellij.lang.javascript.psi.impl.JSChangeUtil;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * This class is responsible for sorting AMD imports. This requires the following steps
 *  - gather all imports in a define statemnet (easy)
 *  - sort them (easy)
 *  - reorder the PsiElements (difficult)
 */
public class AMDImportOrganizer
{
    public class SortingResult
    {
        private SortedPsiElementAdapter[] defines;
        private SortedPsiElementAdapter[] parameters;
        private int singleQuotes;
        private int doubleQuotes;

        public SortingResult(SortedPsiElementAdapter[] defines, SortedPsiElementAdapter[] parameters)
        {
            this.defines = defines;
            this.parameters = parameters;
        }

        public SortedPsiElementAdapter[] getParameters() {
            return parameters;
        }

        public void setParameters(SortedPsiElementAdapter[] parameters) {
            this.parameters = parameters;
        }

        public SortedPsiElementAdapter[] getDefines() {
            return defines;
        }

        public void setDefines(SortedPsiElementAdapter[] defines) {
            this.defines = defines;
        }

        public int getSingleQuotes() {
            return singleQuotes;
        }

        public void setSingleQuotes(int singleQuotes) {
            this.singleQuotes = singleQuotes;
        }

        public int getDoubleQuotes() {
            return doubleQuotes;
        }

        public void setDoubleQuotes(int doubleQuotes) {
            this.doubleQuotes = doubleQuotes;
        }
    }

    public class SortedPsiElementAdapter
    {
        private PsiElement element;
        private boolean inactive;

        public SortedPsiElementAdapter(PsiElement element, boolean inactive) {
            this.element = element;
            this.inactive = inactive;
        }

        public PsiElement getElement() {
            return element;
        }

        public void setElement(PsiElement element) {
            this.element = element;
        }

        public boolean isInactive() {
            return inactive;
        }

        public void setInactive(boolean inactive) {
            this.inactive = inactive;
        }
    }

    private class SortItem
    {
        private PsiElement define;
        private PsiElement parameter;
        private boolean inactive;

        public SortItem(PsiElement define, PsiElement parameter, boolean inactive)
        {
            this.define = define;
            this.parameter = parameter;
        }

        public PsiElement getDefine() {
            return define;
        }

        public void setDefine(PsiElement define) {
            this.define = define;
        }

        public PsiElement getParameter() {
            return parameter;
        }

        public void setParameter(PsiElement parameter) {
            this.parameter = parameter;
        }

        public boolean isInactive() {
            return inactive;
        }

        public void setInactive(boolean inactive) {
            this.inactive = inactive;
        }
    }

    public void reorder(PsiElement[] unsorted, SortedPsiElementAdapter[] sorted, boolean deleteTrailingComma, SortingResult result)
    {
        char quote = '\'';
        if(result.getDoubleQuotes() > result.getSingleQuotes())
        {
            quote = '\"';
        }

        List<PsiElement> deleteList = new ArrayList<PsiElement>();

        // make the sorted ones copies of the original
        // replace the unsorted ones
        for(int i=0;i<unsorted.length;i++)
        {
            if(!sorted[i].isInactive())
            {
                PsiElement newElement = null;
                newElement = sorted[i].getElement().copy();
                Project project = sorted[i].getElement().getProject();
                String text = newElement.getText();
                char mark = text.charAt(0);

                if(mark != quote && (mark == '\'' || mark == '\"'))
                {
                    newElement = JSChangeUtil.createExpressionFromText(project,text.replace(text.charAt(0), quote)).getPsi();
                }

                unsorted[i].replace(newElement);
            }
            else
            {
                deleteList.add(unsorted[i]);
                if(unsorted[i].getNextSibling() != null && deleteTrailingComma)
                {
                    deleteList.add(unsorted[i].getNextSibling());
                }
            }
        }

        for(int i=0;i<deleteList.size();i++)
        {
            try
            {
                deleteList.get(i).delete();;
            }
            catch(Exception e)
            {
                // sometimes deleting comma's will throw an exception (ignorant as to why)
            }
        }
    }

    private List<SortItem> getSortItems(List<PsiElement> defines, List<PsiElement> parameters)
    {<commit_msg>remove trailing comma when organizing imports. fixes #78<commit_after>package com.chrisfolger.needsmoredojo.core.amd;

import com.chrisfolger.needsmoredojo.core.util.DefineUtil;
import com.intellij.lang.javascript.psi.impl.JSChangeUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * This class is responsible for sorting AMD imports. This requires the following steps
 *  - gather all imports in a define statemnet (easy)
 *  - sort them (easy)
 *  - reorder the PsiElements (difficult)
 */
public class AMDImportOrganizer
{
    public class SortingResult
    {
        private SortedPsiElementAdapter[] defines;
        private SortedPsiElementAdapter[] parameters;
        private int singleQuotes;
        private int doubleQuotes;

        public SortingResult(SortedPsiElementAdapter[] defines, SortedPsiElementAdapter[] parameters)
        {
            this.defines = defines;
            this.parameters = parameters;
        }

        public SortedPsiElementAdapter[] getParameters() {
            return parameters;
        }

        public void setParameters(SortedPsiElementAdapter[] parameters) {
            this.parameters = parameters;
        }

        public SortedPsiElementAdapter[] getDefines() {
            return defines;
        }

        public void setDefines(SortedPsiElementAdapter[] defines) {
            this.defines = defines;
        }

        public int getSingleQuotes() {
            return singleQuotes;
        }

        public void setSingleQuotes(int singleQuotes) {
            this.singleQuotes = singleQuotes;
        }

        public int getDoubleQuotes() {
            return doubleQuotes;
        }

        public void setDoubleQuotes(int doubleQuotes) {
            this.doubleQuotes = doubleQuotes;
        }
    }

    public class SortedPsiElementAdapter
    {
        private PsiElement element;
        private boolean inactive;

        public SortedPsiElementAdapter(PsiElement element, boolean inactive) {
            this.element = element;
            this.inactive = inactive;
        }

        public PsiElement getElement() {
            return element;
        }

        public void setElement(PsiElement element) {
            this.element = element;
        }

        public boolean isInactive() {
            return inactive;
        }

        public void setInactive(boolean inactive) {
            this.inactive = inactive;
        }
    }

    private class SortItem
    {
        private PsiElement define;
        private PsiElement parameter;
        private boolean inactive;

        public SortItem(PsiElement define, PsiElement parameter, boolean inactive)
        {
            this.define = define;
            this.parameter = parameter;
        }

        public PsiElement getDefine() {
            return define;
        }

        public void setDefine(PsiElement define) {
            this.define = define;
        }

        public PsiElement getParameter() {
            return parameter;
        }

        public void setParameter(PsiElement parameter) {
            this.parameter = parameter;
        }

        public boolean isInactive() {
            return inactive;
        }

        public void setInactive(boolean inactive) {
            this.inactive = inactive;
        }
    }

    public void reorder(PsiElement[] unsorted, SortedPsiElementAdapter[] sorted, boolean deleteTrailingComma, SortingResult result)
    {
        PsiElement parent = unsorted[0].getParent();

        char quote = '\'';
        if(result.getDoubleQuotes() > result.getSingleQuotes())
        {
            quote = '\"';
        }

        List<PsiElement> deleteList = new ArrayList<PsiElement>();

        // make the sorted ones copies of the original
        // replace the unsorted ones
        for(int i=0;i<unsorted.length;i++)
        {
            if(!sorted[i].isInactive())
            {
                PsiElement newElement = null;
                newElement = sorted[i].getElement().copy();
                Project project = sorted[i].getElement().getProject();
                String text = newElement.getText();
                char mark = text.charAt(0);

                if(mark != quote && (mark == '\'' || mark == '\"'))
                {
                    newElement = JSChangeUtil.createExpressionFromText(project,text.replace(text.charAt(0), quote)).getPsi();
                }

                unsorted[i].replace(newElement);
            }
            else
            {
                deleteList.add(unsorted[i]);
                if(unsorted[i].getNextSibling() != null && deleteTrailingComma)
                {
                    deleteList.add(unsorted[i].getNextSibling());
                }
            }
        }

        for(int i=0;i<deleteList.size();i++)
        {
            try
            {
                deleteList.get(i).delete();;
            }
            catch(Exception e)
            {
                // sometimes deleting comma's will throw an exception (ignorant as to why)
            }
        }

        /*
         sometimes you get a trailing comma after removing duplicate imports. Instead of trying to track
         the cases where this happens, just check for the presence of a trailing comma and delete if necessary
         */
        if(deleteTrailingComma)
        {
            try
            {
                PsiElement trailingComma = DefineUtil.getNearestComma(parent.getLastChild());
                if(trailingComma != null)
                {
                    trailingComma.delete();
                }
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }

    private List<SortItem> getSortItems(List<PsiElement> defines, List<PsiElement> parameters)
    {
{'fraction_non_alphanumeric': 0.08521505376344087, 'fraction_numerical': 0.000985663082437276, 'mean_word_length': 1.7819042871385842, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '4555564', 'n_tokens_mistral': 2921, 'n_tokens_neox': 2761, 'n_words': 650}