Converted to Kotlin

This commit is contained in:
Valentin Kipyatkov
2015-08-14 18:41:35 +03:00
parent 4ba1f423e1
commit b6311d7be7
2 changed files with 57 additions and 79 deletions
@@ -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<JetBinaryExpression> {
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);
}
};
}
}
@@ -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<JetBinaryExpression>(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<JetBinaryExpression>(diagnostic, javaClass<JetBinaryExpression>()) ?: return null
if (expression.getLeft() == null) return null
if (expression.getRight() == null) return null
return ReplaceInfixCallFix(expression)
}
}
}
}
}