From fb5f0efcdb8a2c246514581c89595a93f2a8fc3d Mon Sep 17 00:00:00 2001 From: Abby Berkers Date: Sun, 24 Mar 2024 13:30:45 +0100 Subject: [PATCH 1/3] Fix line wrapping around non-space elements --- .../texifyidea/formatting/LatexBlock.kt | 8 ++++++-- .../formatting/LatexWrappingStrategy.kt | 15 ++++++++++++++- .../formatting/LatexLineWrapStrategyTest.kt | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt b/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt index 40872061ee..a5672c65b5 100644 --- a/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt +++ b/src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt @@ -57,7 +57,7 @@ class LatexBlock( if (child == null && (sectionIndent > 0 || fakeSectionIndent > 0)) { val block = LatexBlock( myNode, - wrappingStrategy.getWrap(), + wrappingStrategy.getNormalWrap(myNode), null, spacingBuilder, wrappingStrategy, @@ -67,6 +67,7 @@ class LatexBlock( blocks.add(block) } + var isPreviousWhiteSpace = child != null && child.elementType !== TokenType.WHITE_SPACE && child !is PsiWhiteSpace // Create child blocks while (child != null) { val isSectionCommand = @@ -86,7 +87,8 @@ class LatexBlock( if (child.elementType !== TokenType.WHITE_SPACE && child !is PsiWhiteSpace) { val block = LatexBlock( child, - wrappingStrategy.getWrap(), + // Only allow wrapping if the previous element is a white space. + if (isPreviousWhiteSpace) wrappingStrategy.getNormalWrap(myNode) else wrappingStrategy.getNoneWrap(), null, spacingBuilder, wrappingStrategy, @@ -94,7 +96,9 @@ class LatexBlock( newFakeSectionIndent ) blocks.add(block) + isPreviousWhiteSpace = false } + else isPreviousWhiteSpace = true child = child.treeNext } return blocks diff --git a/src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt b/src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt index 8d51216ff9..d419a10b7f 100644 --- a/src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt +++ b/src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt @@ -1,7 +1,12 @@ package nl.hannahsten.texifyidea.formatting +import com.intellij.application.options.CodeStyle +import com.intellij.execution.configurations.runConfigurationType import com.intellij.formatting.Wrap import com.intellij.formatting.WrapType +import com.intellij.lang.ASTNode +import com.intellij.psi.codeStyle.CodeStyleSettingsManager +import nl.hannahsten.texifyidea.settings.codestyle.LatexCodeStyleSettings /** * @@ -9,7 +14,15 @@ import com.intellij.formatting.WrapType */ class LatexWrappingStrategy { - fun getWrap(): Wrap? { + fun getNormalWrap(node: ASTNode): Wrap? { + val settings = CodeStyle.getLanguageSettings(node.psi.containingFile) + return if (settings.WRAP_LONG_LINES) { + Wrap.createWrap(WrapType.NORMAL, false) + } + else getNoneWrap() + } + + fun getNoneWrap(): Wrap? { return Wrap.createWrap(WrapType.NONE, false) } } \ No newline at end of file diff --git a/test/nl/hannahsten/texifyidea/formatting/LatexLineWrapStrategyTest.kt b/test/nl/hannahsten/texifyidea/formatting/LatexLineWrapStrategyTest.kt index a26946469d..8c5b22dbde 100644 --- a/test/nl/hannahsten/texifyidea/formatting/LatexLineWrapStrategyTest.kt +++ b/test/nl/hannahsten/texifyidea/formatting/LatexLineWrapStrategyTest.kt @@ -96,4 +96,18 @@ class LatexLineWrapStrategyTest : BasePlatformTestCase() { """.trimIndent() myFixture.checkResult(expected) } + + fun testParenthesisWrap() { + setUpTest() + val text = """ + Lorem ipsum dolor amet (aaaaaaaa) + """.trimIndent() + myFixture.configureByText(LatexFileType, text) + myFixture.performEditorAction("ReformatCode") + val expected = """ + Lorem ipsum dolor amet + (aaaaaaaa) + """.trimIndent() + myFixture.checkResult(expected) + } } \ No newline at end of file From 9293bce05240d8a9ba22562248d92012c6bb4642 Mon Sep 17 00:00:00 2001 From: Abby Berkers Date: Sun, 24 Mar 2024 13:35:01 +0100 Subject: [PATCH 2/3] Fix test cases --- .../formatting/LatexLineWrapStrategyTest.kt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/test/nl/hannahsten/texifyidea/formatting/LatexLineWrapStrategyTest.kt b/test/nl/hannahsten/texifyidea/formatting/LatexLineWrapStrategyTest.kt index 8c5b22dbde..dc477c8932 100644 --- a/test/nl/hannahsten/texifyidea/formatting/LatexLineWrapStrategyTest.kt +++ b/test/nl/hannahsten/texifyidea/formatting/LatexLineWrapStrategyTest.kt @@ -39,8 +39,11 @@ class LatexLineWrapStrategyTest : BasePlatformTestCase() { myFixture.configureByText(LatexFileType, text) myFixture.performEditorAction("ReformatCode") val expected = """ - \section{This includes permissions.} - \href{https://the-very-very-long.url}{This includes} + \section + {This includes permissions.} + \href + {https://the-very-very-long.url} + {This includes} permissions. """.trimIndent() myFixture.checkResult(expected) @@ -82,14 +85,14 @@ class LatexLineWrapStrategyTest : BasePlatformTestCase() { \documentclass{article} \begin{document} Über die grüne Wiese hüpft - er das gemeinsame + er das gemeinsame Frühstück - Beisammensein, onders + Beisammensein, onders ${'$'}^{,}${'$'} bei - jedoch - \footfullcite{author} + jedoch + \footfullcite{author} abhielt \end{document} From 21e1e6292eb9b0468a75615669a554a83ac2e699 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Sat, 30 Mar 2024 15:46:18 +0100 Subject: [PATCH 3/3] Unused imports --- .../hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt b/src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt index d419a10b7f..3de5d9e21c 100644 --- a/src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt +++ b/src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt @@ -1,12 +1,9 @@ package nl.hannahsten.texifyidea.formatting import com.intellij.application.options.CodeStyle -import com.intellij.execution.configurations.runConfigurationType import com.intellij.formatting.Wrap import com.intellij.formatting.WrapType import com.intellij.lang.ASTNode -import com.intellij.psi.codeStyle.CodeStyleSettingsManager -import nl.hannahsten.texifyidea.settings.codestyle.LatexCodeStyleSettings /** *