[java-intentions] CreateLocalVarFromInstanceofAction: exact preview+tests

GitOrigin-RevId: acf0c175cda663c43d5feb5b503ed462c5acf92d
This commit is contained in:
Tagir Valeev
2022-07-27 15:18:07 +02:00
committed by intellij-monorepo-bot
parent 7e76f57f5b
commit c642b9a580
39 changed files with 64 additions and 51 deletions

View File

@@ -200,11 +200,7 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
List<String> names = new VariableNameGenerator(initializer, VariableKind.LOCAL_VARIABLE).byExpression(initializer)
.byType(localVariable.getType()).generateAll(true);
PsiIdentifier identifier = Objects.requireNonNull(localVariable.getNameIdentifier());
if (!file.isPhysical()) {
identifier.replace(JavaPsiFacade.getElementFactory(project).createIdentifier(names.get(0)));
return;
}
TemplateBuilderImpl builder = new TemplateBuilderImpl(localVariable);
builder.setEndVariableAfter(localVariable.getNameIdentifier());
@@ -255,7 +251,7 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
@Override
public void templateFinished(@NotNull Template template, boolean brokenOff) {
ApplicationManager.getApplication().runWriteAction(() -> {
Runnable action = () -> {
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
CaretModel caretModel = editor.getCaretModel();
@@ -264,8 +260,13 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
if (declarationStatement != null) {
caretModel.moveToOffset(declarationStatement.getTextRange().getEndOffset());
}
new EnterAction().actionPerformed(editor, DataManager.getInstance().getDataContext());
});
new EnterAction().getHandler().execute(editor, null, DataManager.getInstance().getDataContext());
};
if (file.isPhysical()) {
ApplicationManager.getApplication().runWriteAction(action);
} else {
action.run();
}
}
});
}

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)this' declaration" "true"
// "Insert '(Runnable)this' declaration" "true-preview"
class C {
void f() {
if (this instanceof Runnable) {

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)this' declaration" "true"
// "Insert '(Runnable)this' declaration" "true-preview"
class C {
void f() {
if (!(this instanceof Runnable)) {

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)this' declaration" "true"
// "Insert '(Runnable)this' declaration" "true-preview"
class C {
void f() {
while (!(this instanceof Runnable)) {

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)this' declaration" "true"
// "Insert '(Runnable)this' declaration" "true-preview"
class C {
void f() {
while (this instanceof Runnable) {

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)this' declaration" "true"
// "Insert '(Runnable)this' declaration" "true-preview"
class C {
void f() {
if (this instanceof Runnable) {

View File

@@ -1,4 +1,4 @@
// "Insert '(String)s' declaration" "true"
// "Insert '(String)s' declaration" "true-preview"
class C {
void f() {
String s = "";

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class C {
void f(Object o) {
if (o instanceof String) {

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class X {
void foo(Object o) {
if (o instanceof String) {

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class C {
void f(Object o, Object f) {
if (o instanceof String) {

View File

@@ -1,4 +1,4 @@
// "Insert '(IOException)o' declaration" "true"
// "Insert '(IOException)o' declaration" "true-preview"
import java.io.IOException;
class C {

View File

@@ -1,4 +1,4 @@
// "Insert '(NodeInfo)parent' declaration" "true"
// "Insert '(NodeInfo)parent' declaration" "true-preview"
public abstract class A {
public void getNodeElements(Object parent) {
if (!(parent instanceof NodeInfo)) return;

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class X {
void foo(Object o) {
if (o instanceof String) {

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class C {
void f(Object o, Object f) {
if (o instanceof String && f == null) {

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class C {
void f(Object o, Object f) {
if (o instanceof String) {//todo comment

View File

@@ -1,4 +1,4 @@
// "Insert '(String)f' declaration" "true"
// "Insert '(String)f' declaration" "true-preview"
class C {
void f(Object o, Object f) {
if (o instanceof String && f instanceof String) {

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)o' declaration" "true"
// "Insert '(Runnable)o' declaration" "true-preview"
class C {
void f(Object o) {
if (o instanceof Runnable) {

View File

@@ -1,4 +1,4 @@
// "Insert '(String)x' declaration" "true"
// "Insert '(String)x' declaration" "true-preview"
class C {
Object s = new Object();

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)this' declaration" "true"
// "Insert '(Runnable)this' declaration" "true-preview"
class C {
void f() {
if (this <caret>instanceof Runnable)

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)this' declaration" "true"
// "Insert '(Runnable)this' declaration" "true-preview"
class C {
void f() {
if (!(this instanceof <caret>Runnable)) {

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)this' declaration" "true"
// "Insert '(Runnable)this' declaration" "true-preview"
class C {
void f() {
while (!(<caret>this instanceof Runnable)) {

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)this' declaration" "true"
// "Insert '(Runnable)this' declaration" "true-preview"
class C {
void f() {
while (<caret>this instanceof Runnable

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)this' declaration" "true"
// "Insert '(Runnable)this' declaration" "true-preview"
class C {
void f() {
if (this instanceof Runnable) { <caret>

View File

@@ -1,4 +1,4 @@
// "Insert '(String)s' declaration" "true"
// "Insert '(String)s' declaration" "true-preview"
class C {
void f() {
String s = "";

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class C {
void f(Object o) {
if (o instanceof String<caret>)

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class X {
void foo(Object o) {
if (o insta<caret>nceof String) {

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class C {
void f(Object o, Object f) {
if (<caret>o instanceof String) {

View File

@@ -1,4 +1,4 @@
// "Insert '(IOException)o' declaration" "true"
// "Insert '(IOException)o' declaration" "true-preview"
import java.io.IOException;
class C {

View File

@@ -1,4 +1,4 @@
// "Insert '(NodeInfo)parent' declaration" "true"
// "Insert '(NodeInfo)parent' declaration" "true-preview"
public abstract class A {
public void getNodeElements(Object parent) {
if (!(parent instanceof <caret>NodeInfo)) return;

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class X {
void foo(Object o) {
if (o insta<caret>nceof String) {

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class C {
void f(Object o, Object f) {
if (o instanceof String && f == null) {

View File

@@ -1,4 +1,4 @@
// "Insert '(String)o' declaration" "true"
// "Insert '(String)o' declaration" "true-preview"
class C {
void f(Object o, Object f) {
if (o instanceof String) {//todo comment

View File

@@ -1,4 +1,4 @@
// "Insert '(String)f' declaration" "true"
// "Insert '(String)f' declaration" "true-preview"
class C {
void f(Object o, Object f) {
if (o instanceof String && f<caret> instanceof String) {

View File

@@ -1,4 +1,4 @@
// "Insert '(Runnable)o' declaration" "true"
// "Insert '(Runnable)o' declaration" "true-preview"
class C {
void f(Object o) {
if (o instanceof Runnable) {

View File

@@ -1,4 +1,4 @@
// "Insert '(String)x' declaration" "true"
// "Insert '(String)x' declaration" "true-preview"
class C {
Object s = new Object();

View File

@@ -21,6 +21,8 @@ internal class IntentionPreviewEditor(psiFileCopy: PsiFile, caretOffset: Int, pr
override fun isViewer(): Boolean = true
override fun isOneLineMode(): Boolean = false
override fun getSettings(): EditorSettings {
return settings
}

View File

@@ -15,6 +15,7 @@
*/
package com.intellij.openapi.editor.actionSystem;
import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.*;
@@ -39,8 +40,11 @@ public abstract class EditorWriteActionHandler extends EditorActionHandler {
@Override
public void doExecute(@NotNull final Editor editor, @Nullable final Caret caret, final DataContext dataContext) {
if (!EditorModificationUtil.checkModificationAllowed(editor)) return;
if (!ApplicationManager.getApplication().isWriteAccessAllowed() && !EditorModificationUtil.requestWriting(editor)) return;
boolean preview = IntentionPreviewUtils.getPreviewEditor() == editor;
if (!preview) {
if (!EditorModificationUtil.checkModificationAllowed(editor)) return;
if (!ApplicationManager.getApplication().isWriteAccessAllowed() && !EditorModificationUtil.requestWriting(editor)) return;
}
DocumentRunnable runnable = new DocumentRunnable(editor.getDocument(), editor.getProject()) {
@Override
@@ -59,7 +63,7 @@ public abstract class EditorWriteActionHandler extends EditorActionHandler {
}
}
};
if (editor instanceof TextComponentEditor) {
if (preview || editor instanceof TextComponentEditor) {
runnable.run();
}
else {

View File

@@ -2,6 +2,7 @@
package com.intellij.openapi.editor.actions;
import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.*;
@@ -22,7 +23,9 @@ public class EnterAction extends EditorAction implements LatencyAwareEditorActio
private static class Handler extends EditorWriteActionHandler.ForEachCaret {
@Override
public void executeWriteAction(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) {
CommandProcessor.getInstance().setCurrentCommandName(EditorBundle.message("typing.command.name"));
if (IntentionPreviewUtils.getPreviewEditor() != editor) {
CommandProcessor.getInstance().setCurrentCommandName(EditorBundle.message("typing.command.name"));
}
insertNewLineAtCaret(editor);
}

View File

@@ -7,6 +7,7 @@ import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.EditorComponentImpl;
import com.intellij.openapi.editor.impl.ImaginaryEditor;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
import com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl;
@@ -74,11 +75,13 @@ public class TestDataProvider implements DataProvider {
if (managerData != null) {
return managerData;
}
JComponent component = editor.getContentComponent();
if (component instanceof EditorComponentImpl) {
Object editorComponentData = ((EditorComponentImpl)component).getData(dataId);
if (editorComponentData != null) {
return editorComponentData;
if (!(editor instanceof ImaginaryEditor)) {
JComponent component = editor.getContentComponent();
if (component instanceof EditorComponentImpl) {
Object editorComponentData = ((EditorComponentImpl)component).getData(dataId);
if (editorComponentData != null) {
return editorComponentData;
}
}
}
}