<commit_before>package nl.hannahsten.texifyidea.highlighting

import com.intellij.codeInsight.editorActions.TypedHandlerDelegate
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile
import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.PsiTreeUtil
import nl.hannahsten.texifyidea.file.LatexFile
import nl.hannahsten.texifyidea.psi.LatexInlineMath
import nl.hannahsten.texifyidea.psi.LatexTypes
import nl.hannahsten.texifyidea.settings.TexifySettings.Companion.getInstance

/**
 * @author <NAME>
 */
class LatexTypedHandler : TypedHandlerDelegate() {
    /**
     * When pressing $ when text is selected, surround with $ signs.
     */
    override fun beforeSelectionRemoved(c: Char, project: Project, editor: Editor, file: PsiFile): Result {
        if (file is LatexFile && c == '$') {
            val selectionModel = editor.selectionModel
            val selectedText = selectionModel.selectedText ?: return Result.CONTINUE
            val selectionStartOffset = selectionModel.selectionStart
            val selectionEndOffset = selectionModel.selectionEnd
            // Remove the selected text.
            editor.document.deleteString(selectionStartOffset, selectionEndOffset)
            // Insert the selected text, surrounded by dollar signs.
            editor.document.insertString(selectionStartOffset, "$c$selectedText$c")
            // Move the caret to just before the second dollar sign (same behaviour as surrounding with quotes).
            editor.caretModel.moveToOffset(selectionEndOffset + 1)
            return Result.STOP
        }
        return super.beforeSelectionRemoved(c, project, editor, file)
    }

    override fun beforeCharTyped(c: Char, project: Project, editor: Editor, file: PsiFile, fileType: FileType): Result {
        if (file is LatexFile) {
            if (c == '$') {
                val caret = editor.caretModel
                val element = file.findElementAt(caret.offset)
                val parent = PsiTreeUtil.getParentOfType(element, LatexInlineMath::class.java)
                        ?: return Result.CONTINUE
                val endOffset = parent.textRange.endOffset
                if (caret.offset == endOffset - 1) { // Caret is at the end of the environment, so run over the closing $
                    caret.moveCaretRelatively(1, 0, false, false, true)
                    return Result.STOP<commit_msg>Only surround with $ when surround with quotes and braces setting is selected<commit_after>package nl.hannahsten.texifyidea.highlighting

import com.intellij.codeInsight.CodeInsightSettings
import com.intellij.codeInsight.editorActions.TypedHandlerDelegate
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile
import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.PsiTreeUtil
import nl.hannahsten.texifyidea.file.LatexFile
import nl.hannahsten.texifyidea.psi.LatexInlineMath
import nl.hannahsten.texifyidea.psi.LatexTypes
import nl.hannahsten.texifyidea.settings.TexifySettings.Companion.getInstance

/**
 * @author <NAME>
 */
class LatexTypedHandler : TypedHandlerDelegate() {
    /**
     * When pressing $ when text is selected, surround with $ signs.
     */
    override fun beforeSelectionRemoved(c: Char, project: Project, editor: Editor, file: PsiFile): Result {
        val selectionModel = editor.selectionModel
        if (CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED && selectionModel.hasSelection()) {
            if (file is LatexFile && c == '$') {
                val selectedText = selectionModel.selectedText
                val selectionStartOffset = selectionModel.selectionStart
                val selectionEndOffset = selectionModel.selectionEnd
                // Remove the selected text.
                editor.document.deleteString(selectionStartOffset, selectionEndOffset)
                // Insert the selected text, surrounded by dollar signs.
                editor.document.insertString(selectionStartOffset, "$c$selectedText$c")
                // Move the caret to just before the second dollar sign (same behaviour as surrounding with quotes).
                editor.caretModel.moveToOffset(selectionEndOffset + 1)
                return Result.STOP
            }
        }
        return super.beforeSelectionRemoved(c, project, editor, file)
    }

    override fun beforeCharTyped(c: Char, project: Project, editor: Editor, file: PsiFile, fileType: FileType): Result {
        if (file is LatexFile) {
            if (c == '$') {
                val caret = editor.caretModel
                val element = file.findElementAt(caret.offset)
                val parent = PsiTreeUtil.getParentOfType(element, LatexInlineMath::class.java)
                        ?: return Result.CONTINUE
                val endOffset = parent.textRange.endOffset
                if (caret.offset == endOffset - 1) { // Caret is at the end of the environment, so run over the closing $
                    caret.moveCaretRelatively(1, 0, false, false, true)
                    return Result.STOP
{'fraction_non_alphanumeric': 0.08330275229357798, 'fraction_numerical': 0.0014678899082568807, 'mean_word_length': 2.8852459016393444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5324473', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1460, 'n_words': 355}