IDEA-106991 introduce parameter to closure from selected part of String literal

This commit is contained in:
Max Medvedev
2013-05-21 15:33:16 +04:00
parent e76bc56485
commit 2b5d97df47
2 changed files with 60 additions and 10 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -17,6 +17,7 @@ package org.jetbrains.plugins.groovy.refactoring.introduce.parameter;
import com.intellij.codeInsight.ChangeContextUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.*;
@@ -24,6 +25,7 @@ import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.search.searches.MethodReferencesSearch;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.refactoring.BaseRefactoringProcessor;
import com.intellij.refactoring.IntroduceParameterRefactoring;
import com.intellij.refactoring.RefactoringBundle;
@@ -68,6 +70,8 @@ import org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureU
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle;
import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil;
import org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceHandlerBase;
import org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo;
import org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.FieldConflictsResolver;
import org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.OldReferencesResolver;
import org.jetbrains.plugins.groovy.refactoring.util.AnySupers;
@@ -95,7 +99,12 @@ public class GrIntroduceClosureParameterProcessor extends BaseRefactoringProcess
toReplaceIn = (GrClosableBlock)mySettings.getToReplaceIn();
toSearchFor = mySettings.getToSearchFor();
myParameterInitializer = new GrExpressionWrapper(mySettings.getExpression());
final StringPartInfo info = settings.getStringPartInfo();
final GrExpression expression = info != null ?
GrIntroduceHandlerBase.generateExpressionFromStringPart(info, settings.getProject()) :
mySettings.getExpression();
myParameterInitializer = new GrExpressionWrapper(expression);
}
@NotNull
@@ -303,6 +312,16 @@ public class GrIntroduceClosureParameterProcessor extends BaseRefactoringProcess
}
}
}
final StringPartInfo info = settings.getStringPartInfo();
if (info != null) {
final GrExpression expr = GrIntroduceHandlerBase.processLiteral(settings.getName(), info, settings.getProject());
final Editor editor = PsiUtilBase.findEditor(expr);
if (editor != null) {
editor.getSelectionModel().removeSelection();
editor.getCaretModel().moveToOffset(expr.getTextRange().getEndOffset());
}
}
}
public static void processExternalUsages(UsageInfo[] usages, GrIntroduceParameterSettings settings, PsiElement expression) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -13,14 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.refactoring.introduceParameter;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.refactoring.IntroduceParameterRefactoring;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.util.TestUtils;
package org.jetbrains.plugins.groovy.refactoring.introduceParameter
import com.intellij.psi.impl.source.PostprocessReformattingAspect
import com.intellij.refactoring.IntroduceParameterRefactoring
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import org.jetbrains.annotations.Nullable
import org.jetbrains.plugins.groovy.util.TestUtils
/**
* @author Max Medvedev
*/
@@ -45,6 +44,24 @@ public class GrIntroduceParameterInClosureTest extends LightCodeInsightFixtureTe
myFixture.checkResultByFile(getTestName(false) + "_after.groovy");
}
private void doTest(final int replaceFieldsWithGetters,
final boolean removeUnusedParameters,
final boolean declareFinal,
@Nullable final String conflicts,
final boolean generateDelegate,
String before,
String after) {
myFixture.configureByText('before.groovy', before);
GrIntroduceParameterTest.execute(replaceFieldsWithGetters, removeUnusedParameters, declareFinal, conflicts, generateDelegate,
getProject(), myFixture.getEditor(), myFixture.getFile());
PostprocessReformattingAspect.getInstance(getProject()).doPostponedFormatting();
myFixture.getEditor().getSelectionModel().removeSelection();
myFixture.checkResult(after);
}
public void testSimpleClosure() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, null, false);
}
@@ -96,4 +113,18 @@ public class GrIntroduceParameterInClosureTest extends LightCodeInsightFixtureTe
public void testDelegateRemoveUnusedParam() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, true, false, null, true);
}*/
void testStringPart0() {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, false, false, null, false, '''\
def cl = {
print 'a<selection>b</selection>c'
}
cl()
''', '''\
def cl = {String anObject ->
print 'a' + anObject<caret> + 'c'
}
cl('b')
''')
}
}