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

import static com.redhat.ceylon.eclipse.code.editor.EditorUtil.getSelection;
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.findDeclarationWithBody;

import java.io.StringWriter;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ltk.core.refactoring.DocumentChange;
import org.eclipse.text.edits.ReplaceEdit;

import ceylon.formatter.format_;
import ceylon.file.Writer;
import ceylon.file.Writer$impl;
import ceylon.language.Destroyable;

import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.compiler.typechecker.tree.Visitor;
import com.redhat.ceylon.eclipse.code.parse.CeylonParseController;
import com.redhat.ceylon.eclipse.util.Nodes;

final class FormatAction extends Action {
    private final CeylonEditor editor;

    FormatAction(CeylonEditor editor) {
        super(null);
        this.editor = editor;
    }
    
    @Override
    public void run() {
        IDocument document = editor.getCeylonSourceViewer().getDocument();
        final ITextSelection ts = getSelection(editor);
        CeylonParseController pc = editor.getParseController();
        Tree.CompilationUnit rootNode = pc.getRootNode();
        if (rootNode==null) return;
        
        final StringBuilder builder = new StringBuilder(document.getLength());
        format_.format(rootNode, format_.format$options(rootNode), new Writer() {

            @Override
            public Object write(String string) {
                builder.append(string);
                return null; // void
            }
            
            // the rest is boring
            
            @Override
            public Writer$impl $ceylon$file$Writer$impl() {
                return new Writer$impl(this);
            }
            @Override
            public Object close() {
                return null; // void
            }
            @Override
            public Object destroy(Throwable arg0) {
                return null; // void
            }
            @Override
            public Object flush() {
                return null; // void
            }
            @Override
            public Object writeLine() {
                // unused; ceylon.formatter has its own newline handling
                throw new UnsupportedOperationException();
            }
            @Override
            public Object writeLine(String line) {
                // unused; ceylon.formatter has its own newline handling
                throw new UnsupportedOperationException();
            }
            @Override
            public String writeLine$line() {
                return ""; // default value for "line" parameter
            }
        });
        
        String text = builder.toString();
        try {
            if (!document.get().equals(text)) {
                DocumentChange change = 
                        new DocumentChange("Format", document);
                change.setEdit(new ReplaceEdit(0, document.getLength(), text));
                change.perform(new NullProgressMonitor());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}
@@ -121,327 +121,8 @@
 ion;
-%0Aimport static com.redhat.ceylon.eclipse.util.Indents.getDefaultIndent;%0Aimport static com.redhat.ceylon.eclipse.util.Indents.getDefaultLineDelimiter;%0Aimport static com.redhat.ceylon.eclipse.util.Indents.getIndent;%0Aimport static com.redhat.ceylon.eclipse.util.Nodes.findDeclarationWithBody;%0A%0Aimport java.io.StringWriter;
 %0A%0Aim
@@ -403,41 +403,8 @@
 t;%0A%0A
-import ceylon.formatter.format_;%0A
 impo
@@ -476,145 +476,28 @@
 lon.
-language.Destroyable;%0A%0Aimport com.redhat.ceylon.compiler.typechecker.tree.Node;%0Aimport com.redhat.ceylon.compiler.typechecker.tree.Tree
+formatter.format_
 ;%0A
+%0A
 impo
@@ -547,15 +547,12 @@
 ree.
-Visitor
+Tree
 ;%0Aim
@@ -619,53 +619,8 @@
 ler;
-%0Aimport com.redhat.ceylon.eclipse.util.Nodes;
 %0A%0Afi

clean imports