Skip to content

Commit

Permalink
Log if erl binary is not found when trying to run mix format
Browse files Browse the repository at this point in the history
Fixes #2718

Log instead of letting it throw up the stack and cause an error report since the SDK not being set will be very common.
  • Loading branch information
KronicDeth committed Jun 17, 2022
1 parent 72cc916 commit 3de395e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@
* [#2717](https://github.com/KronicDeth/intellij-elixir/pull/2717) - [@KronicDeth](https://github.com/KronicDeth)
* Increase `SUSPECT_NAME_SET_SIZE` to `20`.
Increased to cover the `15` `impl`s of `String.Chars` in the `geo` hex package.
* [#2719](https://github.com/KronicDeth/intellij-elixir/pull/2719) - [@KronicDeth](https://github.com/KronicDeth)
* Log if `erl` binary is not found when trying to run `mix format`.
Log instead of letting it throw up the stack and cause an error report since the SDK not being set will be very common.

## v13.1.0

Expand Down
2 changes: 2 additions & 0 deletions resources/META-INF/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ <h1>v13.1.1</h1>
<li>Add the facet in a write action in addition to setting the SDK.</li>
<li>Increase <code class="notranslate">SUSPECT_NAME_SET_SIZE</code> to <code class="notranslate">20</code>.<br>
Increased to cover the <code class="notranslate">15</code> <code class="notranslate">impl</code>s of <code class="notranslate">String.Chars</code> in the <code class="notranslate">geo</code> hex package.</li>
<li>Log if <code class="notranslate">erl</code> binary is not found when trying to run <code class="notranslate">mix format</code>.<br>
Log instead of letting it throw up the stack and cause an error report since the SDK not being set will be very common.</li>
</ul>
</li>
</ul>
Expand Down
28 changes: 22 additions & 6 deletions src/org/elixir_lang/formatter/MixFormatExternalFormatProcessor.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.elixir_lang.formatter

import com.intellij.application.options.CodeStyle
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.process.CapturingProcessHandler
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
Expand All @@ -15,6 +16,7 @@ import org.elixir_lang.Mix
import org.elixir_lang.code_style.CodeStyleSettings
import org.elixir_lang.psi.ElixirFile
import org.elixir_lang.sdk.elixir.Type.Companion.mostSpecificSdk
import java.io.FileNotFoundException
import java.util.concurrent.TimeUnit

@Suppress("UnstableApiUsage")
Expand Down Expand Up @@ -78,12 +80,26 @@ class MixFormatExternalFormatProcessor : ExternalFormatProcessor {
}
?.path

private fun format(workingDirectory: String, sdk: Sdk, unformattedText: String): String? {
val commandline = Mix.commandLine(emptyMap(), workingDirectory, sdk)
commandline.addParameter("format")
// `-` turns on stdin/stdout for text to format
commandline.addParameter("-")
val processHandler = CapturingProcessHandler(commandline)
private fun format(workingDirectory: String, sdk: Sdk, unformattedText: String): String? =
commandLine(workingDirectory, sdk)
?.let { format(it, unformattedText) }

private fun commandLine(workingDirectory: String, sdk: Sdk): GeneralCommandLine? =
try {
val commandLine = Mix.commandLine(emptyMap(), workingDirectory, sdk)
commandLine.addParameter("format")
// `-` turns on stdin/stdout for text to format
commandLine.addParameter("-")

commandLine
} catch (fileNotFoundException: FileNotFoundException) {
LOGGER.info(fileNotFoundException)

null
}

private fun format(commandLine: GeneralCommandLine, unformattedText: String): String? {
val processHandler = CapturingProcessHandler(commandLine)
processHandler.processInput.use { stdin ->
stdin.write(unformattedText.toByteArray())
stdin.flush()
Expand Down

0 comments on commit 3de395e

Please sign in to comment.