From 4ff28108e7bab51f06f7cdd25d4324322666fbf6 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Wed, 18 Dec 2024 18:02:51 +0100 Subject: [PATCH 1/4] Part 1: add sections to breadcrumbs --- .../structure/latex/LatexBreadcrumbsInfo.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt b/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt index 20444aa02..319356256 100644 --- a/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt +++ b/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt @@ -2,10 +2,14 @@ package nl.hannahsten.texifyidea.structure.latex import com.intellij.psi.PsiElement import com.intellij.ui.breadcrumbs.BreadcrumbsProvider +import nl.hannahsten.texifyidea.editor.folding.LatexSectionFoldingBuilder import nl.hannahsten.texifyidea.grammar.LatexLanguage import nl.hannahsten.texifyidea.psi.LatexCommands import nl.hannahsten.texifyidea.psi.LatexEnvironment +import nl.hannahsten.texifyidea.util.files.document import nl.hannahsten.texifyidea.util.parser.name +import nl.hannahsten.texifyidea.util.parser.parents +import nl.hannahsten.texifyidea.util.parser.requiredParameter /** * @author Hannah Schellekens @@ -16,7 +20,7 @@ open class LatexBreadcrumbsInfo : BreadcrumbsProvider { override fun getElementInfo(element: PsiElement) = when (element) { is LatexEnvironment -> element.name()?.text - is LatexCommands -> element.commandToken.text + is LatexCommands -> if (element.name == "\\section") element.requiredParameter(0) else element.name else -> "" } ?: "" @@ -25,4 +29,17 @@ open class LatexBreadcrumbsInfo : BreadcrumbsProvider { is LatexCommands -> true else -> false } + + override fun getParent(element: PsiElement): PsiElement? { + val document = element.containingFile.document() ?: return super.getParent(element) + val parent = LatexSectionFoldingBuilder().buildFoldRegions(element.containingFile, document, quick = true) + // Only top-level elements in the section should have the section as parents, other elements should keep their direct parent (e.g. an environment) + .filter { it.range.contains(element.textRange) } + .filterNot { it.range.contains(element.parent.textRange) } + .firstOrNull { it.element.psi != element } + ?.element?.psi + // Avoid creating a loop + if (parent?.parents()?.contains(element) == true) { return super.getParent(element) } + return parent ?: super.getParent(element) + } } \ No newline at end of file From cd8825507c998c75987232d3ff309790650df765 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Thu, 19 Dec 2024 09:31:29 +0100 Subject: [PATCH 2/4] Fix nullpointer --- CHANGELOG.md | 1 + .../texifyidea/structure/latex/LatexBreadcrumbsInfo.kt | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89c8d6687..cbb94bb35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased] ### Added +* Add sections to breadcrumbs * Change order in structure view to match source file and sectioning level * Add command redefinitions to command definition filter in structure view * Add support for automatic language injection on the minted environment diff --git a/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt b/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt index 319356256..44eac9495 100644 --- a/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt +++ b/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt @@ -7,6 +7,8 @@ import nl.hannahsten.texifyidea.grammar.LatexLanguage import nl.hannahsten.texifyidea.psi.LatexCommands import nl.hannahsten.texifyidea.psi.LatexEnvironment import nl.hannahsten.texifyidea.util.files.document +import nl.hannahsten.texifyidea.util.magic.CommandMagic +import nl.hannahsten.texifyidea.util.magic.cmd import nl.hannahsten.texifyidea.util.parser.name import nl.hannahsten.texifyidea.util.parser.parents import nl.hannahsten.texifyidea.util.parser.requiredParameter @@ -20,7 +22,7 @@ open class LatexBreadcrumbsInfo : BreadcrumbsProvider { override fun getElementInfo(element: PsiElement) = when (element) { is LatexEnvironment -> element.name()?.text - is LatexCommands -> if (element.name == "\\section") element.requiredParameter(0) else element.name + is LatexCommands -> if (element.name in CommandMagic.sectioningCommands.map { it.cmd }) element.requiredParameter(0) ?: element.name else element.name else -> "" } ?: "" @@ -32,10 +34,11 @@ open class LatexBreadcrumbsInfo : BreadcrumbsProvider { override fun getParent(element: PsiElement): PsiElement? { val document = element.containingFile.document() ?: return super.getParent(element) + // Add sections val parent = LatexSectionFoldingBuilder().buildFoldRegions(element.containingFile, document, quick = true) // Only top-level elements in the section should have the section as parents, other elements should keep their direct parent (e.g. an environment) - .filter { it.range.contains(element.textRange) } - .filterNot { it.range.contains(element.parent.textRange) } + .filter { it.range.contains(element.textRange ?: return@filter false) } + .filterNot { it.range.contains(element.parent.textRange ?: return@filterNot true) } .firstOrNull { it.element.psi != element } ?.element?.psi // Avoid creating a loop From fd9a091afa5350c6cb2e2048c3db932947d4b119 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Thu, 19 Dec 2024 09:58:33 +0100 Subject: [PATCH 3/4] Support section levels --- .../texifyidea/structure/latex/LatexBreadcrumbsInfo.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt b/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt index 44eac9495..cd2f1cb6e 100644 --- a/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt +++ b/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt @@ -38,11 +38,12 @@ open class LatexBreadcrumbsInfo : BreadcrumbsProvider { val parent = LatexSectionFoldingBuilder().buildFoldRegions(element.containingFile, document, quick = true) // Only top-level elements in the section should have the section as parents, other elements should keep their direct parent (e.g. an environment) .filter { it.range.contains(element.textRange ?: return@filter false) } - .filterNot { it.range.contains(element.parent.textRange ?: return@filterNot true) } - .firstOrNull { it.element.psi != element } + .filter { !it.range.contains(element.parent.textRange ?: return@filter false) } + // Avoid creating a loop + .filter { it.element.psi != element } + .filter { it.element.psi?.parents()?.contains(element) != true } + .minByOrNull { it.range.endOffset - it.range.startOffset } ?.element?.psi - // Avoid creating a loop - if (parent?.parents()?.contains(element) == true) { return super.getParent(element) } return parent ?: super.getParent(element) } } \ No newline at end of file From 53bbbc78dcc518c8b0f5aedbb0afc1dfc68d1bef Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Thu, 19 Dec 2024 09:59:46 +0100 Subject: [PATCH 4/4] Formatting --- .../texifyidea/structure/latex/LatexBreadcrumbsInfo.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt b/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt index cd2f1cb6e..02c446678 100644 --- a/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt +++ b/src/nl/hannahsten/texifyidea/structure/latex/LatexBreadcrumbsInfo.kt @@ -22,7 +22,7 @@ open class LatexBreadcrumbsInfo : BreadcrumbsProvider { override fun getElementInfo(element: PsiElement) = when (element) { is LatexEnvironment -> element.name()?.text - is LatexCommands -> if (element.name in CommandMagic.sectioningCommands.map { it.cmd }) element.requiredParameter(0) ?: element.name else element.name + is LatexCommands -> if (element.name in CommandMagic.sectioningCommands.map { it.cmd }) element.requiredParameter(0) ?: element.name else element.name else -> "" } ?: ""