From cbc1f20f8e4b92426e46702b42f487e9153281a9 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 22 May 2024 16:17:49 +0200 Subject: [PATCH] [mod-commands] Do not remove selection for all clients, instead use explicit 'select' command when necessary In some cases, original selection is used by the client, so we cannot unconditionally remove it Part of IJPL-149593 GitOrigin-RevId: 3780af1b1162082b2b2bb03c7f1533f865b1b6dd --- .../generation/surroundWith/JavaWithBlockSurrounder.java | 2 +- .../generation/surroundWith/JavaWithForSurrounder.java | 2 +- .../surroundWith/JavaWithIfElseExpressionSurrounder.java | 2 +- .../surroundWith/JavaWithIfExpressionSurrounder.java | 2 +- .../surroundWith/JavaWithNotInstanceofSurrounder.java | 2 +- .../generation/surroundWith/JavaWithNotSurrounder.java | 3 ++- .../generation/surroundWith/JavaWithNullCheckSurrounder.java | 3 ++- .../surroundWith/JavaWithParenthesesSurrounder.java | 3 ++- .../surroundWith/JavaWithSynchronizedSurrounder.java | 2 +- .../generation/surroundWith/SurroundWithHandler.java | 1 - .../surroundWith/expression/KotlinNotSurrounder.kt | 4 ++-- .../surroundWith/expression/KotlinParenthesesSurrounder.java | 3 ++- .../expression/KotlinStringTemplateSurrounder.java | 3 ++- .../surroundWith/expression/KotlinWhenSurrounder.kt | 3 ++- .../surroundWith/statement/KotlinIfSurrounderBase.java | 3 ++- .../surroundWith/statement/KotlinTryFinallySurrounder.java | 2 +- 16 files changed, 23 insertions(+), 17 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithBlockSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithBlockSurrounder.java index 23053483c233..5909252fd823 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithBlockSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithBlockSurrounder.java @@ -60,7 +60,7 @@ public class JavaWithBlockSurrounder extends JavaStatementsModCommandSurrounder PsiElement firstChild = blockStatement.getFirstChild(); if (firstChild != null) { TextRange range = firstChild.getTextRange(); - updater.moveCaretTo(range.getEndOffset()); + updater.select(TextRange.from(range.getEndOffset(), 0)); } } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithForSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithForSurrounder.java index ce9fb5482c2d..f59440361dae 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithForSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithForSurrounder.java @@ -68,6 +68,6 @@ public class JavaWithForSurrounder extends JavaStatementsModCommandSurrounder { if (update == null) return; TextRange range3 = update.getTextRange(); update.getContainingFile().getFileDocument().deleteString(range1.getStartOffset(), range3.getEndOffset()); - updater.moveCaretTo(range1.getStartOffset()); + updater.select(TextRange.from(range1.getStartOffset(), 0)); } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfElseExpressionSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfElseExpressionSurrounder.java index 980b4ba597d1..8471ef13b983 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfElseExpressionSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfElseExpressionSurrounder.java @@ -52,7 +52,7 @@ public class JavaWithIfElseExpressionSurrounder extends JavaWithIfExpressionSurr TextRange range = afterStatement.getTextRange(); afterStatement.getContainingFile().getFileDocument().deleteString(range.getStartOffset(), range.getEndOffset()); - updater.moveCaretTo(range.getStartOffset()); + updater.select(TextRange.from(range.getStartOffset(), 0)); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfExpressionSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfExpressionSurrounder.java index fcc62faa0f31..99ddfc3acf50 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfExpressionSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithIfExpressionSurrounder.java @@ -69,7 +69,7 @@ public class JavaWithIfExpressionSurrounder extends JavaExpressionModCommandSurr block = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(block); TextRange range = block.getStatements()[0].getTextRange(); block.getContainingFile().getFileDocument().deleteString(range.getStartOffset(), range.getEndOffset()); - updater.moveCaretTo(range.getStartOffset()); + updater.select(TextRange.from(range.getStartOffset(), 0)); } } diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNotInstanceofSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNotInstanceofSurrounder.java index e97ac5fac8c8..7bd86e6147d9 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNotInstanceofSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNotInstanceofSurrounder.java @@ -54,7 +54,7 @@ class JavaWithNotInstanceofSurrounder extends JavaExpressionModCommandSurrounder instanceofExpr = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(instanceofExpr); TextRange range = instanceofExpr.getCheckType().getTextRange(); instanceofExpr.getContainingFile().getFileDocument().deleteString(range.getStartOffset(), range.getEndOffset()); - updater.moveCaretTo(range.getStartOffset()); + updater.select(TextRange.from(range.getStartOffset(), 0)); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNotSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNotSurrounder.java index a2912c13367f..3c497b6a13b8 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNotSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNotSurrounder.java @@ -20,6 +20,7 @@ import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.modcommand.ActionContext; import com.intellij.modcommand.ModPsiUpdater; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.refactoring.IntroduceVariableUtil; @@ -37,7 +38,7 @@ public class JavaWithNotSurrounder extends JavaExpressionModCommandSurrounder { ((PsiParenthesizedExpression)prefixExpr.getOperand()).getExpression().replace(expr); expr = (PsiExpression)IntroduceVariableUtil.replace(expr, prefixExpr, project); int offset = expr.getTextRange().getEndOffset(); - updater.moveCaretTo(offset); + updater.select(TextRange.from(offset, 0)); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNullCheckSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNullCheckSurrounder.java index bf7a50e5ad2c..90299a123dff 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNullCheckSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithNullCheckSurrounder.java @@ -19,6 +19,7 @@ import com.intellij.codeInsight.CodeInsightUtilCore; import com.intellij.java.JavaBundle; import com.intellij.modcommand.ActionContext; import com.intellij.modcommand.ModPsiUpdater; +import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.util.FileTypeUtils; @@ -58,7 +59,7 @@ public class JavaWithNullCheckSurrounder extends JavaExpressionModCommandSurroun block = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(block); PsiElement replace = block.getStatements()[0].replace(factory.createStatementFromText(oldText, block)); int offset = replace.getTextRange().getEndOffset(); - updater.moveCaretTo(offset); + updater.select(TextRange.from(offset, 0)); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithParenthesesSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithParenthesesSurrounder.java index 81466d07afd6..fad83ace8ebd 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithParenthesesSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithParenthesesSurrounder.java @@ -19,6 +19,7 @@ import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.modcommand.ActionContext; import com.intellij.modcommand.ModPsiUpdater; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.refactoring.IntroduceVariableUtil; @@ -41,7 +42,7 @@ public class JavaWithParenthesesSurrounder extends JavaExpressionModCommandSurro parenthExpr.getExpression().replace(expr); expr = (PsiExpression)IntroduceVariableUtil.replace(expr, parenthExpr, project); int offset = expr.getTextRange().getEndOffset(); - updater.moveCaretTo(offset); + updater.select(TextRange.from(offset, 0)); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithSynchronizedSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithSynchronizedSurrounder.java index 21c6a3e19cca..e1b8377ffdf6 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithSynchronizedSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithSynchronizedSurrounder.java @@ -63,6 +63,6 @@ public class JavaWithSynchronizedSurrounder extends JavaStatementsModCommandSurr if (lockExpression == null) return; TextRange range = lockExpression.getTextRange(); lockExpression.getContainingFile().getFileDocument().deleteString(range.getStartOffset(), range.getEndOffset()); - updater.moveCaretTo(range.getStartOffset()); + updater.select(TextRange.from(range.getStartOffset(), 0)); } } \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java index 324ad93facee..8cb2bebb4916 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java @@ -242,7 +242,6 @@ public final class SurroundWithHandler implements CodeInsightActionHandler { ); } else { CommandProcessor.getInstance().executeCommand(project, () -> { - editor.getSelectionModel().removeSelection(); TextRange range = ReadAction.compute(() -> surrounder.surroundElements(project, editor, elements)); if (!(surrounder instanceof ModCommandSurrounder)) { updateRange(project, editor, range, line, col); diff --git a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinNotSurrounder.kt b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinNotSurrounder.kt index a3f4635a6f2f..3e6c2767507f 100644 --- a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinNotSurrounder.kt +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinNotSurrounder.kt @@ -2,9 +2,9 @@ package org.jetbrains.kotlin.idea.codeInsight.surroundWith.expression import com.intellij.codeInsight.CodeInsightBundle -import com.intellij.codeInsight.CodeInsightUtilBase import com.intellij.modcommand.ActionContext import com.intellij.modcommand.ModPsiUpdater +import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.analysis.api.KtAllowAnalysisOnEdt import org.jetbrains.kotlin.analysis.api.analyze import org.jetbrains.kotlin.analysis.api.lifetime.allowAnalysisOnEdt @@ -41,6 +41,6 @@ class KotlinNotSurrounder : KotlinExpressionSurrounder() { ) expressionWithoutParentheses.replace(expression) val expr = expression.replace(prefixExpr) as KtExpression - updater.moveCaretTo(expr.textRange.endOffset) + updater.select(TextRange.from(expr.textRange.endOffset, 0)) } } \ No newline at end of file diff --git a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinParenthesesSurrounder.java b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinParenthesesSurrounder.java index f49a918ff004..557b545d605d 100644 --- a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinParenthesesSurrounder.java +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinParenthesesSurrounder.java @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.idea.codeInsight.surroundWith.expression; import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.modcommand.ActionContext; import com.intellij.modcommand.ModPsiUpdater; +import com.intellij.openapi.util.TextRange; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.idea.codeInsight.surroundWith.KotlinExpressionSurrounder; import org.jetbrains.kotlin.psi.KtExpression; @@ -30,6 +31,6 @@ public class KotlinParenthesesSurrounder extends KotlinExpressionSurrounder { expression = (KtExpression) expression.replace(parenthesizedExpression); int offset = expression.getTextRange().getEndOffset(); - updater.moveCaretTo(offset); + updater.select(TextRange.from(offset, 0)); } } diff --git a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinStringTemplateSurrounder.java b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinStringTemplateSurrounder.java index b612774d6399..a3467e8ada16 100644 --- a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinStringTemplateSurrounder.java +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinStringTemplateSurrounder.java @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.idea.codeInsight.surroundWith.expression; import com.intellij.modcommand.ActionContext; import com.intellij.modcommand.ModPsiUpdater; import com.intellij.openapi.util.NlsSafe; +import com.intellij.openapi.util.TextRange; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.idea.codeInsight.surroundWith.KotlinExpressionSurrounder; import org.jetbrains.kotlin.psi.*; @@ -34,7 +35,7 @@ public class KotlinStringTemplateSurrounder extends KotlinExpressionSurrounder { expression = (KtExpression) expression.replace(stringTemplateExpression); int offset = expression.getTextRange().getEndOffset(); - updater.moveCaretTo(offset); + updater.select(TextRange.from(offset, 0)); } private static String getCodeTemplate(KtExpression expression) { diff --git a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinWhenSurrounder.kt b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinWhenSurrounder.kt index b897d58957b2..4156c9320541 100644 --- a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinWhenSurrounder.kt +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/expression/KotlinWhenSurrounder.kt @@ -4,6 +4,7 @@ package org.jetbrains.kotlin.idea.codeInsight.surroundWith.expression import com.intellij.modcommand.ActionContext import com.intellij.modcommand.ModPsiUpdater import com.intellij.openapi.util.NlsSafe +import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiDocumentManager import com.intellij.psi.util.startOffset import org.jetbrains.kotlin.analysis.api.KtAllowAnalysisOnEdt @@ -66,7 +67,7 @@ class KotlinWhenSurrounder : KotlinExpressionSurrounder() { document.deleteString(textRange.startOffset, textRange.endOffset) offset } - updater.moveCaretTo(offset) + updater.select(TextRange.from(offset, 0)) psiDocumentManager.commitDocument(document) } } diff --git a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java index 766a1de54387..02d693f82788 100644 --- a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinIfSurrounderBase.java @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.idea.codeInsight.surroundWith.statement; import com.intellij.modcommand.ActionContext; import com.intellij.modcommand.ModPsiUpdater; +import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.idea.codeInsight.surroundWith.MoveDeclarationsOutHelperKt; @@ -52,7 +53,7 @@ public abstract class KotlinIfSurrounderBase extends KotlinStatementsSurrounder KtExpression condition = ifExpression.getCondition(); assert condition != null : "Condition should exists for created if expression: " + ifExpression.getText(); // Delete condition from created if - updater.moveCaretTo(condition.getTextOffset()); + updater.select(TextRange.from(condition.getTextOffset(), 0)); condition.delete(); } diff --git a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTryFinallySurrounder.java b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTryFinallySurrounder.java index a2936c52ce6b..e38f077c63c5 100644 --- a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTryFinallySurrounder.java +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeInsight/surroundWith/statement/KotlinTryFinallySurrounder.java @@ -35,7 +35,7 @@ public class KotlinTryFinallySurrounder extends KotlinTrySurrounderBase