Skip to content

Commit 347dc04

Browse files
authored
Merge pull request #15 from andipabst/fix/elm-format
Show elm-format syntax error notification
2 parents 5a2e09e + ad8ebcc commit 347dc04

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/main/kotlin/org/elm/workspace/commandLineTools/ElmFormatCLI.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class ElmFormatCLI(private val elmFormatExecutablePath: Path) {
4545
class UnknownFailure(msg: String? = null, cause: Throwable?) : ElmFormatResult(msg ?: "Something went wrong running elm-format", cause)
4646
}
4747

48+
private fun isBadSyntaxErrorMessage(msg: String): Boolean {
49+
return msg.contains("SYNTAX PROBLEM", ignoreCase = true) // Elm-format 0.8.4 and below
50+
|| msg.contains("Unable to parse file") // Elm-format 0.8.5 and above
51+
}
52+
4853
fun formatDocumentAndSetText(project: Project, document: Document, version: Version, addToUndoStack: Boolean): ElmFormatResult {
4954
val processOutput = try {
5055
ProgressManager.getInstance().runProcessWithProgressSynchronously<ProcessOutput, ExecutionException>({
@@ -59,7 +64,12 @@ class ElmFormatCLI(private val elmFormatExecutablePath: Path) {
5964
}
6065
}
6166

62-
if (processOutput.isNotSuccess) return ElmFormatResult.UnknownFailure("Process output exit code was non-zero", cause = null)
67+
if (processOutput.isNotSuccess) {
68+
if (isBadSyntaxErrorMessage(processOutput.stderr)) {
69+
return ElmFormatResult.BadSyntax()
70+
}
71+
return ElmFormatResult.UnknownFailure("Process output exit code was non-zero", cause = null)
72+
}
6373

6474
val formatted = processOutput.stdout
6575
val source = document.text

src/test/kotlin/org/elm/ide/actions/ElmExternalFormatActionTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.elm.ide.actions
22

3+
import com.intellij.notification.Notification
4+
import com.intellij.notification.Notifications
35
import com.intellij.openapi.actionSystem.CommonDataKeys
46
import com.intellij.openapi.command.undo.UndoManager
57
import com.intellij.openapi.fileEditor.FileDocumentManager
68
import com.intellij.openapi.fileEditor.FileEditorManager
9+
import com.intellij.openapi.util.Ref
710
import com.intellij.openapi.vfs.VirtualFile
811
import com.intellij.testFramework.MapDataContext
912
import com.intellij.testFramework.TestActionEvent
@@ -55,6 +58,36 @@ class ElmExternalFormatActionTest : ElmWorkspaceTestBase() {
5558
TestCase.assertEquals(expected, document.text)
5659
}
5760

61+
@Test
62+
fun `test elm-format action on a file with syntax errors`() {
63+
val originalCode = """
64+
module Main exposing (f)
65+
66+
67+
f x =
68+
""".trimIndent()
69+
70+
71+
buildProject {
72+
project("elm.json", manifestElm19)
73+
dir("src") {
74+
elm("Main.elm", originalCode)
75+
}
76+
}
77+
78+
val file = myFixture.configureFromTempProjectFile("src/Main.elm").virtualFile
79+
val document = FileDocumentManager.getInstance().getDocument(file)!!
80+
81+
val ref = connectToBusAndGetNotificationRef()
82+
reformat(file)
83+
84+
TestCase.assertEquals(originalCode, document.text)
85+
86+
TestCase.assertEquals("elm-format encountered syntax errors that it could not fix", ref.get().content)
87+
TestCase.assertEquals(1, ref.get().actions.size)
88+
TestCase.assertEquals("Show Errors", ref.get().actions.first().templatePresentation.text)
89+
}
90+
5891
@Test
5992
fun `test elm-format action shouldn't be active on non-elm files`() {
6093
buildProject {
@@ -116,6 +149,16 @@ class ElmExternalFormatActionTest : ElmWorkspaceTestBase() {
116149
return Pair(action, event)
117150
}
118151

152+
private fun connectToBusAndGetNotificationRef(): Ref<Notification> {
153+
val notificationRef = Ref<Notification>()
154+
project.messageBus.connect(testRootDisposable).subscribe(Notifications.TOPIC,
155+
object : Notifications {
156+
override fun notify(notification: Notification) =
157+
notificationRef.set(notification)
158+
})
159+
return notificationRef
160+
}
161+
119162
}
120163

121164

0 commit comments

Comments
 (0)