From b6311d7be77e44d93ce7995fa24e708689d6fdd8 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 14 Aug 2015 18:41:35 +0300 Subject: [PATCH] Converted to Kotlin --- .../idea/quickfix/ReplaceInfixCallFix.java | 79 ------------------- .../idea/quickfix/ReplaceInfixCallFix.kt | 57 +++++++++++++ 2 files changed, 57 insertions(+), 79 deletions(-) delete mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceInfixCallFix.java create mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceInfixCallFix.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceInfixCallFix.java b/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceInfixCallFix.java deleted file mode 100644 index 25f77bafa5c..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceInfixCallFix.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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.quickfix; - -import com.intellij.codeInsight.intention.IntentionAction; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.diagnostics.Diagnostic; -import org.jetbrains.kotlin.idea.JetBundle; -import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil; -import org.jetbrains.kotlin.psi.JetBinaryExpression; -import org.jetbrains.kotlin.psi.JetExpression; -import org.jetbrains.kotlin.psi.JetFile; -import org.jetbrains.kotlin.psi.JetQualifiedExpression; - -import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory; - -public class ReplaceInfixCallFix extends JetIntentionAction { - public ReplaceInfixCallFix(@NotNull JetBinaryExpression element) { - super(element); - } - - @NotNull - @Override - public String getText() { - return JetBundle.message("replace.with.safe.call"); - } - - @NotNull - @Override - public String getFamilyName() { - return getText(); - } - - @Override - public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException { - JetExpression left = element.getLeft(); - JetExpression right = element.getRight(); - assert left != null && right != null : "Preconditions checked by factory"; - String newText = left.getText() + "?." + element.getOperationReference().getText() - + "(" + right.getText() + ")"; - JetQualifiedExpression newElement = (JetQualifiedExpression) JetPsiFactory(file).createExpression(newText); - element.replace(newElement); - } - - @Override - public boolean startInWriteAction() { - return true; - } - - public static JetSingleIntentionActionFactory createFactory() { - return new JetSingleIntentionActionFactory() { - @Override - public IntentionAction createAction(Diagnostic diagnostic) { - JetBinaryExpression expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpression.class); - if (expression == null) return null; - if (expression.getLeft() == null) return null; - if (expression.getRight() == null) return null; - return new ReplaceInfixCallFix(expression); - } - }; - } -} diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceInfixCallFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceInfixCallFix.kt new file mode 100644 index 00000000000..87dd1eb4b3c --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceInfixCallFix.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.quickfix + +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.idea.JetBundle +import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil +import org.jetbrains.kotlin.psi.JetBinaryExpression +import org.jetbrains.kotlin.psi.JetFile +import org.jetbrains.kotlin.psi.JetPsiFactory + +public class ReplaceInfixCallFix(element: JetBinaryExpression) : JetIntentionAction(element) { + + override fun getText() = JetBundle.message("replace.with.safe.call") + + override fun getFamilyName() = text + + override fun invoke(project: Project, editor: Editor?, file: JetFile) { + val left = element.getLeft() + val right = element.getRight() + assert(left != null && right != null, "Preconditions checked by factory") + val newText = left!!.getText() + "?." + element.getOperationReference().getText() + "(" + right!!.getText() + ")" + element.replace(JetPsiFactory(file).createExpression(newText)) + } + + override fun startInWriteAction() = true + + companion object { + public fun createFactory(): JetSingleIntentionActionFactory { + return object : JetSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val expression = QuickFixUtil.getParentElementOfType(diagnostic, javaClass()) ?: return null + if (expression.getLeft() == null) return null + if (expression.getRight() == null) return null + return ReplaceInfixCallFix(expression) + } + } + } + } +}