From 7e751931e117e9a5cbae6c095bf64dd891ba124d Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 19 Jun 2019 09:47:48 +0200 Subject: [PATCH] introduce variable: enable inplace mode for non-physical subexpressions (IDEA-210792) GitOrigin-RevId: 378a1bb760cae47fe7641c2afb7d9215f76bd5cb --- .../lang/java/JavaRefactoringSupportProvider.java | 2 +- .../inplaceIntroduceVariable/fromSelection.java | 5 +++++ .../inplaceIntroduceVariable/fromSelection_after.java | 6 ++++++ .../java/refactoring/InplaceIntroduceVariableTest.java | 9 +++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromSelection.java create mode 100644 java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromSelection_after.java diff --git a/java/java-impl/src/com/intellij/lang/java/JavaRefactoringSupportProvider.java b/java/java-impl/src/com/intellij/lang/java/JavaRefactoringSupportProvider.java index 875cbd0766b3..7def0a7940d2 100644 --- a/java/java-impl/src/com/intellij/lang/java/JavaRefactoringSupportProvider.java +++ b/java/java-impl/src/com/intellij/lang/java/JavaRefactoringSupportProvider.java @@ -117,7 +117,7 @@ public class JavaRefactoringSupportProvider extends RefactoringSupportProvider { @Override public boolean isInplaceIntroduceAvailable(@NotNull PsiElement element, PsiElement context) { if (!(element instanceof PsiExpression)) return false; - if (context == null || context.getContainingFile() != element.getContainingFile()) return false; + if (context == null) return false; return true; } diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromSelection.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromSelection.java new file mode 100644 index 000000000000..1f2fd06cf9a0 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromSelection.java @@ -0,0 +1,5 @@ +class C { + { + String s = "bac"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromSelection_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromSelection_after.java new file mode 100644 index 000000000000..2df3a4945930 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromSelection_after.java @@ -0,0 +1,6 @@ +class C { + { + String a = "a"; + String s = "b" + a + "c"; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java index 1f3342c48a6c..06e45e78f70b 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/InplaceIntroduceVariableTest.java @@ -6,6 +6,7 @@ import com.intellij.codeInsight.template.impl.TemplateState; import com.intellij.lang.injection.InjectedLanguageManager; import com.intellij.openapi.actionSystem.IdeActions; import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.SelectionModel; import com.intellij.openapi.editor.actionSystem.EditorActionManager; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; @@ -28,6 +29,10 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe @Nullable @Override protected PsiExpression getExpressionFromEditor() { + SelectionModel selectionModel = getEditor().getSelectionModel(); + if (selectionModel.hasSelection()) { + return IntroduceVariableBase.getSelectedExpression(getProject(), getFile(), selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); + } final PsiExpression expression = super.getExpressionFromEditor(); if (expression != null) { return expression; @@ -45,6 +50,10 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe doTest(introducer -> type("expr")); } + public void testFromSelection() { + doTest(introducer -> type("a")); + } + public void testConflictingInnerClassName() { final JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject()); settings.INSERT_INNER_CLASS_IMPORTS = true;