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

Fix some newline issues in enumeration #3383

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 =
Fixed Show fixed Hide fixed
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
Loading