RenameKotlinClassProcessor: J2K

This commit is contained in:
Dmitry Jemerov
2015-08-27 18:13:12 +02:00
parent 3437891848
commit c645b9c237
2 changed files with 40 additions and 52 deletions
+1 -1
View File
@@ -336,7 +336,7 @@
<psi.referenceContributor language="jet" implementation="org.jetbrains.kotlin.idea.references.JetReferenceContributor"/> <psi.referenceContributor language="jet" implementation="org.jetbrains.kotlin.idea.references.JetReferenceContributor"/>
<renamePsiElementProcessor id="KotlinClass" <renamePsiElementProcessor id="KotlinClass"
implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameJetClassProcessor" implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameKotlinClassProcessor"
order="first"/> order="first"/>
<renamePsiElementProcessor implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameKotlinFunctionProcessor" <renamePsiElementProcessor implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameKotlinFunctionProcessor"
id="KotlinFunction" id="KotlinFunction"
@@ -14,80 +14,68 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.idea.refactoring.rename; package org.jetbrains.kotlin.idea.refactoring.rename
import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.Editor
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.RefactoringBundle; import com.intellij.refactoring.util.CommonRefactoringUtil
import com.intellij.refactoring.util.CommonRefactoringUtil; import org.jetbrains.kotlin.asJava.KotlinLightClass
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.asJava.KotlinLightClassForExplicitDeclaration
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.asJava.KotlinLightClassForPackage
import org.jetbrains.kotlin.asJava.KotlinLightClass; import org.jetbrains.kotlin.idea.JetBundle
import org.jetbrains.kotlin.asJava.KotlinLightClassForExplicitDeclaration; import org.jetbrains.kotlin.psi.JetClassOrObject
import org.jetbrains.kotlin.asJava.KotlinLightClassForPackage; import org.jetbrains.kotlin.psi.JetConstructor
import org.jetbrains.kotlin.idea.JetBundle;
import org.jetbrains.kotlin.psi.JetClassOrObject;
import org.jetbrains.kotlin.psi.JetConstructor;
import org.jetbrains.kotlin.psi.JetFile;
import java.util.Map; public class RenameKotlinClassProcessor : RenameKotlinPsiProcessor() {
override fun canProcessElement(element: PsiElement): Boolean {
public class RenameJetClassProcessor extends RenameKotlinPsiProcessor { return element is JetClassOrObject || element is KotlinLightClass || element is JetConstructor<*>
@Override
public boolean canProcessElement(@NotNull PsiElement element) {
return element instanceof JetClassOrObject || element instanceof KotlinLightClass || element instanceof JetConstructor;
} }
@Nullable override fun substituteElementToRename(element: PsiElement, editor: Editor?): PsiElement? {
@Override return getJetClassOrObject(element, true, editor)
public PsiElement substituteElementToRename(PsiElement element, @Nullable Editor editor) {
return getJetClassOrObject(element, true, editor);
} }
@Override override fun prepareRenaming(element: PsiElement, newName: String, allRenames: MutableMap<PsiElement, String>) {
public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames) { val classOrObject = getJetClassOrObject(element, false, null) ?: return
JetClassOrObject classOrObject = getJetClassOrObject(element, false, null);
if (classOrObject != null) { val file = classOrObject.getContainingJetFile()
JetFile file = classOrObject.getContainingJetFile();
VirtualFile virtualFile = file.getVirtualFile(); val virtualFile = file.virtualFile
if (virtualFile != null) { if (virtualFile != null) {
String nameWithoutExtensions = virtualFile.getNameWithoutExtension(); val nameWithoutExtensions = virtualFile.nameWithoutExtension
if (nameWithoutExtensions.equals(classOrObject.getName())) { if (nameWithoutExtensions == classOrObject.name) {
allRenames.put(file, newName + "." + virtualFile.getExtension()); allRenames.put(file, newName + "." + virtualFile.extension)
}
} }
} }
} }
@Nullable private fun getJetClassOrObject(element: PsiElement?, showErrors: Boolean, editor: Editor?): JetClassOrObject? = when (element) {
private static JetClassOrObject getJetClassOrObject(@Nullable PsiElement element, boolean showErrors, @Nullable Editor editor) { is KotlinLightClass ->
if (element instanceof KotlinLightClass) { if (element is KotlinLightClassForExplicitDeclaration) {
if (element instanceof KotlinLightClassForExplicitDeclaration) { element.getOrigin()
return ((KotlinLightClassForExplicitDeclaration) element).getOrigin();
} }
else if (element instanceof KotlinLightClassForPackage) { else if (element is KotlinLightClassForPackage) {
if (showErrors) { if (showErrors) {
CommonRefactoringUtil.showErrorHint( CommonRefactoringUtil.showErrorHint(
element.getProject(), editor, element.project, editor,
JetBundle.message("rename.kotlin.package.class.error"), JetBundle.message("rename.kotlin.package.class.error"),
RefactoringBundle.message("rename.title"), RefactoringBundle.message("rename.title"),
null); null)
} }
// Cancel rename // Cancel rename
return null; null
} }
else { else {
assert false : "Should not be suggested to rename element of type " + element.getClass() + " " + element; assert(false) { "Should not be suggested to rename element of type " + element.javaClass + " " + element }
null
} }
}
else if (element instanceof JetConstructor) {
return ((JetConstructor) element).getContainingClassOrObject();
}
return (JetClassOrObject) element; is JetConstructor<*> ->
element.getContainingClassOrObject()
else ->
element as? JetClassOrObject
} }
} }