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
|
specify.type.explicitly.remove.action.name=Remove explicitly specified type
|
||||||
rename.parameter.to.match.overridden.method=Rename parameter to match overridden method
|
rename.parameter.to.match.overridden.method=Rename parameter to match overridden method
|
||||||
rename.family=Rename
|
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.after.invocation=Add semicolon after invocation of ''{0}''
|
||||||
add.semicolon.family=Add Semicolon
|
add.semicolon.family=Add Semicolon
|
||||||
change.function.return.type=Change ''{0}'' function return type to ''{1}''
|
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.editor.Editor;
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiElement;
|
||||||
|
import com.intellij.refactoring.RefactoringBundle;
|
||||||
import com.intellij.refactoring.rename.RenamePsiElementProcessor;
|
import com.intellij.refactoring.rename.RenamePsiElementProcessor;
|
||||||
|
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.asJava.KotlinLightClass;
|
import org.jetbrains.jet.asJava.KotlinLightClass;
|
||||||
import org.jetbrains.jet.asJava.KotlinLightClassForExplicitDeclaration;
|
import org.jetbrains.jet.asJava.KotlinLightClassForExplicitDeclaration;
|
||||||
import org.jetbrains.jet.asJava.KotlinLightClassForPackage;
|
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.JetClassOrObject;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
|
import org.jetbrains.jet.plugin.JetBundle;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -39,11 +43,41 @@ public class RenameJetClassProcessor extends RenamePsiElementProcessor {
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public PsiElement substituteElementToRename(PsiElement element, @Nullable Editor editor) {
|
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 KotlinLightClass) {
|
||||||
if (element instanceof KotlinLightClassForExplicitDeclaration) {
|
if (element instanceof KotlinLightClassForExplicitDeclaration) {
|
||||||
return element.getNavigationElement();
|
return ((KotlinLightClassForExplicitDeclaration) element).getJetClassOrObject();
|
||||||
}
|
}
|
||||||
else if (element instanceof KotlinLightClassForPackage) {
|
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
|
// Cancel rename
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -52,22 +86,6 @@ public class RenameJetClassProcessor extends RenamePsiElementProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return element;
|
return (JetClassOrObject) 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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("->");
|
String[] strings = renameDirective.split("->");
|
||||||
Assert.assertTrue("'// RENAME:' directive should have at least AbstractRenameTest.RenameType parameter", strings.length > 0);
|
Assert.assertTrue("'// RENAME:' directive should have at least AbstractRenameTest.RenameType parameter", strings.length > 0);
|
||||||
|
|
||||||
|
String hintDirective = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// HINT:");
|
||||||
|
|
||||||
String renameTypeStr = strings[0];
|
String renameTypeStr = strings[0];
|
||||||
RenameType type = RenameType.valueOf(renameTypeStr);
|
RenameType type = RenameType.valueOf(renameTypeStr);
|
||||||
|
|
||||||
FqNameUnsafe fqNameUnsafe = new FqNameUnsafe(strings[1]);
|
try {
|
||||||
|
FqNameUnsafe fqNameUnsafe = new FqNameUnsafe(strings[1]);
|
||||||
switch (type) {
|
|
||||||
case JAVA_CLASS:
|
switch (type) {
|
||||||
renameJavaClassTest(fqNameUnsafe.asString(), strings[2]);
|
case JAVA_CLASS:
|
||||||
break;
|
renameJavaClassTest(fqNameUnsafe.asString(), strings[2]);
|
||||||
case JAVA_METHOD:
|
break;
|
||||||
renameJavaMethodTest(fqNameUnsafe.asString(), strings[2], strings[3]);
|
case JAVA_METHOD:
|
||||||
break;
|
renameJavaMethodTest(fqNameUnsafe.asString(), strings[2], strings[3]);
|
||||||
case KOTLIN_CLASS:
|
break;
|
||||||
renameKotlinClassTest(fqNameUnsafe.toSafe(), strings[2]);
|
case KOTLIN_CLASS:
|
||||||
break;
|
renameKotlinClassTest(fqNameUnsafe.toSafe(), strings[2]);
|
||||||
case KOTLIN_FUNCTION:
|
break;
|
||||||
renameKotlinFunctionTest(fqNameUnsafe.parent().toSafe(),fqNameUnsafe.shortName().asString(), strings[2]);
|
case KOTLIN_FUNCTION:
|
||||||
break;
|
renameKotlinFunctionTest(fqNameUnsafe.parent().toSafe(),fqNameUnsafe.shortName().asString(), strings[2]);
|
||||||
case KOTLIN_PROPERTY:
|
break;
|
||||||
renameKotlinPropertyTest(fqNameUnsafe.parent().toSafe(), fqNameUnsafe.shortName().asString(), strings[2]);
|
case KOTLIN_PROPERTY:
|
||||||
break;
|
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) {
|
catch (Exception e) {
|
||||||
|
|||||||
@@ -61,4 +61,9 @@ public class RenameTestGenerated extends AbstractRenameTest {
|
|||||||
doTest("idea/testData/refactoring/rename/renameKotlinMethod/renameKotlinMethod.test");
|
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