-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3825 from Hannah-Sten/custom-env-labels
Support label references to user defined listings environment
- Loading branch information
Showing
11 changed files
with
115 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/nl/hannahsten/texifyidea/lang/LabelingEnvironmentInformation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package nl.hannahsten.texifyidea.lang | ||
|
||
import arrow.core.NonEmptyList | ||
|
||
/** | ||
* Information about a user-defined environment which has a \label command in the definition. | ||
*/ | ||
data class LabelingEnvironmentInformation( | ||
/** Parameter positions which define a label, starting from 0 (note: LaTeX starts from 1). */ | ||
var positions: NonEmptyList<Int>, | ||
/** Default label prefix, for example in \newcommand{\mylabel}[1]{\label{sec:#1}} it would be sec: */ | ||
var prefix: String = "" | ||
) |
28 changes: 27 additions & 1 deletion
28
src/nl/hannahsten/texifyidea/lang/alias/EnvironmentManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,49 @@ | ||
package nl.hannahsten.texifyidea.lang.alias | ||
|
||
import arrow.core.nonEmptyListOf | ||
import nl.hannahsten.texifyidea.lang.DefaultEnvironment | ||
import nl.hannahsten.texifyidea.lang.LabelingEnvironmentInformation | ||
import nl.hannahsten.texifyidea.lang.commands.LatexNewDefinitionCommand | ||
import nl.hannahsten.texifyidea.psi.LatexCommands | ||
import nl.hannahsten.texifyidea.util.containsAny | ||
import nl.hannahsten.texifyidea.util.magic.EnvironmentMagic | ||
import nl.hannahsten.texifyidea.util.magic.cmd | ||
import nl.hannahsten.texifyidea.util.parser.requiredParameter | ||
|
||
/** | ||
* Similar to the [CommandManager], this manages aliases of environments. | ||
*/ | ||
object EnvironmentManager : AliasManager() { | ||
|
||
/** | ||
* Maintain information about label parameter locations of environments for which that is applicable. | ||
* Maps environment name to parameter index of the \begin command, starting from 0 but including the first parameter which is the environment name | ||
*/ | ||
val labelAliasesInfo = mutableMapOf<String, LabelingEnvironmentInformation>() | ||
|
||
override fun findAllAliases(aliasSet: Set<String>, indexedDefinitions: Collection<LatexCommands>) { | ||
val firstAlias = aliasSet.first() | ||
|
||
// Assume the environment that is defined is the first parameter, and that the first part of the definition is in the second | ||
// e.g. \newenvironment{mytabl}{\begin{tabular}{cc}}{\end{tabular}} | ||
indexedDefinitions.filter { definition -> | ||
val definitions = indexedDefinitions.filter { definition -> | ||
definition.requiredParameter(1)?.containsAny(aliasSet.map { "\\begin{$it}" }.toSet()) == true | ||
// This command always defines an alias for the listings environment | ||
|| (definition.name == LatexNewDefinitionCommand.LSTNEWENVIRONMENT.cmd && aliasSet.contains(DefaultEnvironment.LISTINGS.environmentName)) | ||
} | ||
definitions | ||
.mapNotNull { it.requiredParameter(0) } | ||
.forEach { registerAlias(firstAlias, it) } | ||
|
||
// Update label parameter position information | ||
if (aliasSet.intersect(EnvironmentMagic.labelAsParameter).isNotEmpty()) { | ||
definitions.forEach { | ||
val definedEnvironment = it.requiredParameter(0) ?: return@forEach | ||
// The label may be in an optional parameter of an environment, but it may also be in other places like a \lstset, so for now we do a text-based search | ||
val text = it.requiredParameter(1) ?: return@forEach | ||
val index = "label\\s*=\\s*\\{?\\s*#(\\d)".toRegex().find(text)?.groupValues?.getOrNull(1)?.toInt() ?: return@forEach | ||
labelAliasesInfo[definedEnvironment] = LabelingEnvironmentInformation(nonEmptyListOf(index)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters