Convert intention "a..b-1 -> a until b" into weak warning inspection
So #KT-17895 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
0fd42bc747
commit
5e265b3d17
@@ -1403,11 +1403,6 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.ReplaceRangeToWithUntilIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.ReplaceUntilWithRangeToIntention</className>
|
||||
<category>Kotlin</category>
|
||||
@@ -2188,6 +2183,14 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceRangeToWithUntilInspection"
|
||||
displayName="'rangeTo' or the '..' call can be replaced with 'until'"
|
||||
groupName="Kotlin"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.inspections
|
||||
|
||||
import com.intellij.codeInspection.LocalQuickFix
|
||||
import com.intellij.codeInspection.ProblemDescriptor
|
||||
import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.intentions.getArguments
|
||||
import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
private val REGEX_RANGE_TO = """kotlin.(Char|Byte|Short|Int|Long).rangeTo""".toRegex()
|
||||
|
||||
class ReplaceRangeToWithUntilInspection : AbstractKotlinInspection() {
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
override fun visitExpression(expression: KtExpression) {
|
||||
super.visitExpression(expression)
|
||||
|
||||
if (expression !is KtBinaryExpression && expression !is KtDotQualifiedExpression) return
|
||||
|
||||
val fqName = expression.getCallableDescriptor()?.fqNameUnsafe?.asString() ?: return
|
||||
if (!fqName.matches(REGEX_RANGE_TO)) return
|
||||
|
||||
if (expression.getArguments()?.second?.isMinusOne() != true) return
|
||||
|
||||
holder.registerProblem(
|
||||
expression,
|
||||
"'rangeTo' or the '..' call can be replaced with 'until'",
|
||||
ProblemHighlightType.WEAK_WARNING,
|
||||
ReplaceWithUntilQuickFix()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ReplaceWithUntilQuickFix : LocalQuickFix {
|
||||
|
||||
override fun getName() = "Replace with until"
|
||||
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val element = descriptor.psiElement as KtExpression
|
||||
val args = element.getArguments() ?: return
|
||||
element.replace(KtPsiFactory(element).createExpressionByPattern(
|
||||
"$0 until $1",
|
||||
args.first ?: return,
|
||||
(args.second as? KtBinaryExpression)?.left ?: return)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun KtExpression.isMinusOne(): Boolean {
|
||||
if (this !is KtBinaryExpression) return false
|
||||
if (operationToken != KtTokens.MINUS) return false
|
||||
|
||||
val right = right as? KtConstantExpression ?: return false
|
||||
val context = right.analyze(BodyResolveMode.PARTIAL)
|
||||
val constantValue = ConstantExpressionEvaluator.getConstant(right, context)?.toConstantValue(right.getType(context) ?: return false)
|
||||
val rightValue = (constantValue?.value as? Number)?.toInt() ?: return false
|
||||
return rightValue == 1
|
||||
}
|
||||
}
|
||||
@@ -1,83 +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.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
private fun KtExpression.getCallableDescriptor() = getResolvedCall(analyze())?.resultingDescriptor
|
||||
|
||||
private fun KtExpression.getArguments() = when (this) {
|
||||
is KtBinaryExpression -> this.left to this.right
|
||||
is KtDotQualifiedExpression -> this.receiverExpression to this.callExpression?.valueArguments?.singleOrNull()?.getArgumentExpression()
|
||||
else -> null
|
||||
}
|
||||
|
||||
private val REGEX_RANGE_TO = """kotlin.(Char|Byte|Short|Int|Long).rangeTo""".toRegex()
|
||||
|
||||
class ReplaceRangeToWithUntilIntention : SelfTargetingIntention<KtExpression>(
|
||||
KtExpression::class.java,
|
||||
"Replace with 'until' function call"
|
||||
) {
|
||||
|
||||
override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean {
|
||||
if (element !is KtBinaryExpression && element !is KtDotQualifiedExpression) return false
|
||||
|
||||
val fqName = element.getCallableDescriptor()?.fqNameUnsafe?.asString() ?: return false
|
||||
if (!fqName.matches(REGEX_RANGE_TO)) return false
|
||||
return element.getArguments()?.second?.isMinusOne() == true
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtExpression, editor: Editor?) {
|
||||
val args = element.getArguments() ?: return
|
||||
element.replace(KtPsiFactory(element).createExpressionByPattern("$0 until $1",
|
||||
args.first ?: return,
|
||||
(args.second as? KtBinaryExpression)?.left ?: return))
|
||||
}
|
||||
|
||||
private fun KtExpression.isMinusOne(): Boolean {
|
||||
if (this !is KtBinaryExpression) return false
|
||||
if (operationToken != KtTokens.MINUS) return false
|
||||
|
||||
val right = right as? KtConstantExpression ?: return false
|
||||
val context = right.analyze(BodyResolveMode.PARTIAL)
|
||||
val constantValue = ConstantExpressionEvaluator.getConstant(right, context)?.toConstantValue(right.getType(context) ?: return false)
|
||||
val rightValue = (constantValue?.value as? Number)?.toInt() ?: return false
|
||||
return rightValue == 1
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ReplaceUntilWithRangeToIntention : SelfTargetingIntention<KtExpression>(KtExpression::class.java, "Replace with '..' operator") {
|
||||
override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean {
|
||||
if (element !is KtBinaryExpression && element !is KtDotQualifiedExpression) return false
|
||||
val fqName = element.getCallableDescriptor()?.fqNameUnsafe?.asString() ?: return false
|
||||
return fqName == "kotlin.ranges.until"
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtExpression, editor: Editor?) {
|
||||
val args = element.getArguments() ?: return
|
||||
element.replace(KtPsiFactory(element).createExpressionByPattern("$0..$1 - 1", args.first ?: return, args.second ?: return))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
|
||||
internal fun KtExpression.getCallableDescriptor() = getResolvedCall(analyze())?.resultingDescriptor
|
||||
|
||||
internal fun KtExpression.getArguments() = when (this) {
|
||||
is KtBinaryExpression -> this.left to this.right
|
||||
is KtDotQualifiedExpression -> this.receiverExpression to this.callExpression?.valueArguments?.singleOrNull()?.getArgumentExpression()
|
||||
else -> null
|
||||
}
|
||||
|
||||
class ReplaceUntilWithRangeToIntention : SelfTargetingIntention<KtExpression>(KtExpression::class.java, "Replace with '..' operator") {
|
||||
override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean {
|
||||
if (element !is KtBinaryExpression && element !is KtDotQualifiedExpression) return false
|
||||
val fqName = element.getCallableDescriptor()?.fqNameUnsafe?.asString() ?: return false
|
||||
return fqName == "kotlin.ranges.until"
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtExpression, editor: Editor?) {
|
||||
val args = element.getArguments() ?: return
|
||||
element.replace(KtPsiFactory(element).createExpressionByPattern("$0..$1 - 1", args.first ?: return, args.second ?: return))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user