Skip to content

Commit

Permalink
Merge pull request #3804 from Hannah-Sten/code-folding
Browse files Browse the repository at this point in the history
Do not fold sections in a command definition
  • Loading branch information
PHPirates authored Dec 13, 2024
2 parents dbe93ef + c06ebb7 commit 5f0c25c
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

### Added
* Do not fold sections in a command definition
* Include optional parameters in spellcheck, if it contains text

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
3 changes: 2 additions & 1 deletion src/nl/hannahsten/texifyidea/gutter/LatexCompileGutter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions test/resources/editor/folding/sections.tex
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
\titleformat*{\section}{\itshape}
\begin{document}<fold text='...'>
<fold text='\section{One}...'>\section{One}
Text.
Expand Down

0 comments on commit 5f0c25c

Please sign in to comment.