extract light method object: invoke on expressions

This commit is contained in:
Anna Kozlova
2014-09-10 21:57:43 +04:00
parent d9b93231a2
commit 1304e452d9
3 changed files with 47 additions and 6 deletions
@@ -79,7 +79,7 @@ public class ExtractLightMethodObjectHandler {
final PsiFile file,
@NotNull final PsiCodeFragment fragment,
final String methodName) throws PrepareFailedException {
final PsiElement[] elements = fragment.getChildren();
final PsiElement[] elements = CodeInsightUtil.findStatementsInRange(fragment, 0, fragment.getTextLength());
if (elements.length == 0) {
return null;
}
@@ -96,12 +96,11 @@ public class ExtractLightMethodObjectHandler {
CodeInsightUtil.findElementInRange(copy, range.getStartOffset(), range.getEndOffset(), originalContext.getClass());
//todo before this or super, not found etc
final PsiElement anchor = RefactoringUtil.getParentStatement(originalAnchor, false);
final PsiElement[] elementsCopy = new PsiElement[elements.length];
final PsiElement container = anchor.getParent();
elementsCopy[0] = container.addRangeBefore(elements[0], elements[elements.length - 1], anchor);
for (int i = 1; i < elements.length; i++) {
elementsCopy[i] = elementsCopy[i - 1].getNextSibling();
}
final PsiElement firstElementCopy = container.addRangeBefore(elements[0], elements[elements.length - 1], anchor);
final PsiElement[] elementsCopy = CodeInsightUtil.findStatementsInRange(copy,
firstElementCopy.getTextRange().getStartOffset(),
anchor.getTextRange().getStartOffset());
final int start = elementsCopy[0].getTextRange().getStartOffset();
@@ -0,0 +1,30 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Sample {
void a() {
System.out.println("<caret>");
}
class I {
public I(int i) {
}
void foo() {
bar();
}
}
}
@@ -112,6 +112,18 @@ public class ExtractMethodObject4DebuggerTest extends LightRefactoringTestCase {
" }");
}
public void testInnerClass() throws Exception {
doTest(" new In(2).foo()", "new Test().invoke();",
"public class Test {\n" +
" public void invoke() {\n" +
" new In(2).foo()\n" +
" }\n" +
" }");
}
@Override
protected Sdk getProjectJDK() {
return IdeaTestUtil.getMockJdk18();