From c0c1273388cf04e228a253f4b3bea104d9c8df1e Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Fri, 13 Mar 2015 11:39:43 +0300 Subject: [PATCH] Prohibit `rename` refactoring on secondary ctor Make custom handler that runs when caret is on secondary ctor but do nothing because constructors don't have name. It's neccessary because otherwise default handler will be ran with proposition to rename class. Also note, that we do not run this handler when caret is on constructor's body or argument's list to allow renaming inner declarations. --- idea/src/META-INF/plugin.xml | 1 + .../RenameOnSecondaryConstructorHandler.kt | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameOnSecondaryConstructorHandler.kt diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index da9d571a80b..c0a6725091d 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -304,6 +304,7 @@ order="first"/> + diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameOnSecondaryConstructorHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameOnSecondaryConstructorHandler.kt new file mode 100644 index 00000000000..7818e39ec44 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameOnSecondaryConstructorHandler.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2010-2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.refactoring.rename + +import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.actionSystem.DataContext +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.psi.util.PsiTreeUtil +import com.intellij.refactoring.rename.RenameHandler +import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils +import org.jetbrains.kotlin.psi.JetBlockExpression +import org.jetbrains.kotlin.psi.JetParameterList +import org.jetbrains.kotlin.psi.JetSecondaryConstructor +import org.jetbrains.kotlin.psi.JetValueArgumentList + + +public class RenameOnSecondaryConstructorHandler : RenameHandler { + override fun isAvailableOnDataContext(dataContext: DataContext?): Boolean { + if (dataContext == null) return false + + val editor = CommonDataKeys.EDITOR.getData(dataContext) + val file = CommonDataKeys.PSI_FILE.getData(dataContext) + + val element = PsiTreeUtil.findElementOfClassAtOffsetWithStopSet( + file, editor.getCaretModel().getOffset(), javaClass(), false, + javaClass(), javaClass(), javaClass() + ) + return element != null; + } + + override fun isRenaming(dataContext: DataContext?): Boolean = isAvailableOnDataContext(dataContext) + + override fun invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext?) { + CodeInsightUtils.showErrorHint(project, editor, "Rename is not applicable to secondary constructors", "Rename", null); + } + + override fun invoke(project: Project, elements: Array, dataContext: DataContext?) { + // Do nothing: this method is called not from editor + } +}