mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-30 18:28:55 +07:00
IDEA-302967 Fix freeze in Features Suggester
Invoking CopyPasteManager methods during write action is not safe. GitOrigin-RevId: f3e032c111bbe8f8bceba67f87ea30a874e5b4b4
This commit is contained in:
committed by
intellij-monorepo-bot
parent
ceb2e26bfe
commit
60d7bbf49e
@@ -1,15 +1,12 @@
|
|||||||
package training.featuresSuggester.suggesters
|
package training.featuresSuggester.suggesters
|
||||||
|
|
||||||
import com.intellij.openapi.ide.CopyPasteManager
|
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import training.featuresSuggester.FeatureSuggesterBundle
|
import training.featuresSuggester.FeatureSuggesterBundle
|
||||||
import training.featuresSuggester.NoSuggestion
|
import training.featuresSuggester.NoSuggestion
|
||||||
import training.featuresSuggester.SuggesterSupport
|
import training.featuresSuggester.SuggesterSupport
|
||||||
import training.featuresSuggester.SuggestingUtils.asString
|
|
||||||
import training.featuresSuggester.Suggestion
|
import training.featuresSuggester.Suggestion
|
||||||
import training.featuresSuggester.actions.*
|
import training.featuresSuggester.actions.*
|
||||||
import training.util.WeakReferenceDelegator
|
import training.util.WeakReferenceDelegator
|
||||||
import java.awt.datatransfer.DataFlavor
|
|
||||||
|
|
||||||
class IntroduceVariableSuggester : AbstractFeatureSuggester() {
|
class IntroduceVariableSuggester : AbstractFeatureSuggester() {
|
||||||
override val id: String = "Introduce variable"
|
override val id: String = "Introduce variable"
|
||||||
@@ -49,7 +46,7 @@ class IntroduceVariableSuggester : AbstractFeatureSuggester() {
|
|||||||
when (action) {
|
when (action) {
|
||||||
is BeforeEditorTextRemovedAction -> {
|
is BeforeEditorTextRemovedAction -> {
|
||||||
with(action) {
|
with(action) {
|
||||||
val deletedText = getCopiedContent(textFragment.text) ?: return NoSuggestion
|
val deletedText = textFragment.text.takeIf { it.isNotBlank() }?.trim() ?: return NoSuggestion
|
||||||
val psiFile = this.psiFile ?: return NoSuggestion
|
val psiFile = this.psiFile ?: return NoSuggestion
|
||||||
val contentOffset = caretOffset + textFragment.text.indexOfFirst { it != ' ' && it != '\n' }
|
val contentOffset = caretOffset + textFragment.text.indexOfFirst { it != ' ' && it != '\n' }
|
||||||
val curElement = psiFile.findElementAt(contentOffset) ?: return NoSuggestion
|
val curElement = psiFile.findElementAt(contentOffset) ?: return NoSuggestion
|
||||||
@@ -60,6 +57,12 @@ class IntroduceVariableSuggester : AbstractFeatureSuggester() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
is EditorCutAction -> {
|
||||||
|
val data = extractedExprData ?: return NoSuggestion
|
||||||
|
if (data.exprText != action.text.trim()) {
|
||||||
|
extractedExprData = null
|
||||||
|
}
|
||||||
|
}
|
||||||
is ChildReplacedAction -> {
|
is ChildReplacedAction -> {
|
||||||
if (extractedExprData == null) return NoSuggestion
|
if (extractedExprData == null) return NoSuggestion
|
||||||
with(action) {
|
with(action) {
|
||||||
@@ -105,20 +108,6 @@ class IntroduceVariableSuggester : AbstractFeatureSuggester() {
|
|||||||
return NoSuggestion
|
return NoSuggestion
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getCopiedContent(text: String): String? {
|
|
||||||
if (text.isBlank()) return null
|
|
||||||
val content = text.trim()
|
|
||||||
val copyPasteManager = CopyPasteManager.getInstance()
|
|
||||||
return if (copyPasteManager.areDataFlavorsAvailable(DataFlavor.stringFlavor) &&
|
|
||||||
content == copyPasteManager.contents?.asString()?.trim()
|
|
||||||
) {
|
|
||||||
content
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun SuggesterSupport.isVariableDeclarationAdded(action: ChildReplacedAction): Boolean {
|
private fun SuggesterSupport.isVariableDeclarationAdded(action: ChildReplacedAction): Boolean {
|
||||||
return isExpressionStatement(action.oldChild) && isVariableDeclaration(action.newChild)
|
return isExpressionStatement(action.oldChild) && isVariableDeclaration(action.newChild)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user