<commit_before>package nl.hannahsten.texifyidea.editor.typedhandlers

import com.intellij.codeInsight.editorActions.AutoHardWrapHandler
import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate
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.ide.DataManager
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.EditorActionHandler
import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import nl.hannahsten.texifyidea.editor.ControlTracker
import nl.hannahsten.texifyidea.editor.ShiftTracker
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.magic.EnvironmentMagic

/**
 * @author <NAME>
 */
class LatexEnterInEnumerationHandler : EnterHandlerDelegate {

    override fun postProcessEnter(
        file: PsiFile, editor: Editor,
        context: DataContext
    ): Result {
        if (file.fileType != LatexFileType) {
            return Result.Continue
        }

        // 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) {
            return Result.Continue
        }

        ShiftTracker.setup(editor.contentComponent)
        ControlTracker.setup(editor.contentComponent)

        val caret = editor.caretModel
        val element = file.findElementAt(caret.offset)
        if (hasValidContext(element)) {
            val previousMarker = getPreviousMarker(element!!)
            if (previousMarker == null) {
                editor.insertAtCaretAndMove( "\\item ")
            }
            else {
                // Use live template, so that the user can choose to replace the label and press enter to jump out of the optional argument
                val template = TemplateImpl("", "\\item[\$__Variable0\$] ", "")
                template.addVariable(TextExpression(previousMarker.trim('[', ']')), true)
                TemplateManager.getInstance(file.project).startTemplate(editor, template)
            }
        }
        else {
            if (ControlTracker.isControlPressed) {
                editor.insertAtCaretAndMove( "")
            }
        }

        return Result.Continue
    }

    override fun preprocessEnter(
        file: PsiFile, editor: Editor, p2: Ref<Int>, p3: Ref<Int>,
        context: DataContext,
        p5: EditorActionHandler?
    ): Result {
        return Result.Continue
    }

    /**<commit_msg>Formatting<commit_after>package nl.hannahsten.texifyidea.editor.typedhandlers

import com.intellij.codeInsight.editorActions.AutoHardWrapHandler
import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate
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.ide.DataManager
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.EditorActionHandler
import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import nl.hannahsten.texifyidea.editor.ControlTracker
import nl.hannahsten.texifyidea.editor.ShiftTracker
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.magic.EnvironmentMagic

/**
 * @author <NAME>
 */
class LatexEnterInEnumerationHandler : EnterHandlerDelegate {

    override fun postProcessEnter(
        file: PsiFile, editor: Editor,
        context: DataContext
    ): Result {
        if (file.fileType != LatexFileType) {
            return Result.Continue
        }

        // 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) {
            return Result.Continue
        }

        ShiftTracker.setup(editor.contentComponent)
        ControlTracker.setup(editor.contentComponent)

        val caret = editor.caretModel
        val element = file.findElementAt(caret.offset)
        if (hasValidContext(element)) {
            val previousMarker = getPreviousMarker(element!!)
            if (previousMarker == null) {
                editor.insertAtCaretAndMove("\\item ")
            }
            else {
                // Use live template, so that the user can choose to replace the label and press enter to jump out of the optional argument
                val template = TemplateImpl("", "\\item[\$__Variable0\$] ", "")
                template.addVariable(TextExpression(previousMarker.trim('[', ']')), true)
                TemplateManager.getInstance(file.project).startTemplate(editor, template)
            }
        }
        else {
            if (ControlTracker.isControlPressed) {
                editor.insertAtCaretAndMove("")
            }
        }

        return Result.Continue
    }

    override fun preprocessEnter(
        file: PsiFile, editor: Editor, p2: Ref<Int>, p3: Ref<Int>,
        context: DataContext,
        p5: EditorActionHandler?
    ): Result {
        return Result.Continue
    }

    /**
{'fraction_non_alphanumeric': 0.09043005269420364, 'fraction_numerical': 0.00135985041645419, 'mean_word_length': 3.0832755031228314, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4827540', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1631, 'n_words': 307}