Groovy: in-place introduce field from local var

This commit is contained in:
Max Medvedev
2014-02-10 09:07:51 +04:00
parent 48f76aeaa7
commit f3a4b5f7ee
2 changed files with 43 additions and 17 deletions
@@ -125,26 +125,35 @@ public class GrIntroduceFieldProcessor {
updateCaretPosition(occurrence);
return Collections.singletonList(occurrence);
}
else {
if (mySettings.replaceAllOccurrences()) {
GroovyRefactoringUtil.sortOccurrences(myContext.getOccurrences());
ArrayList<PsiElement> result = ContainerUtil.newArrayList();
for (PsiElement occurrence : myContext.getOccurrences()) {
result.add(replaceOccurrence(field, occurrence, targetClass));
}
return result;
if (mySettings.replaceAllOccurrences()) {
GroovyRefactoringUtil.sortOccurrences(myContext.getOccurrences());
ArrayList<PsiElement> 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.<PsiElement>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.<PsiElement>singletonList(expression);
}
else {
return Collections.singletonList(replaceOccurrence(field, expression, targetClass));
}
}
private void updateCaretPosition(@NotNull PsiElement occurrence) {
@@ -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 <selection>a = 5</selection>
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,