Generate error tip for rename kotlin package klass from java
This commit is contained in:
@@ -55,6 +55,7 @@ specify.type.explicitly.add.action.name=Specify type explicitly
|
||||
specify.type.explicitly.remove.action.name=Remove explicitly specified type
|
||||
rename.parameter.to.match.overridden.method=Rename parameter to match overridden method
|
||||
rename.family=Rename
|
||||
rename.kotlin.package.class.error="Can't rename kotlin package class"
|
||||
add.semicolon.after.invocation=Add semicolon after invocation of ''{0}''
|
||||
add.semicolon.family=Add Semicolon
|
||||
change.function.return.type=Change ''{0}'' function return type to ''{1}''
|
||||
|
||||
@@ -19,14 +19,18 @@ package org.jetbrains.jet.plugin.refactoring.rename;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.rename.RenamePsiElementProcessor;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.asJava.KotlinLightClass;
|
||||
import org.jetbrains.jet.asJava.KotlinLightClassForExplicitDeclaration;
|
||||
import org.jetbrains.jet.asJava.KotlinLightClassForPackage;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -39,11 +43,41 @@ public class RenameJetClassProcessor extends RenamePsiElementProcessor {
|
||||
@Nullable
|
||||
@Override
|
||||
public PsiElement substituteElementToRename(PsiElement element, @Nullable Editor editor) {
|
||||
return getJetClassOrObject(element, true, editor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames) {
|
||||
JetClassOrObject classOrObject = getJetClassOrObject(element, false, null);
|
||||
|
||||
if (classOrObject != null) {
|
||||
JetFile file = (JetFile) classOrObject.getContainingFile();
|
||||
|
||||
VirtualFile virtualFile = file.getVirtualFile();
|
||||
if (virtualFile != null) {
|
||||
String nameWithoutExtensions = virtualFile.getNameWithoutExtension();
|
||||
if (nameWithoutExtensions.equals(classOrObject.getName())) {
|
||||
allRenames.put(file, newName + "." + virtualFile.getExtension());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static JetClassOrObject getJetClassOrObject(@Nullable PsiElement element, boolean showErrors, @Nullable Editor editor) {
|
||||
if (element instanceof KotlinLightClass) {
|
||||
if (element instanceof KotlinLightClassForExplicitDeclaration) {
|
||||
return element.getNavigationElement();
|
||||
return ((KotlinLightClassForExplicitDeclaration) element).getJetClassOrObject();
|
||||
}
|
||||
else if (element instanceof KotlinLightClassForPackage) {
|
||||
if (showErrors) {
|
||||
CommonRefactoringUtil.showErrorHint(
|
||||
element.getProject(), editor,
|
||||
JetBundle.message("rename.kotlin.package.class.error"),
|
||||
RefactoringBundle.message("rename.title"),
|
||||
null);
|
||||
}
|
||||
|
||||
// Cancel rename
|
||||
return null;
|
||||
}
|
||||
@@ -52,22 +86,6 @@ public class RenameJetClassProcessor extends RenamePsiElementProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames) {
|
||||
JetClassOrObject clazz = (JetClassOrObject) element;
|
||||
JetFile file = (JetFile) clazz.getContainingFile();
|
||||
|
||||
VirtualFile virtualFile = file.getVirtualFile();
|
||||
if (virtualFile != null) {
|
||||
String nameWithoutExtensions = virtualFile.getNameWithoutExtension();
|
||||
if (nameWithoutExtensions.equals(clazz.getName())) {
|
||||
allRenames.put(file, newName + "." + virtualFile.getExtension());
|
||||
}
|
||||
}
|
||||
|
||||
super.prepareRenaming(element, newName, allRenames);
|
||||
return (JetClassOrObject) element;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
package testing.rename
|
||||
|
||||
fun test() = 12
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package testing;
|
||||
|
||||
import testing.rename.RenamePackage;
|
||||
|
||||
class JavaClient {
|
||||
void foo() {
|
||||
RenamePackage.test();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package testing.rename
|
||||
|
||||
fun test() = 12
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package testing;
|
||||
|
||||
import testing.rename.RenamePackage;
|
||||
|
||||
class JavaClient {
|
||||
void foo() {
|
||||
RenamePackage.test();
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// RENAME: JAVA_CLASS->testing.rename.RenamePackage->NewPackageName
|
||||
// HINT: Can't rename kotlin package class
|
||||
@@ -68,27 +68,44 @@ public abstract class AbstractRenameTest extends MultiFileTestCase {
|
||||
String[] strings = renameDirective.split("->");
|
||||
Assert.assertTrue("'// RENAME:' directive should have at least AbstractRenameTest.RenameType parameter", strings.length > 0);
|
||||
|
||||
String hintDirective = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// HINT:");
|
||||
|
||||
String renameTypeStr = strings[0];
|
||||
RenameType type = RenameType.valueOf(renameTypeStr);
|
||||
|
||||
FqNameUnsafe fqNameUnsafe = new FqNameUnsafe(strings[1]);
|
||||
|
||||
switch (type) {
|
||||
case JAVA_CLASS:
|
||||
renameJavaClassTest(fqNameUnsafe.asString(), strings[2]);
|
||||
break;
|
||||
case JAVA_METHOD:
|
||||
renameJavaMethodTest(fqNameUnsafe.asString(), strings[2], strings[3]);
|
||||
break;
|
||||
case KOTLIN_CLASS:
|
||||
renameKotlinClassTest(fqNameUnsafe.toSafe(), strings[2]);
|
||||
break;
|
||||
case KOTLIN_FUNCTION:
|
||||
renameKotlinFunctionTest(fqNameUnsafe.parent().toSafe(),fqNameUnsafe.shortName().asString(), strings[2]);
|
||||
break;
|
||||
case KOTLIN_PROPERTY:
|
||||
renameKotlinPropertyTest(fqNameUnsafe.parent().toSafe(), fqNameUnsafe.shortName().asString(), strings[2]);
|
||||
break;
|
||||
try {
|
||||
FqNameUnsafe fqNameUnsafe = new FqNameUnsafe(strings[1]);
|
||||
|
||||
switch (type) {
|
||||
case JAVA_CLASS:
|
||||
renameJavaClassTest(fqNameUnsafe.asString(), strings[2]);
|
||||
break;
|
||||
case JAVA_METHOD:
|
||||
renameJavaMethodTest(fqNameUnsafe.asString(), strings[2], strings[3]);
|
||||
break;
|
||||
case KOTLIN_CLASS:
|
||||
renameKotlinClassTest(fqNameUnsafe.toSafe(), strings[2]);
|
||||
break;
|
||||
case KOTLIN_FUNCTION:
|
||||
renameKotlinFunctionTest(fqNameUnsafe.parent().toSafe(),fqNameUnsafe.shortName().asString(), strings[2]);
|
||||
break;
|
||||
case KOTLIN_PROPERTY:
|
||||
renameKotlinPropertyTest(fqNameUnsafe.parent().toSafe(), fqNameUnsafe.shortName().asString(), strings[2]);
|
||||
break;
|
||||
}
|
||||
|
||||
if (hintDirective != null) {
|
||||
Assert.fail(String.format("Hint \"%s\" was expected", hintDirective));
|
||||
}
|
||||
}
|
||||
catch (CommonRefactoringUtil.RefactoringErrorHintException hintException) {
|
||||
String hintExceptionUnquoted = StringUtil.unquoteString(hintException.getMessage());
|
||||
if (hintDirective != null) {
|
||||
Assert.assertEquals(hintDirective, hintExceptionUnquoted);
|
||||
}
|
||||
else {
|
||||
Assert.fail(String.format("Unexpected hint: // HINT: %s", hintExceptionUnquoted));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
@@ -61,4 +61,9 @@ public class RenameTestGenerated extends AbstractRenameTest {
|
||||
doTest("idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test");
|
||||
}
|
||||
|
||||
@TestMetadata("renameKotlinPackageClass/javaWrapperForKotlinPackageClass.test")
|
||||
public void testRenameKotlinPackageClass_JavaWrapperForKotlinPackageClass() throws Exception {
|
||||
doTest("idea/testData/refactoring/rename/renameKotlinPackageClass/javaWrapperForKotlinPackageClass.test");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user