package com.github.itechbear.macroexpansion.formatter;

import com.intellij.codeInsight.actions.ReformatCodeProcessor;
import com.intellij.lang.Language;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.RunResult;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.impl.PsiFileFactoryImpl;

import java.lang.reflect.Field;

/**
 * Created by nicholas on 7/27/14.
 */
public class Formatter {
    public static String format(PsiElement psiElement, String text) {
        Project project = psiElement.getProject();
        Language language = psiElement.getLanguage();
        return format(project, language, text);
    }

    public static String format(final Project project, Language language, String text) {
        final PsiFile psiFile = PsiFileFactoryImpl.getInstance(project).createFileFromText("virtual", language, text);
        WriteCommandAction.Simple<String> command = new WriteCommandAction.Simple<String>(project, psiFile) {

            @Override
            protected void run() throws Throwable {
                CodeStyleManager.getInstance(project).reformat(psiFile, false);
            }
        };
        command.execute();
        String formatted = psiFile.getText();
        if (formatted == null) {
            return text;
        }
        return formatted.replace(" ", "&nbsp;");
    }
}

@@ -77,193 +77,247 @@
 code
-Insight.actions.ReformatCodeProcessor;%0Aimport com.intellij.lang.Language;%0Aimport com.intellij.openapi.application.Application;%0Aimport com.intellij.openapi.application.ApplicationManager
+Editor.printing.FileSeparatorProvider;%0Aimport com.intellij.codeInsight.daemon.LineMarkerInfo;%0Aimport com.intellij.ide.highlighter.HighlighterFactory;%0Aimport com.intellij.lang.Language;%0Aimport com.intellij.openapi.command.WriteCommandAction
 ;%0Aim
@@ -346,28 +346,22 @@
 api.
-application.RunResul
+editor.Documen
 t;%0Ai
@@ -391,88 +391,112 @@
 api.
-command.CommandProcessor;%0Aimport com.intellij.openapi.command.WriteCommandAction
+editor.highlighter.EditorHighlighter;%0Aimport com.intellij.openapi.editor.highlighter.HighlighterIterator
 ;%0Aim
@@ -532,25 +532,29 @@
 tor.
-impl.DocumentImpl
+markup.TextAttributes
 ;%0Aim
@@ -697,59 +697,13 @@
 Comp
-utable;%0Aimport com.intellij.openapi.vfs.VirtualFile
+aring
 ;%0Aim
@@ -874,39 +874,189 @@
 pl;%0A
-%0Aimport java.lang.reflect.Field
+import com.intellij.psi.util.PsiUtilBase;%0Aimport org.jetbrains.annotations.NonNls;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0A/
@@ -1301,30 +1301,42 @@
 ();%0A        
-return
+String formatted =
  format(proj
@@ -1353,24 +1353,56 @@
 age, text);%0A
+        return wrap(formatted);%0A
     %7D%0A%0A    p
@@ -2089,11 +2089,232 @@
 );%0A    %7D
+%0A%0A    public static String getFontname() %7B%0A        return %22monospace%22;%0A    %7D%0A%0A    public static String wrap(String text) %7B%0A        return String.format(%22%3Cdiv style=%5C%22font-family:%25s%5C%22%3E%25s%3C/div%3E%22, getFontname(), text);%0A    %7D
 %0A%7D%0A

add fontname