<NME> ElmExternalFormatActionTest.kt
<BEF> package org.elm.ide.actions

import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.testFramework.MapDataContext
import com.intellij.testFramework.TestActionEvent
import junit.framework.TestCase
import com.intellij.testFramework.TestActionEvent
import junit.framework.TestCase
import org.elm.workspace.ElmWorkspaceTestBase
import org.intellij.lang.annotations.Language

class ElmExternalFormatActionTest : ElmWorkspaceTestBase() {


    fun `test elm-format action with elm 19`() {
        buildProject {
            project("elm.json", manifestElm19)
            dir("src") {
                elm("Main.elm", """
                    module Main exposing (f)


                    f x = x

                """.trimIndent())
            }
        }

        val file = myFixture.configureFromTempProjectFile("src/Main.elm").virtualFile
        val document = FileDocumentManager.getInstance().getDocument(file)!!
        reformat(file)
        val expected = """
                    module Main exposing (f)


                    f x =
                        x

                """.trimIndent()
        TestCase.assertEquals(expected, document.text)
    }

    fun `test elm-format action shouldn't be active on non-elm files`() {
        buildProject {
            project("elm.json", manifestElm19.trimIndent())
            dir("src") {
                elm("Main.elm")
                file("foo.txt", "")
            }

        val file = myFixture.configureFromTempProjectFile(fileWithCaret).virtualFile
        val document = FileDocumentManager.getInstance().getDocument(file)!!
        reformat(PsiManager.getInstance(project).findFile(file)!!)
        val expected = """
                    module Main exposing (f)

    }

    fun `test elm-format action should add to the undo stack`() {
        buildProject {
            project("elm.json", manifestElm19)
            dir("src") {
                elm("Main.elm", """
                    module Main exposing (f)


                    f x = x

                """.trimIndent())
            }
        }

        val file = myFixture.configureFromTempProjectFile("src/Main.elm").virtualFile
        reformat(file)

        val fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(file)

        val undoManager = UndoManager.getInstance(project)
        TestCase.assertTrue(undoManager.isUndoAvailable(fileEditor))
    }

    private fun reformat(file: VirtualFile) {
        val (action, event) = makeTestAction(file)
        check(event.presentation.isEnabledAndVisible) {
            "The elm-format action should be enabled in this context"
        }
        action.actionPerformed(event)
    }

    private fun makeTestAction(file: VirtualFile): Pair<ElmExternalFormatAction, TestActionEvent> {
        val dataContext = MapDataContext(mapOf(
                CommonDataKeys.PROJECT to project,
                CommonDataKeys.VIRTUAL_FILE to file,
                CommonDataKeys.EDITOR to editor
        ))

        val file = myFixture.configureFromTempProjectFile(fileWithCaret).virtualFile
        val document = FileDocumentManager.getInstance().getDocument(file)!!
        reformat(PsiManager.getInstance(project).findFile(file)!!)
        val expected = """
                    module Main exposing (f)



@Language("JSON")
private val manifestElm19 = """
        {
        TestCase.assertEquals(expected, document.text)
    }

    private fun reformat(file: PsiFile) {
        val dataContext = MapDataContext(mapOf(
                CommonDataKeys.PROJECT to project,
                CommonDataKeys.PSI_FILE to file
        ))
        val action = ElmExternalFormatAction()
        val e = TestActionEvent(dataContext, action)
        action.beforeActionPerformedUpdate(e)
        check(e.presentation.isEnabledAndVisible) {
            "The elm-format action should be enabled in this context"
        }
        action.actionPerformed(e)
    }
}

<MSG> Make elm-format work nicely with undo (#261)

Previously, if you tried to undo after invoking external elm-format, IntelliJ would show you a warning dialog about "undoing external changes from disk" (or something like that). Now you don't get annoying dialog.
<DFF> @@ -2,8 +2,7 @@ package org.elm.ide.actions
 
 import com.intellij.openapi.actionSystem.CommonDataKeys
 import com.intellij.openapi.fileEditor.FileDocumentManager
-import com.intellij.psi.PsiFile
-import com.intellij.psi.PsiManager
+import com.intellij.openapi.vfs.VirtualFile
 import com.intellij.testFramework.MapDataContext
 import com.intellij.testFramework.TestActionEvent
 import junit.framework.TestCase
@@ -53,7 +52,7 @@ class ElmExternalFormatActionTest : ElmWorkspaceTestBase() {
 
         val file = myFixture.configureFromTempProjectFile(fileWithCaret).virtualFile
         val document = FileDocumentManager.getInstance().getDocument(file)!!
-        reformat(PsiManager.getInstance(project).findFile(file)!!)
+        reformat(file)
         val expected = """
                     module Main exposing (f)
 
@@ -99,7 +98,7 @@ class ElmExternalFormatActionTest : ElmWorkspaceTestBase() {
 
         val file = myFixture.configureFromTempProjectFile(fileWithCaret).virtualFile
         val document = FileDocumentManager.getInstance().getDocument(file)!!
-        reformat(PsiManager.getInstance(project).findFile(file)!!)
+        reformat(file)
         val expected = """
                     module Main exposing (f)
 
@@ -111,17 +110,60 @@ class ElmExternalFormatActionTest : ElmWorkspaceTestBase() {
         TestCase.assertEquals(expected, document.text)
     }
 
-    private fun reformat(file: PsiFile) {
+    fun `test elm-format action shouldn't be active on non-elm files`() {
+        val fileWithCaret = buildProject {
+            project("elm.json", """
+            {
+                "type": "application",
+                "source-directories": [
+                    "src"
+                ],
+                "elm-version": "0.18.0",
+                "dependencies": {
+                    "direct": {},
+                    "indirect": {}
+                },
+                "test-dependencies": {
+                    "direct": {},
+                    "indirect": {}
+                }
+            }
+            """.trimIndent())
+            dir("src") {
+                elm("Main.scala", """
+                    module Main exposing (f)
+
+
+                    f x = x{-caret-}
+
+                """.trimIndent())
+            }
+        }.fileWithCaret
+
+        val file = myFixture.configureFromTempProjectFile(fileWithCaret).virtualFile
+        val (_, e) = makeTestAction(file)
+        check(!e.presentation.isEnabledAndVisible) {
+            "The elm-format action shouldn't be enabled in this context"
+        }
+    }
+
+    private fun reformat(file: VirtualFile) {
+        val (action, event) = makeTestAction(file)
+        action.beforeActionPerformedUpdate(event)
+        check(event.presentation.isEnabledAndVisible) {
+            "The elm-format action should be enabled in this context"
+        }
+        action.actionPerformed(event)
+    }
+
+    private fun makeTestAction(file: VirtualFile): Pair<ElmExternalFormatAction, TestActionEvent> {
         val dataContext = MapDataContext(mapOf(
                 CommonDataKeys.PROJECT to project,
-                CommonDataKeys.PSI_FILE to file
+                CommonDataKeys.VIRTUAL_FILE to file
         ))
         val action = ElmExternalFormatAction()
-        val e = TestActionEvent(dataContext, action)
-        action.beforeActionPerformedUpdate(e)
-        check(e.presentation.isEnabledAndVisible) {
-            "The elm-format action should be enabled in this context"
-        }
-        action.actionPerformed(e)
+        val event = TestActionEvent(dataContext, action)
+        action.beforeActionPerformedUpdate(event)
+        return Pair(action, event)
     }
 }
\ No newline at end of file

Make elm-format work nicely with undo (#261)