Merge remote-tracking branch 'origin/master'

This commit is contained in:
Vladimir.Orlov
2016-03-01 14:03:48 +03:00
3 changed files with 54 additions and 9 deletions
@@ -259,17 +259,17 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducer<GrIntr
boolean replaceAllOccurrences) {
EnumSet<GrIntroduceFieldSettings.Init> 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()};
@@ -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)) {
@@ -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(<selection>aaa + bbb</selection>)
'''
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(<selection>a + b</selection>)
''', EnumSet.of(CUR_METHOD)
doTestInitInTarget '''
def a = 1
def b = 2
println(<selection>a + b</selection>)
''', EnumSet.of(CUR_METHOD), ReplaceChoice.NO
doTestInitInTarget '''
def a = 1
def b = 2
def c = a + b
println(<selection>a + b</selection>)
''', EnumSet.of(CUR_METHOD)
doTestInitInTarget '''
def a = 1
def b = 2
def c = a + b
println(<selection>a + b</selection>)
''', 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()