From 8a4487fa696616f3476bd2af9545b54b9884e8ff Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 14 Nov 2022 18:13:24 +0100 Subject: [PATCH] [spellchecker] ChangeTo: disable when document changed Fixes EA-654994 - IOOBE: CharSequenceSubSequence. GitOrigin-RevId: 22b0e05a72db4dd083bbcca816ba25994e9e2c14 --- .../spellchecker/quickfixes/ChangeTo.kt | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.kt b/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.kt index 0af0bcb9337d..eeeecaa1b53e 100644 --- a/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.kt +++ b/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.kt @@ -7,7 +7,7 @@ import com.intellij.codeInsight.intention.HighPriorityAction import com.intellij.codeInsight.intention.choice.ChoiceTitleIntentionAction import com.intellij.codeInsight.intention.choice.ChoiceVariantIntentionAction import com.intellij.codeInsight.intention.choice.DefaultIntentionActionWithChoice -import com.intellij.codeInsight.intention.impl.config.LazyEditor +import com.intellij.openapi.editor.Document import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.openapi.util.NlsSafe @@ -50,6 +50,7 @@ class ChangeTo(typo: String, element: PsiElement, private val range: TextRange) override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean { val suggestions = getSuggestions(project) if (suggestions.size <= index) return false + if (getRange(file.viewProvider.document) == null) return false suggestion = suggestions[index] return true } @@ -57,17 +58,22 @@ class ChangeTo(typo: String, element: PsiElement, private val range: TextRange) override fun applyFix(project: Project, file: PsiFile, editor: Editor?) { val suggestion = suggestion ?: return - val myEditor = editor ?: LazyEditor(file) + val document = file.viewProvider.document + val myRange = getRange(document) ?: return - val myElement = pointer.element ?: return - val myRange = range.shiftRight(myElement.startOffset) + UpdateHighlightersUtil.removeHighlightersWithExactRange(document, project, myRange) - val myText = myEditor.document.getText(myRange) - if (myText != typo) return + document.replaceString(myRange.startOffset, myRange.endOffset, suggestion) + } + + private fun getRange(document: Document): TextRange? { + val element = pointer.element ?: return null + val range = range.shiftRight(element.startOffset) + if (range.startOffset < 0 || range.endOffset > document.textLength) return null - UpdateHighlightersUtil.removeHighlightersWithExactRange(myEditor.document, project, myRange) - - myEditor.document.replaceString(myRange.startOffset, myRange.endOffset, suggestion) + val text = document.getText(range) + if (text != typo) return null + return range } override fun getFileModifierForPreview(target: PsiFile): FileModifier {