Skip to content

Commit

Permalink
Fix #3336: disable alignment of & in \url commands in table
Browse files Browse the repository at this point in the history
  • Loading branch information
slideclimb committed Dec 30, 2023
1 parent e093c0c commit 7e28097
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import nl.hannahsten.texifyidea.psi.getEnvironmentName
import nl.hannahsten.texifyidea.util.getIndent
import nl.hannahsten.texifyidea.util.magic.EnvironmentMagic
import nl.hannahsten.texifyidea.util.parser.firstParentOfType
import nl.hannahsten.texifyidea.util.parser.inUrl
import kotlin.math.min

/** At this length, we put table cells on their own line. */
Expand All @@ -20,9 +21,10 @@ const val LINE_LENGTH = 80
* Align spaces to the right of &
*/
fun rightTableSpaceAlign(latexCommonSettings: CommonCodeStyleSettings, parent: ASTBlock, left: ASTBlock): Spacing? {
// Only add spaces after &, unless escaped
// Only add spaces after &, unless escaped or inside url.
if (left.node?.text?.endsWith("&") == false) return null
if (left.node?.text?.endsWith("\\&") == true) return null
if (left.node?.psi?.inUrl() == true) return null

if (parent.node?.psi?.firstParentOfType(LatexEnvironmentContent::class)
?.firstParentOfType(LatexEnvironment::class)?.getEnvironmentName() !in EnvironmentMagic.getAllTableEnvironments(
Expand All @@ -43,6 +45,8 @@ fun rightTableSpaceAlign(latexCommonSettings: CommonCodeStyleSettings, parent: A
* Align spaces to the left of & or \\
*/
fun leftTableSpaceAlign(latexCommonSettings: CommonCodeStyleSettings, parent: ASTBlock, right: ASTBlock): Spacing? {
if (right.node?.psi?.inUrl() == true) return null

// Check if parent is in environment content of a table environment
val contentElement = parent.node?.psi?.firstParentOfType(LatexEnvironmentContent::class)
val project = parent.node?.psi?.project ?: ProjectManager.getInstance().defaultProject
Expand Down
10 changes: 10 additions & 0 deletions src/nl/hannahsten/texifyidea/util/parser/Psi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import nl.hannahsten.texifyidea.lang.DefaultEnvironment
import nl.hannahsten.texifyidea.lang.Environment
import nl.hannahsten.texifyidea.lang.magic.TextBasedMagicCommentParser
import nl.hannahsten.texifyidea.psi.*
import nl.hannahsten.texifyidea.util.magic.CommandMagic
import nl.hannahsten.texifyidea.util.magic.EnvironmentMagic
import kotlin.reflect.KClass

Expand Down Expand Up @@ -179,6 +180,15 @@ fun PsiElement.inComment() = inDirectEnvironmentContext(Environment.Context.COMM
else -> this is LeafPsiElement && elementType == LatexTypes.COMMAND_TOKEN
}

/**
* Check if the element is in a url command.
*/
fun PsiElement?.inUrl(): Boolean {
return this?.hasParentMatching(100) {
(it as? LatexCommands)?.name in CommandMagic.urls
} ?: false
}

/**
* Checks if the element is inside a verbatim context.
*/
Expand Down
22 changes: 22 additions & 0 deletions test/nl/hannahsten/texifyidea/formatting/TableAlignTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,28 @@ class TableAlignTest : BasePlatformTestCase() {
""".trimIndent()
}

fun testAmpersandsInUrl() {
"""
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\begin{tabular}{ll}
a & \url{https://youtu.be/dQw4w9WgXcQ?si=SHq6ADlwRNZ1hwOB&t=18} & c \\
be & \url{https://youtu.be/dQw4w9WgXcQ?si=SHq6ADlwRNZ1hwOB&t=18} & d
\end{tabular}
\end{document}
""".trimIndent() `should be reformatted to` """
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\begin{tabular}{ll}
a & \url{https://youtu.be/dQw4w9WgXcQ?si=SHq6ADlwRNZ1hwOB&t=18} & c \\
be & \url{https://youtu.be/dQw4w9WgXcQ?si=SHq6ADlwRNZ1hwOB&t=18} & d
\end{tabular}
\end{document}
""".trimIndent()
}

fun testVeryWideTable() {
val start = """
\documentclass[11pt]{article}
Expand Down

0 comments on commit 7e28097

Please sign in to comment.