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

When wrapping, only break lines at whitespace #3500

Merged
merged 3 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 6 additions & 2 deletions src/nl/hannahsten/texifyidea/formatting/LatexBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 =
Expand All @@ -86,15 +87,18 @@ 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,
targetIndent,
newFakeSectionIndent
)
blocks.add(block)
isPreviousWhiteSpace = false
}
else isPreviousWhiteSpace = true
child = child.treeNext
}
return blocks
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package nl.hannahsten.texifyidea.formatting

import com.intellij.application.options.CodeStyle
import com.intellij.execution.configurations.runConfigurationType

Check warning on line 4 in src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
Fixed Show fixed Hide fixed
import com.intellij.formatting.Wrap
import com.intellij.formatting.WrapType
import com.intellij.lang.ASTNode
import com.intellij.psi.codeStyle.CodeStyleSettingsManager

Check warning on line 8 in src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
Fixed Show fixed Hide fixed
import nl.hannahsten.texifyidea.settings.codestyle.LatexCodeStyleSettings

Check warning on line 9 in src/nl/hannahsten/texifyidea/formatting/LatexWrappingStrategy.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
Fixed Show fixed Hide fixed

/**
*
* @author Sten Wessel
*/
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -82,18 +85,32 @@ 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}
""".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)
}
}
Loading