package com.reason.build.bs.console;

import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.reason.Platform;
import com.reason.build.bs.Bucklescript;
import com.reason.build.bs.BucklescriptManager;
import com.reason.build.bs.compiler.BsCompiler;
import com.reason.build.bs.compiler.CliType;
import com.reason.hints.InsightManagerImpl;

public class MakeWorldAction extends DumbAwareAction {

    private final ConsoleView m_console;
    private final Project m_project;

    MakeWorldAction(ConsoleView console, Project project) {
        super("Clean and make world", "Clean and make world", AllIcons.General.Web);
        m_project = project;
        m_console = console;
    }

    @Override
    public void actionPerformed(AnActionEvent e) {
        Bucklescript bucklescript = BucklescriptManager.getInstance(m_project);
        BsCompiler bsc = bucklescript.getOrCreateCompiler();
        if (bsc != null) {
            VirtualFile baseRoot = Platform.findBaseRoot(m_project);
            VirtualFile sourceFile = baseRoot.findChild("bsconfig.json");
            if (sourceFile != null) {
                ProcessHandler bscProcess = bsc.recreate(sourceFile, CliType.cleanMake);
                if (bscProcess != null) {
                    m_console.attachToProcess(bscProcess);
                    bsc.startNotify();
                    InsightManagerImpl.getInstance(m_project).downloadRincewindIfNeeded();
                }
            }
        }
    }
}

@@ -131,16 +131,73 @@
 leView;%0A
+import com.intellij.execution.ui.ConsoleViewContentType;%0A
 import c
@@ -280,16 +280,162 @@
 nEvent;%0A
+import com.intellij.openapi.editor.Document;%0Aimport com.intellij.openapi.editor.Editor;%0Aimport com.intellij.openapi.fileEditor.FileEditorManager;%0A
 import c
@@ -569,16 +569,93 @@
 alFile;%0A
+import com.intellij.psi.PsiDocumentManager;%0Aimport com.intellij.psi.PsiFile;%0A
 import c
@@ -1507,20 +1507,638 @@
 ile base
-Root
+Dir = Platform.findBaseRoot(m_project);%0A%0A            // Try to detect the current active editor%0A            Editor editor = FileEditorManager.getInstance(m_project).getSelectedTextEditor();%0A            if (editor == null) %7B%0A                m_console.print(%22No active text editor found, using %22 + baseDir.getPath() + %22 as root directory%5Cn%22, ConsoleViewContentType.NORMAL_OUTPUT);%0A            %7D else %7B%0A                Document document = editor.getDocument();%0A                PsiFile psiFile = PsiDocumentManager.getInstance(m_project).getPsiFile(document);%0A                if (psiFile != null) %7B%0A                    baseDir
  = Platf
@@ -2145,32 +2145,40 @@
 orm.findBaseRoot
+FromFile
 (m_project);%0A   
@@ -2163,34 +2163,93 @@
 omFile(m_project
-);
+, psiFile.getVirtualFile());%0A                %7D%0A            %7D%0A
 %0A            Vir
@@ -2274,20 +2274,19 @@
 e = base
-Root
+Dir
 .findChi

fix 'make all' button