Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sections to breadcrumbs #3815

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ 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.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

/**
* @author Hannah Schellekens
Expand All @@ -16,7 +22,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 in CommandMagic.sectioningCommands.map { it.cmd }) element.requiredParameter(0) ?: element.name else element.name
else -> ""
} ?: ""

Expand All @@ -25,4 +31,19 @@ open class LatexBreadcrumbsInfo : BreadcrumbsProvider {
is LatexCommands -> true
else -> false
}

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 ?: return@filter false) }
.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
return parent ?: super.getParent(element)
}
}
Loading