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 742c7cde76a7..d48ad50d2bbf 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 @@ -125,26 +125,35 @@ public class GrIntroduceFieldProcessor { updateCaretPosition(occurrence); return Collections.singletonList(occurrence); } - else { - if (mySettings.replaceAllOccurrences()) { - GroovyRefactoringUtil.sortOccurrences(myContext.getOccurrences()); - ArrayList result = ContainerUtil.newArrayList(); - for (PsiElement occurrence : myContext.getOccurrences()) { - result.add(replaceOccurrence(field, occurrence, targetClass)); - } - return result; + + if (mySettings.replaceAllOccurrences()) { + GroovyRefactoringUtil.sortOccurrences(myContext.getOccurrences()); + ArrayList result = ContainerUtil.newArrayList(); + for (PsiElement occurrence : myContext.getOccurrences()) { + result.add(replaceOccurrence(field, occurrence, targetClass)); + } + return result; + } + + GrVariable var = myContext.getVar(); + if (var != null) { + GrExpression initializer = var.getInitializerGroovy(); + if (initializer != null) { + return Collections.singletonList(replaceOccurrence(field, initializer, targetClass)); } else { - final GrExpression expression = myContext.getExpression(); - assert expression != null; - if (PsiUtil.isExpressionStatement(expression)) { - return Collections.singletonList(expression); - } - else { - return Collections.singletonList(replaceOccurrence(field, expression, targetClass)); - } + return Collections.emptyList(); } } + + final GrExpression expression = myContext.getExpression(); + assert expression != null; + if (PsiUtil.isExpressionStatement(expression)) { + return Collections.singletonList(expression); + } + else { + return Collections.singletonList(replaceOccurrence(field, expression, targetClass)); + } } private void updateCaretPosition(@NotNull PsiElement occurrence) { 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 911f3a4118ab..945eb1fd6287 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 @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -328,6 +328,23 @@ class A { ''', false, false, false, CUR_METHOD, false, null) } + void testFromVar() { + doTest('''\ +class A { + def foo() { + def a = 5 + print a + } +}''', '''\ +class A { + def f = 5 + + def foo() { + print f + } +}''', false, true, false, FIELD_DECLARATION, true, null) + } + private void doTest(final boolean isStatic, final boolean removeLocal,