i18n: make text from intention lazy

#KT-37483
This commit is contained in:
Dmitry Gridin
2020-04-03 21:05:26 +07:00
parent d8ca21d728
commit 25dfc6dd63
63 changed files with 343 additions and 565 deletions
@@ -28,11 +28,10 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.util.OperatorNameConventions
class OperatorToFunctionIntention :
SelfTargetingIntention<KtExpression>(
KtExpression::class.java,
KotlinIdeaAnalysisBundle.message("replace.overloaded.operator.with.function.call")
) {
class OperatorToFunctionIntention : SelfTargetingIntention<KtExpression>(
KtExpression::class.java,
KotlinIdeaAnalysisBundle.lazyMessage("replace.overloaded.operator.with.function.call"),
) {
companion object {
private fun isApplicableUnary(element: KtUnaryExpression, caretOffset: Int): Boolean {
if (element.baseExpression == null) return false
@@ -65,7 +64,8 @@ class OperatorToFunctionIntention :
return when (opRef.getReferencedNameElementType()) {
KtTokens.PLUS, KtTokens.MINUS, KtTokens.MUL, KtTokens.DIV, KtTokens.PERC, KtTokens.RANGE,
KtTokens.IN_KEYWORD, KtTokens.NOT_IN, KtTokens.PLUSEQ, KtTokens.MINUSEQ, KtTokens.MULTEQ, KtTokens.DIVEQ, KtTokens.PERCEQ,
KtTokens.GT, KtTokens.LT, KtTokens.GTEQ, KtTokens.LTEQ -> true
KtTokens.GT, KtTokens.LT, KtTokens.GTEQ, KtTokens.LTEQ
-> true
KtTokens.EQEQ, KtTokens.EXCLEQ -> listOf(element.left, element.right).none { it?.node?.elementType == KtNodeTypes.NULL }
KtTokens.EQ -> element.left is KtArrayAccessExpression
else -> false
@@ -30,7 +30,7 @@ abstract class SelfTargetingIntention<TElement : PsiElement>(
@Nls private val familyNameGetter: () -> String = textGetter,
) : IntentionAction {
@Deprecated("Replace with primary constructor", ReplaceWith("SelfTargetingIntention(elementType, { text }, { familyName })"))
@Deprecated("Replace with primary constructor", ReplaceWith("SelfTargetingIntention<TElement>(elementType, { text }, { familyName })"))
constructor(
elementType: Class<TElement>,
@Nls text: String,
@@ -130,7 +130,7 @@ abstract class SelfTargetingRangeIntention<TElement : PsiElement>(
@Nls familyNameGetter: () -> String = textGetter,
) : SelfTargetingIntention<TElement>(elementType, textGetter, familyNameGetter) {
@Deprecated("Replace with primary constructor", ReplaceWith("SelfTargetingRangeIntention(elementType, { text }, { familyName })"))
@Deprecated("Replace with primary constructor", ReplaceWith("SelfTargetingRangeIntention<TElement>(elementType, { text }, { familyName })"))
constructor(
elementType: Class<TElement>,
@Nls text: String,
@@ -153,7 +153,7 @@ abstract class SelfTargetingOffsetIndependentIntention<TElement : KtElement>(
@Deprecated(
"Replace with primary constructor",
ReplaceWith("SelfTargetingOffsetIndependentIntention(elementType, { text }, { familyName })")
ReplaceWith("SelfTargetingOffsetIndependentIntention<TElement>(elementType, { text }, { familyName })")
)
constructor(
elementType: Class<TElement>,
@@ -2181,4 +2181,7 @@ inspection.trailing.comma.add.trailing.comma=Add trailing comma
inspection.trailing.comma.missing.trailing.comma=Missing trailing comma
inspection.trailing.comma.fix.comma.position=Fix comma position
inspection.trailing.comma.comma.loses.the.advantages.in.this.position=Comma loses the advantages in this position
inspection.redundant.label.text=Redundant label
inspection.redundant.label.text=Redundant label
intention.convert.lambda.line=Convert to {0,choice,0#single|1#multi}-line lambda
intention.trailing.comma.custom.text={0,choice,0#Enable|1#Disable} a trailing comma by default in the formatter
intention.trailing.comma.text=Enable/disable a trailing comma in the formatter
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -103,10 +92,10 @@ abstract class AbstractAddAccessorsIntention(
}
}
private fun createFamilyName(addGetter: Boolean, addSetter: Boolean): String = when {
addGetter && addSetter -> KotlinBundle.message("text.add.getter.and.setter")
addGetter -> KotlinBundle.message("text.add.getter")
addSetter -> KotlinBundle.message("text.add.setter")
private fun createFamilyName(addGetter: Boolean, addSetter: Boolean): () -> String = when {
addGetter && addSetter -> KotlinBundle.lazyMessage("text.add.getter.and.setter")
addGetter -> KotlinBundle.lazyMessage("text.add.getter")
addSetter -> KotlinBundle.lazyMessage("text.add.setter")
else -> throw AssertionError("At least one from (addGetter, addSetter) should be true")
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2000-2020 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.
*/
@@ -26,14 +26,15 @@ import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class AddAnnotationUseSiteTargetIntention : SelfTargetingIntention<KtAnnotationEntry>(
KtAnnotationEntry::class.java, KotlinBundle.message("add.use.site.target")
KtAnnotationEntry::class.java,
KotlinBundle.lazyMessage("add.use.site.target")
) {
override fun isApplicableTo(element: KtAnnotationEntry, caretOffset: Int): Boolean {
val useSiteTargets = element.applicableUseSiteTargets()
if (useSiteTargets.isEmpty()) return false
if (useSiteTargets.size == 1) {
text = KotlinBundle.message("text.add.use.site.target.0", useSiteTargets.first().renderName)
setTextGetter(KotlinBundle.lazyMessage("text.add.use.site.target.0", useSiteTargets.first().renderName))
}
return true
}
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -28,7 +17,7 @@ import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*
class AddBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class.java, KotlinBundle.message("add.braces")) {
class AddBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class.java, KotlinBundle.lazyMessage("add.braces")) {
override fun isApplicableTo(element: KtElement, caretOffset: Int): Boolean {
val expression = element.getTargetExpression(caretOffset) ?: return false
if (expression is KtBlockExpression) return false
@@ -36,11 +25,11 @@ class AddBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class.ja
return when (val parent = expression.parent) {
is KtContainerNode -> {
val description = parent.description() ?: return false
text = KotlinBundle.message("add.braces.to.0.statement", description)
setTextGetter(KotlinBundle.lazyMessage("add.braces.to.0.statement", description))
true
}
is KtWhenEntry -> {
text = KotlinBundle.message("add.braces.to.when.entry")
setTextGetter(KotlinBundle.lazyMessage("add.braces.to.when.entry"))
true
}
else -> {
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2014 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.
* Copyright 2010-2020 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.intentions
@@ -34,9 +23,8 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class AddForLoopIndicesIntention : SelfTargetingRangeIntention<KtForExpression>(
KtForExpression::class.java,
KotlinBundle.message("add.indices.to.for.loop")
),
LowPriorityAction {
KotlinBundle.lazyMessage("add.indices.to.for.loop"),
), LowPriorityAction {
private val WITH_INDEX_NAME = "withIndex"
private val WITH_INDEX_FQ_NAMES: Set<String> by lazy {
sequenceOf("collections", "sequences", "text", "ranges").map { "kotlin.$it.$WITH_INDEX_NAME" }.toSet()
@@ -88,8 +76,7 @@ class AddForLoopIndicesIntention : SelfTargetingRangeIntention<KtForExpression>(
val templateBuilder = TemplateBuilderImpl(forExpression)
templateBuilder.replaceElement(indexVariable, ChooseStringExpression(listOf("index", "i")))
val body = forExpression.body
when (body) {
when (val body = forExpression.body) {
is KtBlockExpression -> {
val statement = body.statements.firstOrNull()
if (statement != null) {
@@ -107,10 +94,9 @@ class AddForLoopIndicesIntention : SelfTargetingRangeIntention<KtForExpression>(
templateBuilder.run(editor, true)
}
private fun createWithIndexExpression(originalExpression: KtExpression, reformat: Boolean): KtExpression {
return KtPsiFactory(originalExpression).createExpressionByPattern(
private fun createWithIndexExpression(originalExpression: KtExpression, reformat: Boolean): KtExpression =
KtPsiFactory(originalExpression).createExpressionByPattern(
"$0.$WITH_INDEX_NAME()", originalExpression,
reformat = reformat
)
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -21,9 +21,9 @@ import org.jetbrains.kotlin.utils.addToStdlib.safeAs
private val annotationFqName = FqName("kotlin.jvm.JvmOverloads")
class AddJvmOverloadsIntention : SelfTargetingIntention<KtModifierListOwner>(
KtModifierListOwner::class.java, KotlinBundle.message("add.jvmoverloads.annotation")
KtModifierListOwner::class.java,
KotlinBundle.lazyMessage("add.jvmoverloads.annotation"),
), LowPriorityAction {
override fun isApplicableTo(element: KtModifierListOwner, caretOffset: Int): Boolean {
val (targetName, parameters) = when (element) {
is KtNamedFunction -> {
@@ -59,7 +59,7 @@ class AddJvmOverloadsIntention : SelfTargetingIntention<KtModifierListOwner>(
else -> return false
}
text = KotlinBundle.message("add.jvmoverloads.annotation.to.0", targetName)
setTextGetter(KotlinBundle.lazyMessage("add.jvmoverloads.annotation.to.0", targetName))
return TargetPlatformDetector.getPlatform(element.containingKtFile).isJvm()
&& parameters.any { it.hasDefaultValue() }
@@ -32,7 +32,7 @@ import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
class AddJvmStaticIntention : SelfTargetingRangeIntention<KtNamedDeclaration>(
KtNamedDeclaration::class.java,
KotlinBundle.message("add.jvmstatic.annotation")
KotlinBundle.lazyMessage("add.jvmstatic.annotation")
), LowPriorityAction {
private val JvmStaticFqName = FqName("kotlin.jvm.JvmStatic")
private val JvmFieldFqName = FqName("kotlin.jvm.JvmField")
@@ -19,8 +19,8 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
class AddLabeledReturnInLambdaIntention : SelfTargetingRangeIntention<KtBlockExpression>(
KtBlockExpression::class.java,
KotlinBundle.message("add.labeled.return.to.last.expression.in.a.lambda")
), LowPriorityAction {
{ KotlinBundle.message("add.labeled.return.to.last.expression.in.a.lambda") },
{ text }), LowPriorityAction {
override fun applicabilityRange(element: KtBlockExpression): TextRange? {
if (!isApplicableTo(element)) return null
val labelName = element.getParentLambdaLabelName() ?: return null
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -16,11 +16,10 @@ import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createDestructuringDeclarationByPattern
class AddMissingDestructuringIntention :
SelfTargetingIntention<KtDestructuringDeclaration>(
KtDestructuringDeclaration::class.java,
KotlinBundle.message("add.missing.component")
) {
class AddMissingDestructuringIntention : SelfTargetingIntention<KtDestructuringDeclaration>(
KtDestructuringDeclaration::class.java,
KotlinBundle.lazyMessage("add.missing.component")
) {
override fun isApplicableTo(element: KtDestructuringDeclaration, caretOffset: Int): Boolean {
val entriesCount = element.entries.size
@@ -41,16 +40,18 @@ class AddMissingDestructuringIntention :
)
val entriesSize = entries.size
val newEntries = entries.joinToString(postfix = if (entriesSize == 0) "" else ", ") { it.text } +
primaryParameters.asSequence().drop(entriesSize).joinToString {
KotlinNameSuggester.suggestNameByName(it.name.asString(), nameValidator)
}
val initializer = element.initializer ?: return
val newEntries = entries.joinToString(postfix = if (entriesSize == 0) "" else ", ") {
it.text
} + primaryParameters.asSequence().drop(entriesSize).joinToString {
KotlinNameSuggester.suggestNameByName(it.name.asString(), nameValidator)
}
val initializer = element.initializer ?: return
val newDestructuringDeclaration = factory.createDestructuringDeclarationByPattern(
if (element.isVar) "var ($0) = $1" else "val ($0) = $1",
newEntries, initializer
)
element.replace(newDestructuringDeclaration)
}
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -38,9 +27,8 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument
class AddNameToArgumentIntention : SelfTargetingIntention<KtValueArgument>(
KtValueArgument::class.java, KotlinBundle.message("add.name.to.argument")
KtValueArgument::class.java, KotlinBundle.lazyMessage("add.name.to.argument")
), LowPriorityAction {
override fun isApplicableTo(element: KtValueArgument, caretOffset: Int): Boolean {
val expression = element.getArgumentExpression() ?: return false
val name = detectNameToAdd(
@@ -48,7 +36,7 @@ class AddNameToArgumentIntention : SelfTargetingIntention<KtValueArgument>(
shouldBeLastUnnamed = !element.languageVersionSettings.supportsFeature(LanguageFeature.MixedNamedArgumentsInTheirOwnPosition)
) ?: return false
text = KotlinBundle.message("add.0.to.argument", name)
setTextGetter(KotlinBundle.lazyMessage("add.0.to.argument", name))
if (expression is KtLambdaExpression) {
val range = expression.textRange
@@ -58,8 +46,9 @@ class AddNameToArgumentIntention : SelfTargetingIntention<KtValueArgument>(
return true
}
override fun allowCaretInsideElement(element: PsiElement) =
element !is KtValueArgumentList && element !is KtContainerNode && super.allowCaretInsideElement(element)
override fun allowCaretInsideElement(element: PsiElement) = element !is KtValueArgumentList &&
element !is KtContainerNode &&
super.allowCaretInsideElement(element)
override fun applyTo(element: KtValueArgument, editor: Editor?) {
apply(element)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.psi.KtValueArgument
import org.jetbrains.kotlin.psi.KtValueArgumentList
class AddNamesToFollowingArgumentsIntention : SelfTargetingIntention<KtValueArgument>(
KtValueArgument::class.java, KotlinBundle.message("add.names.to.this.argument.and.following.arguments")
KtValueArgument::class.java, KotlinBundle.lazyMessage("add.names.to.this.argument.and.following.arguments")
), LowPriorityAction {
override fun isApplicableTo(element: KtValueArgument, caretOffset: Int): Boolean {
val argumentList = element.parent as? KtValueArgumentList ?: return false
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -16,19 +16,16 @@ import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
class AddOpenModifierIntention : SelfTargetingIntention<KtCallableDeclaration>(
KtCallableDeclaration::class.java, KotlinBundle.message("make.open")
KtCallableDeclaration::class.java, KotlinBundle.lazyMessage("make.open")
), LowPriorityAction {
override fun isApplicableTo(element: KtCallableDeclaration, caretOffset: Int): Boolean {
if (element !is KtProperty && element !is KtNamedFunction) {
return false
}
if (element !is KtProperty && element !is KtNamedFunction) return false
if (element.hasModifier(KtTokens.OPEN_KEYWORD)
|| element.hasModifier(KtTokens.ABSTRACT_KEYWORD)
|| element.hasModifier(KtTokens.PRIVATE_KEYWORD)
) {
return false
}
) return false
val implicitModality = element.implicitModality()
if (implicitModality == KtTokens.OPEN_KEYWORD || implicitModality == KtTokens.ABSTRACT_KEYWORD) return false
val ktClassOrObject = element.containingClassOrObject ?: return false
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -27,9 +27,8 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.getAbbreviatedType
class AddThrowsAnnotationIntention : SelfTargetingIntention<KtThrowExpression>(
KtThrowExpression::class.java, KotlinBundle.message("add.throws.annotation")
KtThrowExpression::class.java, KotlinBundle.lazyMessage("add.throws.annotation")
) {
override fun isApplicableTo(element: KtThrowExpression, caretOffset: Int): Boolean {
if (!element.platform.isJvm()) return false
val containingDeclaration = element.getContainingDeclaration() ?: return false
@@ -115,19 +114,16 @@ private fun KtDeclaration.findThrowsAnnotation(context: BindingContext): KtAnnot
}
}
private fun ValueArgument.hasType(type: KotlinType, context: BindingContext): Boolean {
val argumentExpression = getArgumentExpression()
val expressions = when (argumentExpression) {
private fun ValueArgument.hasType(type: KotlinType, context: BindingContext): Boolean =
when (val argumentExpression = getArgumentExpression()) {
is KtClassLiteralExpression -> listOf(argumentExpression)
is KtCollectionLiteralExpression -> argumentExpression.getInnerExpressions().filterIsInstance(KtClassLiteralExpression::class.java)
is KtCallExpression -> argumentExpression.valueArguments.mapNotNull { it.getArgumentExpression() as? KtClassLiteralExpression }
else -> emptyList()
}
return expressions.any { it.getType(context)?.arguments?.firstOrNull()?.type == type }
}
}.any { it.getType(context)?.arguments?.firstOrNull()?.type == type }
private fun KtPsiFactory.createCollectionLiteral(expressions: List<KtExpression>, lastExpression: String): KtCollectionLiteralExpression {
return buildExpression {
private fun KtPsiFactory.createCollectionLiteral(expressions: List<KtExpression>, lastExpression: String): KtCollectionLiteralExpression =
buildExpression {
appendFixedText("[")
expressions.forEach {
appendExpression(it)
@@ -135,5 +131,4 @@ private fun KtPsiFactory.createCollectionLiteral(expressions: List<KtExpression>
}
appendFixedText(lastExpression)
appendFixedText("]")
} as KtCollectionLiteralExpression
}
} as KtCollectionLiteralExpression
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -11,7 +11,7 @@ import org.jetbrains.kotlin.idea.quickfix.AddWhenRemainingBranchesFix
import org.jetbrains.kotlin.psi.KtWhenExpression
class AddWhenRemainingBranchesIntention : SelfTargetingIntention<KtWhenExpression>(
KtWhenExpression::class.java, KotlinBundle.message("add.remaining.branches")
KtWhenExpression::class.java, KotlinBundle.lazyMessage("add.remaining.branches")
) {
override fun isApplicableTo(element: KtWhenExpression, caretOffset: Int): Boolean {
if (element.entries.none { it.isElse }) return false
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -17,7 +17,6 @@ import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
class ConvertArrayParameterToVarargIntention : SelfTargetingIntention<KtParameter>(
KtParameter::class.java, DEFAULT_TEXT
) {
override fun isApplicableTo(element: KtParameter, caretOffset: Int): Boolean {
val typeReference = element.getChildOfType<KtTypeReference>() ?: return false
if (element.parent.parent is KtFunctionLiteral) return false
@@ -26,20 +25,22 @@ class ConvertArrayParameterToVarargIntention : SelfTargetingIntention<KtParamete
val type = element.descriptor?.type ?: return false
return when {
KotlinBuiltIns.isPrimitiveArray(type) -> {
text = DEFAULT_TEXT
setTextGetter(DEFAULT_TEXT)
true
}
KotlinBuiltIns.isArray(type) -> {
val typeArgument = typeReference.typeElement?.typeArgumentsAsTypes?.firstOrNull()
val typeProjection = typeArgument?.parent as? KtTypeProjection
if (typeProjection?.hasModifier(KtTokens.IN_KEYWORD) == false) {
text = if (!typeProjection.hasModifier(KtTokens.OUT_KEYWORD)
&& !KotlinBuiltIns.isPrimitiveType(element.builtIns.getArrayElementType(type))
) {
BREAKING_TEXT
} else {
DEFAULT_TEXT
}
setTextGetter(
if (!typeProjection.hasModifier(KtTokens.OUT_KEYWORD)
&& !KotlinBuiltIns.isPrimitiveType(element.builtIns.getArrayElementType(type))
) {
BREAKING_TEXT
} else {
DEFAULT_TEXT
}
)
true
} else {
false
@@ -61,9 +62,9 @@ class ConvertArrayParameterToVarargIntention : SelfTargetingIntention<KtParamete
}
companion object {
private val DEFAULT_TEXT: String get() = KotlinBundle.message("convert.to.vararg.parameter")
private val DEFAULT_TEXT: () -> String get() = KotlinBundle.lazyMessage("convert.to.vararg.parameter")
private val BREAKING_TEXT: String get() = KotlinBundle.message("0.may.break.code", DEFAULT_TEXT)
private val BREAKING_TEXT: () -> String get() = KotlinBundle.lazyMessage("0.may.break.code", DEFAULT_TEXT)
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -14,11 +14,9 @@ import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtPsiFactory
class ConvertBlockCommentToLineCommentIntention : SelfTargetingIntention<PsiComment>(
PsiComment::class.java, KotlinBundle.message("replace.with.end.of.line.comment")
PsiComment::class.java, KotlinBundle.lazyMessage("replace.with.end.of.line.comment")
) {
override fun isApplicableTo(element: PsiComment, caretOffset: Int): Boolean {
return element.isBlockComment()
}
override fun isApplicableTo(element: PsiComment, caretOffset: Int): Boolean = element.isBlockComment()
override fun applyTo(element: PsiComment, editor: Editor?) {
val psiFactory = KtPsiFactory(element)
@@ -36,6 +34,7 @@ class ConvertBlockCommentToLineCommentIntention : SelfTargetingIntention<PsiComm
.trim()
.split("\n")
.reversed()
val lastIndex = comments.size - 1
val parent = element.parent
comments.forEachIndexed { index, comment ->
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
class ConvertCollectionConstructorToFunction : SelfTargetingIntention<KtCallExpression>(
KtCallExpression::class.java, KotlinBundle.message("convert.collection.constructor.to.function")
KtCallExpression::class.java, KotlinBundle.lazyMessage("convert.collection.constructor.to.function")
) {
private val functionMap = hashMapOf(
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -58,7 +47,7 @@ import org.jetbrains.kotlin.types.typeUtil.supertypes
import java.util.*
class ConvertFunctionToPropertyIntention :
SelfTargetingIntention<KtNamedFunction>(KtNamedFunction::class.java, KotlinBundle.message("convert.function.to.property")),
SelfTargetingIntention<KtNamedFunction>(KtNamedFunction::class.java, KotlinBundle.lazyMessage("convert.function.to.property")),
LowPriorityAction {
private var KtNamedFunction.typeFqNameToAdd: String? by UserDataProperty(Key.create("TYPE_FQ_NAME_TO_ADD"))
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiComment
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.inspections.RedundantSemicolonInspection
import org.jetbrains.kotlin.idea.refactoring.getLineNumber
import org.jetbrains.kotlin.lexer.KtTokens
@@ -17,7 +18,8 @@ import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespace
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespace
sealed class ConvertLambdaLineIntention(private val toMultiLine: Boolean) : SelfTargetingIntention<KtLambdaExpression>(
KtLambdaExpression::class.java, "Convert to ${if (toMultiLine) "multi" else "single"}-line lambda"
KtLambdaExpression::class.java,
KotlinBundle.lazyMessage("intention.convert.lambda.line", 1.takeIf { toMultiLine } ?: 0),
) {
override fun isApplicableTo(element: KtLambdaExpression, caretOffset: Int): Boolean {
val functionLiteral = element.functionLiteral
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -19,14 +19,12 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.typeUtil.makeNullable
class ConvertLateinitPropertyToNullableIntention : SelfTargetingIntention<KtProperty>(
KtProperty::class.java, KotlinBundle.message("convert.to.nullable.var")
KtProperty::class.java, KotlinBundle.lazyMessage("convert.to.nullable.var")
) {
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean {
return element.hasModifier(KtTokens.LATEINIT_KEYWORD)
&& element.isVar
&& element.typeReference?.typeElement !is KtNullableType
&& element.initializer == null
}
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean = element.hasModifier(KtTokens.LATEINIT_KEYWORD)
&& element.isVar
&& element.typeReference?.typeElement !is KtNullableType
&& element.initializer == null
override fun applyTo(element: KtProperty, editor: Editor?) {
val typeReference: KtTypeReference = element.typeReference ?: return
@@ -1,6 +1,6 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
* Copyright 2010-2020 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.intentions
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.allChildren
class ConvertLazyPropertyToOrdinaryIntention : SelfTargetingIntention<KtProperty>(
KtProperty::class.java, KotlinBundle.message("convert.to.ordinary.property")
KtProperty::class.java, KotlinBundle.lazyMessage("convert.to.ordinary.property")
) {
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean {
val delegateExpression = element.delegate?.expression as? KtCallExpression ?: return false
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -16,12 +16,11 @@ import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespace
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespace
class ConvertLineCommentToBlockCommentIntention : SelfTargetingIntention<PsiComment>(
PsiComment::class.java, KotlinBundle.message("replace.with.block.comment")
PsiComment::class.java,
KotlinBundle.lazyMessage("replace.with.block.comment")
) {
override fun isApplicableTo(element: PsiComment, caretOffset: Int): Boolean {
return element.isEndOfLineComment()
}
override fun isApplicableTo(element: PsiComment, caretOffset: Int): Boolean = element.isEndOfLineComment()
override fun applyTo(element: PsiComment, editor: Editor?) {
val project = element.project
@@ -59,11 +58,7 @@ private fun PsiElement.isEndOfLineComment() = node.elementType == KtTokens.EOL_C
private fun PsiComment.commentText() = text.substring(2).replace("/*", "/ *").replace("*/", "* /").trim()
private fun PsiComment.nextComment(): PsiComment? {
return (getNextSiblingIgnoringWhitespace() as? PsiComment)?.takeIf { it.isEndOfLineComment() }
}
private fun PsiComment.nextComment(): PsiComment? = (getNextSiblingIgnoringWhitespace() as? PsiComment)?.takeIf { it.isEndOfLineComment() }
private fun PsiComment.prevComment(): PsiComment? {
return (getPrevSiblingIgnoringWhitespace() as? PsiComment)?.takeIf { it.isEndOfLineComment() }
}
private fun PsiComment.prevComment(): PsiComment? = (getPrevSiblingIgnoringWhitespace() as? PsiComment)?.takeIf { it.isEndOfLineComment() }
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
class ConvertNullablePropertyToLateinitIntention : SelfTargetingIntention<KtProperty>(
KtProperty::class.java, KotlinBundle.message("convert.to.lateinit.var")
KtProperty::class.java, KotlinBundle.lazyMessage("convert.to.lateinit.var")
) {
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean {
if (element.hasModifier(KtTokens.LATEINIT_KEYWORD) || element.hasModifier(KtTokens.ABSTRACT_KEYWORD)) return false
@@ -1,6 +1,6 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
* Copyright 2010-2020 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.intentions
@@ -15,11 +15,10 @@ import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
class ConvertOrdinaryPropertyToLazyIntention : SelfTargetingIntention<KtProperty>(
KtProperty::class.java, KotlinBundle.message("convert.to.lazy.property")
KtProperty::class.java, KotlinBundle.lazyMessage("convert.to.lazy.property")
) {
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean {
return !element.isVar && element.initializer != null && element.getter == null && !element.isLocal
}
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean =
!element.isVar && element.initializer != null && element.getter == null && !element.isLocal
override fun applyTo(element: KtProperty, editor: Editor?) {
val initializer = element.initializer ?: return
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -31,7 +20,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class ConvertParameterToReceiverIntention : SelfTargetingIntention<KtParameter>(
KtParameter::class.java,
KotlinBundle.message("convert.parameter.to.receiver")
KotlinBundle.lazyMessage("convert.parameter.to.receiver")
) {
override fun isApplicableTo(element: KtParameter, caretOffset: Int): Boolean {
val identifier = element.nameIdentifier ?: return false
@@ -41,15 +30,14 @@ class ConvertParameterToReceiverIntention : SelfTargetingIntention<KtParameter>(
return function.valueParameterList == element.parent && function.receiverTypeReference == null
}
private fun configureChangeSignature(parameterIndex: Int): KotlinChangeSignatureConfiguration {
return object : KotlinChangeSignatureConfiguration {
private fun configureChangeSignature(parameterIndex: Int): KotlinChangeSignatureConfiguration =
object : KotlinChangeSignatureConfiguration {
override fun configure(originalDescriptor: KotlinMethodDescriptor): KotlinMethodDescriptor {
return originalDescriptor.modify { it.receiver = originalDescriptor.parameters[parameterIndex] }
}
override fun performSilently(affectedFunctions: Collection<PsiElement>) = true
}
}
override fun startInWriteAction() = false
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -28,7 +28,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.parents
class ConvertPrimaryConstructorToSecondaryIntention : SelfTargetingIntention<KtPrimaryConstructor>(
KtPrimaryConstructor::class.java,
KotlinBundle.message("convert.to.secondary.constructor")
KotlinBundle.lazyMessage("convert.to.secondary.constructor")
) {
override fun isApplicableTo(element: KtPrimaryConstructor, caretOffset: Int): Boolean {
val containingClass = element.containingClassOrObject as? KtClass ?: return false
@@ -38,15 +38,15 @@ class ConvertPrimaryConstructorToSecondaryIntention : SelfTargetingIntention<KtP
return element.valueParameters.all { !it.hasValOrVar() || (it.name != null && it.annotationEntries.isEmpty()) }
}
private fun KtReferenceExpression.isIndependent(classDescriptor: ClassDescriptor, context: BindingContext): Boolean {
val referencedDescriptor = context[BindingContext.REFERENCE_TARGET, this] ?: return false
return when (referencedDescriptor) {
private fun KtReferenceExpression.isIndependent(classDescriptor: ClassDescriptor, context: BindingContext): Boolean =
when (val referencedDescriptor = context[BindingContext.REFERENCE_TARGET, this]) {
null ->
false
is ValueParameterDescriptor ->
(referencedDescriptor.containingDeclaration as? ConstructorDescriptor)?.containingDeclaration != classDescriptor
else ->
classDescriptor !in referencedDescriptor.parents
}
}
private fun KtProperty.isIndependent(klass: KtClass, context: BindingContext): Boolean {
val propertyInitializer = initializer ?: return true
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -15,7 +15,7 @@ import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.psi.psiUtil.endOffset
class ConvertPropertyGetterToInitializerIntention : SelfTargetingIntention<KtPropertyAccessor>(
KtPropertyAccessor::class.java, KotlinBundle.message("convert.property.getter.to.initializer")
KtPropertyAccessor::class.java, KotlinBundle.lazyMessage("convert.property.getter.to.initializer")
) {
override fun isApplicableTo(element: KtPropertyAccessor, caretOffset: Int): Boolean {
@@ -41,10 +41,7 @@ class ConvertPropertyGetterToInitializerIntention : SelfTargetingIntention<KtPro
}
}
private fun KtPropertyAccessor.singleExpression(): KtExpression? {
val bodyExpression = this.bodyExpression
return if (bodyExpression is KtBlockExpression)
(bodyExpression.statements.singleOrNull() as? KtReturnExpression)?.returnedExpression
else
bodyExpression
private fun KtPropertyAccessor.singleExpression(): KtExpression? = when (val bodyExpression = bodyExpression) {
is KtBlockExpression -> (bodyExpression.statements.singleOrNull() as? KtReturnExpression)?.returnedExpression
else -> bodyExpression
}
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -50,9 +39,8 @@ import java.util.*
class ConvertPropertyToFunctionIntention : SelfTargetingIntention<KtProperty>(
KtProperty::class.java,
KotlinBundle.message("convert.property.to.function")
),
LowPriorityAction {
KotlinBundle.lazyMessage("convert.property.to.function")
), LowPriorityAction {
private inner class Converter(
project: Project,
descriptor: CallableDescriptor
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -33,7 +22,8 @@ import org.jetbrains.kotlin.types.typeUtil.isNothing
import org.jetbrains.kotlin.types.typeUtil.isUnit
class ConvertToBlockBodyIntention : SelfTargetingIntention<KtDeclarationWithBody>(
KtDeclarationWithBody::class.java, KotlinBundle.message("convert.to.block.body")
KtDeclarationWithBody::class.java,
KotlinBundle.lazyMessage("convert.to.block.body")
) {
override fun isApplicableTo(element: KtDeclarationWithBody, caretOffset: Int): Boolean {
if (element is KtFunctionLiteral || element.hasBlockBody() || !element.hasBody()) return false
@@ -58,7 +48,6 @@ class ConvertToBlockBodyIntention : SelfTargetingIntention<KtDeclarationWithBody
}
companion object {
fun convert(declaration: KtDeclarationWithBody): KtDeclarationWithBody {
val body = declaration.bodyExpression!!
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -29,7 +18,7 @@ import java.util.*
class ConvertToForEachFunctionCallIntention : SelfTargetingIntention<KtForExpression>(
KtForExpression::class.java,
KotlinBundle.message("replace.with.a.foreach.function.call")
KotlinBundle.lazyMessage("replace.with.a.foreach.function.call")
) {
override fun isApplicableTo(element: KtForExpression, caretOffset: Int): Boolean {
val rParen = element.rightParenthesis ?: return false
@@ -42,18 +31,19 @@ class ConvertToForEachFunctionCallIntention : SelfTargetingIntention<KtForExpres
val labelName = element.getLabelName()
val body = element.body!!
val loopParameter = element.loopParameter!!
val body = element.body ?: return
val loopParameter = element.loopParameter ?: return
val loopRange = element.loopRange ?: return
val functionBodyArgument: Any = (body as? KtBlockExpression)?.contentRange() ?: body
val psiFactory = KtPsiFactory(element)
val foreachExpression = psiFactory.createExpressionByPattern(
"$0.forEach{$1->\n$2}", element.loopRange!!, loopParameter, functionBodyArgument
"$0.forEach{$1->\n$2}", loopRange, loopParameter, functionBodyArgument
)
val result = element.replace(foreachExpression) as KtElement
result.findDescendantOfType<KtFunctionLiteral>()!!.getContinuesWithLabel(labelName).forEach {
val result = element.replace(foreachExpression) as KtElement
result.findDescendantOfType<KtFunctionLiteral>()?.getContinuesWithLabel(labelName)?.forEach {
it.replace(psiFactory.createExpression("return@forEach"))
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -25,10 +25,10 @@ import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.siblings
sealed class ConvertToScopeIntention(
private val scopeFunction: ScopeFunction
) : SelfTargetingIntention<KtExpression>(KtExpression::class.java, KotlinBundle.message("convert.to.0", scopeFunction.functionName)) {
sealed class ConvertToScopeIntention(private val scopeFunction: ScopeFunction) : SelfTargetingIntention<KtExpression>(
KtExpression::class.java,
KotlinBundle.lazyMessage("convert.to.0", scopeFunction.functionName)
) {
enum class ScopeFunction(val functionName: String, val isParameterScope: Boolean) {
ALSO(functionName = "also", isParameterScope = true),
APPLY(functionName = "apply", isParameterScope = false),
@@ -87,8 +87,10 @@ sealed class ConvertToScopeIntention(
val psiFactory = KtPsiFactory(expressionToApply)
val (scopeFunctionCall, block) =
createScopeFunctionCall(psiFactory, refactoringTarget.targetElement) ?: return false
val (scopeFunctionCall, block) = createScopeFunctionCall(
psiFactory,
refactoringTarget.targetElement
) ?: return false
replaceReference(referenceElement, refactoringTarget.targetElementValue, lastTarget, psiFactory)
@@ -110,6 +112,7 @@ sealed class ConvertToScopeIntention(
val thisDotSomethingExpressions = block.collectDescendantsOfType<KtDotQualifiedExpression> {
it.receiverExpression is KtThisExpression && it.selectorExpression !== null
}
thisDotSomethingExpressions.forEach { thisDotSomethingExpression ->
thisDotSomethingExpression.selectorExpression?.let { selector ->
thisDotSomethingExpression.replace(selector)
@@ -211,11 +214,10 @@ sealed class ConvertToScopeIntention(
}
}
private fun KtExpression.collectTargetElements(referenceName: String, forward: Boolean): Sequence<PsiElement> {
return siblings(forward, withItself = false)
private fun KtExpression.collectTargetElements(referenceName: String, forward: Boolean): Sequence<PsiElement> =
siblings(forward, withItself = false)
.filter { it !is PsiWhiteSpace && it !is PsiComment && !(it is LeafPsiElement && it.elementType == KtTokens.SEMICOLON) }
.takeWhile { it.isTarget(referenceName) }
}
private fun PsiElement.isTarget(referenceName: String): Boolean {
when (this) {
@@ -246,18 +248,15 @@ sealed class ConvertToScopeIntention(
}
else -> return false
}
return !anyDescendantOfType<KtNameReferenceExpression> { it.text == scopeFunction.receiver }
}
private fun KtExpression.prevProperty(): KtProperty? {
val blockChildExpression = PsiTreeUtil.findFirstParent(this) {
it.parent is KtBlockExpression
} ?: return null
return blockChildExpression
.siblings(forward = false, withItself = true)
.firstOrNull { it is KtProperty && it.isLocal } as? KtProperty
private fun KtExpression.prevProperty(): KtProperty? = PsiTreeUtil.findFirstParent(this) {
it.parent is KtBlockExpression
}
?.siblings(forward = false, withItself = true)
?.firstOrNull { it is KtProperty && it.isLocal } as? KtProperty
private fun createScopeFunctionCall(factory: KtPsiFactory, element: PsiElement): ScopedFunctionCallAndBlock? {
val scopeFunctionName = scopeFunction.functionName
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2000-2020 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.
*/
@@ -17,7 +17,8 @@ import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
class ConvertUnsafeCastCallToUnsafeCastIntention : SelfTargetingIntention<KtDotQualifiedExpression>(
KtDotQualifiedExpression::class.java, KotlinBundle.message("convert.to.unsafe.cast")
KtDotQualifiedExpression::class.java,
KotlinBundle.lazyMessage("convert.to.unsafe.cast"),
) {
override fun isApplicableTo(element: KtDotQualifiedExpression, caretOffset: Int): Boolean {
@@ -29,7 +30,7 @@ class ConvertUnsafeCastCallToUnsafeCastIntention : SelfTargetingIntention<KtDotQ
val type = element.callExpression?.typeArguments?.singleOrNull() ?: return false
text = KotlinBundle.message("convert.to.0.as.1", element.receiverExpression.text, type.text)
setTextGetter(KotlinBundle.lazyMessage("convert.to.0.as.1", element.receiverExpression.text, type.text))
return true
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2000-2020 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.
*/
@@ -19,9 +19,9 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.TypeUtils
class ConvertUnsafeCastToUnsafeCastCallIntention : SelfTargetingIntention<KtBinaryExpressionWithTypeRHS>(
KtBinaryExpressionWithTypeRHS::class.java, KotlinBundle.message("convert.to.unsafecast.call")
KtBinaryExpressionWithTypeRHS::class.java,
KotlinBundle.lazyMessage("convert.to.unsafecast.call"),
) {
override fun isApplicableTo(element: KtBinaryExpressionWithTypeRHS, caretOffset: Int): Boolean {
if (!element.platform.isJs()) return false
@@ -32,7 +32,7 @@ class ConvertUnsafeCastToUnsafeCastCallIntention : SelfTargetingIntention<KtBina
val type = context[BindingContext.TYPE, right] ?: return false
if (TypeUtils.isNullableType(type)) return false
text = KotlinBundle.message("convert.to.0.unsafecast.1", element.left.text, right.text)
setTextGetter(KotlinBundle.lazyMessage("convert.to.0.unsafecast.1", element.left.text, right.text))
return true
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -16,9 +16,9 @@ import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
class ConvertVarargParameterToArrayIntention : SelfTargetingIntention<KtParameter>(
KtParameter::class.java, KotlinBundle.message("convert.to.array.parameter")
KtParameter::class.java,
KotlinBundle.lazyMessage("convert.to.array.parameter"),
) {
override fun isApplicableTo(element: KtParameter, caretOffset: Int): Boolean {
if (element.getChildOfType<KtTypeReference>() == null) return false
return element.isVarArg
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -24,8 +24,10 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.scopes.receivers.ClassQualifier
class ImportAllMembersIntention : SelfTargetingIntention<KtElement>(KtElement::class.java, KotlinBundle.message("import.members.with")),
HighPriorityAction {
class ImportAllMembersIntention : SelfTargetingIntention<KtElement>(
KtElement::class.java,
KotlinBundle.lazyMessage("import.members.with")
), HighPriorityAction {
override fun isApplicableTo(element: KtElement, caretOffset: Int): Boolean {
val receiverExpression = element.receiverExpression() ?: return false
if (!receiverExpression.range.containsOffset(caretOffset)) return false
@@ -42,13 +44,11 @@ class ImportAllMembersIntention : SelfTargetingIntention<KtElement>(KtElement::c
val helper = ImportInsertHelper.getInstance(project)
if (helper.importDescriptor(dummyFile, target, forceAllUnderImport = true) == ImportDescriptorResult.FAIL) return false
text = KotlinBundle.message("import.members.from.0", targetFqName.parent().asString())
setTextGetter(KotlinBundle.lazyMessage("import.members.from.0", targetFqName.parent().asString()))
return true
}
override fun applyTo(element: KtElement, editor: Editor?) {
element.importReceiverMembers()
}
override fun applyTo(element: KtElement, editor: Editor?) = element.importReceiverMembers()
companion object {
fun KtElement.importReceiverMembers() {
@@ -56,12 +56,12 @@ class ImportAllMembersIntention : SelfTargetingIntention<KtElement>(KtElement::c
val classFqName = target.importableFqName!!.parent()
ImportInsertHelper.getInstance(project).importDescriptor(containingKtFile, target, forceAllUnderImport = true)
val qualifiedExpressions = containingKtFile.collectDescendantsOfType<KtDotQualifiedExpression> { qualifiedExpression ->
val qualifierName = qualifiedExpression.receiverExpression.getQualifiedElementSelector() as? KtNameReferenceExpression
qualifierName?.getReferencedNameAsName() == classFqName.shortName() && target(qualifiedExpression)?.importableFqName
?.parent() == classFqName
}
val userTypes = containingKtFile.collectDescendantsOfType<KtUserType> { userType ->
val receiver = userType.receiverExpression()?.getQualifiedElementSelector() as? KtNameReferenceExpression
receiver?.getReferencedNameAsName() == classFqName.shortName() && target(userType)?.importableFqName
@@ -77,6 +77,7 @@ class ImportAllMembersIntention : SelfTargetingIntention<KtElement>(KtElement::c
if (bindingContext[BindingContext.QUALIFIER, receiverExpression] !is ClassQualifier) {
return null
}
val selector = qualifiedElement.getQualifiedElementSelector() as? KtNameReferenceExpression ?: return null
return selector.mainReference.resolveToDescriptors(bindingContext).firstOrNull()
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -10,11 +10,10 @@ import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
class InfixCallToOrdinaryIntention :
SelfTargetingIntention<KtBinaryExpression>(
KtBinaryExpression::class.java,
KotlinBundle.message("replace.infix.call.with.ordinary.call")
) {
class InfixCallToOrdinaryIntention : SelfTargetingIntention<KtBinaryExpression>(
KtBinaryExpression::class.java,
KotlinBundle.lazyMessage("replace.infix.call.with.ordinary.call")
) {
override fun isApplicableTo(element: KtBinaryExpression, caretOffset: Int): Boolean {
if (element.operationToken != KtTokens.IDENTIFIER || element.left == null || element.right == null) return false
return element.operationReference.textRange.containsOffset(caretOffset)
@@ -31,8 +30,14 @@ class InfixCallToOrdinaryIntention :
is KtLambdaExpression -> " $2:'{}'"
else -> "($2)"
}
val replacement =
KtPsiFactory(element).createExpressionByPattern(pattern, element.left!!, element.operationReference.text, argument)
val replacement = KtPsiFactory(element).createExpressionByPattern(
pattern,
element.left!!,
element.operationReference.text,
argument
)
return element.replace(replacement) as KtExpression
}
}
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -31,16 +20,14 @@ import org.jetbrains.kotlin.resolve.BindingContext
class IntroduceBackingPropertyIntention : SelfTargetingIntention<KtProperty>(
KtProperty::class.java,
KotlinBundle.message("introduce.backing.property")
KotlinBundle.lazyMessage("introduce.backing.property")
) {
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean {
if (!canIntroduceBackingProperty(element)) return false
return element.nameIdentifier?.textRange?.containsOffset(caretOffset) == true
}
override fun applyTo(element: KtProperty, editor: Editor?) {
introduceBackingProperty(element)
}
override fun applyTo(element: KtProperty, editor: Editor?) = introduceBackingProperty(element)
companion object {
fun canIntroduceBackingProperty(property: KtProperty): Boolean {
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull
class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(
KtIfExpression::class.java,
KotlinBundle.message("invert.if.condition")
KotlinBundle.lazyMessage("invert.if.condition")
) {
override fun isApplicableTo(element: KtIfExpression, caretOffset: Int): Boolean {
if (!element.ifKeyword.textRange.containsOffset(caretOffset)) return false
@@ -39,10 +39,9 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(
PsiChildRange(element, rBrace)
else
PsiChildRange.singleElement(element)
val commentSaver = CommentSaver(commentSavingRange)
if (rBrace != null) {
element.nextEolCommentOnSameLine()?.delete()
}
if (rBrace != null) element.nextEolCommentOnSameLine()?.delete()
val condition = element.condition!!
val newCondition = (condition as? KtQualifiedExpression)?.invertSelectorFunction() ?: condition.negate()
@@ -53,6 +52,7 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(
PsiChildRange(newIf, rBrace)
else
PsiChildRange(newIf, parentBlockRBrace(newIf) ?: newIf)
commentSaver.restore(commentRestoreRange)
val newIfCondition = newIf.condition
@@ -207,12 +207,13 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(
val lastStatement = parent.statements.last()
return if (expression == lastStatement) {
exitStatementExecutedAfter(parent)
} else if (lastStatement.isExitStatement() && expression.siblings(withItself = false)
.firstIsInstance<KtExpression>() == lastStatement
} else if (lastStatement.isExitStatement() &&
expression.siblings(withItself = false).firstIsInstance<KtExpression>() == lastStatement
) {
lastStatement
} else
} else {
null
}
}
when (parent) {
@@ -225,18 +226,16 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(
}
}
is KtContainerNode -> {
when (val pparent = parent.parent) {
is KtLoopExpression -> {
if (expression == pparent.body) {
return KtPsiFactory(expression).createExpression("continue")
}
is KtContainerNode -> when (val pparent = parent.parent) {
is KtLoopExpression -> {
if (expression == pparent.body) {
return KtPsiFactory(expression).createExpression("continue")
}
}
is KtIfExpression -> {
if (expression == pparent.then || expression == pparent.`else`) {
return exitStatementExecutedAfter(pparent)
}
is KtIfExpression -> {
if (expression == pparent.then || expression == pparent.`else`) {
return exitStatementExecutedAfter(pparent)
}
}
}
@@ -244,13 +243,10 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(
return null
}
private fun parentBlockRBrace(element: KtIfExpression): PsiElement? {
return (element.parent as? KtBlockExpression)?.rBrace
}
private fun parentBlockRBrace(element: KtIfExpression): PsiElement? = (element.parent as? KtBlockExpression)?.rBrace
private fun KtIfExpression.nextEolCommentOnSameLine(): PsiElement? {
val lastLineNumber = getLineNumber(false)
return siblings(withItself = false)
private fun KtIfExpression.nextEolCommentOnSameLine(): PsiElement? = getLineNumber(false).let { lastLineNumber ->
siblings(withItself = false)
.takeWhile { it.getLineNumber() == lastLineNumber }
.firstOrNull { it is PsiComment && it.node.elementType == KtTokens.EOL_COMMENT }
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -28,7 +28,7 @@ import org.jetbrains.kotlin.types.KotlinType
class IterateExpressionIntention : SelfTargetingIntention<KtExpression>(
KtExpression::class.java,
KotlinBundle.message("iterate.over.collection")
KotlinBundle.lazyMessage("iterate.over.collection")
),
HighPriorityAction {
override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean {
@@ -36,10 +36,13 @@ class IterateExpressionIntention : SelfTargetingIntention<KtExpression>(
val range = element.textRange
if (caretOffset != range.startOffset && caretOffset != range.endOffset) return false
val data = data(element) ?: return false
text = KotlinBundle.message(
"iterate.over.0",
IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_NO_ANNOTATIONS.renderType(data.collectionType)
setTextGetter(
KotlinBundle.lazyMessage(
"iterate.over.0",
IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_NO_ANNOTATIONS.renderType(data.collectionType)
)
)
return true
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -31,8 +31,8 @@ import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
class LambdaToAnonymousFunctionIntention : SelfTargetingIntention<KtLambdaExpression>(
KtLambdaExpression::class.java,
KotlinBundle.message("convert.to.anonymous.function"),
KotlinBundle.message("convert.lambda.expression.to.anonymous.function")
KotlinBundle.lazyMessage("convert.to.anonymous.function"),
KotlinBundle.lazyMessage("convert.lambda.expression.to.anonymous.function")
), LowPriorityAction {
override fun isApplicableTo(element: KtLambdaExpression, caretOffset: Int): Boolean {
if (element.getStrictParentOfType<KtValueArgument>() == null) return false
@@ -75,6 +75,7 @@ class LambdaToAnonymousFunctionIntention : SelfTargetingIntention<KtLambdaExpres
functionDescriptor.extensionReceiverParameter?.type?.let {
receiver(typeSourceCode.renderType(it))
}
name(functionName)
for (parameter in functionDescriptor.valueParameters) {
val type = parameter.type.let { if (it.isFlexible()) it.makeNotNullable() else it }
@@ -85,6 +86,7 @@ class LambdaToAnonymousFunctionIntention : SelfTargetingIntention<KtLambdaExpres
param(parameter.name.asString(), renderType)
}
}
functionDescriptor.returnType?.takeIf { !it.isUnit() }?.let {
val lastStatement = bodyExpression.statements.lastOrNull()
if (lastStatement != null && lastStatement !is KtReturnExpression) {
@@ -103,6 +105,7 @@ class LambdaToAnonymousFunctionIntention : SelfTargetingIntention<KtLambdaExpres
blockBody(" " + bodyExpression.text)
}.asString()
)
return replaceElement(function).also { ShortenReferences.DEFAULT.process(it) }
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -10,7 +10,10 @@ import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.*
class MergeElseIfIntention : SelfTargetingIntention<KtIfExpression>(KtIfExpression::class.java, KotlinBundle.message("merge.else.if")) {
class MergeElseIfIntention : SelfTargetingIntention<KtIfExpression>(
KtIfExpression::class.java,
KotlinBundle.lazyMessage("merge.else.if")
) {
override fun isApplicableTo(element: KtIfExpression, caretOffset: Int): Boolean {
val elseBody = element.`else` ?: return false
val nestedIf = elseBody.nestedIf() ?: return false
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -20,8 +9,7 @@ import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.*
class MergeIfsIntention : SelfTargetingIntention<KtIfExpression>(KtIfExpression::class.java, KotlinBundle.message("merge.if.s")) {
class MergeIfsIntention : SelfTargetingIntention<KtIfExpression>(KtIfExpression::class.java, KotlinBundle.lazyMessage("merge.if.s")) {
override fun isApplicableTo(element: KtIfExpression, caretOffset: Int): Boolean {
if (element.`else` != null) return false
val then = element.then ?: return false
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -25,7 +14,7 @@ import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.psi.psiUtil.containsInside
class MoveLambdaInsideParenthesesIntention : SelfTargetingIntention<KtLambdaArgument>(
KtLambdaArgument::class.java, KotlinBundle.message("move.lambda.argument.into.parentheses")
KtLambdaArgument::class.java, KotlinBundle.lazyMessage("move.lambda.argument.into.parentheses")
), LowPriorityAction {
override fun isApplicableTo(element: KtLambdaArgument, caretOffset: Int): Boolean {
val body = element.getLambdaExpression()?.bodyExpression ?: return true
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -21,17 +21,15 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class MovePropertyToClassBodyIntention : SelfTargetingIntention<KtParameter>(
KtParameter::class.java,
KotlinBundle.message("move.to.class.body")
KotlinBundle.lazyMessage("move.to.class.body")
) {
override fun isApplicableTo(element: KtParameter, caretOffset: Int): Boolean {
return element.isPropertyParameter() && (element.ownerFunction as KtPrimaryConstructor).isNotContainedInAnnotation()
}
override fun isApplicableTo(element: KtParameter, caretOffset: Int): Boolean =
element.isPropertyParameter() && (element.ownerFunction as KtPrimaryConstructor).isNotContainedInAnnotation()
override fun applyTo(element: KtParameter, editor: Editor?) {
val parentClass = PsiTreeUtil.getParentOfType(element, KtClass::class.java) ?: return
val propertyDeclaration = KtPsiFactory(element)
.createProperty("${element.valOrVarKeyword?.text} ${element.name} = ${element.name}")
val propertyDeclaration = KtPsiFactory(element).createProperty("${element.valOrVarKeyword?.text} ${element.name} = ${element.name}")
val firstProperty = parentClass.getProperties().firstOrNull()
parentClass.addDeclarationBefore(propertyDeclaration, firstProperty).apply {
@@ -59,6 +57,7 @@ class MovePropertyToClassBodyIntention : SelfTargetingIntention<KtParameter>(
} else {
element.modifierList?.delete()
}
if (hasVararg) element.addModifier(KtTokens.VARARG_KEYWORD)
}
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -47,7 +36,7 @@ import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.types.KotlinType
class MovePropertyToConstructorIntention :
SelfTargetingIntention<KtProperty>(KtProperty::class.java, KotlinBundle.message("move.to.constructor")),
SelfTargetingIntention<KtProperty>(KtProperty::class.java, KotlinBundle.lazyMessage("move.to.constructor")),
LocalQuickFix {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
@@ -102,8 +91,10 @@ class MovePropertyToConstructorIntention :
commentSaver.restore(this)
}
} else {
val typeText =
element.typeReference?.text ?: (element.resolveToDescriptorIfAny() as? PropertyDescriptor)?.type?.render() ?: return
val typeText = element.typeReference?.text
?: (element.resolveToDescriptorIfAny() as? PropertyDescriptor)?.type?.render()
?: return
val parameterText = buildString {
element.modifierList?.getModifiersText()?.let(this::append)
propertyAnnotationsText?.takeIf(String::isNotBlank)?.let { appendWithSpaceBefore(it) }
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -16,7 +16,7 @@ import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.typeUtil.isBooleanOrNullableBoolean
class NullableBooleanEqualityCheckToElvisIntention : SelfTargetingIntention<KtBinaryExpression>(
KtBinaryExpression::class.java, KotlinBundle.message("convert.boolean.const.to.elvis")
KtBinaryExpression::class.java, KotlinBundle.lazyMessage("convert.boolean.const.to.elvis")
) {
override fun isApplicableTo(element: KtBinaryExpression, caretOffset: Int): Boolean {
if (element.operationToken != KtTokens.EQEQ && element.operationToken != KtTokens.EXCLEQ) return false
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -26,8 +15,7 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class RemoveBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class.java, KotlinBundle.message("remove.braces")) {
class RemoveBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class.java, KotlinBundle.lazyMessage("remove.braces")) {
private fun KtElement.findChildBlock() = when (this) {
is KtBlockExpression -> this
is KtLoopExpression -> body as? KtBlockExpression
@@ -47,11 +35,11 @@ class RemoveBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class
}
val description = container.description() ?: return false
text = KotlinBundle.message("remove.braces.from.0.statement", description)
setTextGetter(KotlinBundle.lazyMessage("remove.braces.from.0.statement", description))
return true
}
is KtWhenEntry -> {
text = KotlinBundle.message("remove.braces.from.when.entry")
setTextGetter(KotlinBundle.lazyMessage("remove.braces.from.when.entry"))
return singleStatement !is KtNamedDeclaration
}
else -> return false
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -13,7 +13,7 @@ import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
class RemoveConstructorKeywordIntention : SelfTargetingIntention<KtPrimaryConstructor>(
KtPrimaryConstructor::class.java,
KotlinBundle.message("remove.constructor.keyword")
KotlinBundle.lazyMessage("remove.constructor.keyword")
) {
override fun applyTo(element: KtPrimaryConstructor, editor: Editor?) {
element.removeRedundantConstructorKeywordAndSpace()
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -11,11 +11,10 @@ import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.endOffset
class RemoveExplicitLambdaParameterTypesIntention :
SelfTargetingIntention<KtLambdaExpression>(
KtLambdaExpression::class.java,
KotlinBundle.message("remove.explicit.lambda.parameter.types.may.break.code")
) {
class RemoveExplicitLambdaParameterTypesIntention : SelfTargetingIntention<KtLambdaExpression>(
KtLambdaExpression::class.java,
KotlinBundle.lazyMessage("remove.explicit.lambda.parameter.types.may.break.code")
) {
override fun isApplicableTo(element: KtLambdaExpression, caretOffset: Int): Boolean {
if (element.valueParameters.none { it.typeReference != null }) return false
val arrow = element.functionLiteral.arrow ?: return false
@@ -28,6 +27,7 @@ class RemoveExplicitLambdaParameterTypesIntention :
val parameterString = oldParameterList.parameters.asSequence().map {
it.destructuringDeclaration?.text ?: it.name
}.joinToString(", ")
val newParameterList = KtPsiFactory(element).createLambdaParameterList(parameterString)
oldParameterList.replace(newParameterList)
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2000-2020 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.
*/
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class RemoveLabeledReturnInLambdaIntention : SelfTargetingIntention<KtReturnExpression>(
KtReturnExpression::class.java,
KotlinBundle.message("remove.labeled.return.from.last.expression.in.a.lambda")
KotlinBundle.lazyMessage("remove.labeled.return.from.last.expression.in.a.lambda")
), LowPriorityAction {
override fun isApplicableTo(element: KtReturnExpression, caretOffset: Int): Boolean {
val labelName = element.getLabelName() ?: return false
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -29,7 +18,7 @@ internal fun KtExpression.getArguments() = when (this) {
class ReplaceUntilWithRangeToIntention : SelfTargetingIntention<KtExpression>(
KtExpression::class.java,
KotlinBundle.message("replace.with.0.operator", "..")
KotlinBundle.lazyMessage("replace.with.0.operator", "..")
) {
override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean {
if (element !is KtBinaryExpression && element !is KtDotQualifiedExpression) return false
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -14,9 +14,10 @@ import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
class ReplaceWithOrdinaryAssignmentIntention :
SelfTargetingIntention<KtBinaryExpression>(KtBinaryExpression::class.java, KotlinBundle.message("replace.with.ordinary.assignment")),
LowPriorityAction {
class ReplaceWithOrdinaryAssignmentIntention : SelfTargetingIntention<KtBinaryExpression>(
KtBinaryExpression::class.java,
KotlinBundle.lazyMessage("replace.with.ordinary.assignment")
), LowPriorityAction {
override fun isApplicableTo(element: KtBinaryExpression, caretOffset: Int): Boolean {
if (element.operationToken !in KtTokens.AUGMENTED_ASSIGNMENTS) return false
if (element.left !is KtNameReferenceExpression) return false
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -19,7 +19,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.lastBlockStatementOrThis
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class SplitIfIntention : SelfTargetingIntention<KtExpression>(KtExpression::class.java, KotlinBundle.message("split.if.into.two")) {
class SplitIfIntention : SelfTargetingIntention<KtExpression>(KtExpression::class.java, KotlinBundle.lazyMessage("split.if.into.two")) {
override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean {
return when (element) {
is KtOperationReferenceExpression -> isOperatorValid(element)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -20,9 +20,8 @@ import org.jetbrains.kotlin.types.expressions.OperatorConventions
class SwapBinaryExpressionIntention : SelfTargetingIntention<KtBinaryExpression>(
KtBinaryExpression::class.java,
KotlinBundle.message("flip.binary.expression")
),
LowPriorityAction {
KotlinBundle.lazyMessage("flip.binary.expression")
), LowPriorityAction {
companion object {
private val SUPPORTED_OPERATIONS: Set<KtSingleValueToken> by lazy {
setOf(PLUS, MUL, OROR, ANDAND, EQEQ, EXCLEQ, EQEQEQ, EXCLEQEQEQ, GT, LT, GTEQ, LTEQ)
@@ -47,7 +46,7 @@ class SwapBinaryExpressionIntention : SelfTargetingIntention<KtBinaryExpression>
if (operationToken in SUPPORTED_OPERATIONS
|| operationToken == IDENTIFIER && operationTokenText in SUPPORTED_OPERATION_NAMES
) {
text = KotlinBundle.message("flip.0", operationTokenText)
setTextGetter(KotlinBundle.lazyMessage("flip.0", operationTokenText))
return true
}
return false
@@ -55,16 +54,16 @@ class SwapBinaryExpressionIntention : SelfTargetingIntention<KtBinaryExpression>
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
// Have to use text here to preserve names like "plus"
val operator = element.operationReference.text!!
val convertedOperator = when (operator) {
val convertedOperator = when (val operator = element.operationReference.text!!) {
">" -> "<"
"<" -> ">"
"<=" -> ">="
">=" -> "<="
else -> operator
}
val left = leftSubject(element)!!
val right = rightSubject(element)!!
val left = leftSubject(element) ?: return
val right = rightSubject(element) ?: return
val rightCopy = right.copied()
val leftCopy = left.copied()
left.replace(rightCopy)
@@ -72,13 +71,11 @@ class SwapBinaryExpressionIntention : SelfTargetingIntention<KtBinaryExpression>
element.replace(KtPsiFactory(element).createExpressionByPattern("$0 $convertedOperator $1", element.left!!, element.right!!))
}
private fun leftSubject(element: KtBinaryExpression): KtExpression? {
return firstDescendantOfTighterPrecedence(element.left, PsiPrecedences.getPrecedence(element), KtBinaryExpression::getRight)
}
private fun leftSubject(element: KtBinaryExpression): KtExpression? =
firstDescendantOfTighterPrecedence(element.left, PsiPrecedences.getPrecedence(element), KtBinaryExpression::getRight)
private fun rightSubject(element: KtBinaryExpression): KtExpression? {
return firstDescendantOfTighterPrecedence(element.right, PsiPrecedences.getPrecedence(element), KtBinaryExpression::getLeft)
}
private fun rightSubject(element: KtBinaryExpression): KtExpression? =
firstDescendantOfTighterPrecedence(element.right, PsiPrecedences.getPrecedence(element), KtBinaryExpression::getLeft)
private fun firstDescendantOfTighterPrecedence(
expression: KtExpression?,
@@ -91,6 +88,7 @@ class SwapBinaryExpressionIntention : SelfTargetingIntention<KtBinaryExpression>
return firstDescendantOfTighterPrecedence(expression.getChild(), precedence, getChild)
}
}
return expression
}
}
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -27,7 +16,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class ToInfixCallIntention : SelfTargetingIntention<KtCallExpression>(
KtCallExpression::class.java,
KotlinBundle.message("replace.with.infix.function.call")
KotlinBundle.lazyMessage("replace.with.infix.function.call")
) {
override fun isApplicableTo(element: KtCallExpression, caretOffset: Int): Boolean {
val calleeExpr = element.calleeExpression as? KtNameReferenceExpression ?: return false
@@ -8,21 +8,22 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.application.options.CodeStyle
import com.intellij.codeInsight.intention.LowPriorityAction
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.formatter.kotlinCustomSettings
import org.jetbrains.kotlin.idea.util.addTrailingCommaIsAllowedForThis
import org.jetbrains.kotlin.psi.KtElement
class TrailingCommaIntention : SelfTargetingIntention<KtElement>(KtElement::class.java, "Enable/disable a trailing comma in the formatter"),
LowPriorityAction {
class TrailingCommaIntention : SelfTargetingIntention<KtElement>(
KtElement::class.java,
KotlinBundle.lazyMessage("intention.trailing.comma.text")
), LowPriorityAction {
override fun applyTo(element: KtElement, editor: Editor?) {
val kotlinCustomSettings = CodeStyle.getSettings(element.project).kotlinCustomSettings
kotlinCustomSettings.ALLOW_TRAILING_COMMA = !kotlinCustomSettings.ALLOW_TRAILING_COMMA
}
override fun isApplicableTo(element: KtElement, caretOffset: Int): Boolean {
return element.addTrailingCommaIsAllowedForThis().also {
val action = if (CodeStyle.getSettings(element.project).kotlinCustomSettings.ALLOW_TRAILING_COMMA) "Disable" else "Enable"
text = "$action a trailing comma by default in the formatter"
}
override fun isApplicableTo(element: KtElement, caretOffset: Int): Boolean = element.addTrailingCommaIsAllowedForThis().also {
val actionNumber = 1.takeIf { CodeStyle.getSettings(element.project).kotlinCustomSettings.ALLOW_TRAILING_COMMA } ?: 0
setTextGetter(KotlinBundle.lazyMessage("intention.trailing.comma.custom.text", actionNumber))
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -13,7 +13,7 @@ import org.jetbrains.kotlin.psi.stubs.ConstantValueKind
import org.jetbrains.kotlin.psi.stubs.elements.KtConstantExpressionElementType
class AddUnderscoresToNumericLiteralIntention : SelfTargetingIntention<KtConstantExpression>(
KtConstantExpression::class.java, KotlinBundle.message("add.underscores")
KtConstantExpression::class.java, KotlinBundle.lazyMessage("add.underscores")
) {
override fun isApplicableTo(element: KtConstantExpression, caretOffset: Int): Boolean {
val text = element.text
@@ -32,11 +32,10 @@ class AddUnderscoresToNumericLiteralIntention : SelfTargetingIntention<KtConstan
}
class RemoveUnderscoresFromNumericLiteralIntention : SelfTargetingIntention<KtConstantExpression>(
KtConstantExpression::class.java, KotlinBundle.message("remove.underscores")
KtConstantExpression::class.java, KotlinBundle.lazyMessage("remove.underscores")
) {
override fun isApplicableTo(element: KtConstantExpression, caretOffset: Int): Boolean {
return element.isNumeric() && element.text.hasUnderscore()
}
override fun isApplicableTo(element: KtConstantExpression, caretOffset: Int): Boolean =
element.isNumeric() && element.text.hasUnderscore()
override fun applyTo(element: KtConstantExpression, editor: Editor?) {
element.replace(KtPsiFactory(element).createExpression(element.text.replace("_", "")))
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions
@@ -26,9 +15,8 @@ import org.jetbrains.kotlin.psi.psiUtil.endOffset
class ValToObjectIntention : SelfTargetingIntention<KtProperty>(
KtProperty::class.java,
KotlinBundle.message("convert.to.object.declaration")
KotlinBundle.lazyMessage("convert.to.object.declaration")
) {
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean {
if (element.isVar) return false
if (!element.isTopLevel) return false
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -18,7 +18,7 @@ import org.jetbrains.kotlin.psi.buildExpression
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class EliminateWhenSubjectIntention :
SelfTargetingIntention<KtWhenExpression>(KtWhenExpression::class.java, KotlinBundle.message("eliminate.argument.of.when")),
SelfTargetingIntention<KtWhenExpression>(KtWhenExpression::class.java, KotlinBundle.lazyMessage("eliminate.argument.of.when")),
LowPriorityAction {
override fun isApplicableTo(element: KtWhenExpression, caretOffset: Int): Boolean {
if (element.subjectExpression !is KtNameReferenceExpression) return false
@@ -27,7 +27,7 @@ class EliminateWhenSubjectIntention :
}
override fun applyTo(element: KtWhenExpression, editor: Editor?) {
val subject = element.subjectExpression!!
val subject = element.subjectExpression ?: return
val commentSaver = CommentSaver(element, saveLineBreaks = true)
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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.intentions.branchedTransformations.intentions
@@ -28,7 +17,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
class FlattenWhenIntention : SelfTargetingIntention<KtWhenExpression>(
KtWhenExpression::class.java,
KotlinBundle.message("flatten.when.expression")
KotlinBundle.lazyMessage("flatten.when.expression")
) {
override fun isApplicableTo(element: KtWhenExpression, caretOffset: Int): Boolean {
val subject = element.subjectExpression
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2020 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
@@ -69,15 +58,12 @@ class AddConstModifierFix(property: KtProperty) : AddModifierFix(property, KtTok
}
class AddConstModifierIntention : SelfTargetingIntention<KtProperty>(
KtProperty::class.java, KotlinBundle.message("fix.add.const.modifier")
KtProperty::class.java,
KotlinBundle.lazyMessage("fix.add.const.modifier"),
) {
override fun applyTo(element: KtProperty, editor: Editor?) {
AddConstModifierFix.addConstModifier(element)
}
override fun applyTo(element: KtProperty, editor: Editor?) = AddConstModifierFix.addConstModifier(element)
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean {
return isApplicableTo(element)
}
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean = isApplicableTo(element)
companion object {
fun isApplicableTo(element: KtProperty): Boolean {