Minor: change highlight range for '...ToElvis' & '...ToSafeAccess' inspections
New range: from `if` keyword to right parenthesis Relates to #KT-19643 #KT-16067
This commit is contained in:
+2
-2
@@ -48,12 +48,12 @@ abstract class AbstractApplicabilityBasedInspection<TElement : KtElement>(
|
||||
inspectionText(element),
|
||||
isOnTheFly,
|
||||
inspectionHighlightType(element),
|
||||
inspectionRange(element),
|
||||
inspectionHighlightRangeInElement(element),
|
||||
LocalFix(fixText(element))
|
||||
)
|
||||
}
|
||||
|
||||
open fun inspectionRange(element: TElement): TextRange? = null
|
||||
open fun inspectionHighlightRangeInElement(element: TElement): TextRange? = null
|
||||
|
||||
open fun inspectionTarget(element: TElement): PsiElement = element
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.util.OperatorChecks
|
||||
|
||||
class AddOperatorModifierInspection : AbstractApplicabilityBasedInspection<KtNamedFunction>(KtNamedFunction::class.java) {
|
||||
override fun inspectionRange(element: KtNamedFunction) = element.nameIdentifier?.textRange?.shiftLeft(element.startOffset)
|
||||
override fun inspectionHighlightRangeInElement(element: KtNamedFunction) =
|
||||
element.nameIdentifier?.textRange?.shiftLeft(element.startOffset)
|
||||
|
||||
override fun inspectionText(element: KtNamedFunction) = "Function should have 'operator' modifier"
|
||||
|
||||
|
||||
+3
-8
@@ -21,11 +21,11 @@ import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.idea.core.setType
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.expressionComparedToNull
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.shouldBeTransformed
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.textRange
|
||||
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
@@ -41,7 +41,7 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti
|
||||
|
||||
override val defaultFixText: String = "Replace 'if' with elvis operator"
|
||||
|
||||
override fun inspectionRange(element: KtIfExpression): TextRange? = textRange(element)?.shiftLeft(element.startOffset)
|
||||
override fun inspectionHighlightRangeInElement(element: KtIfExpression): TextRange? = element.textRange().shiftLeft(element.startOffset)
|
||||
|
||||
override fun inspectionHighlightType(element: KtIfExpression): ProblemHighlightType =
|
||||
if (element.shouldBeTransformed()) ProblemHighlightType.GENERIC_ERROR_OR_WARNING else ProblemHighlightType.INFORMATION
|
||||
@@ -60,12 +60,7 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti
|
||||
val type = data.ifNullExpression.analyze().getType(data.ifNullExpression) ?: return null
|
||||
if (!type.isNothing()) return null
|
||||
|
||||
return textRange(element)
|
||||
}
|
||||
|
||||
private fun textRange(element: KtIfExpression): TextRange? {
|
||||
val rightOffset = element.rightParenthesis?.endOffset ?: return null
|
||||
return TextRange(element.startOffset, rightOffset)
|
||||
return element.textRange()
|
||||
}
|
||||
|
||||
fun isApplicable(element: KtIfExpression): Boolean = applicabilityRange(element) != null
|
||||
|
||||
+1
-5
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
|
||||
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
@@ -49,10 +48,7 @@ class IfThenToElvisInspection(
|
||||
)
|
||||
}
|
||||
|
||||
override fun inspectionRange(element: KtIfExpression): TextRange? = TextRange(
|
||||
element.ifKeyword.startOffset,
|
||||
element.condition?.endOffset ?: element.ifKeyword.endOffset
|
||||
).shiftLeft(element.startOffset)
|
||||
override fun inspectionHighlightRangeInElement(element: KtIfExpression): TextRange? = element.textRange().shiftLeft(element.startOffset)
|
||||
|
||||
override fun createOptionsPanel(): JComponent? = MultipleCheckboxOptionsPanel(this).also {
|
||||
it.addCheckbox("Report also on statement", "highlightStatement")
|
||||
|
||||
+3
-4
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.inspections.branchedTransformations
|
||||
import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
@@ -30,7 +31,6 @@ import org.jetbrains.kotlin.idea.refactoring.rename.KotlinVariableInplaceRenameH
|
||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
|
||||
@@ -40,7 +40,7 @@ class IfThenToSafeAccessInspection : AbstractApplicabilityBasedInspection<KtIfEx
|
||||
|
||||
override fun isApplicable(element: KtIfExpression): Boolean = isApplicableTo(element, expressionShouldBeStable = true)
|
||||
|
||||
override fun inspectionTarget(element: KtIfExpression) = element.ifKeyword
|
||||
override fun inspectionHighlightRangeInElement(element: KtIfExpression): TextRange? = element.textRange().shiftLeft(element.startOffset)
|
||||
|
||||
override fun inspectionText(element: KtIfExpression) = "Foldable if-then"
|
||||
|
||||
@@ -54,8 +54,7 @@ class IfThenToSafeAccessInspection : AbstractApplicabilityBasedInspection<KtIfEx
|
||||
override val startFixInWriteAction = false
|
||||
|
||||
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
|
||||
val ifExpression = element.getParentOfType<KtIfExpression>(true) ?: return
|
||||
convert(ifExpression, editor)
|
||||
convert(element as KtIfExpression, editor)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ abstract class ReplaceSubstringInspection :
|
||||
} else
|
||||
false
|
||||
|
||||
override fun inspectionRange(element: KtDotQualifiedExpression): TextRange? =
|
||||
override fun inspectionHighlightRangeInElement(element: KtDotQualifiedExpression): TextRange? =
|
||||
element.callExpression?.calleeExpression?.textRange?.shiftLeft(element.startOffset)
|
||||
|
||||
protected fun isIndexOfCall(expression: KtExpression?, expectedReceiver: KtExpression): Boolean {
|
||||
|
||||
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.search.LocalSearchScope
|
||||
import com.intellij.psi.search.searches.ReferencesSearch
|
||||
import org.jetbrains.kotlin.KtNodeTypes
|
||||
@@ -27,6 +28,8 @@ import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
@@ -312,6 +315,11 @@ internal fun KtIfExpression.shouldBeTransformed(): Boolean = when (val condition
|
||||
else -> false
|
||||
}
|
||||
|
||||
fun KtIfExpression.textRange(): TextRange {
|
||||
val rightOffset = rightParenthesis?.endOffset ?: return ifKeyword.textRange
|
||||
return TextRange(ifKeyword.startOffset, rightOffset)
|
||||
}
|
||||
|
||||
private fun KtExpression.checkedExpression() = when (this) {
|
||||
is KtBinaryExpression -> expressionComparedToNull()
|
||||
is KtIsExpression -> leftHandSide
|
||||
|
||||
Reference in New Issue
Block a user