From 249c9cf01d2ad9f80c78e12af487e606ce2b77d8 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Tue, 1 Mar 2016 13:55:55 +0300 Subject: [PATCH] [groovy] introduce field: disable initializing in field for script fields & do not use script class fqn for creating a reference to a introduced field (IDEA-152323) --- .../field/GrInplaceFieldIntroducer.java | 12 ++--- .../field/GrIntroduceFieldProcessor.java | 4 +- .../field/GrIntroduceFieldTest.groovy | 47 ++++++++++++++++++- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrInplaceFieldIntroducer.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrInplaceFieldIntroducer.java index 3f463677f5d3..d6057ad9bd5c 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrInplaceFieldIntroducer.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrInplaceFieldIntroducer.java @@ -259,17 +259,17 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducer result = EnumSet.noneOf(GrIntroduceFieldSettings.Init.class); - if (context.getExpression() != null || - context.getVar() != null && context.getVar().getInitializerGroovy() != null || - context.getStringPart() != null) { - result.add(GrIntroduceFieldSettings.Init.FIELD_DECLARATION); - } - if (!(context.getScope() instanceof GroovyScriptClass || context.getScope() instanceof GroovyFileBase)) { + if (context.getExpression() != null || + context.getVar() != null && context.getVar().getInitializerGroovy() != null || + context.getStringPart() != null) { + result.add(GrIntroduceFieldSettings.Init.FIELD_DECLARATION); + } result.add(GrIntroduceFieldSettings.Init.CONSTRUCTOR); } PsiElement scope = context.getScope(); + if (scope instanceof GroovyScriptClass) scope = scope.getContainingFile(); if (replaceAllOccurrences || context.getExpression() != null) { PsiElement[] occurrences = replaceAllOccurrences ? context.getOccurrences() : new PsiElement[]{context.getExpression()}; diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrIntroduceFieldProcessor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrIntroduceFieldProcessor.java index 35d3fab209f0..2dc0dd2746a5 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrIntroduceFieldProcessor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrIntroduceFieldProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -366,7 +366,7 @@ public class GrIntroduceFieldProcessor { private static GrReferenceExpression createRefExpression(@NotNull GrVariable field, @NotNull PsiElement place, @NotNull PsiClass containingClass) { - final String qname = containingClass.getQualifiedName(); + final String qname = containingClass instanceof GroovyScriptClass ? null : containingClass.getQualifiedName(); final String prefix = qname != null ? qname + "." : ""; final String refText; if (field.hasModifierProperty(PsiModifier.STATIC)) { diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrIntroduceFieldTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrIntroduceFieldTest.groovy index 3d6bbb3bd25c..66f347466df3 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrIntroduceFieldTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/introduce/field/GrIntroduceFieldTest.groovy @@ -544,6 +544,24 @@ class TestClass { ''', false, false, false, CONSTRUCTOR } + void 'test introduce field in script with invalid class name'() { + myFixture.configureByText "abcd-efgh.groovy", '''\ +def aaa = "foo" +def bbb = "bar" +println(aaa + bbb) +''' + performRefactoring(null, false, false, false, CUR_METHOD, false) + myFixture.checkResult '''\ +import groovy.transform.Field + +@Field f +def aaa = "foo" +def bbb = "bar" +f = aaa + bbb +println(f) +''' + } + void 'test cannot initialize in current method when introducing from field initializer'() { doTestInitInTarget ''' class A { @@ -596,6 +614,34 @@ class A { ''', EnumSet.of(CONSTRUCTOR, FIELD_DECLARATION, CUR_METHOD), ReplaceChoice.NO } + void 'test can initialize script field in current method only'() { + doTestInitInTarget ''' +def a = 1 +def b = 2 +println(a + b) +''', EnumSet.of(CUR_METHOD) + + doTestInitInTarget ''' +def a = 1 +def b = 2 +println(a + b) +''', EnumSet.of(CUR_METHOD), ReplaceChoice.NO + + doTestInitInTarget ''' +def a = 1 +def b = 2 +def c = a + b +println(a + b) +''', EnumSet.of(CUR_METHOD) + + doTestInitInTarget ''' +def a = 1 +def b = 2 +def c = a + b +println(a + b) +''', EnumSet.of(CUR_METHOD), ReplaceChoice.NO + } + private void doTest(final boolean isStatic, final boolean removeLocal, final boolean declareFinal, @@ -620,7 +666,6 @@ class A { myFixture.checkResult(textAfter); } - private void performRefactoring(String selectedType, boolean isStatic, boolean removeLocal, boolean declareFinal, GrIntroduceFieldSettings.Init initIn, boolean replaceAll) { final PsiType type = selectedType == null ? null : JavaPsiFacade.getElementFactory(project).createTypeFromText(selectedType, myFixture.file) def accessToken = WriteAction.start()