[groovy] allow to select 'this' reference for 'introduce X' refactoring
authorDaniil Ovchinnikov <daniil.ovchinnikov@jetbrains.com>
Wed, 26 Oct 2016 10:50:11 +0000 (13:50 +0300)
committerDaniil Ovchinnikov <daniil.ovchinnikov@jetbrains.com>
Wed, 26 Oct 2016 11:13:26 +0000 (14:13 +0300)
plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/GrIntroduceHandlerBase.java
plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrIntroduceFieldTest.groovy

index 7699ba8fd5e97b360290ed1f42956f9ee07c7b64..c9f5b5258e74f5d7a2d6522a9c75dce50e9839e6 100644 (file)
@@ -210,9 +210,6 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
       if (expression instanceof GrParenthesizedExpression && !expressions.contains(((GrParenthesizedExpression)expression).getOperand())) {
         expressions.add(((GrParenthesizedExpression)expression).getOperand());
       }
-      if (expression.getParent() instanceof GrReferenceExpression
-          && expression instanceof GrReferenceExpression
-          && ((GrReferenceExpression)expression).resolve() instanceof PsiClass) continue;
       if (expressionIsIncorrect(expression, acceptVoidCalls)) continue;
 
       expressions.add(expression);
@@ -229,7 +226,9 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
       final PsiElement resolved = resolveResult.getElement();
       return resolved instanceof PsiMethod && !resolveResult.isInvokedOnProperty() || resolved instanceof PsiClass;
     }
-
+    if (expression instanceof GrReferenceExpression && expression.getParent() instanceof GrReferenceExpression) {
+      return !PsiUtil.isThisReference(expression) && ((GrReferenceExpression)expression).resolve() instanceof PsiClass;
+    }
     if (expression instanceof GrClosableBlock && expression.getParent() instanceof GrStringInjection) return true;
     if (!acceptVoidCalls && expression instanceof GrMethodCall && PsiType.VOID.equals(expression.getType())) return true;
 
@@ -282,7 +281,7 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
       if (expressions.isEmpty()) {
         updateSelectionForVariable(editor, file, selectionModel, offset);
       }
-      else if (expressions.size() == 1) {
+      else if (expressions.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
         final TextRange textRange = expressions.get(0).getTextRange();
         selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
       }
index e8786a904721ffa470fc34af86928116c6bb136f..76d07f61c204d0e6d06e203d232668812e6f49a8 100644 (file)
@@ -642,6 +642,26 @@ println(<selection>a + b</selection>)
 ''', EnumSet.of(CUR_METHOD), ReplaceChoice.NO
   }
 
+  void 'test introduce field from this'() {
+    doTest '''\
+class A {
+    def bar 
+    def foo() {
+        th<caret>is.bar
+    }
+}
+''', '''\
+class A {
+    def bar
+    def f = this
+
+    def foo() {
+        f.bar
+    }
+}
+''', false, false, false, FIELD_DECLARATION
+  }
+
   private void doTest(final boolean isStatic,
                       final boolean removeLocal,
                       final boolean declareFinal,