Remove Right Part of Binary Expression Quick-Fix: Split to separate quick fixes for cast- and elvis-expressions
This commit is contained in:
@@ -201,4 +201,10 @@ fun KtParameter.dropDefaultValue() {
|
||||
val from = equalsToken ?: return
|
||||
val to = defaultValue ?: from
|
||||
deleteChildRange(from, to)
|
||||
}
|
||||
|
||||
fun dropEnclosingParenthesesIfPossible(expression: KtExpression): KtExpression {
|
||||
val parent = expression.parent as? KtParenthesizedExpression ?: return expression
|
||||
if (!KtPsiUtil.areParenthesesUseless(parent)) return expression
|
||||
return parent.replaced(expression)
|
||||
}
|
||||
@@ -29,7 +29,7 @@ import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.I
|
||||
import org.jetbrains.kotlin.idea.intentions.conventionNameCalls.ReplaceGetOrSetInspection
|
||||
import org.jetbrains.kotlin.idea.intentions.conventionNameCalls.ReplaceGetOrSetIntention
|
||||
import org.jetbrains.kotlin.idea.quickfix.RemoveModifierFix
|
||||
import org.jetbrains.kotlin.idea.quickfix.RemoveRightPartOfBinaryExpressionFix
|
||||
import org.jetbrains.kotlin.idea.quickfix.RemoveUselessCastFix
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
@@ -67,7 +67,7 @@ object J2KPostProcessingRegistrar {
|
||||
registerIntentionBasedProcessing(RemoveUnnecessaryParenthesesIntention()) { applyTo(it) }
|
||||
|
||||
registerDiagnosticBasedProcessing<KtBinaryExpressionWithTypeRHS>(Errors.USELESS_CAST) { element, diagnostic ->
|
||||
val expression = RemoveRightPartOfBinaryExpressionFix(element, "").invoke()
|
||||
val expression = RemoveUselessCastFix.invoke(element)
|
||||
|
||||
val variable = expression.parent as? KtProperty
|
||||
if (variable != null && expression == variable.initializer && variable.isLocal) {
|
||||
|
||||
@@ -93,12 +93,12 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
AddFunctionToSupertypeFix)
|
||||
VIRTUAL_MEMBER_HIDDEN.registerFactory(AddModifierFix.createFactory(OVERRIDE_KEYWORD))
|
||||
|
||||
USELESS_CAST.registerFactory(RemoveRightPartOfBinaryExpressionFix.RemoveCastFactory)
|
||||
USELESS_CAST.registerFactory(RemoveUselessCastFix)
|
||||
|
||||
WRONG_SETTER_PARAMETER_TYPE.registerFactory(ChangeAccessorTypeFix)
|
||||
WRONG_GETTER_RETURN_TYPE.registerFactory(ChangeAccessorTypeFix)
|
||||
|
||||
USELESS_ELVIS.registerFactory(RemoveRightPartOfBinaryExpressionFix.RemoveElvisOperatorFactory)
|
||||
USELESS_ELVIS.registerFactory(RemoveUselessElvisFix)
|
||||
|
||||
val removeRedundantModifierFactory = RemoveModifierFix.createRemoveModifierFactory(true)
|
||||
REDUNDANT_MODIFIER.registerFactory(removeRedundantModifierFactory)
|
||||
|
||||
@@ -1,67 +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.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.util.IncorrectOperationException
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
|
||||
class RemoveRightPartOfBinaryExpressionFix<T : KtExpression>(
|
||||
element: T,
|
||||
private val message: String
|
||||
) : KotlinQuickFixAction<T>(element), CleanupFix {
|
||||
override fun getFamilyName() = "Remove right part of a binary expression"
|
||||
|
||||
override fun getText(): String = message
|
||||
|
||||
public override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
invoke()
|
||||
}
|
||||
|
||||
fun invoke(): KtExpression {
|
||||
val newExpression = when (element) {
|
||||
is KtBinaryExpression -> element.replace((element.copy() as KtBinaryExpression).left!!) as KtExpression
|
||||
is KtBinaryExpressionWithTypeRHS -> element.replace((element.copy() as KtBinaryExpressionWithTypeRHS).left) as KtExpression
|
||||
else -> throw IncorrectOperationException("Unexpected element: " + element.getElementTextWithContext())
|
||||
}
|
||||
|
||||
val parent = newExpression.parent
|
||||
if (parent is KtParenthesizedExpression && KtPsiUtil.areParenthesesUseless(parent)) {
|
||||
return parent.replace(newExpression) as KtExpression
|
||||
}
|
||||
return newExpression
|
||||
}
|
||||
|
||||
object RemoveCastFactory : KotlinSingleIntentionActionFactory() {
|
||||
public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS>? {
|
||||
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>() ?: return null
|
||||
return RemoveRightPartOfBinaryExpressionFix(expression, "Remove cast")
|
||||
}
|
||||
}
|
||||
|
||||
object RemoveElvisOperatorFactory : KotlinSingleIntentionActionFactory() {
|
||||
public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtBinaryExpression>? {
|
||||
val expression = diagnostic.psiElement as? KtBinaryExpression ?: return null
|
||||
return RemoveRightPartOfBinaryExpressionFix(expression, "Remove elvis operator")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.core.dropEnclosingParenthesesIfPossible
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpressionWithTypeRHS
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
|
||||
class RemoveUselessCastFix(element: KtBinaryExpressionWithTypeRHS) : KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS>(element), CleanupFix {
|
||||
override fun getFamilyName() = "Remove useless cast"
|
||||
|
||||
override fun getText(): String = familyName
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
invoke(element)
|
||||
}
|
||||
|
||||
companion object : KotlinSingleIntentionActionFactory() {
|
||||
fun invoke(element: KtBinaryExpressionWithTypeRHS) = dropEnclosingParenthesesIfPossible(element.replaced(element.left))
|
||||
|
||||
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS>? {
|
||||
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>() ?: return null
|
||||
return RemoveUselessCastFix(expression)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.core.dropEnclosingParenthesesIfPossible
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
|
||||
class RemoveUselessElvisFix(element: KtBinaryExpression) : KotlinQuickFixAction<KtBinaryExpression>(element), CleanupFix {
|
||||
override fun getFamilyName(): String = "Remove useless elvis operator"
|
||||
|
||||
override fun getText(): String = familyName
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
dropEnclosingParenthesesIfPossible(element.replaced(element.left!!))
|
||||
}
|
||||
|
||||
companion object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtBinaryExpression>? {
|
||||
val expression = diagnostic.psiElement as? KtBinaryExpression ?: return null
|
||||
return RemoveUselessElvisFix(expression)
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove cast" "true"
|
||||
// "Remove useless cast" "true"
|
||||
fun foo(a: String) {
|
||||
val b = a <caret>as String
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Remove cast" "true"
|
||||
// "Remove useless cast" "true"
|
||||
fun foo(a: String) {
|
||||
val b = a
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Remove cast" "true"
|
||||
// "Remove useless cast" "true"
|
||||
fun test(x: Any): Int {
|
||||
if (x is String) {
|
||||
return (x <caret>as String).length
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Remove cast" "true"
|
||||
// "Remove useless cast" "true"
|
||||
fun test(x: Any): Int {
|
||||
if (x is String) {
|
||||
return x.length
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Remove cast" "true"
|
||||
// "Remove useless cast" "true"
|
||||
fun test(x: Any): String? {
|
||||
if (x is String) {
|
||||
return x <caret>as String
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove cast" "true"
|
||||
// "Remove useless cast" "true"
|
||||
fun test(x: Any): String? {
|
||||
if (x is String) {
|
||||
return x
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Remove cast" "true"
|
||||
// "Remove useless cast" "true"
|
||||
fun foo(a: Any) {
|
||||
val b = a <caret>as Any
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Remove cast" "true"
|
||||
// "Remove useless cast" "true"
|
||||
fun foo(a: Any) {
|
||||
val b = a<caret>
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove elvis operator" "true"
|
||||
// "Remove useless elvis operator" "true"
|
||||
fun foo(a: String) {
|
||||
val b : String = a <caret>?: "s"
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Remove elvis operator" "true"
|
||||
// "Remove useless elvis operator" "true"
|
||||
fun foo(a: String) {
|
||||
val b : String = a<caret>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user