<commit_before>/*
 * Copyright (c) 2017. tangzx(<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tang.intellij.lua.refactoring.rename;

import com.intellij.codeInsight.CodeInsightUtilCore;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pass;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.refactoring.introduce.inplace.InplaceVariableIntroducer;
import com.intellij.refactoring.introduce.inplace.OccurrencesChooser;
import com.tang.intellij.lua.psi.LuaElementFactory;
import com.tang.intellij.lua.psi.LuaExpr;
import com.tang.intellij.lua.psi.LuaNameDef;
import com.tang.intellij.lua.psi.LuaStatement;
import com.tang.intellij.lua.refactoring.LuaRefactoringUtil;
import kotlin.reflect.jvm.internal.impl.utils.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;

/**
 *
 * Created by tangzx on 2017/4/25.
 */
public class LuaIntroduceVarHandler implements RefactoringActionHandler {

    class IntroduceOperation {
        public Project project;
        public Editor editor;
        public PsiFile file;
        private boolean replaceAll;
        private List<PsiElement> occurrences;
        public String name = "var";
        private List<PsiElement> newOccurrences;
        private LuaNameDef var;
        private PsiElement element;

        IntroduceOperation(Project project, Editor editor, PsiFile psiFile) {
            this.project = project;
            this.editor = editor;
            file = psiFile;
        }

        void setReplaceAll(boolean replaceAll) {
            this.replaceAll = replaceAll;
        }

        boolean isReplaceAll() {
            return replaceAll;
        }

        void setOccurrences(List<PsiElement> occurrences) {
            this.occurrences = occurrences;
        }

        List<PsiElement> getOccurrences() {
            return occurrences;
        }

        void setNewOccurrences(List<PsiElement> newOccurrences) {
            this.newOccurrences = newOccurrences;
        }

        PsiElement[] getNewOccurrences() {
            return newOccurrences.toArray(new PsiElement[newOccurrences.size()]);
        }

        public PsiNamedElement getVar() {
            return var;
        }

        public void setVar(LuaNameDef var) {
            this.var = var;
        }

        public PsiElement getElement() {
            return element;
        }

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

    @Override
    public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile, DataContext dataContext) {

    }

    @Override
    public void invoke(@NotNull Project project, @NotNull PsiElement[] psiElements, DataContext dataContext) {

    }

    public void invoke(Project project, Editor editor, LuaExpr expr) {
        IntroduceOperation operation = new IntroduceOperation(project, editor, expr.getContainingFile());
        operation.setElement(expr);
        operation.setOccurrences(getOccurrences(expr));
        OccurrencesChooser.simpleChooser(editor).showChooser(expr, operation.getOccurrences(), new Pass<OccurrencesChooser.ReplaceChoice>() {
            @Override
            public void pass(OccurrencesChooser.ReplaceChoice choice) {
                operation.setReplaceAll(choice == OccurrencesChooser.ReplaceChoice.ALL);
                WriteCommandAction.runWriteCommandAction(operation.project, () -> {
                    performReplace(operation);
                });
                performInplaceIntroduce(operation);
            }
        });
    }

    private List<PsiElement> getOccurrences(LuaExpr expr) {
        return LuaRefactoringUtil.getOccurrences(expr, expr.getContainingFile());
    }

    private static PsiElement findAnchor(List<PsiElement> occurrences) {
        PsiElement anchor = occurrences.get(0);
        next:
        do {
            final LuaStatement statement = PsiTreeUtil.getParentOfType(anchor, LuaStatement.class);
            if (statement != null) {
                final PsiElement parent = statement.getParent();
                for (PsiElement element : occurrences) {
                    if (!PsiTreeUtil.isAncestor(parent, element, true)) {
                        anchor = statement;
                        continue next;
                    }
                }
            }
            return statement;
        }
        while (true);
    }

    private boolean isInline(PsiElement commonParent, IntroduceOperation operation) {
        return (commonParent instanceof LuaStatement || commonParent == operation.element) &&
                (!operation.isReplaceAll() || operation.getOccurrences().size() == 1);
    }<commit_msg>introduce var<commit_after>/*
 * Copyright (c) 2017. tangzx(<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tang.intellij.lua.refactoring.rename;

import com.intellij.codeInsight.CodeInsightUtilCore;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pass;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.refactoring.introduce.inplace.InplaceVariableIntroducer;
import com.intellij.refactoring.introduce.inplace.OccurrencesChooser;
import com.tang.intellij.lua.psi.LuaElementFactory;
import com.tang.intellij.lua.psi.LuaExpr;
import com.tang.intellij.lua.psi.LuaNameDef;
import com.tang.intellij.lua.psi.LuaStatement;
import com.tang.intellij.lua.refactoring.LuaRefactoringUtil;
import kotlin.reflect.jvm.internal.impl.utils.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;

/**
 *
 * Created by tangzx on 2017/4/25.
 */
public class LuaIntroduceVarHandler implements RefactoringActionHandler {

    class IntroduceOperation {
        public Project project;
        public Editor editor;
        public PsiFile file;
        private boolean replaceAll;
        private List<PsiElement> occurrences;
        public String name = "var";
        private List<PsiElement> newOccurrences;
        private LuaNameDef var;
        private PsiElement element;

        IntroduceOperation(Project project, Editor editor, PsiFile psiFile) {
            this.project = project;
            this.editor = editor;
            file = psiFile;
        }

        void setReplaceAll(boolean replaceAll) {
            this.replaceAll = replaceAll;
        }

        boolean isReplaceAll() {
            return replaceAll;
        }

        void setOccurrences(List<PsiElement> occurrences) {
            this.occurrences = occurrences;
        }

        List<PsiElement> getOccurrences() {
            return occurrences;
        }

        void setNewOccurrences(List<PsiElement> newOccurrences) {
            this.newOccurrences = newOccurrences;
        }

        PsiElement[] getNewOccurrences() {
            return newOccurrences.toArray(new PsiElement[newOccurrences.size()]);
        }

        public PsiNamedElement getVar() {
            return var;
        }

        public void setVar(LuaNameDef var) {
            this.var = var;
        }

        public PsiElement getElement() {
            return element;
        }

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

    @Override
    public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile, DataContext dataContext) {

    }

    @Override
    public void invoke(@NotNull Project project, @NotNull PsiElement[] psiElements, DataContext dataContext) {

    }

    public void invoke(Project project, Editor editor, LuaExpr expr) {
        IntroduceOperation operation = new IntroduceOperation(project, editor, expr.getContainingFile());
        operation.setElement(expr);
        operation.setOccurrences(getOccurrences(expr));
        OccurrencesChooser.simpleChooser(editor).showChooser(expr, operation.getOccurrences(), new Pass<OccurrencesChooser.ReplaceChoice>() {
            @Override
            public void pass(OccurrencesChooser.ReplaceChoice choice) {
                operation.setReplaceAll(choice == OccurrencesChooser.ReplaceChoice.ALL);
                WriteCommandAction.runWriteCommandAction(operation.project, () -> {
                    performReplace(operation);
                });
                performInplaceIntroduce(operation);
            }
        });
    }

    private List<PsiElement> getOccurrences(LuaExpr expr) {
        return LuaRefactoringUtil.getOccurrences(expr, expr.getContainingFile());
    }

    private static PsiElement findAnchor(List<PsiElement> occurrences) {
        PsiElement anchor = occurrences.get(0);
        next:
        do {
            final LuaStatement statement = PsiTreeUtil.getParentOfType(anchor, LuaStatement.class);
            if (statement != null) {
                final PsiElement parent = statement.getParent();
                for (PsiElement element : occurrences) {
                    if (!PsiTreeUtil.isAncestor(parent, element, true)) {
                        anchor = statement;
                        continue next;
                    }
                }
            }
            return statement;
        }
        while (true);
    }

    private boolean isInline(PsiElement commonParent, IntroduceOperation operation) {
        if (commonParent == operation.element)
            commonParent = operation.element.getParent();
        return commonParent instanceof LuaStatement && (!operation.isReplaceAll() || operation.getOccurrences().size() == 1);
    }
{'fraction_non_alphanumeric': 0.08604038630377524, 'fraction_numerical': 0.0029850746268656717, 'mean_word_length': 2.740886699507389, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7395370', 'n_tokens_mistral': 3257, 'n_tokens_neox': 3061, 'n_words': 700}