Repository: hannah-sten/texify-idea
Issue: Unnecessary whitespace when using \\section in \\newcommand or \\newenvironment

#### Type of JetBrains IDE (IntelliJ, PyCharm, etc.) and version
IntelliJ Ultimate 2021.2.1

#### Operating System 
Debian 12 (testing)

#### TeXiFy IDEA version
0.7.9

#### What I did (steps to reproduce)
Use `\\section` (or `\\subsection` or `\\subsubsection`) at the start of a `\\newcommand` or `\\newenvironment`, then invoke the formatter.

#### Minimal example to reproduce the problem
##### `\\newcommand`
```latex
\\documentclass{article}
\\newcommand{\\sectionlorem}[2]{\\section{#1}\\label{#2}}

\\begin{document}
    \\sectionlorem{Title}{sec:label}
\\end{document}
```

##### `\\newenvironment`
```latex
\\documentclass{article}
\\newenvironment{sectionitemize}[2]{\\section{#1}\\label{#2}\\begin{itemize}}{\\end{itemize}}

\\begin{document}
    \\begin{sectionitemize}{Title}{sec:label}
        \\item Lorem
        \\item Ipsum
    \\end{sectionitemize}
\\end{document}
```

#### Expected behavior
After auto-formatting, the document does not change _or_ the `\\section` is put on a separate line; but either way no empty lines are added.

#### Actual behavior
After auto-formatting, the command definition is changed to
```latex
\\documentclass{article}
\\newcommand{\\sectionlorem}[2]{


    \\section{#1}\\label{#2}}

\\begin{document}
    \\sectionlorem{Title}{sec:label}
\\end{document}
```
and similarly so for `\\newenvironment`.


Changed files:
src/nl/hannahsten/texifyidea/formatting/LatexSpacingRules.kt, test/nl/hannahsten/texifyidea/formatting/LatexFormattingTest.kt

diff --git a/src/nl/hannahsten/texifyidea/formatting/LatexSpacingRules.kt b/src/nl/hannahsten/texifyidea/formatting/LatexSpacingRules.kt
index 56f71f0b0..d5e1cf099 100644
--- a/src/nl/hannahsten/texifyidea/formatting/LatexSpacingRules.kt
+++ b/src/nl/hannahsten/texifyidea/formatting/LatexSpacingRules.kt
@@ -5,10 +5,13 @@ import com.intellij.psi.codeStyle.CodeStyleSettings
 import nl.hannahsten.texifyidea.LatexLanguage
 import nl.hannahsten.texifyidea.formatting.spacingrules.leftTableSpaceAlign
 import nl.hannahsten.texifyidea.formatting.spacingrules.rightTableSpaceAlign
+import nl.hannahsten.texifyidea.psi.LatexCommands
 import nl.hannahsten.texifyidea.psi.LatexTypes.*
 import nl.hannahsten.texifyidea.settings.codestyle.LatexCodeStyleSettings
 import nl.hannahsten.texifyidea.util.inDirectEnvironment
+import nl.hannahsten.texifyidea.util.magic.CommandMagic
 import nl.hannahsten.texifyidea.util.magic.EnvironmentMagic
+import nl.hannahsten.texifyidea.util.parentOfType
 
 fun createSpacing(minSpaces: Int, maxSpaces: Int, minLineFeeds: Int, keepLineBreaks: Boolean, keepBlankLines: Int): Spacing =
     Spacing.createSpacing(minSpaces, maxSpaces, minLineFeeds, keepLineBreaks, keepBlankLines)
@@ -86,7 +89,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings): TexSpacingBuilder {
             // BUG OR FEATURE? Does not work for a command that immediately follows \\begin{document}.
             customRule { _, _, right ->
                 LatexCodeStyleSettings.blankLinesOptions.forEach {
-                    if (right.node?.text?.matches(Regex("\\\\" + "${it.value}\\\\{.*\\\\}")) == true) {
+                    if (right.node?.text?.matches(Regex("\\\\" + "${it.value}\\\\{.*\\\\}")) == true &&
+                        !CommandMagic.definitions.contains(right.node?.psi?.parentOfType(LatexCommands::class)?.name)) {
                         return@customRule createSpacing(
                             minSpaces = 0,
                             maxSpaces = Int.MAX_VALUE,
diff --git a/test/nl/hannahsten/texifyidea/formatting/LatexFormattingTest.kt b/test/nl/hannahsten/texifyidea/formatting/LatexFormattingTest.kt
index f786b21be..d84332505 100644
--- a/test/nl/hannahsten/texifyidea/formatting/LatexFormattingTest.kt
+++ b/test/nl/hannahsten/texifyidea/formatting/LatexFormattingTest.kt
@@ -188,6 +188,24 @@ fun Int?.ifPositiveAddTwo(): Int =
         """.trimIndent()
     }
 
+    fun `test section used in command definition`() {
+        """
+            \\documentclass{article}
+            \\newcommand{\\sectionlorem}[2]{\\section{#1}\\label{#2}}
+
+            \\begin{document}
+                \\sectionlorem{Title}{sec:label}
+            \\end{document}
+        """.trimIndent() `should be reformatted to` """
+            \\documentclass{article}
+            \\newcommand{\\sectionlorem}[2]{\\section{#1}\\label{#2}}
+
+            \\begin{document}
+                \\sectionlorem{Title}{sec:label}
+            \\end{document}
+        """.trimIndent()
+    }
+
     fun testComments() {
         // Wanted to test line breaking, but not sure how to enable it in test
         val text = """