mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
ReplaceWithTernaryOperatorFix: use template instead of selection (IDEA-202345)
This commit is contained in:
@@ -15,9 +15,12 @@
|
||||
*/
|
||||
package com.intellij.codeInspection;
|
||||
|
||||
import com.intellij.ide.SelectInEditorManager;
|
||||
import com.intellij.codeInsight.template.TemplateBuilder;
|
||||
import com.intellij.codeInsight.template.TemplateBuilderFactory;
|
||||
import com.intellij.codeInsight.template.impl.ConstantNode;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.pom.Navigatable;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
@@ -77,10 +80,14 @@ public class ReplaceWithTernaryOperatorFix implements LocalQuickFix {
|
||||
static void selectElseBranch(PsiFile file, PsiConditionalExpression conditionalExpression) {
|
||||
PsiExpression elseExpression = conditionalExpression.getElseExpression();
|
||||
if (elseExpression != null) {
|
||||
((Navigatable)elseExpression).navigate(true);
|
||||
SelectInEditorManager.getInstance(file.getProject())
|
||||
.selectInEditor(file.getVirtualFile(), elseExpression.getTextRange().getStartOffset(), elseExpression.getTextRange().getEndOffset(),
|
||||
false, true);
|
||||
Project project = file.getProject();
|
||||
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
|
||||
if (editor != null) {
|
||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
|
||||
TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(elseExpression);
|
||||
builder.replaceElement(elseExpression, new ConstantNode(elseExpression.getText()));
|
||||
builder.run(editor, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,6 @@ import java.lang.Integer;
|
||||
class A{
|
||||
void test(){
|
||||
Integer integer = null;
|
||||
int i = integer != null ? integer.toString().length() : <caret><selection>0</selection>;
|
||||
int i = integer != null ? integer.toString().length() : 0<caret>;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
class A{
|
||||
void test(@NotNull List l) {
|
||||
final List list = null;
|
||||
test(list != null ? list : <selection>null</selection>);
|
||||
test(list != null ? list : null<caret>);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,6 @@ import java.lang.Integer;
|
||||
class A{
|
||||
void test(){
|
||||
Integer integer = null;
|
||||
int i = integer != null ? integer.intValue() : <selection>0</selection>;
|
||||
int i = integer != null ? integer.intValue() : 0<caret>;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,6 @@
|
||||
class A{
|
||||
void test(){
|
||||
List list = null;
|
||||
Object o = list != null ? list.get(0) : <selection>null</selection>;
|
||||
Object o = list != null ? list.get(0) : null<caret>;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user