Skip to content

Commit

Permalink
Merge pull request #2347 from Hannah-Sten/indent-document-environment
Browse files Browse the repository at this point in the history
Add option to disable indentation of document environment
  • Loading branch information
PHPirates authored May 1, 2022
2 parents 83b88ac + 40ca28b commit 1369964
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,41 @@ 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?) {
super.apply(settings, options)

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) {
super.reset(settings, options)

val latexSettings = settings.getCustomSettings(LatexCodeStyleSettings::class.java)
sectionIndents.isSelected = latexSettings.INDENT_SECTIONS
documentIndent.isSelected = latexSettings.INDENT_DOCUMENT_ENVIRONMENT
}
}
38 changes: 38 additions & 0 deletions test/nl/hannahsten/texifyidea/formatting/LatexFormattingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 = """
Expand Down

0 comments on commit 1369964

Please sign in to comment.