mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
IDEA-231426 Inline pattern variable
GitOrigin-RevId: 8376adc5271b22d46cb50125b2d5243ab9e1a28e
This commit is contained in:
committed by
intellij-monorepo-bot
parent
d7869b5a91
commit
e542a2bf2f
@@ -897,6 +897,7 @@
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inline.InlineStaticImportHandler"/>
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inline.InlineConstantFieldHandler"/>
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inline.InlineLocalHandler"/>
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inline.InlinePatternVariableHandler"/>
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inline.InlineMethodHandler"/>
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inlineSuperClass.InlineSuperClassRefactoringHandler"/>
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inline.InlineToAnonymousClassHandler"/>
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.intellij.codeInsight.highlighting.HighlightManager;
|
||||
import com.intellij.codeInsight.hint.HintManager;
|
||||
import com.intellij.codeInsight.hint.HintManagerImpl;
|
||||
import com.intellij.codeInsight.intention.QuickFixFactory;
|
||||
import com.intellij.find.FindBundle;
|
||||
import com.intellij.openapi.actionSystem.Shortcut;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.ModalityState;
|
||||
@@ -32,12 +33,9 @@ import com.intellij.openapi.editor.colors.EditorColors;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsManager;
|
||||
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import com.intellij.openapi.keymap.Keymap;
|
||||
import com.intellij.openapi.keymap.KeymapManager;
|
||||
import com.intellij.openapi.keymap.KeymapUtil;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.wm.WindowManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.controlFlow.AnalysisCanceledException;
|
||||
@@ -102,10 +100,7 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
|
||||
if (query.findFirst() == null) {
|
||||
LOG.assertTrue(refExpr == null);
|
||||
ApplicationManager.getApplication().invokeLater(() -> {
|
||||
String message = RefactoringBundle.message("variable.is.never.used", localName);
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.INLINE_VARIABLE);
|
||||
}, ModalityState.NON_MODAL);
|
||||
showNoUsagesMessage(project, editor, localName);
|
||||
return;
|
||||
}
|
||||
query.forEach(psiReference -> {
|
||||
@@ -128,8 +123,7 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
}, "Find Usages", true, project)) {
|
||||
}, FindBundle.message("find.usages.dialog.title"), true, project)) {
|
||||
return;
|
||||
}
|
||||
final PsiCodeBlock containerBlock = PsiTreeUtil.getParentOfType(local, PsiCodeBlock.class);
|
||||
@@ -178,22 +172,8 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
|
||||
if (!BaseRefactoringProcessor.processConflicts(project, conflicts)) return;
|
||||
|
||||
final Ref<Boolean> inlineAll = new Ref<>(true);
|
||||
if (editor != null && !ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
int occurrencesCount = refsToInlineList.size();
|
||||
if (refExpr != null && EditorSettingsExternalizable.getInstance().isShowInlineLocalDialog()) {
|
||||
final InlineLocalDialog inlineLocalDialog = new InlineLocalDialog(project, local, refExpr, occurrencesCount);
|
||||
if (!inlineLocalDialog.showAndGet()) {
|
||||
WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (inlineLocalDialog.isInlineThis()) {
|
||||
refsToInlineList = Collections.singletonList(refExpr);
|
||||
inlineAll.set(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
boolean inlineAll = editor == null || askInlineAll(project, local, refExpr, refsToInlineList);
|
||||
if (refsToInlineList.isEmpty()) return;
|
||||
|
||||
final PsiElement[] refsToInline = PsiUtilCore.toPsiElementArray(refsToInlineList);
|
||||
|
||||
@@ -214,7 +194,9 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
if (refExpr != null && PsiUtil.isAccessedForReading(refExpr) && ArrayUtil.find(refsToInline, refExpr) < 0) {
|
||||
final PsiElement[] defs = DefUseUtil.getDefs(containerBlock, local, refExpr);
|
||||
LOG.assertTrue(defs.length > 0);
|
||||
highlightManager.addOccurrenceHighlights(editor, defs, attributes, true, null);
|
||||
if (editor != null) {
|
||||
highlightManager.addOccurrenceHighlights(editor, defs, attributes, true, null);
|
||||
}
|
||||
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.accessed.for.writing", localName));
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.INLINE_VARIABLE);
|
||||
WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
|
||||
@@ -248,8 +230,10 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
isSameDefinition &= isSameDefinition(def, defToInline);
|
||||
}
|
||||
if (!isSameDefinition) {
|
||||
highlightManager.addOccurrenceHighlights(editor, defs, writeAttributes, true, null);
|
||||
highlightManager.addOccurrenceHighlights(editor, new PsiElement[]{ref}, attributes, true, null);
|
||||
if (editor != null) {
|
||||
highlightManager.addOccurrenceHighlights(editor, defs, writeAttributes, true, null);
|
||||
highlightManager.addOccurrenceHighlights(editor, new PsiElement[]{ref}, attributes, true, null);
|
||||
}
|
||||
String message =
|
||||
RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.accessed.for.writing.and.used.with.inlined", localName));
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.INLINE_VARIABLE);
|
||||
@@ -260,7 +244,9 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
|
||||
final PsiElement writeAccess = checkRefsInAugmentedAssignmentOrUnaryModified(refsToInline, defToInline);
|
||||
if (writeAccess != null) {
|
||||
HighlightManager.getInstance(project).addOccurrenceHighlights(editor, new PsiElement[]{writeAccess}, writeAttributes, true, null);
|
||||
if (editor != null) {
|
||||
HighlightManager.getInstance(project).addOccurrenceHighlights(editor, new PsiElement[]{writeAccess}, writeAttributes, true, null);
|
||||
}
|
||||
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.accessed.for.writing", localName));
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.INLINE_VARIABLE);
|
||||
WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
|
||||
@@ -276,20 +262,15 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
final Runnable runnable = () -> {
|
||||
final String refactoringId = "refactoring.inline.local.variable";
|
||||
try {
|
||||
SmartPsiElementPointer<PsiExpression>[] exprs = new SmartPsiElementPointer[refsToInline.length];
|
||||
|
||||
RefactoringEventData beforeData = new RefactoringEventData();
|
||||
beforeData.addElements(refsToInline);
|
||||
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, beforeData);
|
||||
|
||||
WriteAction.run(() -> {
|
||||
final SmartPointerManager pointerManager = SmartPointerManager.getInstance(project);
|
||||
for (int idx = 0; idx < refsToInline.length; idx++) {
|
||||
PsiJavaCodeReferenceElement refElement = (PsiJavaCodeReferenceElement)refsToInline[idx];
|
||||
exprs[idx] = pointerManager.createSmartPsiElementPointer(InlineUtil.inlineVariable(local, defToInline, refElement));
|
||||
}
|
||||
List<SmartPsiElementPointer<PsiExpression>> exprs = WriteAction.compute(() -> {
|
||||
List<SmartPsiElementPointer<PsiExpression>> pointers = inlineOccurrences(project, local, defToInline, refsToInline);
|
||||
|
||||
if (inlineAll.get()) {
|
||||
if (inlineAll) {
|
||||
if (!isInliningVariableInitializer(defToInline)) {
|
||||
deleteInitializer(defToInline);
|
||||
}
|
||||
@@ -297,29 +278,14 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
defToInline.delete();
|
||||
}
|
||||
}
|
||||
return pointers;
|
||||
});
|
||||
|
||||
if (inlineAll.get() && ReferencesSearch.search(local).findFirst() == null && editor != null) {
|
||||
if (inlineAll && ReferencesSearch.search(local).findFirst() == null && editor != null) {
|
||||
QuickFixFactory.getInstance().createRemoveUnusedVariableFix(local).invoke(project, editor, local.getContainingFile());
|
||||
}
|
||||
|
||||
if (editor != null && !ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
highlightManager.addOccurrenceHighlights(editor, ContainerUtil.convert(exprs, new PsiExpression[refsToInline.length],
|
||||
pointer -> pointer.getElement()), attributes, true, null);
|
||||
if (exprs.length > 1) {
|
||||
Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
|
||||
Shortcut[] shortcuts = keymap.getShortcuts("FindNext");
|
||||
String message;
|
||||
if (shortcuts.length > 0) {
|
||||
message = "Press " + KeymapUtil.getShortcutText(shortcuts[0]) + " to go through " + exprs.length + " inlined occurrences";
|
||||
}
|
||||
else {
|
||||
message = exprs.length + " occurrences were inlined";
|
||||
}
|
||||
HintManagerImpl.getInstanceImpl().showInformationHint(editor, message, HintManager.UNDER);
|
||||
}
|
||||
WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
|
||||
}
|
||||
highlightOccurrences(project, editor, exprs);
|
||||
|
||||
WriteAction.run(() -> {
|
||||
for (SmartPsiElementPointer<PsiExpression> expr : exprs) {
|
||||
@@ -334,16 +300,85 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
}
|
||||
};
|
||||
|
||||
CommandProcessor.getInstance().executeCommand(project, () -> PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(runnable), RefactoringBundle.message("inline.command", localName), null);
|
||||
CommandProcessor.getInstance()
|
||||
.executeCommand(project, () -> PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(runnable),
|
||||
RefactoringBundle.message("inline.command", localName), null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static List<SmartPsiElementPointer<PsiExpression>> inlineOccurrences(@NotNull Project project,
|
||||
@NotNull PsiVariable local,
|
||||
PsiExpression defToInline,
|
||||
PsiElement[] refsToInline) {
|
||||
List<SmartPsiElementPointer<PsiExpression>> pointers = new ArrayList<>();
|
||||
final SmartPointerManager pointerManager = SmartPointerManager.getInstance(project);
|
||||
for (PsiElement element : refsToInline) {
|
||||
PsiJavaCodeReferenceElement refElement = (PsiJavaCodeReferenceElement)element;
|
||||
pointers.add(pointerManager.createSmartPsiElementPointer(InlineUtil.inlineVariable(local, defToInline, refElement)));
|
||||
}
|
||||
return pointers;
|
||||
}
|
||||
|
||||
static boolean askInlineAll(@NotNull Project project,
|
||||
@NotNull PsiVariable variable,
|
||||
@Nullable PsiReferenceExpression refExpr,
|
||||
@NotNull List<PsiElement> refsToInlineList) {
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) return true;
|
||||
int occurrencesCount = refsToInlineList.size();
|
||||
if (refExpr != null && EditorSettingsExternalizable.getInstance().isShowInlineLocalDialog()) {
|
||||
final InlineLocalDialog inlineLocalDialog = new InlineLocalDialog(project, variable, refExpr, occurrencesCount);
|
||||
if (!inlineLocalDialog.showAndGet()) {
|
||||
WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
|
||||
refsToInlineList.clear();
|
||||
return false;
|
||||
}
|
||||
else if (inlineLocalDialog.isInlineThis()) {
|
||||
refsToInlineList.clear();
|
||||
refsToInlineList.add(refExpr);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void showNoUsagesMessage(@NotNull Project project, Editor editor, String localName) {
|
||||
ApplicationManager.getApplication().invokeLater(() -> {
|
||||
String message = RefactoringBundle.message("variable.is.never.used", localName);
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.INLINE_VARIABLE);
|
||||
}, ModalityState.NON_MODAL);
|
||||
}
|
||||
|
||||
static void highlightOccurrences(@NotNull Project project,
|
||||
@Nullable Editor editor,
|
||||
@NotNull List<SmartPsiElementPointer<PsiExpression>> exprs) {
|
||||
final EditorColorsManager manager = EditorColorsManager.getInstance();
|
||||
final TextAttributes attributes = manager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
|
||||
if (editor != null && !ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
PsiExpression[] occurrences = ContainerUtil.map2Array(exprs, new PsiExpression[exprs.size()], pointer -> pointer.getElement());
|
||||
HighlightManager.getInstance(project).addOccurrenceHighlights(editor, occurrences, attributes, true, null);
|
||||
if (exprs.size() > 1) {
|
||||
Shortcut shortcut = KeymapUtil.getPrimaryShortcut("FindNext");
|
||||
String message;
|
||||
if (shortcut != null) {
|
||||
message = "Press " + KeymapUtil.getShortcutText(shortcut) + " to go through " + exprs.size() + " inlined occurrences";
|
||||
}
|
||||
else {
|
||||
message = exprs.size() + " occurrences were inlined";
|
||||
}
|
||||
HintManagerImpl.getInstanceImpl().showInformationHint(editor, message, HintManager.UNDER);
|
||||
}
|
||||
WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
|
||||
}
|
||||
}
|
||||
|
||||
private static void processWrappedAnalysisCanceledException(@NotNull Project project,
|
||||
Editor editor,
|
||||
RuntimeException e) {
|
||||
Throwable cause = e.getCause();
|
||||
if (cause instanceof AnalysisCanceledException) {
|
||||
CommonRefactoringUtil.showErrorHint(project, editor,
|
||||
RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("extract.method.control.flow.analysis.failed")),
|
||||
RefactoringBundle.getCannotRefactorMessage(
|
||||
RefactoringBundle.message("extract.method.control.flow.analysis.failed")),
|
||||
getRefactoringName(), HelpID.INLINE_VARIABLE);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
// Copyright 2000-2020 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.
|
||||
package com.intellij.refactoring.inline;
|
||||
|
||||
import com.intellij.codeInsight.TargetElementUtil;
|
||||
import com.intellij.codeInsight.highlighting.HighlightManager;
|
||||
import com.intellij.codeInsight.intention.QuickFixFactory;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.ModalityState;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.colors.EditorColors;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsManager;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.JavaPsiPatternUtil;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import com.intellij.refactoring.HelpID;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.listeners.RefactoringEventData;
|
||||
import com.intellij.refactoring.listeners.RefactoringEventListener;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class InlinePatternVariableHandler extends JavaInlineActionHandler {
|
||||
@Override
|
||||
public boolean canInlineElement(PsiElement element) {
|
||||
return element instanceof PsiPatternVariable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inlineElement(Project project, Editor editor, PsiElement element) {
|
||||
final PsiReference psiReference = TargetElementUtil.findReference(editor);
|
||||
final PsiReferenceExpression refExpr = psiReference instanceof PsiReferenceExpression ? (PsiReferenceExpression)psiReference : null;
|
||||
invoke(project, editor, (PsiPatternVariable)element, refExpr);
|
||||
}
|
||||
|
||||
public static void invoke(@NotNull final Project project,
|
||||
final Editor editor,
|
||||
@NotNull PsiPatternVariable pattern,
|
||||
PsiReferenceExpression refExpr) {
|
||||
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, pattern)) return;
|
||||
|
||||
String initializerText = JavaPsiPatternUtil.getEffectiveInitializerText(pattern);
|
||||
if (initializerText == null) {
|
||||
ApplicationManager.getApplication().invokeLater(() -> {
|
||||
String message = RefactoringBundle.message("cannot.perform.refactoring");
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.INLINE_VARIABLE);
|
||||
}, ModalityState.NON_MODAL);
|
||||
return;
|
||||
}
|
||||
List<PsiElement> refsToInlineList = new ArrayList<>(ReferencesSearch.search(pattern).mapping(PsiReference::getElement).findAll());
|
||||
String name = pattern.getName();
|
||||
if (refsToInlineList.isEmpty()) {
|
||||
InlineLocalHandler.showNoUsagesMessage(project, editor, name);
|
||||
return;
|
||||
}
|
||||
boolean inlineAll = editor == null || InlineLocalHandler.askInlineAll(project, pattern, refExpr, refsToInlineList);
|
||||
if (refsToInlineList.isEmpty()) return;
|
||||
final PsiElement[] refsToInline = PsiUtilCore.toPsiElementArray(refsToInlineList);
|
||||
PsiExpression defToInline = JavaPsiFacade.getElementFactory(project).createExpressionFromText(initializerText, pattern);
|
||||
|
||||
final EditorColorsManager manager = EditorColorsManager.getInstance();
|
||||
final TextAttributes attributes = manager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
|
||||
|
||||
if (editor != null && !ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
HighlightManager.getInstance(project).addOccurrenceHighlights(editor, refsToInline, attributes, true, null);
|
||||
}
|
||||
|
||||
final Runnable runnable = () -> {
|
||||
final String refactoringId = "refactoring.inline.pattern.variable";
|
||||
PsiElement scope = pattern.getDeclarationScope();
|
||||
try {
|
||||
RefactoringEventData beforeData = new RefactoringEventData();
|
||||
beforeData.addElements(refsToInline);
|
||||
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, beforeData);
|
||||
|
||||
List<SmartPsiElementPointer<PsiExpression>> exprs = WriteAction.compute(
|
||||
() -> InlineLocalHandler.inlineOccurrences(project, pattern, defToInline, refsToInline));
|
||||
|
||||
if (inlineAll && ReferencesSearch.search(pattern).findFirst() == null && editor != null) {
|
||||
QuickFixFactory.getInstance().createRemoveUnusedVariableFix(pattern).invoke(project, editor, pattern.getContainingFile());
|
||||
}
|
||||
|
||||
InlineLocalHandler.highlightOccurrences(project, editor, exprs);
|
||||
}
|
||||
finally {
|
||||
final RefactoringEventData afterData = new RefactoringEventData();
|
||||
afterData.addElement(scope);
|
||||
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, afterData);
|
||||
}
|
||||
};
|
||||
|
||||
CommandProcessor.getInstance()
|
||||
.executeCommand(project, () -> PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(runnable),
|
||||
RefactoringBundle.message("inline.command", name), null);
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getActionName(PsiElement element) {
|
||||
return getRefactoringName();
|
||||
}
|
||||
|
||||
private static String getRefactoringName() {
|
||||
return RefactoringBundle.message("inline.pattern.variable.title");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,9 +3,10 @@ package com.intellij.psi.util;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -33,6 +34,21 @@ public class JavaPsiPatternUtil {
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param variable pattern variable
|
||||
* @return effective initializer expression for the variable; null if cannot be determined
|
||||
*/
|
||||
public static @Nullable String getEffectiveInitializerText(@NotNull PsiPatternVariable variable) {
|
||||
PsiPattern pattern = variable.getPattern();
|
||||
if (pattern == null) return null;
|
||||
PsiInstanceOfExpression instanceOf = ObjectUtils.tryCast(pattern.getParent(), PsiInstanceOfExpression.class);
|
||||
if (instanceOf == null) return null;
|
||||
if (pattern instanceof PsiTypeTestPattern) {
|
||||
return "(" + ((PsiTypeTestPattern)pattern).getCheckType().getText() + ")" + instanceOf.getOperand().getText();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void collectPatternVariableCandidates(@NotNull PsiExpression scope, @NotNull PsiExpression expression,
|
||||
Collection<PsiPatternVariable> candidates) {
|
||||
while (true) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof String <caret>s) {
|
||||
System.out.println(s.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof String) {
|
||||
System.out.println(((String) obj).trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof String s) {
|
||||
System.out.println(<caret>s.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof String) {
|
||||
System.out.println(((String) obj).trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void test(boolean b, Object o1, Object o2) {
|
||||
if (!((b ? o1 : o2) instanceof String <caret>s)) return;
|
||||
System.out.println(s.isEmpty());
|
||||
System.out.println(s.trim().isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void test(boolean b, Object o1, Object o2) {
|
||||
if (!((b ? o1 : o2) instanceof String)) return;
|
||||
System.out.println(((String) (b ? o1 : o2)).isEmpty());
|
||||
System.out.println(((String) (b ? o1 : o2)).trim().isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright 2000-2020 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.
|
||||
package com.intellij.java.refactoring.inline;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.codeInsight.TargetElementUtil;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiLocalVariable;
|
||||
import com.intellij.psi.PsiPatternVariable;
|
||||
import com.intellij.psi.PsiReferenceExpression;
|
||||
import com.intellij.refactoring.inline.InlinePatternVariableHandler;
|
||||
import com.intellij.testFramework.LightJavaCodeInsightTestCase;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class InlinePatternVariableTest extends LightJavaCodeInsightTestCase {
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return JavaTestUtil.getJavaTestDataPath();
|
||||
}
|
||||
|
||||
public void testSimple() { doTest(); }
|
||||
public void testSimpleAtRef() { doTest(); }
|
||||
public void testTernary() { doTest(); }
|
||||
|
||||
@Override
|
||||
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
|
||||
return LightJavaCodeInsightFixtureTestCase.JAVA_14;
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
String name = getTestName(false);
|
||||
String fileName = "/refactoring/inlinePatternVariable/" + name + ".java";
|
||||
configureByFile(fileName);
|
||||
PsiElement element = TargetElementUtil
|
||||
.findTargetElement(getEditor(), TargetElementUtil.ELEMENT_NAME_ACCEPTED | TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED);
|
||||
if (element instanceof PsiPatternVariable) {
|
||||
InlinePatternVariableHandler.invoke(getProject(), getEditor(), (PsiPatternVariable)element, null);
|
||||
} else {
|
||||
assertTrue(element instanceof PsiReferenceExpression);
|
||||
PsiPatternVariable patternVariable = (PsiPatternVariable)((PsiReferenceExpression)element).resolve();
|
||||
InlinePatternVariableHandler.invoke(getProject(), getEditor(), patternVariable, (PsiReferenceExpression)element);
|
||||
}
|
||||
checkResultByFile(fileName + ".after");
|
||||
}
|
||||
}
|
||||
@@ -881,4 +881,6 @@ move.files=Move Files...
|
||||
move.file=Move File...
|
||||
|
||||
open.copy.in.editor=Open copy in editor
|
||||
name.is.not.a.valid.file.name=Name is not a valid file name
|
||||
name.is.not.a.valid.file.name=Name is not a valid file name
|
||||
|
||||
inline.pattern.variable.title=Inline Pattern Variable
|
||||
|
||||
Reference in New Issue
Block a user