package com.redhat.ceylon.eclipse.code.correct;

import java.util.Collection;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.ltk.core.refactoring.Change;
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.Tree;

class FillInArgumentNameProposal extends CorrectionProposal {

    public FillInArgumentNameProposal(String name, Change change) {
        super("Fill in argument name '" + name + "'", change, null);
    }

    static void addFillInArgumentNameProposal(Collection<ICompletionProposal> proposals, 
            IDocument doc, IFile file, Tree.SpecifiedArgument sa) {
        Tree.Identifier id = sa.getIdentifier();
        if (id.getToken()==null) {
            TextChange change = new TextFileChange("Convert to Block", file);
            change.setEdit(new MultiTextEdit());
            Tree.Expression e = sa.getSpecifierExpression().getExpression();
            if (e!=null) {
                final String name = id.getText();
                if (e.getTerm() instanceof Tree.FunctionArgument) {
                    Tree.FunctionArgument fa = (Tree.FunctionArgument) e.getTerm();
                    if (!fa.getParameterLists().isEmpty()) {
                        int startIndex = fa.getParameterLists().get(0).getStartIndex();
                        if (fa.getType().getToken()==null) {
                            change.addEdit(new InsertEdit(startIndex, "function "));
                        }
                        change.addEdit(new InsertEdit(startIndex, name));
                        try {
                            if (doc.getChar(sa.getStopIndex())==';') {
                                change.addEdit(new DeleteEdit(sa.getStopIndex(), 1));
                            }
                        }
                        catch (Exception ex) {}
                    }
                }
                else {
                    change.addEdit(new InsertEdit(sa.getStartIndex(), name + " = "));
                }
                if (change.getEdit().hasChildren()) {
                    proposals.add(new FillInArgumentNameProposal(name, change));
                }
            }
        }
    }

}

@@ -1357,24 +1357,468 @@
 Argument) %7B%0A
+                    //convert anon functions to typed named argument%0A                    //i.e.     (Param param) =%3E result;%0A                    //becomes  function fun(Param param) =%3E result;%0A                    //and      (Param param) %7B return result; %7D;%0A                    //becomes  function fun(Param param) %7B return result; %7D%0A                    //and      void (Param param) %7B%7D;%0A                    //becomes  void fun(Param param) %7B%7D%0A
             
@@ -1885,24 +1885,24 @@
 .getTerm();%0A
-
             
@@ -2091,32 +2091,163 @@
 oken()==null) %7B%0A
+                            //only really necessary if the anon %0A                            //function has a block instead of =%3E %0A
                 
@@ -2473,20 +2473,285 @@
         
-if (
+//if it is an anon function with a body,%0A                            //we must remove the trailing ; which is%0A                            //required by the named arg list syntax%0A                            if (fa.getBlock()!=null &&%0A                                    
 doc.getC
@@ -3003,32 +3003,32 @@
               %7D%0A
-
                 
@@ -3034,16 +3034,160 @@
  else %7B%0A
+                    //convert other args to specified named args%0A                    //i.e.     arg;%0A                    //becomes  name = arg;%0A
         

fix minor bug in Fill in argument name