Convert to Kotlin: ChangeParameterTypeFix.java

This commit is contained in:
Alexey Sedunov
2015-12-23 18:51:54 +03:00
committed by Alexey
parent 3af7c7b57a
commit 9180a99342
@@ -14,63 +14,47 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.idea.quickfix; package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiTreeUtil
import com.intellij.util.IncorrectOperationException; import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.KotlinBundle; import org.jetbrains.kotlin.idea.util.ShortenReferences
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers; import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.idea.util.ShortenReferences; import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.types.KotlinType;
public class ChangeParameterTypeFix extends KotlinQuickFixAction<KtParameter> { class ChangeParameterTypeFix(element: KtParameter, private val type: KotlinType) : KotlinQuickFixAction<KtParameter>(element) {
private final KotlinType type; private val containingDeclarationName: String?
private final String containingDeclarationName; private val isPrimaryConstructorParameter: Boolean
private final boolean isPrimaryConstructorParameter;
public ChangeParameterTypeFix(@NotNull KtParameter element, @NotNull KotlinType type) { init {
super(element); val declaration = PsiTreeUtil.getParentOfType(element, KtNamedDeclaration::class.java)
this.type = type; val declarationFQName = declaration?.fqName
KtNamedDeclaration declaration = PsiTreeUtil.getParentOfType(element, KtNamedDeclaration.class); isPrimaryConstructorParameter = declaration is KtPrimaryConstructor
isPrimaryConstructorParameter = declaration instanceof KtPrimaryConstructor; containingDeclarationName = if (declarationFQName != null) declarationFQName.asString() else declaration?.name
FqName declarationFQName = declaration == null ? null : declaration.getFqName();
containingDeclarationName = declarationFQName != null ? declarationFQName.asString()
: declaration != null ? declaration.getName()
: null;
} }
@Override override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiFile file) { return super.isAvailable(project, editor, file) && containingDeclarationName != null
return super.isAvailable(project, editor, file) && containingDeclarationName != null;
} }
@NotNull override fun getText(): String {
@Override val renderedType = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type)
public String getText() { return if (isPrimaryConstructorParameter)
String renderedType = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type); KotlinBundle.message("change.primary.constructor.parameter.type", element.name, containingDeclarationName, renderedType)
return isPrimaryConstructorParameter ? else
KotlinBundle KotlinBundle.message("change.function.parameter.type", element.name, containingDeclarationName, renderedType)
.message("change.primary.constructor.parameter.type", getElement().getName(), containingDeclarationName, renderedType) :
KotlinBundle.message("change.function.parameter.type", getElement().getName(), containingDeclarationName, renderedType);
} }
@NotNull override fun getFamilyName() = KotlinBundle.message("change.type.family")
@Override
public String getFamilyName() {
return KotlinBundle.message("change.type.family");
}
@Override public override operator fun invoke(project: Project, editor: Editor?, file: KtFile) {
public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException { val newTypeRef = KtPsiFactory(file).createType(IdeDescriptorRenderers.SOURCE_CODE.renderType(type))
KtTypeReference newTypeRef = KtPsiFactoryKt.KtPsiFactory(file).createType(IdeDescriptorRenderers.SOURCE_CODE.renderType(type)); element.setTypeReference(newTypeRef)?.let {
newTypeRef = getElement().setTypeReference(newTypeRef); ShortenReferences.DEFAULT.process(it)
assert newTypeRef != null; }
ShortenReferences.DEFAULT.process(newTypeRef);
} }
} }