compiling debugger: shift original context if on private, so 'make public' won't invalidate anchor (IDEA-132525)

This commit is contained in:
Anna Kozlova
2014-11-10 17:53:58 +01:00
parent 97ca5a97be
commit a05adb95b5
3 changed files with 29 additions and 1 deletions
@@ -85,10 +85,21 @@ public class ExtractLightMethodObjectHandler {
final PsiFile copy = PsiFileFactory.getInstance(project)
.createFileFromText(file.getName(), file.getFileType(), file.getText(), file.getModificationStamp(), false);
final PsiElement originalContext = fragment.getContext();
PsiElement originalContext = fragment.getContext();
if (originalContext == null) {
return null;
}
if (originalContext instanceof PsiKeyword && PsiModifier.PRIVATE.equals(originalContext.getText())) {
final PsiNameIdentifierOwner identifierOwner = PsiTreeUtil.getParentOfType(originalContext, PsiNameIdentifierOwner.class);
if (identifierOwner != null) {
final PsiElement identifier = identifierOwner.getNameIdentifier();
if (identifier != null) {
originalContext = identifier;
}
}
}
final TextRange range = originalContext.getTextRange();
final PsiElement originalAnchor =
CodeInsightUtil.findElementInRange(copy, range.getStartOffset(), range.getEndOffset(), originalContext.getClass());
@@ -0,0 +1,7 @@
class Sample {
pri<caret>vate int field;
int foo() {
return 1;
}
}
@@ -264,6 +264,16 @@ public class ExtractMethodObject4DebuggerTest extends LightRefactoringTestCase {
" }");
}
public void testOnPrivateField() throws Exception {
doTest(" foo()", "int result = new Test().invoke();",
"public class Test {\n" +
" public int invoke() {\n" +
" return foo();\n" +
" }\n" +
" }");
}
@Override
protected Sdk getProjectJDK() {
return IdeaTestUtil.getMockJdk18();