Repository: hannah-sten/texify-idea
Issue: LatexCiteBeforePeriodInspection IndexOutOfBoundsException at the end of a file.

### Description
Performed when performing a quick fix for the LatexCiteBeforePeriodInspection. Whenever the citation is at the end of a file, this crash appears and the quick fix is not carried out.

### Stacktrace
```
java.lang.IndexOutOfBoundsException: chars sequence.length:164, start:164, end:165
	at com.intellij.util.text.CharSequenceSubSequence.<init>(CharSequenceSubSequence.java:34)
	at com.intellij.util.text.ImmutableText.subSequence(ImmutableText.java:193)
	at com.intellij.openapi.editor.impl.DocumentImpl.lambda$getText$1(DocumentImpl.java:923)
	at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:970)
	at com.intellij.openapi.application.ReadAction.compute(ReadAction.java:57)
	at com.intellij.openapi.editor.impl.DocumentImpl.getText(DocumentImpl.java:923)
	at nl.rubensten.texifyidea.util.DocumentsKt.get(Documents.kt:49)
	at nl.rubensten.texifyidea.inspections.latex.LatexCiteBeforePeriodInspection.applyFix(LatexCiteBeforePeriodInspection.kt:32)
	at nl.rubensten.texifyidea.inspections.TexifyRegexInspection$inspectFile$1.invoke(TexifyRegexInspection.kt:152)
	at nl.rubensten.texifyidea.inspections.TexifyRegexInspection$inspectFile$1.invoke(TexifyRegexInspection.kt:23)
	at nl.rubensten.texifyidea.inspections.TexifyRegexInspection$RegexFix.applyFix(TexifyRegexInspection.kt:199)
	at nl.rubensten.texifyidea.inspections.TexifyRegexInspection$RegexFix.applyFix(TexifyRegexInspection.kt:188)
	at com.intellij.codeInspection.ex.QuickFixWrapper.invoke(QuickFixWrapper.java:73)
	at com.intellij.codeInsight.intention.impl.IntentionActionWithTextCaching$MyIntentionAction.invoke(IntentionActionWithTextCaching.java:179)
	at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.lambda$invokeIntention$4(ShowIntentionActionsHandler.java:206)
	at com.intellij.openapi.application.WriteAction.run(WriteAction.java:92)
	at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.invokeIntention(ShowIntentionActionsHandler.java:208)
	at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.lambda$null$2(ShowIntentionActionsHandler.java:183)
	at com.intellij.openapi.application.TransactionGuardImpl.runSyncTransaction(TransactionGuardImpl.java:88)
	at com.intellij.openapi.application.TransactionGuardImpl.submitTransactionAndWait(TransactionGuardImpl.java:153)
	at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.lambda$chooseActionAndInvoke$3(ShowIntentionActionsHandler.java:182)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:220)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:178)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:168)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:154)
	at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.chooseActionAndInvoke(ShowIntentionActionsHandler.java:181)
	at com.intellij.codeInsight.intention.impl.IntentionListStep.lambda$applyAction$0(IntentionListStep.java:103)
	at com.intellij.openapi.application.TransactionGuardImpl.performUserActivity(TransactionGuardImpl.java:195)
	at com.intellij.ui.popup.AbstractPopup.lambda$dispose$14(AbstractPopup.java:1425)
	at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:3075)
	at com.intellij.ide.IdeEventQueue.ifFocusEventsInTheQueue(IdeEventQueue.java:159)
	at com.intellij.ide.IdeEventQueue.executeWhenAllFocusEventsLeftTheQueue(IdeEventQueue.java:111)
	at com.intellij.openapi.wm.impl.FocusManagerImpl.doWhenFocusSettlesDown(FocusManagerImpl.java:179)
	at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.doWhenFocusSettlesDown(IdeFocusManagerImpl.java:58)
	at com.intellij.ui.popup.AbstractPopup.dispose(AbstractPopup.java:1421)
	at com.intellij.ui.popup.WizardPopup.dispose(WizardPopup.java:160)
	at com.intellij.ui.popup.list.ListPopupImpl.dispose(ListPopupImpl.java:306)
	at com.intellij.openapi.util.Disposer$1.execute(Disposer.java:48)
	at com.intellij.openapi.util.Disposer$1.execute(Disposer.java:44)
	at com.intellij.openapi.util.objectTree.ObjectNode$1.execute(ObjectNode.java:136)
	at com.intellij.openapi.util.objectTree.ObjectNode$1.execute(ObjectNode.java:105)
	at com.intellij.openapi.util.objectTree.ObjectTree.executeActionWithRecursiveGuard(ObjectTree.java:214)
	at com.intellij.openapi.util.objectTree.ObjectNode.execute(ObjectNode.java:105)
	at com.intellij.openapi.util.objectTree.ObjectTree.executeAll(ObjectTree.java:162)
	at com.intellij.openapi.util.Disposer.dispose(Disposer.java:129)
	at com.intellij.openapi.util.Disposer.dispose(Disposer.java:125)
	at com.intellij.ui.popup.WizardPopup.disposeAllParents(WizardPopup.java:263)
	at com.intellij.ui.popup.list.ListPopupImpl.handleNextStep(ListPopupImpl.java:442)
	at com.intellij.ui.popup.list.ListPopupImpl._handleSelect(ListPopupImpl.java:395)
	at com.intellij.ui.popup.list.ListPopupImpl.handleSelect(ListPopupImpl.java:336)
	at com.intellij.ui.popup.list.ListPopupImpl$1.actionPerformed(ListPopupImpl.java:249)
	at com.intellij.ui.popup.WizardPopup.proceedKeyEvent(WizardPopup.java:378)
	at com.intellij.ui.popup.WizardPopup.dispatch(WizardPopup.java:358)
	at com.intellij.ui.popup.PopupDispatcher.dispatchKeyEvent(PopupDispatcher.java:114)
	at com.intellij.ui.popup.PopupDispatcher.dispatch(PopupDispatcher.java:150)
	at com.intellij.ide.IdePopupManager.dispatch(IdePopupManager.java:86)
	at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:647)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:391)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
```

Changed files:
src/nl/rubensten/texifyidea/inspections/latex/LatexCiteBeforePeriodInspection.kt

diff --git a/src/nl/rubensten/texifyidea/inspections/latex/LatexCiteBeforePeriodInspection.kt b/src/nl/rubensten/texifyidea/inspections/latex/LatexCiteBeforePeriodInspection.kt
index 6d1277f69..ff2362d15 100644
--- a/src/nl/rubensten/texifyidea/inspections/latex/LatexCiteBeforePeriodInspection.kt
+++ b/src/nl/rubensten/texifyidea/inspections/latex/LatexCiteBeforePeriodInspection.kt
@@ -29,7 +29,7 @@ open class LatexCiteBeforePeriodInspection : TexifyRegexInspection(
         val cite = file.findElementAt(replacementRange.endInclusive + 3)?.parentOfType(LatexCommands::class) ?: return
         val char = groups[0]
 
-        if (document[cite.endOffset()] != char) {
+        if (cite.endOffset() >= document.textLength || document[cite.endOffset()] != char) {
             document.insertString(cite.endOffset(), char)
         }