Skip to content

Commit

Permalink
Don't insert \right when brace is already matched, fixes #3493 for ma…
Browse files Browse the repository at this point in the history
…tched brace pairs)
  • Loading branch information
slideclimb committed May 26, 2024
1 parent 11aeebd commit c20606a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package nl.hannahsten.texifyidea.completion.handlers

import com.intellij.codeInsight.completion.InsertHandler
import com.intellij.codeInsight.completion.InsertionContext
import com.intellij.codeInsight.highlighting.BraceMatchingUtil
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.openapi.editor.Editor

Check warning on line 7 in src/nl/hannahsten/texifyidea/completion/handlers/RightInsertHandler.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import nl.hannahsten.texifyidea.file.LatexFileType
import nl.hannahsten.texifyidea.lang.commands.LatexCommand
import nl.hannahsten.texifyidea.lang.commands.LatexDelimiterCommand

Expand All @@ -19,12 +21,13 @@ open class RightInsertHandler : InsertHandler<LookupElement> {
val command = element.`object` as? LatexCommand ?: return

if (command is LatexDelimiterCommand && command.isLeft) {
insertRightCommand(editor, command)
val hasMatchingBrace = BraceMatchingUtil.matchBrace(context.editor.document.text, LatexFileType, editor.highlighter.createIterator(context.editor.caretModel.offset - 1), true)
if (hasMatchingBrace) {
editor.document.insertString(editor.caretModel.offset, " ")
} else {
editor.document.insertString(editor.caretModel.offset, " \\" + command.matchingName)
}
editor.caretModel.moveToOffset(editor.caretModel.offset + 1)
}
}

private fun insertRightCommand(editor: Editor, leftCommand: LatexDelimiterCommand) {
editor.document.insertString(editor.caretModel.offset, " \\" + leftCommand.matchingName)
editor.caretModel.moveToOffset(editor.caretModel.offset + 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ class RightInsertHandlerTest : BasePlatformTestCase() {
myFixture.finishLookup(Lookup.NORMAL_SELECT_CHAR)
myFixture.checkResult("""$\left( <caret> \right) $""")
}

fun testLeftRightMatchingBraces() {
myFixture.configureByText(LatexFileType, """$\lef<caret> )$""")
myFixture.complete(CompletionType.BASIC)
myFixture.finishLookup(Lookup.NORMAL_SELECT_CHAR)
myFixture.checkResult("""$\left( <caret> )$""")
}
}

0 comments on commit c20606a

Please sign in to comment.