<commit_before>package com.redhat.ceylon.eclipse.code.correct;

import static com.redhat.ceylon.eclipse.util.Indents.getDefaultIndent;
import static com.redhat.ceylon.eclipse.util.Indents.getDefaultLineDelimiter;
import static com.redhat.ceylon.eclipse.util.Indents.getIndent;

import java.util.Collection;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.ltk.core.refactoring.TextChange;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;

import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.eclipse.util.Nodes;
import com.redhat.ceylon.model.typechecker.model.Parameter;

public class ConvertToDefaultConstructorProposal {

    static void addConvertToDefaultConstructorProposal(
            Collection<ICompletionProposal> proposals, 
            IDocument doc, IFile file, 
            Tree.CompilationUnit rootNode, 
            Tree.Statement statement) {
        if (statement instanceof Tree.ClassDefinition) {
            Tree.ClassDefinition cd = 
                    (Tree.ClassDefinition) 
                        statement;
            Tree.ParameterList pl = cd.getParameterList();
            if (pl!=null) {
                TextChange change = 
                        new TextFileChange(
                                "Convert to Class with Default Constructor", 
                                file);
                String indent = getIndent(statement, doc);
                String delim = getDefaultLineDelimiter(doc);
                String defIndent = getDefaultIndent();
                int insertLoc = cd.getClassBody().getStartIndex()+1;
                StringBuilder declarations = new StringBuilder();
                StringBuilder assignments = new StringBuilder();
                StringBuilder params = new StringBuilder();
                for (Tree.Parameter p: pl.getParameters()) {
                    if (p instanceof Tree.InitializerParameter) {
                        Node pdn = 
                                Nodes.findReferencedNode(rootNode, 
                                        p.getParameterModel().getModel());
                        if (pdn!=null) {
                            //the constructor has to come 
                            //after the declarations of the
                            //parameters
                            insertLoc = pdn.getEndIndex();
                        }
                    }
                    Parameter model = p.getParameterModel();
                    String attDef = "";
                    String paramDef = "";
                    String pname = model.getName();
                    try {
                        attDef = 
                                doc.get(p.getStartIndex(), 
                                        p.getDistance());
                        if (p instanceof Tree.ParameterDeclaration) {
                            Tree.ParameterDeclaration pd = 
                                    (Tree.ParameterDeclaration) p;
                            Tree.TypedDeclaration td = 
                                    pd.getTypedDeclaration();
                            Tree.Type t = td.getType();
                            paramDef = 
                                    doc.get(t.getStartIndex(), 
                                            p.getEndIndex()
                                            - t.getStartIndex());
                        }
                        else {
                            paramDef = 
                                    model.getType()
                                        .asString(cd.getUnit()) 
                                    + " " + pname;
                        }
                    }
                    catch (BadLocationException e) {
                        e.printStackTrace();<commit_msg>handle more parameter list syntax in convert to default constructor<commit_after>package com.redhat.ceylon.eclipse.code.correct;

import static com.redhat.ceylon.eclipse.util.Indents.getDefaultIndent;
import static com.redhat.ceylon.eclipse.util.Indents.getDefaultLineDelimiter;
import static com.redhat.ceylon.eclipse.util.Indents.getIndent;
import static com.redhat.ceylon.eclipse.util.Nodes.findReferencedNode;

import java.util.Collection;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.ltk.core.refactoring.TextChange;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;

import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.model.typechecker.model.Function;
import com.redhat.ceylon.model.typechecker.model.FunctionOrValue;
import com.redhat.ceylon.model.typechecker.model.Parameter;
import com.redhat.ceylon.model.typechecker.model.ParameterList;
import com.redhat.ceylon.model.typechecker.model.Type;
import com.redhat.ceylon.model.typechecker.model.Unit;

public class ConvertToDefaultConstructorProposal {

    static void addConvertToDefaultConstructorProposal(
            Collection<ICompletionProposal> proposals, 
            IDocument doc, IFile file, 
            Tree.CompilationUnit rootNode, 
            Tree.Statement statement) {
        if (statement instanceof Tree.ClassDefinition) {
            Tree.ClassDefinition cd = 
                    (Tree.ClassDefinition) 
                        statement;
            Tree.ParameterList pl = cd.getParameterList();
            if (pl!=null) {
                TextChange change = 
                        new TextFileChange(
                                "Convert to Class with Default Constructor", 
                                file);
                String indent = getIndent(statement, doc);
                String delim = getDefaultLineDelimiter(doc);
                String defIndent = getDefaultIndent();
                int insertLoc = cd.getClassBody().getStartIndex()+1;
                StringBuilder declarations = new StringBuilder();
                StringBuilder assignments = new StringBuilder();
                StringBuilder params = new StringBuilder();
                for (Tree.Parameter p: pl.getParameters()) {
                    if (p instanceof Tree.InitializerParameter) {
                        Node pdn = 
                                findReferencedNode(rootNode, 
                                        p.getParameterModel()
                                            .getModel());
                        if (pdn!=null) {
                            //the constructor has to come 
                            //after the declarations of the
                            //parameters
                            insertLoc = pdn.getEndIndex();
                        }
                    }
                    Parameter model = p.getParameterModel();
                    String attDef = "";
                    StringBuilder paramDef = new StringBuilder();
                    String pname = model.getName();
                    Unit unit = cd.getUnit();
                    try {
                        attDef = 
                                doc.get(p.getStartIndex(), 
                                        p.getDistance());
                        if (p instanceof Tree.ParameterDeclaration) {
                            Tree.ParameterDeclaration pd = 
                                    (Tree.ParameterDeclaration) p;
                            Tree.TypedDeclaration td = 
                                    pd.getTypedDeclaration();
                            Tree.Type t = td.getType();
                            String text = 
                                    doc.get(t.getStartIndex(), 
                                            p.getEndIndex()
                                            - t.getStartIndex());
                            paramDef.append(text);
;
                        }
                        else if (p instanceof Tree.InitializerParameter) {
                            Tree.InitializerParameter ip =
                                    (Tree.InitializerParameter) p;
                            Type pt = model.getType();
                            paramDef.append(pt.asString(unit))
                                    .append(" ")
                                    .append(pname);
                            FunctionOrValue dec = model.getModel();
                            if (dec instanceof Function) {
                                Function run = (Function) dec;
                                for (ParameterList npl: 
                                        run.getParameterLists()) {
                                    paramDef.append("(");
                                    boolean first = true;
                                    for (Parameter np: 
                                            npl.getParameters()) {
                                        if (first) {
                                            first = false;
                                        }
                                        else {
                                            paramDef.append(", ");
                                        }
                                        Type npt = np.getType();
                                        paramDef.append(npt.asString(unit) )
                                                .append(" ")
                                                .append(np.getName());
                                    }
                                    paramDef.append(")");
                                }
                            }
                            Tree.SpecifierExpression se = 
                                    ip.getSpecifierExpression();
                            if (se!=null) {
                                String text = 
                                        doc.get(se.getStartIndex(), 
                                                se.getDistance());
                                paramDef.append(text);
                            }
                        }
                        else {
                            //impossible
                            return;
                        }
                    }
                    catch (BadLocationException e) {
                        e.printStackTrace();
{'fraction_non_alphanumeric': 0.07492716678805535, 'fraction_numerical': 0.0001820830298616169, 'mean_word_length': 1.0903901046622264, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 55, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4251659', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2248, 'n_words': 434}