From c63265bd698ff31293674c86793130bdf2187c4b Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Mon, 18 Nov 2013 18:47:47 +0400 Subject: [PATCH] Translate RemoveUnnecessaryParenthesesIntention and apply refactoring --- ...RemoveUnnecessaryParenthesesIntention.java | 60 ------------------- .../RemoveUnnecessaryParenthesesIntention.kt | 42 +++++++++++++ 2 files changed, 42 insertions(+), 60 deletions(-) delete mode 100644 idea/src/org/jetbrains/jet/plugin/intentions/RemoveUnnecessaryParenthesesIntention.java create mode 100644 idea/tests/org/jetbrains/jet/plugin/intentions/RemoveUnnecessaryParenthesesIntention.kt diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/RemoveUnnecessaryParenthesesIntention.java b/idea/src/org/jetbrains/jet/plugin/intentions/RemoveUnnecessaryParenthesesIntention.java deleted file mode 100644 index fec605186b1..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/intentions/RemoveUnnecessaryParenthesesIntention.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.plugin.intentions; - -import com.intellij.codeInsight.intention.impl.BaseIntentionAction; -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.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.psi.JetExpression; -import org.jetbrains.jet.lang.psi.JetParenthesizedExpression; -import org.jetbrains.jet.lang.psi.JetPsiUtil; -import org.jetbrains.jet.plugin.JetBundle; - -public class RemoveUnnecessaryParenthesesIntention extends BaseIntentionAction { - private JetParenthesizedExpression expression; - - public RemoveUnnecessaryParenthesesIntention() { - setText(JetBundle.message("remove.unnecessary.parentheses")); - } - - @NotNull - @Override - public String getFamilyName() { - return JetBundle.message("remove.unnecessary.parentheses.family"); - } - - @Override - public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { - PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); - expression = PsiTreeUtil.getParentOfType(element, JetParenthesizedExpression.class); - return expression != null && JetPsiUtil.areParenthesesUseless(expression); - } - - @Override - public void invoke( - @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file - ) throws IncorrectOperationException { - JetExpression innerExpression = expression.getExpression(); - assert innerExpression != null : "parenthesizedExpression.getExpression() == null despite @IfNotParsed annotation"; - expression.replace(innerExpression); - } -} diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/RemoveUnnecessaryParenthesesIntention.kt b/idea/tests/org/jetbrains/jet/plugin/intentions/RemoveUnnecessaryParenthesesIntention.kt new file mode 100644 index 00000000000..42ac30eb24b --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/RemoveUnnecessaryParenthesesIntention.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2013 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.jet.plugin.intentions + +import com.intellij.codeInsight.intention.impl.BaseIntentionAction +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.util.IncorrectOperationException +import org.jetbrains.jet.lang.psi.JetExpression +import org.jetbrains.jet.lang.psi.JetParenthesizedExpression +import org.jetbrains.jet.lang.psi.JetPsiUtil +import org.jetbrains.jet.plugin.JetBundle + +public class RemoveUnnecessaryParenthesesIntention : JetSelfTargetingIntention( + "remove.unnecessary.parentheses", javaClass() +) { + override fun isApplicableTo(element: JetParenthesizedExpression): Boolean = JetPsiUtil.areParenthesesUseless(element) + + override fun applyTo(element: JetParenthesizedExpression, editor: Editor) { + with (element.getExpression()) { + assert (this != null, "parenthesizedExpression.getExpression() == null despite @IfNotParsed annotation") + element.replace(this!!) + } + } +}