Skip to content

Commit

Permalink
Merge pull request #3538 from jojo2357/left-right
Browse files Browse the repository at this point in the history
Complete left with right always.
  • Loading branch information
PHPirates authored May 6, 2024
2 parents d9af740 + 172a25e commit 00b0ef5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)`.
Expand All @@ -17,24 +17,14 @@ open class RightInsertHandler : InsertHandler<LookupElement> {
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)
}
}
80 changes: 42 additions & 38 deletions src/nl/hannahsten/texifyidea/lang/commands/LatexDelimiterCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,57 @@ 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,
override val isMathMode: Boolean = true,
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")
}
15 changes: 15 additions & 0 deletions test/nl/hannahsten/texifyidea/completion/RightInsertHandlerTest.kt
Original file line number Diff line number Diff line change
@@ -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<caret> $""")
myFixture.complete(CompletionType.BASIC)
myFixture.finishLookup(Lookup.NORMAL_SELECT_CHAR)
myFixture.checkResult("""$\left( <caret> \right) $""")
}
}

0 comments on commit 00b0ef5

Please sign in to comment.