Skip to content

Commit

Permalink
Allow mix format external formatter to be disabled
Browse files Browse the repository at this point in the history
While I disagree with using a non-community-standard style, some users prefer it and having the settings allows the internal formatter settings to remain working.

1. Preferences
2. Editor > Code Style > Elixir
3. Click the `mix format` tab
4. Expand the General group
4. Uncheck "Format files with `mix format`".
  • Loading branch information
KronicDeth committed May 30, 2022
1 parent 02f937e commit 45fd95e
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@
* [#2578](https://github.com/KronicDeth/intellij-elixir/pull/2578) - [@yordis](https://github.com/yordis)
* Add `mix format` external formatter.
Requires project or module SDK be set in order to run. If the SDK is not available, only the internal formatter will be used. The internal formatter is still used for file subsection formatting and new line indenting as `mix format` works at the file-level.
* Allow `mix format` external formatter to be disabled.

1. Preferences
2. Editor > Code Style > Elixir
3. Click the `mix format` tab
4. Expand the General group
5. Uncheck "Format files with `mix format`".

### Bug Fixes

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4258,6 +4258,16 @@ new lines use the internal formatter until the next full file format uses `mix f

**NOTE: The module or project SDK *MUST* be set in order to run `mix format`. If the SDK is not set, only the internal formatter will be run.**

###### Disabling

`mix format` can be disabled if you only want to use the Internal Formatter.

1. Preferences
2. Editor > Code Style > Elixir
3. Click the `mix format` tab
4. Expand the General group
4. Uncheck "Format files with `mix format`".

#### Format on Save

To tun on format on save:
Expand Down
15 changes: 14 additions & 1 deletion resources/META-INF/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,20 @@ <h1>v13.1.0</h1>
</ul>
</li>
<li>Add <code class="notranslate">mix format</code> external formatter.<br>
Requires project or module SDK be set in order to run. If the SDK is not available, only the internal formatter will be used. The internal formatter is still used for file subsection formatting and new line indenting as <code class="notranslate">mix format</code> works at the file-level.</li>
Requires project or module SDK be set in order to run. If the SDK is not available, only the internal formatter will be used. The internal formatter is still used for file subsection formatting and new line indenting as <code class="notranslate">mix format</code> works at the file-level.
<ul dir="auto">
<li>
<p dir="auto">Allow <code class="notranslate">mix format</code> external formatter to be disabled.</p>
<ol dir="auto">
<li>Preferences</li>
<li>Editor &gt; Code Style &gt; Elixir</li>
<li>Click the <code class="notranslate">mix format</code> tab</li>
<li>Expand the General group</li>
<li>Uncheck "Format files with <code class="notranslate">mix format</code>".</li>
</ol>
</li>
</ul>
</li>
</ul>
</li>
<li>
Expand Down
2 changes: 1 addition & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
<configurationType implementation="org.elixir_lang.mix.configuration.Type"/>

<library.type implementation="org.elixir_lang.mix.library.Type"/>
<externalFormatProcessor implementation="org.elixir_lang.formatter.MixFormatExternalFormatProcessor"/>

<!-- Testing -->
<configurationType implementation="org.elixir_lang.espec.configuration.Type"/>
Expand Down Expand Up @@ -204,6 +203,7 @@
implementationClass="com.intellij.psi.templateLanguages.SimpleTemplateLanguageFormattingModelBuilder"/>
<langCodeStyleSettingsProvider
implementation="org.elixir_lang.formatter.settings.LanguageCodeStyleSettingsProvider"/>
<externalFormatProcessor implementation="org.elixir_lang.formatter.MixFormatExternalFormatProcessor"/>

<!-- test -->
<runLineMarkerContributor language="Elixir"
Expand Down
1 change: 1 addition & 0 deletions src/org/elixir_lang/code_style/CodeStyleSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class CodeStyleSettings extends CustomCodeStyleSettings {
public boolean ALIGN_TWO_OPERANDS = true;
public boolean ALIGN_TYPE_DEFINITION_TO_RIGHT_OF_OPERATOR = true;
public int ALIGN_UNMATCHED_CALL_DO_BLOCKS = UnmatchedCallDoBlockAlignment.LINE.value;
public boolean MIX_FORMAT = true;
public boolean SPACE_AFTER_CAPTURE_OPERATOR = false;
public boolean SPACE_AROUND_AND_OPERATORS = true;
public boolean SPACE_AROUND_ARROW_OPERATORS = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.elixir_lang.formatter

import com.intellij.application.options.CodeStyle
import com.intellij.execution.process.CapturingProcessHandler
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
Expand All @@ -11,16 +12,16 @@ import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile
import com.intellij.psi.codeStyle.ExternalFormatProcessor
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.util.concurrent.TimeUnit

@Suppress("UnstableApiUsage")
@SuppressWarnings("UnstableApiUsage")
class MixFormatExternalFormatProcessor : ExternalFormatProcessor {
override fun activeForFile(source: PsiFile): Boolean {
return source is ElixirFile
}
override fun activeForFile(source: PsiFile): Boolean =
source is ElixirFile && CodeStyle.getCustomSettings(source, CodeStyleSettings::class.java).MIX_FORMAT

override fun getId(): String = javaClass.name

Expand Down
17 changes: 17 additions & 0 deletions src/org/elixir_lang/formatter/settings/ElixirCodeStyleMainPanel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.elixir_lang.formatter.settings

import com.intellij.application.options.TabbedLanguageCodeStylePanel
import com.intellij.psi.codeStyle.CodeStyleSettings
import org.elixir_lang.ElixirLanguage

class ElixirCodeStyleMainPanel(currentSettings: CodeStyleSettings?, baseSettings: CodeStyleSettings) :
TabbedLanguageCodeStylePanel(ElixirLanguage, currentSettings, baseSettings) {

override fun initTabs(settings: CodeStyleSettings?) {
addTab(MixFormatPanel(settings))
addIndentOptionsTab(settings)
addSpacesTab(settings)
addWrappingAndBracesTab(settings)
// DO NOT have Blank Lines tab
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
package org.elixir_lang.formatter.settings

import com.intellij.application.options.CodeStyleAbstractConfigurable
import com.intellij.application.options.CodeStyleAbstractPanel
import com.intellij.application.options.IndentOptionsEditor
import com.intellij.application.options.SmartIndentOptionsEditor
import com.intellij.lang.Language
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizableOptions
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
import com.intellij.psi.codeStyle.*
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider
import org.elixir_lang.ElixirLanguage
import org.elixir_lang.code_style.CodeStyleSettings

class LanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvider() {
override fun createCustomSettings(settings: com.intellij.psi.codeStyle.CodeStyleSettings): CustomCodeStyleSettings =
CodeStyleSettings(settings)

override fun createConfigurable(
baseSettings: com.intellij.psi.codeStyle.CodeStyleSettings,
modelSettings: com.intellij.psi.codeStyle.CodeStyleSettings
): CodeStyleConfigurable = object : CodeStyleAbstractConfigurable(baseSettings, modelSettings, "Elixir") {
override fun createPanel(settings: com.intellij.psi.codeStyle.CodeStyleSettings): CodeStyleAbstractPanel =
ElixirCodeStyleMainPanel(currentSettings, settings)
}

override fun customizeSettings(consumer: CodeStyleSettingsCustomizable, settingsType: SettingsType) {
if (settingsType == SettingsType.SPACING_SETTINGS) {
customizeSpaceSettings(consumer)
} else if (settingsType == SettingsType.WRAPPING_AND_BRACES_SETTINGS) {
customizeWrappingAndBracesSettings(consumer)
when (settingsType) {
SettingsType.SPACING_SETTINGS -> customizeSpaceSettings(consumer)
SettingsType.WRAPPING_AND_BRACES_SETTINGS -> customizeWrappingAndBracesSettings(consumer)
SettingsType.LANGUAGE_SPECIFIC -> customizeLanguageSpecific(consumer)
SettingsType.BLANK_LINES_SETTINGS,
SettingsType.INDENT_SETTINGS,
SettingsType.COMMENTER_SETTINGS -> Unit
}
}

Expand Down Expand Up @@ -178,14 +192,24 @@ class LanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvider() {
)
}

private fun customizeLanguageSpecific(consumer: CodeStyleSettingsCustomizable) {
consumer.showCustomOption(
CodeStyleSettings::class.java,
"MIX_FORMAT",
"Format files with `mix format`",
"General"
)
}

override fun getCodeSample(settingsType: SettingsType): String =
when (settingsType) {
SettingsType.BLANK_LINES_SETTINGS -> "Blank Line Settings Code Sample"
SettingsType.SPACING_SETTINGS -> SPACING_CODE_SAMPLE
SettingsType.WRAPPING_AND_BRACES_SETTINGS -> WRAPPING_AND_BRACES_SETTINGS_CODE_SAMPLE
SettingsType.INDENT_SETTINGS -> INDENT_CODE_SAMPLE
SettingsType.LANGUAGE_SPECIFIC -> "Language Specific Code Sample"
else -> TODO()
SettingsType.LANGUAGE_SPECIFIC ->
INDENT_CODE_SAMPLE + SPACING_CODE_SAMPLE + WRAPPING_AND_BRACES_SETTINGS_CODE_SAMPLE
SettingsType.BLANK_LINES_SETTINGS,
SettingsType.COMMENTER_SETTINGS -> TODO()
}

override fun customizeDefaults(
Expand Down
38 changes: 38 additions & 0 deletions src/org/elixir_lang/formatter/settings/MixFormatPanel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.elixir_lang.formatter.settings

import com.intellij.application.options.codeStyle.OptionTreeWithPreviewPanel
import com.intellij.lang.Language
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.util.NlsContexts.TabTitle
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider.SettingsType
import org.elixir_lang.ElixirFileType
import org.elixir_lang.ElixirLanguage

// Based on [JavaDocFormattingPanel](https://github.com/JetBrains/intellij-community/blob/f3f09fc61977c612ac83ccba2233d0708745e12a/java/java-impl/src/com/intellij/application/options/JavaDocFormattingPanel.java)
class MixFormatPanel(settings: CodeStyleSettings?) : OptionTreeWithPreviewPanel(settings) {
init {
init()
}

override fun getSettingsType(): SettingsType = SettingsType.LANGUAGE_SPECIFIC

override fun initTables() {
initCustomOptions("General")
}

override fun getRightMargin(): Int = 47

override fun getPreviewText(): String = LanguageCodeStyleSettingsProvider.forLanguage(ElixirLanguage)!!
.getCodeSample(SettingsType.LANGUAGE_SPECIFIC)!!

override fun getFileType(): FileType = ElixirFileType.INSTANCE

override fun customizeSettings() {
LanguageCodeStyleSettingsProvider.forLanguage(ElixirLanguage)!!.customizeSettings(this, settingsType)
}

override fun getTabTitle(): @TabTitle String = "mix format"
override fun getDefaultLanguage(): Language = ElixirLanguage
}
Loading

0 comments on commit 45fd95e

Please sign in to comment.