spi binding: be ready for short names in handleRename (IDEA-164272)

This commit is contained in:
Anna.Kozlova
2016-11-22 20:22:29 +01:00
parent f57a85115b
commit 85fdebf831
7 changed files with 43 additions and 14 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -16,14 +16,15 @@
package com.intellij.spi.psi;
import com.intellij.extapi.psi.PsiFileBase;
import com.intellij.lang.spi.SPILanguage;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.util.ClassUtil;
import com.intellij.spi.SPIFileType;
import com.intellij.lang.spi.SPILanguage;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
@@ -159,7 +160,8 @@ public class SPIFile extends PsiFileBase {
@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
return getElement().setName(newElementName + getElement().getName().substring(getCanonicalText().length()));
String newPackageQName = StringUtil.getQualifiedName(StringUtil.getPackageName(getCanonicalText()), newElementName);
return getElement().setName(newPackageQName + getElement().getName().substring(getCanonicalText().length()));
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -57,7 +57,8 @@ public class SPIPackageOrClassReferenceElement extends ASTWrapperPsiElement impl
final SPIClassProvidersElementList firstChild =
(SPIClassProvidersElementList)PsiFileFactory.getInstance(getProject())
.createFileFromText("spi_dummy", SPIFileType.INSTANCE, newElementName).getFirstChild();
return replace(firstChild.getElements().get(0));
PsiTreeUtil.getDeepestLast(this).replace(PsiTreeUtil.getDeepestLast(firstChild.getElements().get(0)));
return this;
}
@Nullable
@@ -72,11 +73,21 @@ public class SPIPackageOrClassReferenceElement extends ASTWrapperPsiElement impl
@Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
String newElementName;
if (element instanceof PsiPackage) {
return handleElementRename(((PsiPackage)element).getQualifiedName());
} else if (element instanceof PsiClass) {
final String className = ClassUtil.getJVMClassName((PsiClass)element);
return className != null ? handleElementRename(className) : null;
newElementName = ((PsiPackage)element).getQualifiedName();
}
else if (element instanceof PsiClass) {
newElementName = ClassUtil.getJVMClassName((PsiClass)element);
}
else {
return null;
}
if (newElementName != null) {
final SPIClassProvidersElementList firstChild =
(SPIClassProvidersElementList)PsiFileFactory.getInstance(getProject())
.createFileFromText("spi_dummy", SPIFileType.INSTANCE, newElementName).getFirstChild();
return replace(firstChild.getElements().get(0));
}
return null;
}
@@ -0,0 +1,5 @@
package bar.foo1;
public class FooRunnable implements Runnable {
public void run() {
}
}
@@ -0,0 +1,5 @@
package bar.f<caret>oo;
public class FooRunnable implements Runnable {
public void run() {
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -28,18 +28,22 @@ import org.jetbrains.annotations.NotNull;
*/
public class SPIRenameTest extends MultiFileTestCase {
public void testRenameProviderImplementation() throws Exception {
doRenameTest("Test.java", "Test1");
doRenameTest("Test1", "foo/Test.java");
}
public void testRenameProviderImplementationContainingClass() throws Exception {
doRenameTest("Test.java", "Test1");
doRenameTest("Test1", "foo/Test.java");
}
private void doRenameTest(final String editorFile, final String newName) throws Exception {
public void testRenamePackageWithImplementation() throws Exception {
doRenameTest("foo1", "bar/foo/FooRunnable.java");
}
private void doRenameTest(final String newName, final String relPath) throws Exception {
doTest(new PerformAction() {
@Override
public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception {
final VirtualFile file = rootDir.findFileByRelativePath("foo/" + editorFile);
final VirtualFile file = rootDir.findFileByRelativePath(relPath);
assert file != null;
configureByExistingFile(file);
final PsiElement element = TargetElementUtil.findTargetElement(myEditor,