Skip to content

Commit

Permalink
Merge pull request #3374 from Hannah-Sten/3336-table-formatting-url
Browse files Browse the repository at this point in the history
Fix #3336: disable alignment of & in \url commands in table
  • Loading branch information
PHPirates authored Jan 14, 2024
2 parents 930896c + 7cb227a commit c44ada5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.psi.codeStyle.CommonCodeStyleSettings
import nl.hannahsten.texifyidea.formatting.createSpacing
import nl.hannahsten.texifyidea.psi.LatexEnvironment
import nl.hannahsten.texifyidea.psi.LatexEnvironmentContent
import nl.hannahsten.texifyidea.psi.LatexTypes
import nl.hannahsten.texifyidea.psi.getEnvironmentName
import nl.hannahsten.texifyidea.util.getIndent
import nl.hannahsten.texifyidea.util.magic.EnvironmentMagic
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?.elementType == LatexTypes.RAW_TEXT_TOKEN || parent.node?.elementType == LatexTypes.RAW_TEXT) 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?.elementType == LatexTypes.RAW_TEXT_TOKEN || parent.node?.elementType == LatexTypes.RAW_TEXT) 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
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 c44ada5

Please sign in to comment.