Skip to content

Commit

Permalink
Merge pull request #3383 from jojo2357/enumerate-newlines
Browse files Browse the repository at this point in the history
Fix some newline issues in enumeration
  • Loading branch information
PHPirates authored Jan 11, 2024
2 parents 464de53 + b429731 commit 7bb87e8
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import com.intellij.openapi.editor.actions.SplitLineAction.SPLIT_LINE_KEY
import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.util.parentOfTypes
import com.jetbrains.rd.util.first
import nl.hannahsten.texifyidea.file.LatexFileType
import nl.hannahsten.texifyidea.psi.*
import nl.hannahsten.texifyidea.settings.TexifySettings
Expand All @@ -37,7 +39,12 @@ class LatexEnterInEnumerationHandler : EnterHandlerDelegate {

// Don't insert \item when the enter was triggered by the word wrap
@Suppress("UnstableApiUsage") // This internal api is fine to use, see https://youtrack.jetbrains.com/issue/IDEA-324754
if (DataManager.getInstance().loadFromDataContext(context, LineWrappingUtil.WRAP_LONG_LINE_DURING_FORMATTING_IN_PROGRESS_KEY) == true || DataManager.getInstance().loadFromDataContext(context, AutoHardWrapHandler.AUTO_WRAP_LINE_IN_PROGRESS_KEY) == true) {
if (DataManager.getInstance().loadFromDataContext(
context,
LineWrappingUtil.WRAP_LONG_LINE_DURING_FORMATTING_IN_PROGRESS_KEY
) == true || DataManager.getInstance()
.loadFromDataContext(context, AutoHardWrapHandler.AUTO_WRAP_LINE_IN_PROGRESS_KEY) == true
) {
return Result.Continue
}

Expand Down Expand Up @@ -71,7 +78,7 @@ class LatexEnterInEnumerationHandler : EnterHandlerDelegate {
* Get the special marker that is used at the previous item (if any).
*/
private fun getPreviousMarker(element: PsiElement): String? {
val environment = element.parentOfType(LatexEnvironment::class) ?: return null
val environment = element.parentOfTypes(LatexEnvironment::class) ?: return null

// Last element in the list => Find last \item.
val label = if (element.parent is LatexEnvironment) {
Expand All @@ -83,8 +90,11 @@ class LatexEnterInEnumerationHandler : EnterHandlerDelegate {
} ?: return null // when no label could befound.

// Extract optional parameters.
val optionals = label.childrenOfType(LatexOptionalParam::class).firstOrNull() ?: return null
return optionals.text
val paramMap = label.getOptionalParameterMap()
if (paramMap.isEmpty())
return null
else
return paramMap.first().key.text ?: return null
}

/**
Expand Down Expand Up @@ -142,6 +152,9 @@ class LatexEnterInEnumerationHandler : EnterHandlerDelegate {

val isGluedToTheBeginCommand = element.hasParent(LatexBeginCommand::class)
val isInsideAnEnumeration = element.inDirectEnvironment(EnvironmentMagic.listingEnvironments)
return isInsideAnEnumeration && !isGluedToTheBeginCommand
val environment = element.parentOfTypes(LatexEnvironment::class)
val isInsideRequiredParam =
element.firstParentOfType(LatexRequiredParam::class)?.isChildOf(environment) ?: false
return isInsideAnEnumeration && !isGluedToTheBeginCommand && !isInsideRequiredParam
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,92 @@ class LatexEnterInEnumerationHandlerTest : BasePlatformTestCase() {
)
}

fun testItemizeBreakLine() {
myFixture.configureByText(
LatexFileType,
"""
\begin{itemize}
\item This sentence is <caret>broken
\end{itemize}
""".trimIndent()
)
myFixture.type("\n")
myFixture.checkResult(
"""
\begin{itemize}
\item This sentence is
\item <caret>broken
\end{itemize}
""".trimIndent()
)
}

fun testItemizeBreakLineNoItem() {
myFixture.configureByText(
LatexFileType,
"""
\begin{itemize}
\item {This sentence is <caret>broken}
\item Second item
\end{itemize}
""".trimIndent()
)
myFixture.type("\n")
myFixture.checkResult(
"""
\begin{itemize}
\item {This sentence is
<caret>broken}
\item Second item
\end{itemize}
""".trimIndent()
)
}

fun testItemizeBreakLineInCommand() {
myFixture.configureByText(
LatexFileType,
"""
\begin{itemize}
\item \textit{This sentence is <caret>broken}
\item Second item
\end{itemize}
""".trimIndent()
)
myFixture.type("\n")
myFixture.checkResult(
"""
\begin{itemize}
\item \textit{This sentence is
<caret>broken}
\item Second item
\end{itemize}
""".trimIndent()
)
}

fun testItemizeBreakLineInCommandExtraBrackets() {
myFixture.configureByText(
LatexFileType,
"""
\begin{itemize}
\item {\textit{This sentence is <caret>broken}}
\item Second item
\end{itemize}
""".trimIndent()
)
myFixture.type("\n")
myFixture.checkResult(
"""
\begin{itemize}
\item {\textit{This sentence is
<caret>broken}}
\item Second item
\end{itemize}
""".trimIndent()
)
}

fun `test nested enumeration with prefix`() {
myFixture.configureByText(
LatexFileType,
Expand Down

0 comments on commit 7bb87e8

Please sign in to comment.