From b85e2d09f8a8521c1e66d924c6f78fb367977bf6 Mon Sep 17 00:00:00 2001 From: jojo2357 <66704796+jojo2357@users.noreply.github.com> Date: Sat, 6 Jan 2024 01:33:36 -0800 Subject: [PATCH 1/4] Fix some newline issues in enumeration --- .../LatexEnterInEnumerationHandler.kt | 17 ++++++-- .../LatexEnterInEnumerationHandlerTest.kt | 40 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt b/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt index d303906ee..1453289cd 100644 --- a/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt +++ b/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt @@ -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.parentOfType +import com.jetbrains.rd.util.first import nl.hannahsten.texifyidea.file.LatexFileType import nl.hannahsten.texifyidea.psi.* import nl.hannahsten.texifyidea.settings.TexifySettings @@ -82,8 +84,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 } /** @@ -141,6 +146,12 @@ class LatexEnterInEnumerationHandler : EnterHandlerDelegate { val isGluedToTheBeginCommand = element.hasParent(LatexBeginCommand::class) val isInsideAnEnumeration = element.inDirectEnvironment(EnvironmentMagic.listingEnvironments) - return isInsideAnEnumeration && !isGluedToTheBeginCommand + val environment = element.parentOfType(LatexEnvironment::class) + val isInsideRequiredParam = + if (environment != null) + element.firstParentOfType(LatexRequiredParam::class)?.isChildOf(getLastLabel(environment)) ?: false + else + false + return isInsideAnEnumeration && !isGluedToTheBeginCommand && !isInsideRequiredParam } } diff --git a/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt b/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt index d09fc56ce..89d028826 100644 --- a/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt +++ b/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt @@ -46,6 +46,46 @@ class LatexEnterInEnumerationHandlerTest : BasePlatformTestCase() { ) } + fun testItemizeBreakLine() { + myFixture.configureByText( + LatexFileType, + """ + \begin{itemize} + \item This sentence is broken + \end{itemize} + """.trimIndent() + ) + myFixture.type("\n") + myFixture.checkResult( + """ + \begin{itemize} + \item This sentence is + \item broken + \end{itemize} + """.trimIndent() + ) + } + + fun testItemizeBreakLineNoItem() { + myFixture.configureByText( + LatexFileType, + """ + \begin{itemize} + \item {This sentence is broken} + \end{itemize} + """.trimIndent() + ) + myFixture.type("\n") + myFixture.checkResult( + """ + \begin{itemize} + \item {This sentence is + broken} + \end{itemize} + """.trimIndent() + ) + } + fun `test nested enumeration with prefix`() { myFixture.configureByText( LatexFileType, From 1006dce8eb88045df140573fe6eb2038a5d55dc6 Mon Sep 17 00:00:00 2001 From: jojo2357 <66704796+jojo2357@users.noreply.github.com> Date: Sun, 7 Jan 2024 17:06:32 -0800 Subject: [PATCH 2/4] Qodana is ornery --- .../editor/typedhandlers/LatexEnterInEnumerationHandler.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt b/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt index 1453289cd..cc349e06c 100644 --- a/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt +++ b/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt @@ -16,6 +16,7 @@ import com.intellij.openapi.util.Ref import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.util.parentOfType +import com.intellij.psi.util.parentOfTypes import com.jetbrains.rd.util.first import nl.hannahsten.texifyidea.file.LatexFileType import nl.hannahsten.texifyidea.psi.* @@ -146,7 +147,7 @@ class LatexEnterInEnumerationHandler : EnterHandlerDelegate { val isGluedToTheBeginCommand = element.hasParent(LatexBeginCommand::class) val isInsideAnEnumeration = element.inDirectEnvironment(EnvironmentMagic.listingEnvironments) - val environment = element.parentOfType(LatexEnvironment::class) + val environment = element.parentOfTypes(LatexEnvironment::class) val isInsideRequiredParam = if (environment != null) element.firstParentOfType(LatexRequiredParam::class)?.isChildOf(getLastLabel(environment)) ?: false From 042065c35b9be0411a41584df2ed4c301dd52443 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Mon, 8 Jan 2024 21:49:29 +0100 Subject: [PATCH 3/4] Update test to demonstrate an issue when the caret is not at the last item --- .../editor/typedhandlers/LatexEnterInEnumerationHandler.kt | 3 +-- .../editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt b/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt index cc349e06c..83ba513dc 100644 --- a/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt +++ b/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt @@ -15,7 +15,6 @@ 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.parentOfType import com.intellij.psi.util.parentOfTypes import com.jetbrains.rd.util.first import nl.hannahsten.texifyidea.file.LatexFileType @@ -73,7 +72,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) { diff --git a/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt b/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt index 89d028826..888ca58d3 100644 --- a/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt +++ b/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt @@ -72,6 +72,7 @@ class LatexEnterInEnumerationHandlerTest : BasePlatformTestCase() { """ \begin{itemize} \item {This sentence is broken} + \item Second item \end{itemize} """.trimIndent() ) @@ -81,6 +82,7 @@ class LatexEnterInEnumerationHandlerTest : BasePlatformTestCase() { \begin{itemize} \item {This sentence is broken} + \item Second item \end{itemize} """.trimIndent() ) From 59091dc44f568e1a7c50027be5cf1a19f0a75eee Mon Sep 17 00:00:00 2001 From: jojo2357 <66704796+jojo2357@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:40:23 -0700 Subject: [PATCH 4/4] Change new item algo Add `\item` iff inside a required param which is a child of an enumerate --- .../LatexEnterInEnumerationHandler.kt | 12 ++--- .../LatexEnterInEnumerationHandlerTest.kt | 44 +++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt b/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt index 83ba513dc..c8181cf54 100644 --- a/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt +++ b/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt @@ -38,7 +38,12 @@ class LatexEnterInEnumerationHandler : EnterHandlerDelegate { } // Don't insert \item when the enter was triggered by the word wrap - 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 } @@ -148,10 +153,7 @@ class LatexEnterInEnumerationHandler : EnterHandlerDelegate { val isInsideAnEnumeration = element.inDirectEnvironment(EnvironmentMagic.listingEnvironments) val environment = element.parentOfTypes(LatexEnvironment::class) val isInsideRequiredParam = - if (environment != null) - element.firstParentOfType(LatexRequiredParam::class)?.isChildOf(getLastLabel(environment)) ?: false - else - false + element.firstParentOfType(LatexRequiredParam::class)?.isChildOf(environment) ?: false return isInsideAnEnumeration && !isGluedToTheBeginCommand && !isInsideRequiredParam } } diff --git a/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt b/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt index 888ca58d3..e5b0b9df1 100644 --- a/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt +++ b/test/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandlerTest.kt @@ -88,6 +88,50 @@ class LatexEnterInEnumerationHandlerTest : BasePlatformTestCase() { ) } + fun testItemizeBreakLineInCommand() { + myFixture.configureByText( + LatexFileType, + """ + \begin{itemize} + \item \textit{This sentence is broken} + \item Second item + \end{itemize} + """.trimIndent() + ) + myFixture.type("\n") + myFixture.checkResult( + """ + \begin{itemize} + \item \textit{This sentence is + broken} + \item Second item + \end{itemize} + """.trimIndent() + ) + } + + fun testItemizeBreakLineInCommandExtraBrackets() { + myFixture.configureByText( + LatexFileType, + """ + \begin{itemize} + \item {\textit{This sentence is broken}} + \item Second item + \end{itemize} + """.trimIndent() + ) + myFixture.type("\n") + myFixture.checkResult( + """ + \begin{itemize} + \item {\textit{This sentence is + broken}} + \item Second item + \end{itemize} + """.trimIndent() + ) + } + fun `test nested enumeration with prefix`() { myFixture.configureByText( LatexFileType,