Repository: hannah-sten/texify-idea
Issue: Hard wrap in an itemize block

#### Type of JetBrains IDE (IntelliJ, PyCharm, etc.) and version
IntelliJ IDEA 2022.2.3 (Community Edition)
Build #IC-222.4345.14, built on October 5, 2022
Runtime version: 17.0.4.1+7-b469.62 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 13.2.1
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 12
Metal Rendering is ON
Registry:
    debugger.watches.in.variables=false
    scala.erase.compiler.process.jdk.once=false

Non-Bundled Plugins:
    org.intellij.plugins.hcl (222.4345.14)
    com.intellij.ideolog (203.0.30.0)
    YourKit Java Profiler 2020.7-b376 Integration (2020.7.376.0)
    org.sonarlint.idea (8.3.0.71062)
    nl.rubensten.texifyidea (0.7.23)
    com.firsttimeinforever.intellij.pdf.viewer.intellij-pdf-viewer (0.14.0)
    PlantUML integration (5.17.1)
    PythonCore (222.4345.14)
    Docker (222.4345.23)
    DBN (3.3.2160.0)
    CMD Support (1.0.5)
    SequenceDiagram (2.2.2)
    org.intellij.scala (2022.2.13)

Kotlin: 222-1.7.10-release-334-IJ4345.14

#### Operating System 
macOS 13.2.1 (22D68)


#### TeXiFy IDEA version
nl.rubensten.texifyidea (0.7.23)

#### What I did (steps to reproduce)
Format (⌥+⌘+L) LaTex `itemize` block with a long `\\item`. 
The formatter split the long `\\item` into two lines (as expected) but the second line starts with an unwanted `\\item`.

Thanks for your help 🙂 

#### Minimal example to reproduce the problem
<!-- Make sure you have read and understood how to create a minimal example: https://tex.meta.stackexchange.com/questions/228/ive-just-been-asked-to-write-a-minimal-example-what-is-that -->

```latex
\\documentclass{article}
\\begin{document}

\\begin{itemize}
  \\item Lorem ipsum dolor sit amet, consectetur adipiscing elit Quisque et sagittis purus. Duis imperdiet cursus purus. Integer vel urna turpis. Phasellus gravida aliquet purus sed molestie. 
\\end{itemize}
    
\\end{document}
```

#### Expected behavior

```latex
\\documentclass{article}
\\begin{document}

\\begin{itemize}
  \\item Lorem ipsum dolor sit amet, consectetur adipiscing elit Quisque et sagittis purus. Duis imperdiet cursus 
        purus. Integer vel urna turpis. Phasellus gravida aliquet purus sed molestie.
\\end{itemize}
    
\\end{document}
```

#### Actual behavior

```latex
\\documentclass{article}
\\begin{document}

\\begin{itemize}
  \\item Lorem ipsum dolor sit amet, consectetur adipiscing elit Quisque et sagittis purus. Duis imperdiet cursus
  \\item purus. Integer vel urna turpis. Phasellus gravida aliquet purus sed molestie.
\\end{itemize}
    
\\end{document}
```


#### (if applicable) The full stacktrace of the exception thrown
No exception was thrown.

<!-- If you see a minor issue related to commands or environments metadata, e.g. which commands are cite or verbatim commands, you are encouraged to try to add the missing information yourself, see https://github.com/Hannah-Sten/TeXiFy-IDEA/wiki/Contributing-to-TeXiFy#editing-magic -->

Changed files:
src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt

diff --git a/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt b/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt
index 8781c4cb1..d303906ee 100644
--- a/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt
+++ b/src/nl/hannahsten/texifyidea/editor/typedhandlers/LatexEnterInEnumerationHandler.kt
@@ -6,6 +6,7 @@ import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate.Result
 import com.intellij.codeInsight.template.TemplateManager
 import com.intellij.codeInsight.template.impl.TemplateImpl
 import com.intellij.codeInsight.template.impl.TextExpression
+import com.intellij.formatting.LineWrappingUtil
 import com.intellij.ide.DataManager
 import com.intellij.openapi.actionSystem.DataContext
 import com.intellij.openapi.editor.Editor
@@ -17,7 +18,7 @@ import com.intellij.psi.PsiFile
 import nl.hannahsten.texifyidea.file.LatexFileType
 import nl.hannahsten.texifyidea.psi.*
 import nl.hannahsten.texifyidea.settings.TexifySettings
-import nl.hannahsten.texifyidea.util.*
+import nl.hannahsten.texifyidea.util.insertAtCaretAndMove
 import nl.hannahsten.texifyidea.util.magic.EnvironmentMagic
 import nl.hannahsten.texifyidea.util.parser.*
 
@@ -35,7 +36,7 @@ class LatexEnterInEnumerationHandler : EnterHandlerDelegate {
         }
 
         // Don't insert \\item when the enter was triggered by the word wrap
-        if (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
         }
 
@@ -129,7 +130,10 @@ class LatexEnterInEnumerationHandler : EnterHandlerDelegate {
         if (
             !TexifySettings.getInstance().automaticItemInItemize ||
             element == null ||
-            DataManager.getInstance().loadFromDataContext(context, SPLIT_LINE_KEY) == true || // SplitLineAction means Ctrl+Enter was pressed, we decide to not insert \\item in that case
+            DataManager.getInstance().loadFromDataContext(
+                context,
+                SPLIT_LINE_KEY
+            ) == true || // SplitLineAction means Ctrl+Enter was pressed, we decide to not insert \\item in that case
             element.inMathContext()
         ) {
             return false