diff --git a/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt b/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt index 6dd637023..6298f6041 100644 --- a/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt +++ b/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt @@ -8,11 +8,10 @@ import com.intellij.psi.TokenType import com.intellij.psi.formatter.common.AbstractBlock import com.intellij.psi.util.prevLeaf import nl.hannahsten.texifyidea.lang.commands.LatexCommand -import nl.hannahsten.texifyidea.psi.LatexCommands -import nl.hannahsten.texifyidea.psi.LatexNoMathContent -import nl.hannahsten.texifyidea.psi.LatexTypes +import nl.hannahsten.texifyidea.psi.* import nl.hannahsten.texifyidea.settings.codestyle.LatexCodeStyleSettings import nl.hannahsten.texifyidea.util.firstChildOfType +import nl.hannahsten.texifyidea.util.firstParentOfType import nl.hannahsten.texifyidea.util.magic.CommandMagic import nl.hannahsten.texifyidea.util.magic.cmd import java.lang.Integer.max @@ -141,8 +140,13 @@ class LatexBlock( } override fun getIndent(): Indent? { - if (myNode.elementType === LatexTypes.ENVIRONMENT_CONTENT || - myNode.elementType === LatexTypes.PSEUDOCODE_BLOCK_CONTENT || + val shouldIndentDocumentEnvironment = CodeStyle.getCustomSettings(node.psi.containingFile, LatexCodeStyleSettings::class.java).INDENT_DOCUMENT_ENVIRONMENT + val shouldIndentEnvironment = myNode.elementType === LatexTypes.ENVIRONMENT_CONTENT && ((myNode.psi as LatexEnvironmentContent) + .firstParentOfType(LatexEnvironment::class) + ?.firstChildOfType(LatexBeginCommand::class) + ?.firstChildOfType(LatexParameterText::class)?.text != "document" || shouldIndentDocumentEnvironment) + + if (shouldIndentEnvironment || myNode.elementType === LatexTypes.PSEUDOCODE_BLOCK_CONTENT || // Fix for leading comments inside an environment, because somehow they are not placed inside environments. // Note that this does not help to insert the indentation, but at least the indent is not removed // when formatting. diff --git a/src/nl/hannahsten/texifyidea/settings/codestyle/LatexCodeStyleSettings.kt b/src/nl/hannahsten/texifyidea/settings/codestyle/LatexCodeStyleSettings.kt index 75b59c5ab..a3b95ba62 100644 --- a/src/nl/hannahsten/texifyidea/settings/codestyle/LatexCodeStyleSettings.kt +++ b/src/nl/hannahsten/texifyidea/settings/codestyle/LatexCodeStyleSettings.kt @@ -31,6 +31,11 @@ class LatexCodeStyleSettings(container: CodeStyleSettings) : CustomCodeStyleSett */ @JvmField var INDENT_SECTIONS = false + /** + * Indent the document environment as a normal environment. + */ + @JvmField var INDENT_DOCUMENT_ENVIRONMENT = true + companion object { val blankLinesOptions = mapOf( diff --git a/src/nl/hannahsten/texifyidea/settings/codestyle/LatexIndentOptionsEditor.kt b/src/nl/hannahsten/texifyidea/settings/codestyle/LatexIndentOptionsEditor.kt index 833bd4e53..8276359e9 100644 --- a/src/nl/hannahsten/texifyidea/settings/codestyle/LatexIndentOptionsEditor.kt +++ b/src/nl/hannahsten/texifyidea/settings/codestyle/LatexIndentOptionsEditor.kt @@ -14,21 +14,26 @@ import javax.swing.JCheckBox class LatexIndentOptionsEditor(provider: LatexLanguageCodeStyleSettingsProvider) : SmartIndentOptionsEditor(provider) { private val sectionIndents = JCheckBox("Nested indent of sections") + private val documentIndent = JCheckBox("Indent document environment") override fun addComponents() { super.addComponents() add(sectionIndents) + add(documentIndent) } override fun setEnabled(enabled: Boolean) { super.setEnabled(enabled) sectionIndents.isEnabled = enabled + documentIndent.isEnabled = enabled } override fun isModified(settings: CodeStyleSettings?, options: CommonCodeStyleSettings.IndentOptions?): Boolean { val isModified = super.isModified(settings, options) val latexSettings = settings?.getCustomSettings(LatexCodeStyleSettings::class.java) ?: return false - return isModified || IndentOptionsEditor.isFieldModified(sectionIndents, latexSettings.INDENT_SECTIONS) + return isModified || + IndentOptionsEditor.isFieldModified(sectionIndents, latexSettings.INDENT_SECTIONS) || + IndentOptionsEditor.isFieldModified(documentIndent, latexSettings.INDENT_DOCUMENT_ENVIRONMENT) } override fun apply(settings: CodeStyleSettings?, options: CommonCodeStyleSettings.IndentOptions?) { @@ -36,6 +41,7 @@ class LatexIndentOptionsEditor(provider: LatexLanguageCodeStyleSettingsProvider) val latexSettings = settings?.getCustomSettings(LatexCodeStyleSettings::class.java) latexSettings?.INDENT_SECTIONS = sectionIndents.isSelected + latexSettings?.INDENT_DOCUMENT_ENVIRONMENT = documentIndent.isSelected } override fun reset(settings: CodeStyleSettings, options: CommonCodeStyleSettings.IndentOptions) { @@ -43,5 +49,6 @@ class LatexIndentOptionsEditor(provider: LatexLanguageCodeStyleSettingsProvider) val latexSettings = settings.getCustomSettings(LatexCodeStyleSettings::class.java) sectionIndents.isSelected = latexSettings.INDENT_SECTIONS + documentIndent.isSelected = latexSettings.INDENT_DOCUMENT_ENVIRONMENT } } \ No newline at end of file diff --git a/test/nl/hannahsten/texifyidea/formatting/LatexFormattingTest.kt b/test/nl/hannahsten/texifyidea/formatting/LatexFormattingTest.kt index 80cfd8e7f..1c387c982 100644 --- a/test/nl/hannahsten/texifyidea/formatting/LatexFormattingTest.kt +++ b/test/nl/hannahsten/texifyidea/formatting/LatexFormattingTest.kt @@ -5,6 +5,7 @@ import com.intellij.psi.codeStyle.CodeStyleManager import com.intellij.psi.codeStyle.CommonCodeStyleSettings import com.intellij.testFramework.fixtures.BasePlatformTestCase import nl.hannahsten.texifyidea.file.LatexFileType +import nl.hannahsten.texifyidea.settings.codestyle.LatexCodeStyleSettings import nl.hannahsten.texifyidea.testutils.writeCommand class LatexFormattingTest : BasePlatformTestCase() { @@ -236,6 +237,43 @@ fun Int?.ifPositiveAddTwo(): Int = """.trimIndent() } + fun `test indentation of environments`() { + val text = """ + \begin{document} + Don't indent this if turned off. + \begin{some-env} + Indent this. + \end{some-env} + \end{document} + """.trimIndent() + val file = myFixture.configureByText(LatexFileType, text) + CodeStyle.getCustomSettings(file, LatexCodeStyleSettings::class.java).INDENT_DOCUMENT_ENVIRONMENT = false + writeCommand(project) { CodeStyleManager.getInstance(project).reformat(myFixture.file) } + + val expected = """ + \begin{document} + Don't indent this if turned off. + \begin{some-env} + Indent this. + \end{some-env} + \end{document} + """.trimIndent() + myFixture.checkResult(expected) + + CodeStyle.getCustomSettings(file, LatexCodeStyleSettings::class.java).INDENT_DOCUMENT_ENVIRONMENT = true + writeCommand(project) { CodeStyleManager.getInstance(project).reformat(myFixture.file) } + + val expected2 = """ + \begin{document} + Don't indent this if turned off. + \begin{some-env} + Indent this. + \end{some-env} + \end{document} + """.trimIndent() + myFixture.checkResult(expected2) + } + fun testComments() { // Wanted to test line breaking, but not sure how to enable it in test val text = """