[java-intentions] IntroduceVariableErrorFixAction: preview-friendly

GitOrigin-RevId: 9db632643fe116f835b32ec42b1b03be917f9000
This commit is contained in:
Andrey Cherkasov
2023-01-19 16:48:28 +00:00
committed by intellij-monorepo-bot
parent 30d9784c64
commit 13c0dc41a1
3 changed files with 18 additions and 3 deletions
@@ -1,6 +1,7 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.java.JavaBundle;
import com.intellij.lang.LanguageRefactoringSupport;
@@ -10,6 +11,8 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.PreviewableRefactoringActionHandler;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.refactoring.introduceVariable.JavaIntroduceVariableHandlerBase;
import org.jetbrains.annotations.NotNull;
@@ -46,4 +49,16 @@ public class IntroduceVariableErrorFixAction extends LocalQuickFixAndIntentionAc
public @NotNull String getFamilyName() {
return getText();
}
@Override
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
PsiElement element = PsiTreeUtil.findSameElementInCopy(myStartElement.getElement(), file);
if (element == null) return IntentionPreviewInfo.EMPTY;
RefactoringActionHandler handler = LanguageRefactoringSupport.INSTANCE.forLanguage(JavaLanguage.INSTANCE).getIntroduceVariableHandler();
assert handler != null;
if (handler instanceof PreviewableRefactoringActionHandler previewableRefactoringActionHandler) {
return previewableRefactoringActionHandler.generatePreview(project, element);
}
return IntentionPreviewInfo.EMPTY;
}
}
@@ -1,4 +1,4 @@
// "Introduce local variable" "true"
// "Introduce local variable" "true-preview"
class a {
void a() {
list.add(new Runnable(){
@@ -1,4 +1,4 @@
// "Introduce local variable" "true"
// "Introduce local variable" "true-preview"
class a {
void a() {
list.add(new Runnable(){