diff --git a/src/nl/hannahsten/texifyidea/completion/handlers/RightInsertHandler.kt b/src/nl/hannahsten/texifyidea/completion/handlers/RightInsertHandler.kt index 2cfa605d3..6b0973673 100644 --- a/src/nl/hannahsten/texifyidea/completion/handlers/RightInsertHandler.kt +++ b/src/nl/hannahsten/texifyidea/completion/handlers/RightInsertHandler.kt @@ -5,7 +5,7 @@ import com.intellij.codeInsight.completion.InsertionContext import com.intellij.codeInsight.lookup.LookupElement import com.intellij.openapi.editor.Editor import nl.hannahsten.texifyidea.lang.commands.LatexCommand -import nl.hannahsten.texifyidea.util.magic.TypographyMagic +import nl.hannahsten.texifyidea.lang.commands.LatexDelimiterCommand /** * Inserts the right part of left-right command pairs, like `\left( \right)`. @@ -17,24 +17,14 @@ open class RightInsertHandler : InsertHandler { override fun handleInsert(context: InsertionContext, element: LookupElement) { val editor = context.editor val command = element.`object` as? LatexCommand ?: return - val name = command.command - when { - name.startsWith("left") -> insertRightBraceCommand(name, editor) - name == "langle" -> insertRightCommand(editor, "\\rangle", spacing = " ") - name == "lq" -> insertRightCommand(editor, "\\rq", spacing = "", suffix = "{}") + if (command is LatexDelimiterCommand && command.isLeft) { + insertRightCommand(editor, command) } } - private fun insertRightBraceCommand(commandName: String, editor: Editor) { - val char = commandName.substring(4) - val opposite = TypographyMagic.braceOpposites[char] ?: return - editor.document.insertString(editor.caretModel.offset, " \\right$opposite") + private fun insertRightCommand(editor: Editor, leftCommand: LatexDelimiterCommand) { + editor.document.insertString(editor.caretModel.offset, " \\" + leftCommand.matchingName) editor.caretModel.moveToOffset(editor.caretModel.offset + 1) } - - private fun insertRightCommand(editor: Editor, rightCommand: String, spacing: String = "", suffix: String = "") { - editor.document.insertString(editor.caretModel.offset, "$suffix$spacing$spacing$rightCommand$suffix") - editor.caretModel.moveToOffset(editor.caretModel.offset + spacing.length + suffix.length) - } } diff --git a/src/nl/hannahsten/texifyidea/lang/commands/LatexDelimiterCommand.kt b/src/nl/hannahsten/texifyidea/lang/commands/LatexDelimiterCommand.kt index e71607973..7f32dfc72 100644 --- a/src/nl/hannahsten/texifyidea/lang/commands/LatexDelimiterCommand.kt +++ b/src/nl/hannahsten/texifyidea/lang/commands/LatexDelimiterCommand.kt @@ -9,6 +9,7 @@ import nl.hannahsten.texifyidea.lang.LatexPackage.Companion.STMARYRD */ enum class LatexDelimiterCommand( override val command: String, + val matchingName: String, override vararg val arguments: Argument = emptyArray(), override val dependency: LatexPackage = LatexPackage.DEFAULT, override val display: String? = null, @@ -16,46 +17,49 @@ enum class LatexDelimiterCommand( val collapse: Boolean = false ) : LatexCommand { - LEFT_PARENTH("left(", display = "("), - RIGHT_PARENTH("right)", display = ")"), - LEFT_BRACKET("left[", display = "["), - RIGHT_BRACKET("right]", display = "]"), - LEFT_BRACE("left\\{", display = "{"), - RIGHT_BRACE("right\\}", display = "}"), - LEFT_ANGULAR("left<", display = "<"), - RIGHT_ANGULAR("right>", display = ">"), - LEFT_PIPE("left|", display = "|"), - RIGHT_PIPE("right|", display = "|"), - LEFT_DOUBLE_PIPE("left\\|", display = "||"), - RIGHT_DOUBLE_PIPE("right\\|", display = "||"), - LEFT_ANGLE_BRACKET("langle", display = "<"), - RIGHT_ANGLE_BRACKET("rangle", display = ">"), - LEFT_CEIL("lceil", display = "⌈"), - RIGHT_CEIL("rceil", display = "⌉"), - LEFT_FLOOR("lfloor", display = "⌊"), - RIGHT_FLOOR("rfloor", display = "⌋"), - LEFT_MOUSTACHE("lmoustache", display = "⎰"), - RIGHT_MOUSTACHE("rmoustache", display = "⎱"), - LEFT_GROUP("lgroup"), - RIGHT_GROUP("rgroup"), - LEFT_VERTICAL_LINE("lvert", dependency = AMSMATH), - RIGHT_VERTICAL_LINE("rvert", dependency = AMSMATH), - LEFT_DOUBLE_VERTICAL_LINES("lVert", dependency = AMSMATH), - RIGHT_DOUBLE_VERTICAL_LINES("rVert", dependency = AMSMATH), - LEFT_BAG("lbag", display = "⟅", dependency = STMARYRD), - RIGHT_BAG("rbag", display = "⟆", dependency = STMARYRD), - LEFT_BAG_BOLD("Lbag", display = "⟅", dependency = STMARYRD), - RIGHT_BAG_BOLD("Rbag", display = "⟆", dependency = STMARYRD), - LEFT_BRACKET_BOLD("llbracket", display = "⟦", dependency = STMARYRD), - RIGHT_BRACKET_BOLD("rrbracket", display = "⟧", dependency = STMARYRD), - LEFT_CEIL_BOLD("llceil", dependency = STMARYRD), - RIGHT_CEIL_BOLD("rrceil", dependency = STMARYRD), - LEFT_FLOOR_BOLD("llfloor", dependency = STMARYRD), - RIGHT_FLOOR_BOLD("rrfloor", dependency = STMARYRD), - LEFT_PARENTHESIS_BOLD("llparenthesis", display = "⦅", dependency = STMARYRD), - RIGHT_PARENTHESIS_BOLD("rrparenthesis", display = "⦆", dependency = STMARYRD), + LEFT_PARENTH("left(", "right)", display = "("), + RIGHT_PARENTH("right)", "left(", display = ")"), + LEFT_BRACKET("left[", "right]", display = "["), + RIGHT_BRACKET("right]", "left[", display = "]"), + LEFT_BRACE("left\\{", "right\\}", display = "{"), + RIGHT_BRACE("right\\}", "left\\{", display = "}"), + LEFT_ANGULAR("left<", "right>", display = "<"), + RIGHT_ANGULAR("right>", "left<", display = ">"), + LEFT_PIPE("left|", "right|", display = "|"), + RIGHT_PIPE("right|", "left|", display = "|"), + LEFT_DOUBLE_PIPE("left\\|", "right\\|", display = "||"), + RIGHT_DOUBLE_PIPE("right\\|", "left\\|", display = "||"), + LEFT_ANGLE_BRACKET("langle", "rangle", display = "<"), + RIGHT_ANGLE_BRACKET("rangle", "langle", display = ">"), + LEFT_CEIL("lceil", "rceil", display = "⌈"), + RIGHT_CEIL("rceil", "lceil", display = "⌉"), + LEFT_FLOOR("lfloor", "rfloor", display = "⌊"), + RIGHT_FLOOR("rfloor", "lfloor", display = "⌋"), + LEFT_MOUSTACHE("lmoustache", "rmoustache", display = "⎰"), + RIGHT_MOUSTACHE("rmoustache", "lmoustache", display = "⎱"), + LEFT_GROUP("lgroup", "rgroup"), + RIGHT_GROUP("rgroup", "lgroup"), + LEFT_VERTICAL_LINE("lvert", "rvert", dependency = AMSMATH), + RIGHT_VERTICAL_LINE("rvert", "lvert", dependency = AMSMATH), + LEFT_DOUBLE_VERTICAL_LINES("lVert", "rVert", dependency = AMSMATH), + RIGHT_DOUBLE_VERTICAL_LINES("rVert", "lVert", dependency = AMSMATH), + LEFT_BAG("lbag", "rbag", display = "⟅", dependency = STMARYRD), + RIGHT_BAG("rbag", "lbag", display = "⟆", dependency = STMARYRD), + LEFT_BAG_BOLD("Lbag", "Rbag", display = "⟅", dependency = STMARYRD), + RIGHT_BAG_BOLD("Rbag", "Lbag", display = "⟆", dependency = STMARYRD), + LEFT_BRACKET_BOLD("llbracket", "rrbracket", display = "⟦", dependency = STMARYRD), + RIGHT_BRACKET_BOLD("rrbracket", "llbracket", display = "⟧", dependency = STMARYRD), + LEFT_CEIL_BOLD("llceil", "rrceil", dependency = STMARYRD), + RIGHT_CEIL_BOLD("rrceil", "llceil", dependency = STMARYRD), + LEFT_FLOOR_BOLD("llfloor", "rrfloor", dependency = STMARYRD), + RIGHT_FLOOR_BOLD("rrfloor", "llfloor", dependency = STMARYRD), + LEFT_PARENTHESIS_BOLD("llparenthesis", "rrparenthesis", display = "⦅", dependency = STMARYRD), + RIGHT_PARENTHESIS_BOLD("rrparenthesis", "llparenthesis", display = "⦆", dependency = STMARYRD), ; override val identifier: String get() = name + + val isLeft = this.command.startsWith("l") + val isRight = this.command.startsWith("r") } \ No newline at end of file diff --git a/test/nl/hannahsten/texifyidea/completion/RightInsertHandlerTest.kt b/test/nl/hannahsten/texifyidea/completion/RightInsertHandlerTest.kt new file mode 100644 index 000000000..4f62517ba --- /dev/null +++ b/test/nl/hannahsten/texifyidea/completion/RightInsertHandlerTest.kt @@ -0,0 +1,15 @@ +package nl.hannahsten.texifyidea.completion + +import com.intellij.codeInsight.completion.CompletionType +import com.intellij.codeInsight.lookup.Lookup +import com.intellij.testFramework.fixtures.BasePlatformTestCase +import nl.hannahsten.texifyidea.file.LatexFileType + +class RightInsertHandlerTest : BasePlatformTestCase() { + fun testLeftRightBraces() { + myFixture.configureByText(LatexFileType, """$\lef $""") + myFixture.complete(CompletionType.BASIC) + myFixture.finishLookup(Lookup.NORMAL_SELECT_CHAR) + myFixture.checkResult("""$\left( \right) $""") + } +} \ No newline at end of file