[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
This commit is contained in:
Tagir Valeev
2024-05-23 11:08:45 +00:00
committed by intellij-monorepo-bot
parent 941b59cc1a
commit cbc1f20f8e
16 changed files with 23 additions and 17 deletions
@@ -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));
}
}
}
@@ -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));
}
}
@@ -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
@@ -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));
}
}
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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));
}
}
@@ -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);
@@ -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))
}
}
@@ -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));
}
}
@@ -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) {
@@ -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)
}
}
@@ -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();
}
@@ -35,7 +35,7 @@ public class KotlinTryFinallySurrounder extends KotlinTrySurrounderBase<KtFinall
TextRange finallyBlockRange = expression.getTextRange();
int newLineOffset = finallyBlockRange.getStartOffset() + 2;
int offset = CodeStyleManager.getInstance(project).adjustLineIndent(document, newLineOffset);
navigator.moveCaretTo(offset);
navigator.select(TextRange.from(offset, 0));
psiDocumentManager.commitDocument(document);
}