FIR IDE: Enable RemoveUselessElvisFix.
This commit is contained in:
committed by
TeamCityServer
parent
157046153f
commit
a778cc673e
@@ -33,7 +33,6 @@ import org.jetbrains.kotlin.name.Name
|
|||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.addRemoveModifier.MODIFIERS_ORDER
|
import org.jetbrains.kotlin.psi.addRemoveModifier.MODIFIERS_ORDER
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
import org.jetbrains.kotlin.psi.synthetics.findClassDescriptor
|
|
||||||
import org.jetbrains.kotlin.psi.typeRefHelpers.setReceiverTypeReference
|
import org.jetbrains.kotlin.psi.typeRefHelpers.setReceiverTypeReference
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.OverridingUtil
|
import org.jetbrains.kotlin.resolve.OverridingUtil
|
||||||
@@ -518,12 +517,6 @@ fun KtParameter.dropDefaultValue() {
|
|||||||
deleteChildRange(from, to)
|
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun KtTypeParameterListOwner.addTypeParameter(typeParameter: KtTypeParameter): KtTypeParameter? {
|
fun KtTypeParameterListOwner.addTypeParameter(typeParameter: KtTypeParameter): KtTypeParameter? {
|
||||||
typeParameterList?.let { return it.addParameter(typeParameter) }
|
typeParameterList?.let { return it.addParameter(typeParameter) }
|
||||||
|
|
||||||
|
|||||||
@@ -104,6 +104,8 @@ class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
|
|||||||
private val expressions = KtQuickFixesListBuilder.registerPsiQuickFix {
|
private val expressions = KtQuickFixesListBuilder.registerPsiQuickFix {
|
||||||
registerPsiQuickFixes(KtFirDiagnostic.UnnecessarySafeCall::class, ReplaceWithDotCallFix)
|
registerPsiQuickFixes(KtFirDiagnostic.UnnecessarySafeCall::class, ReplaceWithDotCallFix)
|
||||||
registerPsiQuickFixes(KtFirDiagnostic.UnnecessaryNotNullAssertion::class, RemoveExclExclCallFix)
|
registerPsiQuickFixes(KtFirDiagnostic.UnnecessaryNotNullAssertion::class, RemoveExclExclCallFix)
|
||||||
|
registerPsiQuickFixes(KtFirDiagnostic.UselessElvis::class, RemoveUselessElvisFix)
|
||||||
|
registerPsiQuickFixes(KtFirDiagnostic.UselessElvisRightIsNull::class, RemoveUselessElvisFix)
|
||||||
registerApplicator(ReplaceCallFixFactories.unsafeCallFactory)
|
registerApplicator(ReplaceCallFixFactories.unsafeCallFactory)
|
||||||
registerApplicator(AddExclExclCallFixFactories.unsafeCallFactory)
|
registerApplicator(AddExclExclCallFixFactories.unsafeCallFactory)
|
||||||
registerApplicator(AddExclExclCallFixFactories.unsafeInfixCallFactory)
|
registerApplicator(AddExclExclCallFixFactories.unsafeInfixCallFactory)
|
||||||
|
|||||||
Generated
+5
@@ -690,6 +690,11 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
|
|||||||
public void testUselessElvis() throws Exception {
|
public void testUselessElvis() throws Exception {
|
||||||
runTest("idea/testData/quickfix/expressions/uselessElvis.kt");
|
runTest("idea/testData/quickfix/expressions/uselessElvis.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("uselessElvisRightIsNull.kt")
|
||||||
|
public void testUselessElvisRightIsNull() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/expressions/uselessElvisRightIsNull.kt");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/lateinit")
|
@TestMetadata("idea/testData/quickfix/lateinit")
|
||||||
|
|||||||
+7
@@ -6,6 +6,7 @@
|
|||||||
package org.jetbrains.kotlin.idea.core
|
package org.jetbrains.kotlin.idea.core
|
||||||
|
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
import org.jetbrains.kotlin.psi.KtParenthesizedExpression
|
import org.jetbrains.kotlin.psi.KtParenthesizedExpression
|
||||||
import org.jetbrains.kotlin.psi.KtPsiUtil
|
import org.jetbrains.kotlin.psi.KtPsiUtil
|
||||||
|
|
||||||
@@ -14,6 +15,12 @@ inline fun <reified T : PsiElement> PsiElement.replaced(newElement: T): T {
|
|||||||
return result as? T ?: (result as KtParenthesizedExpression).expression as T
|
return result as? T ?: (result as KtParenthesizedExpression).expression as T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun KtExpression.dropEnclosingParenthesesIfPossible(): KtExpression {
|
||||||
|
val parent = parent as? KtParenthesizedExpression ?: return this
|
||||||
|
if (!KtPsiUtil.areParenthesesUseless(parent)) return this
|
||||||
|
return parent.replaced(this)
|
||||||
|
}
|
||||||
|
|
||||||
//todo make inline
|
//todo make inline
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
fun <T : PsiElement> T.copied(): T = copy() as T
|
fun <T : PsiElement> T.copied(): T = copy() as T
|
||||||
|
|||||||
+34
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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.psi.PsiElement
|
||||||
|
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||||
|
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) : KotlinPsiOnlyQuickFixAction<KtBinaryExpression>(element), CleanupFix {
|
||||||
|
override fun getFamilyName(): String = KotlinBundle.message("remove.useless.elvis.operator")
|
||||||
|
|
||||||
|
override fun getText(): String = familyName
|
||||||
|
|
||||||
|
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||||
|
val element = element ?: return
|
||||||
|
element.replaced(element.left!!).dropEnclosingParenthesesIfPossible()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object : QuickFixesPsiBasedFactory<PsiElement>(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) {
|
||||||
|
override fun doCreateQuickFix(psiElement: PsiElement): List<IntentionAction> {
|
||||||
|
val expression = psiElement as? KtBinaryExpression ?: return emptyList()
|
||||||
|
return listOf(RemoveUselessElvisFix(expression))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,7 +26,7 @@ class RemoveUselessCastFix(element: KtBinaryExpressionWithTypeRHS) : KotlinQuick
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object : KotlinSingleIntentionActionFactory() {
|
companion object : KotlinSingleIntentionActionFactory() {
|
||||||
operator fun invoke(element: KtBinaryExpressionWithTypeRHS) = dropEnclosingParenthesesIfPossible(element.replaced(element.left))
|
operator fun invoke(element: KtBinaryExpressionWithTypeRHS) = element.replaced(element.left).dropEnclosingParenthesesIfPossible()
|
||||||
|
|
||||||
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS>? {
|
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS>? {
|
||||||
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>() ?: return null
|
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>() ?: return null
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.KotlinBundle
|
|
||||||
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 = KotlinBundle.message("remove.useless.elvis.operator")
|
|
||||||
|
|
||||||
override fun getText(): String = familyName
|
|
||||||
|
|
||||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
|
||||||
val element = element ?: return
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,5 +2,3 @@
|
|||||||
fun foo(a: String) {
|
fun foo(a: String) {
|
||||||
val b : String = a <caret>?: "s"
|
val b : String = a <caret>?: "s"
|
||||||
}
|
}
|
||||||
|
|
||||||
/* IGNORE_FIR */
|
|
||||||
@@ -2,5 +2,3 @@
|
|||||||
fun foo(a: String) {
|
fun foo(a: String) {
|
||||||
val b : String = a<caret>
|
val b : String = a<caret>
|
||||||
}
|
}
|
||||||
|
|
||||||
/* IGNORE_FIR */
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// "Remove useless elvis operator" "true"
|
||||||
|
fun foo(a: String?) {
|
||||||
|
val b = a <caret>?: null
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// "Remove useless elvis operator" "true"
|
||||||
|
fun foo(a: String?) {
|
||||||
|
val b = a<caret>
|
||||||
|
}
|
||||||
@@ -7879,6 +7879,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
public void testUselessElvis() throws Exception {
|
public void testUselessElvis() throws Exception {
|
||||||
runTest("idea/testData/quickfix/expressions/uselessElvis.kt");
|
runTest("idea/testData/quickfix/expressions/uselessElvis.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("uselessElvisRightIsNull.kt")
|
||||||
|
public void testUselessElvisRightIsNull() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/expressions/uselessElvisRightIsNull.kt");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/foldTryCatch")
|
@TestMetadata("idea/testData/quickfix/foldTryCatch")
|
||||||
|
|||||||
Reference in New Issue
Block a user