method refs: rename methods

This commit is contained in:
anna
2012-10-05 11:11:06 +02:00
parent 21be923dc9
commit 30feb7255d
4 changed files with 54 additions and 0 deletions
@@ -17,6 +17,7 @@ package com.intellij.psi.impl.source.tree.java;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.pom.java.LanguageLevel;
@@ -42,6 +43,7 @@ import com.intellij.psi.util.MethodSignature;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -97,6 +99,8 @@ public class PsiMethodReferenceExpressionImpl extends PsiReferenceExpressionBase
final IElementType elType = child.getElementType();
if (elType == JavaTokenType.DOUBLE_COLON) {
return ChildRole.DOUBLE_COLON;
} else if (elType == JavaTokenType.IDENTIFIER) {
return ChildRole.REFERENCE_NAME;
}
return ChildRole.EXPRESSION;
}
@@ -177,6 +181,24 @@ public class PsiMethodReferenceExpressionImpl extends PsiReferenceExpressionBase
}
}
@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
PsiElement oldIdentifier = findChildByRoleAsPsiElement(ChildRole.REFERENCE_NAME);
if (oldIdentifier == null) {
throw new IncorrectOperationException();
}
final String oldRefName = oldIdentifier.getText();
if (PsiKeyword.THIS.equals(oldRefName) ||
PsiKeyword.SUPER.equals(oldRefName) ||
PsiKeyword.NEW.equals(oldRefName) ||
Comparing.strEqual(oldRefName, newElementName)) {
return this;
}
PsiIdentifier identifier = JavaPsiFacade.getInstance(getProject()).getElementFactory().createIdentifier(newElementName);
oldIdentifier.replace(identifier);
return this;
}
@Override
public String toString() {
return "PsiMethodReferenceExpression:" + getText();
@@ -0,0 +1,14 @@
class MyTest {
static class Foo {
}
interface I {
Foo m();
}
static Foo f<caret>oo() { return null; }
public static void main(String[] args) {
I i = MyTest::foo;
}
}
@@ -0,0 +1,14 @@
class MyTest {
static class Foo {
}
interface I {
Foo m();
}
static Foo bar() { return null; }
public static void main(String[] args) {
I i = MyTest::bar;
}
}
@@ -68,6 +68,10 @@ public class RenameMembersInplaceTest extends LightCodeInsightTestCase {
doTestInplaceRename("Bar");
}
public void testMethodWithMethodRef() throws Exception {
doTestInplaceRename("bar");
}
public void testConflictingMethodName() throws Exception {
try {
doTestInplaceRename("bar");