From 988ac81514f446bbaefd67fabad7db8521764847 Mon Sep 17 00:00:00 2001
From: Thomas Schouten <hollandpirates@gmail.com>
Date: Wed, 11 Dec 2024 17:40:47 +0100
Subject: [PATCH 1/2] Do not fold sections in a command definition

---
 CHANGELOG.md                                             | 1 +
 .../editor/folding/LatexSectionFoldingBuilder.kt         | 9 +++++----
 test/resources/editor/folding/sections.tex               | 1 +
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7975c0b53b..0f1f6c6d93 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
 ## [Unreleased]
 
 ### Added
+* Do not fold sections in a command definition
 
 ### Fixed
 * Fix LaTeX files not showing up when choosing main file in run configuration
diff --git a/src/nl/hannahsten/texifyidea/editor/folding/LatexSectionFoldingBuilder.kt b/src/nl/hannahsten/texifyidea/editor/folding/LatexSectionFoldingBuilder.kt
index 0faa43f9f9..d5fc0f27cb 100644
--- a/src/nl/hannahsten/texifyidea/editor/folding/LatexSectionFoldingBuilder.kt
+++ b/src/nl/hannahsten/texifyidea/editor/folding/LatexSectionFoldingBuilder.kt
@@ -9,10 +9,7 @@ import com.intellij.psi.PsiElement
 import nl.hannahsten.texifyidea.lang.magic.DefaultMagicKeys
 import nl.hannahsten.texifyidea.psi.*
 import nl.hannahsten.texifyidea.util.magic.CommandMagic
-import nl.hannahsten.texifyidea.util.parser.childrenOfType
-import nl.hannahsten.texifyidea.util.parser.nextSiblingIgnoreWhitespace
-import nl.hannahsten.texifyidea.util.parser.parentOfType
-import nl.hannahsten.texifyidea.util.parser.previousSiblingIgnoreWhitespace
+import nl.hannahsten.texifyidea.util.parser.*
 
 /**
  * Recursively folds section commands
@@ -31,6 +28,10 @@ open class LatexSectionFoldingBuilder : FoldingBuilderEx() {
     override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
         val descriptors = ArrayList<FoldingDescriptor>()
         val commands = root.childrenOfType<LatexCommands>().filter { it.name in sectionCommands }
+            // If it has no parameters, it is probably not an actual section but in a command definition
+            .filter { it.parameterList.isNotEmpty() }
+            // Similarly, if the section command is in a parameter it is probably in the preamble, and we should not fold
+            .filter { it.firstParentOfType<LatexParameter>() == null }
             .sortedBy { it.textOffset }
         val comments = root.childrenOfType<LatexMagicComment>().filter { it.key() == DefaultMagicKeys.FAKE }
         val sectionElements: List<PsiElement> = (commands + comments).sortedBy { it.textOffset }
diff --git a/test/resources/editor/folding/sections.tex b/test/resources/editor/folding/sections.tex
index 6a7085618e..b62862a3ca 100644
--- a/test/resources/editor/folding/sections.tex
+++ b/test/resources/editor/folding/sections.tex
@@ -1,3 +1,4 @@
+\titleformat*{\section}{\itshape}
 \begin{document}<fold text='...'>
 <fold text='\section{One}...'>\section{One}
 Text.

From c06ebb76f5ba7d37e4f154dc57568c6b28412eed Mon Sep 17 00:00:00 2001
From: Thomas Schouten <hollandpirates@gmail.com>
Date: Thu, 12 Dec 2024 20:10:20 +0100
Subject: [PATCH 2/2] Ignore Qodana warnings

---
 .../editor/postfix/LatexStringBasedPostfixTemplates.kt         | 2 ++
 .../editor/postfix/editable/LatexEditablePostfixTemplate.kt    | 1 +
 src/nl/hannahsten/texifyidea/gutter/LatexCompileGutter.kt      | 3 ++-
 3 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/nl/hannahsten/texifyidea/editor/postfix/LatexStringBasedPostfixTemplates.kt b/src/nl/hannahsten/texifyidea/editor/postfix/LatexStringBasedPostfixTemplates.kt
index f8361c6f5f..40f23e270c 100644
--- a/src/nl/hannahsten/texifyidea/editor/postfix/LatexStringBasedPostfixTemplates.kt
+++ b/src/nl/hannahsten/texifyidea/editor/postfix/LatexStringBasedPostfixTemplates.kt
@@ -63,6 +63,8 @@ internal object LatexWrapWithMathbbPostfixTemplate : LatexWrapWithCommandPostfix
 internal object LatexWrapWithBmPostfixTemplate : LatexWrapWithCommandPostfixTemplate("bm", mathOnly = true, pack = LatexPackage.BM)
 internal object LatexWrapWithMathcalPostfixTemplate : LatexWrapWithCommandPostfixTemplate("mathcal", name = "cal", mathOnly = true)
 
+// Not actually a postfix template, just a base class
+@Suppress("PostfixTemplateDescriptionNotFound")
 internal open class LatexWrapWithCommandPostfixTemplate(commandName: String, name: String = commandName, mathOnly: Boolean = false, textOnly: Boolean = false, pack: LatexPackage? = null) : ConstantStringBasedPostfixTemplate(
     name,
     "\\$commandName{expr}",
diff --git a/src/nl/hannahsten/texifyidea/editor/postfix/editable/LatexEditablePostfixTemplate.kt b/src/nl/hannahsten/texifyidea/editor/postfix/editable/LatexEditablePostfixTemplate.kt
index 0499efd4d3..885fdf4141 100644
--- a/src/nl/hannahsten/texifyidea/editor/postfix/editable/LatexEditablePostfixTemplate.kt
+++ b/src/nl/hannahsten/texifyidea/editor/postfix/editable/LatexEditablePostfixTemplate.kt
@@ -7,6 +7,7 @@ import com.intellij.psi.PsiElement
 import nl.hannahsten.texifyidea.editor.postfix.LatexPostFixTemplateProvider
 import nl.hannahsten.texifyidea.editor.postfix.LatexPostfixExpressionSelector
 
+@Suppress("PostfixTemplateDescriptionNotFound")
 class LatexEditablePostfixTemplate(templateId: String, templateName: String, template: TemplateImpl, conditions: Set<LatexPostfixTemplateExpressionCondition>, provider: LatexPostFixTemplateProvider) :
     EditablePostfixTemplateWithMultipleExpressions<LatexPostfixTemplateExpressionCondition>(templateId, templateName, template, "", conditions, true, provider) {
 
diff --git a/src/nl/hannahsten/texifyidea/gutter/LatexCompileGutter.kt b/src/nl/hannahsten/texifyidea/gutter/LatexCompileGutter.kt
index 316a04eec4..8ea8863ed2 100644
--- a/src/nl/hannahsten/texifyidea/gutter/LatexCompileGutter.kt
+++ b/src/nl/hannahsten/texifyidea/gutter/LatexCompileGutter.kt
@@ -3,6 +3,7 @@ package nl.hannahsten.texifyidea.gutter
 import com.intellij.execution.lineMarker.ExecutorAction
 import com.intellij.execution.lineMarker.RunLineMarkerContributor
 import com.intellij.openapi.actionSystem.ActionManager
+import com.intellij.openapi.actionSystem.IdeActions
 import com.intellij.psi.PsiElement
 import nl.hannahsten.texifyidea.TexifyIcons
 import nl.hannahsten.texifyidea.psi.LatexBeginCommand
@@ -29,7 +30,7 @@ class LatexCompileGutter : RunLineMarkerContributor() {
 
         // Lookup actions.
         val actionManager = ActionManager.getInstance()
-        val editConfigs = actionManager.getAction("editRunConfigurations")
+        val editConfigs = actionManager.getAction(IdeActions.ACTION_EDIT_RUN_CONFIGURATIONS)
         val actions = ExecutorAction.getActions(0)
 
         // Create icon.