Implement an intention converting several calls with same receiver to with/apply/run #KT-12183 Fixed

This commit is contained in:
shiraji
2017-02-04 09:03:39 +09:00
committed by Mikhail Glukhikh
parent c2e5fc5215
commit 0e5603f644
71 changed files with 1352 additions and 0 deletions
@@ -0,0 +1,7 @@
fun foo() {
<spot>val a = MyClass().apply {
a.setFoo(1)
a.setBar(2)
a.setBaz(3)
}</spot>
}
@@ -0,0 +1,6 @@
fun foo() {
<spot>val a = MyClass()
a.setFoo(1)
a.setBar(2)
a.setBaz(3)</spot>
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts several calls with same receiver to 'apply'
</body>
</html>
@@ -0,0 +1,7 @@
fun foo() {
<spot>a.run {
setFoo(1)
setBar(2)
setBaz(3)
}</spot>
}
@@ -0,0 +1,5 @@
fun foo() {
<spot>a.setFoo(1)
a.setBar(2)
a.setBaz(3)</spot>
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts several calls with same receiver to 'run'
</body>
</html>
@@ -0,0 +1,7 @@
fun foo() {
<spot>with(a) {
setFoo(1)
setBar(2)
setBaz(3)
}</spot>
}
@@ -0,0 +1,5 @@
fun foo() {
<spot>a.setFoo(1)
a.setBar(2)
a.setBaz(3)</spot>
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts several calls with same receiver to 'with'
</body>
</html>
+15
View File
@@ -1512,6 +1512,21 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertToApplyIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertToWithIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertToRunIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
abstract class ConvertDotQualifiedToScopeIntention(
text: String
) : ConvertToScopeIntention<KtDotQualifiedExpression>(KtDotQualifiedExpression::class.java, text) {
override fun isApplicableTo(element: KtDotQualifiedExpression, caretOffset: Int): Boolean {
val receiverExpressionText = element.getReceiverExpressionText() ?: return false
if (receiverExpressionText in BLACKLIST_RECEIVER_NAME) return false
if (!isApplicableWithGivenReceiverText(element, receiverExpressionText)) return false
val nextSibling = element.getDotQualifiedSiblingIfAny(forward = true)
if (nextSibling != null && isApplicableWithGivenReceiverText(nextSibling, receiverExpressionText)) return true
val prevSibling = element.getDotQualifiedSiblingIfAny(forward = false)
return prevSibling != null && isApplicableWithGivenReceiverText(prevSibling, receiverExpressionText)
}
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
val receiverExpressionText = element.getReceiverExpressionText() ?: return
applyWithGivenReceiverText(element, receiverExpressionText)
}
}
@@ -0,0 +1,80 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
class ConvertToApplyIntention : ConvertToScopeIntention<KtExpression>(
KtExpression::class.java, "Convert to apply"
) {
override fun findCallExpressionFrom(scopeExpression: KtExpression) =
((scopeExpression as? KtProperty)?.initializer as? KtQualifiedExpression)?.callExpression
override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean {
return when (element) {
is KtProperty -> element.isApplicable()
is KtDotQualifiedExpression -> {
val receiverExpressionText = element.getLeftMostReceiverExpression().text
isApplicableWithGivenReceiverText(element, receiverExpressionText) &&
element.findTargetProperty(receiverExpressionText)?.isApplicable() ?: false
}
else -> false
}
}
private fun KtProperty.isApplicable(): Boolean {
if (!isLocal) return false
val nextSibling = getDotQualifiedSiblingIfAny(forward = true)
val localVariableName = name ?: return false
return nextSibling != null && isApplicableWithGivenReceiverText(nextSibling, localVariableName)
}
private fun KtDotQualifiedExpression.findTargetProperty(receiverExpressionText: String): KtProperty? {
val target = getPrevSiblingIgnoringWhitespaceAndComments(false)
when (target) {
is KtProperty ->
if (target.name == receiverExpressionText) {
return target
}
is KtDotQualifiedExpression ->
if (isApplicableWithGivenReceiverText(target, receiverExpressionText)) {
return target.findTargetProperty(receiverExpressionText)
}
}
return null
}
override fun applyTo(element: KtExpression, editor: Editor?) {
when (element) {
is KtProperty -> applyWithGivenReceiverText(element, element.name ?: return)
is KtDotQualifiedExpression -> {
val receiverExpressionText = element.getReceiverExpressionText() ?: return
val property = element.findTargetProperty(receiverExpressionText) ?: return
applyWithGivenReceiverText(property, receiverExpressionText)
}
}
}
override fun createScopeExpression(factory: KtPsiFactory, element: KtExpression): KtProperty? {
if (element !is KtProperty) return null
val receiverExpressionText = element.name ?: return null
return factory.createProperty(receiverExpressionText, element.typeReference?.text, element.isVar,
"${element.initializer?.text}.apply{}")
}
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions
import org.jetbrains.kotlin.psi.*
class ConvertToRunIntention : ConvertDotQualifiedToScopeIntention("Convert to run") {
override fun createScopeExpression(factory: KtPsiFactory, element: KtDotQualifiedExpression) =
factory.createExpressionByPattern("$0.run {}", element.getLeftMostReceiverExpression())
override fun findCallExpressionFrom(scopeExpression: KtExpression) =
(scopeExpression as? KtQualifiedExpression)?.callExpression
}
@@ -0,0 +1,99 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
abstract class ConvertToScopeIntention<TExpression : KtExpression>(
elementType: Class<TExpression>,
text: String
) : SelfTargetingIntention<TExpression>(elementType, text) {
protected val BLACKLIST_RECEIVER_NAME = listOf("this", "it")
protected abstract fun createScopeExpression(factory: KtPsiFactory, element: TExpression): KtExpression?
protected abstract fun findCallExpressionFrom(scopeExpression: KtExpression): KtCallExpression?
protected fun KtDotQualifiedExpression.getReceiverExpressionText(): String? = getLeftMostReceiverExpression().text
protected fun isApplicableWithGivenReceiverText(expression: KtDotQualifiedExpression, receiverExpressionText: String): Boolean {
if (receiverExpressionText != expression.getReceiverExpressionText()) return false
val callExpression = expression.callExpression ?: return false
if (!callExpression.isApplicable()) return false
val receiverExpression = expression.receiverExpression
return receiverExpression !is KtDotQualifiedExpression ||
isApplicableWithGivenReceiverText(receiverExpression, receiverExpressionText)
}
protected fun applyWithGivenReceiverText(expression: TExpression, receiverExpressionText: String) {
val factory = KtPsiFactory(expression)
val scopeBlockExpression = createScopeExpression(factory, expression) ?: return
val callExpression = findCallExpressionFrom(scopeBlockExpression) ?: return
val blockExpression = callExpression.getFirstLambdaArgumentBody() ?: return
val parent = expression.parent
val lastExpressionToMove = findLastExpressionToMove(receiverExpressionText, expression)
val firstTargetExpression =
if (expression is KtProperty) expression
else findFirstExpressionToMove(receiverExpressionText, expression)
val firstExpressionToMove = if (expression is KtProperty) expression.nextSibling else firstTargetExpression
blockExpression.moveRangeInto(firstExpressionToMove, lastExpressionToMove)
parent.addBefore(scopeBlockExpression, firstTargetExpression)
parent.deleteChildRange(firstTargetExpression, lastExpressionToMove)
}
protected fun KtExpression.getDotQualifiedSiblingIfAny(forward: Boolean): KtDotQualifiedExpression? {
val sibling =
if (forward) getNextSiblingIgnoringWhitespaceAndComments(false)
else getPrevSiblingIgnoringWhitespaceAndComments(false)
return sibling as? KtDotQualifiedExpression
}
private fun KtCallExpression.getFirstLambdaArgumentBody() =
lambdaArguments.firstOrNull()?.getLambdaExpression()?.bodyExpression
private fun KtBlockExpression.moveRangeInto(
firstElement: PsiElement, lastElement: PsiElement
) {
addRange(firstElement, lastElement)
children.filterIsInstance(KtDotQualifiedExpression::class.java)
.forEach { it.deleteFirstReceiver() }
}
private fun KtCallExpression.isApplicable() = lambdaArguments.isEmpty() && valueArguments.all { it.text !in BLACKLIST_RECEIVER_NAME }
private fun findFirstExpressionToMove(receiverExpressionText: String, expression: KtExpression) =
findBoundaryExpression(receiverExpressionText, expression, forward = false)
private fun findLastExpressionToMove(receiverExpressionText: String, expression: KtExpression) =
findBoundaryExpression(receiverExpressionText, expression, forward = true)
private fun findBoundaryExpression(receiverExpressionText: String, expression: KtExpression, forward: Boolean): KtExpression {
var targetExpression: KtExpression = expression
while (true) {
val dotQualifiedSibling = targetExpression.getDotQualifiedSiblingIfAny(forward)
if (dotQualifiedSibling == null || !isApplicableWithGivenReceiverText(dotQualifiedSibling, receiverExpressionText)) {
return targetExpression
}
targetExpression = dotQualifiedSibling
}
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions
import org.jetbrains.kotlin.psi.*
class ConvertToWithIntention : ConvertDotQualifiedToScopeIntention("Convert to with") {
override fun createScopeExpression(factory: KtPsiFactory, element: KtDotQualifiedExpression) =
factory.createExpressionByPattern("with($0) {}", element.getLeftMostReceiverExpression())
override fun findCallExpressionFrom(scopeExpression: KtExpression) = scopeExpression as? KtCallExpression
}
+1
View File
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertToApplyIntention
+14
View File
@@ -0,0 +1,14 @@
// WITH_RUNTIME
class MyClass {
fun foo1(): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4() {
val a = MyClass()
a.foo1().foo2().foo3()
a.foo2()<caret>
a.foo3()
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1(): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4() {
val a = MyClass().apply {
foo1().foo2().foo3()
foo2()
foo3()
}
}
}
@@ -0,0 +1,17 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1(a: MyClass): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4() {
listOf<MyClass>().forEach {
val a = MyClass()
a.foo1(it).foo2().foo3()
a.foo2()<caret>
a.foo3()
}
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1(a: MyClass): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4() {
val a = MyClass()
a.foo1(this).foo2().foo3()
a.foo2()<caret>
a.foo3()
}
}
+14
View File
@@ -0,0 +1,14 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4() {
val a = MyClass()
a.foo1()<caret>
a.foo2()
a.foo3()
}
}
+15
View File
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4() {
val a = MyClass().apply {
foo1()
foo2()
foo3()
}
}
}
+14
View File
@@ -0,0 +1,14 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
val a = MyClass()
a.foo1()
a.foo2()<caret>
a.foo3()
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
val a = MyClass().apply {
foo1()
foo2()
foo3()
}
}
}
+14
View File
@@ -0,0 +1,14 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
val a = MyClass()
a.foo1()
a.foo2()
a.foo3()<caret>
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
val a = MyClass().apply {
foo1()
foo2()
foo3()
}
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1() = Unit
fun foo2(a: MyClass) = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
val a = MyClass()
a.foo1()
a.foo2(this)<caret>
a.foo3()
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1() = Unit
fun foo2(a: MyClass) = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
val a = MyClass()
a.foo1()
a.foo2(this)
a.foo3()<caret>
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2(a: MyClass) = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
val a = MyClass()
a.foo1()
a.foo3()<caret>
a.foo2(this)
a.foo3()
}
}
@@ -0,0 +1,16 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2(a: MyClass) = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
val a = MyClass().apply {
foo1()
foo3()
}
a.foo2(this)
a.foo3()
}
}
+14
View File
@@ -0,0 +1,14 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4() {
var a = MyClass()
a.foo1()<caret>
a.foo2()
a.foo3()
}
}
+15
View File
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4() {
var a = MyClass().apply {
foo1()
foo2()
foo3()
}
}
}
@@ -0,0 +1,20 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
// top comment
val a = MyClass()
// here is comment
a.foo1()<caret>
// comment
// bbb
a.foo2()
a.foo3()
// last comment won't be in
}
}
@@ -0,0 +1,21 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
// top comment
val a = MyClass().apply {
// here is comment
foo1()
// comment
// bbb
foo2()
foo3()
}
// last comment won't be in
}
}
+1
View File
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertToRunIntention
+16
View File
@@ -0,0 +1,16 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.let {
it.foo1()<caret>
it.foo2()
it.foo3()
}
}
}
+13
View File
@@ -0,0 +1,13 @@
// WITH_RUNTIME
class MyClass {
fun foo1(): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4(a: MyClass) {
a.foo1().foo2().foo3()
a.foo2()<caret>
a.foo3()
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1(): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4(a: MyClass) {
a.run {
foo1().foo2().foo3()
foo2()
foo3()
}
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1(a: MyClass): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4(a: MyClass) {
listOf<MyClass>().forEach {
a.foo1(it).foo2().foo3()
a.foo2()<caret>
}
}
}
@@ -0,0 +1,13 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1(a: MyClass): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4(a: MyClass) {
a.foo1(this).foo2().foo3()
a.foo2()<caret>
}
}
+13
View File
@@ -0,0 +1,13 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.foo1()<caret>
a.foo2()
a.foo3()
}
}
+15
View File
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.run {
foo1()
foo2()
foo3()
}
}
}
+13
View File
@@ -0,0 +1,13 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.foo1()
a.foo2()<caret>
a.foo3()
}
}
+15
View File
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.run {
foo1()
foo2()
foo3()
}
}
}
+13
View File
@@ -0,0 +1,13 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.foo1()
a.foo2()
a.foo3()<caret>
}
}
+15
View File
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.run {
foo1()
foo2()
foo3()
}
}
}
+14
View File
@@ -0,0 +1,14 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1() = Unit
fun foo2(a: MyClass) = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.foo1()
a.foo2(this)<caret>
a.foo3()
}
}
+14
View File
@@ -0,0 +1,14 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
this.foo1()<caret>
this.foo2()
this.foo3()
}
}
@@ -0,0 +1,14 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2(a: MyClass) = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.foo1()
a.foo3()<caret>
a.foo2(this)
a.foo3()
}
}
@@ -0,0 +1,16 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2(a: MyClass) = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.run {
foo1()
foo3()
}
a.foo2(this)
a.foo3()
}
}
@@ -0,0 +1,18 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
// top comment
a.foo1()<caret>
// comment
// bbb
a.foo2()
a.foo3()
// last comment
}
}
@@ -0,0 +1,20 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
// top comment
a.run {
foo1()
// comment
// bbb
foo2()
foo3()
}
// last comment
}
}
+1
View File
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertToWithIntention
+16
View File
@@ -0,0 +1,16 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.let {
it.foo1()<caret>
it.foo2()
it.foo3()
}
}
}
+13
View File
@@ -0,0 +1,13 @@
// WITH_RUNTIME
class MyClass {
fun foo1(): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4(a: MyClass) {
a.foo1().foo2().foo3()
a.foo2()<caret>
a.foo3()
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1(): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4(a: MyClass) {
with(a) {
foo1().foo2().foo3()
foo2()
foo3()
}
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1(a: MyClass): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4(a: MyClass) {
listOf<MyClass>().forEach {
a.foo1(it).foo2().foo3()
a.foo2()<caret>
}
}
}
@@ -0,0 +1,13 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1(a: MyClass): MyClass = this
fun foo2(): MyClass = this
fun foo3(): MyClass = this
fun foo4(a: MyClass) {
a.foo1(this).foo2().foo3()
a.foo2()<caret>
}
}
+13
View File
@@ -0,0 +1,13 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.foo1()<caret>
a.foo2()
a.foo3()
}
}
+15
View File
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
with(a) {
foo1()
foo2()
foo3()
}
}
}
+13
View File
@@ -0,0 +1,13 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.foo1()
a.foo2()<caret>
a.foo3()
}
}
+15
View File
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
with(a) {
foo1()
foo2()
foo3()
}
}
}
+13
View File
@@ -0,0 +1,13 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.foo1()
a.foo2()
a.foo3()<caret>
}
}
+15
View File
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
with(a) {
foo1()
foo2()
foo3()
}
}
}
+14
View File
@@ -0,0 +1,14 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1() = Unit
fun foo2(a: MyClass) = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.foo1()
a.foo2(this)<caret>
a.foo3()
}
}
+14
View File
@@ -0,0 +1,14 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
this.foo1()<caret>
this.foo2()
this.foo3()
}
}
@@ -0,0 +1,14 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2(a: MyClass) = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
a.foo1()
a.foo3()<caret>
a.foo2(this)
a.foo3()
}
}
@@ -0,0 +1,16 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2(a: MyClass) = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
with(a) {
foo1()
foo3()
}
a.foo2(this)
a.foo3()
}
}
@@ -0,0 +1,18 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
// top comment
a.foo1()<caret>
// comment
// bbb
a.foo2()
a.foo3()
// last comment
}
}
@@ -0,0 +1,20 @@
// WITH_RUNTIME
class MyClass {
fun foo1() = Unit
fun foo2() = Unit
fun foo3() = Unit
fun foo4(a: MyClass) {
// top comment
with(a) {
foo1()
// comment
// bbb
foo2()
foo3()
}
// last comment
}
}
@@ -5637,6 +5637,81 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/convertToApply")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertToApply extends AbstractIntentionTest {
public void testAllFilesPresentInConvertToApply() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertToApply"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("methodChain.kt")
public void testMethodChain() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/methodChain.kt");
doTest(fileName);
}
@TestMetadata("methodChainWithItParameter.kt")
public void testMethodChainWithItParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/methodChainWithItParameter.kt");
doTest(fileName);
}
@TestMetadata("methodChainWithThisParameter.kt")
public void testMethodChainWithThisParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/methodChainWithThisParameter.kt");
doTest(fileName);
}
@TestMetadata("normal.kt")
public void testNormal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/normal.kt");
doTest(fileName);
}
@TestMetadata("normal2.kt")
public void testNormal2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/normal2.kt");
doTest(fileName);
}
@TestMetadata("normal3.kt")
public void testNormal3() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/normal3.kt");
doTest(fileName);
}
@TestMetadata("thisParameter.kt")
public void testThisParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/thisParameter.kt");
doTest(fileName);
}
@TestMetadata("thisParameter2.kt")
public void testThisParameter2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/thisParameter2.kt");
doTest(fileName);
}
@TestMetadata("untilThisParameter.kt")
public void testUntilThisParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/untilThisParameter.kt");
doTest(fileName);
}
@TestMetadata("var.kt")
public void testVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/var.kt");
doTest(fileName);
}
@TestMetadata("withCommentAndSpaces.kt")
public void testWithCommentAndSpaces() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToApply/withCommentAndSpaces.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/convertToBlockBody")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -6318,6 +6393,81 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/convertToRun")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertToRun extends AbstractIntentionTest {
public void testAllFilesPresentInConvertToRun() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertToRun"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("itReceiver.kt")
public void testItReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/itReceiver.kt");
doTest(fileName);
}
@TestMetadata("methodChain.kt")
public void testMethodChain() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/methodChain.kt");
doTest(fileName);
}
@TestMetadata("methodChainWithItParameter.kt")
public void testMethodChainWithItParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/methodChainWithItParameter.kt");
doTest(fileName);
}
@TestMetadata("methodChainWithThisParameter.kt")
public void testMethodChainWithThisParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/methodChainWithThisParameter.kt");
doTest(fileName);
}
@TestMetadata("normal.kt")
public void testNormal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/normal.kt");
doTest(fileName);
}
@TestMetadata("normal2.kt")
public void testNormal2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/normal2.kt");
doTest(fileName);
}
@TestMetadata("normal3.kt")
public void testNormal3() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/normal3.kt");
doTest(fileName);
}
@TestMetadata("thisParameter.kt")
public void testThisParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/thisParameter.kt");
doTest(fileName);
}
@TestMetadata("thisReceiver.kt")
public void testThisReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/thisReceiver.kt");
doTest(fileName);
}
@TestMetadata("untilThisParameter.kt")
public void testUntilThisParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/untilThisParameter.kt");
doTest(fileName);
}
@TestMetadata("withCommentAndSpaces.kt")
public void testWithCommentAndSpaces() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRun/withCommentAndSpaces.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/convertToStringTemplate")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -6556,6 +6706,81 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
@TestMetadata("idea/testData/intentions/convertToWith")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertToWith extends AbstractIntentionTest {
public void testAllFilesPresentInConvertToWith() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertToWith"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("itReceiver.kt")
public void testItReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/itReceiver.kt");
doTest(fileName);
}
@TestMetadata("methodChain.kt")
public void testMethodChain() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/methodChain.kt");
doTest(fileName);
}
@TestMetadata("methodChainWithItParameter.kt")
public void testMethodChainWithItParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/methodChainWithItParameter.kt");
doTest(fileName);
}
@TestMetadata("methodChainWithThisParameter.kt")
public void testMethodChainWithThisParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/methodChainWithThisParameter.kt");
doTest(fileName);
}
@TestMetadata("normal.kt")
public void testNormal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/normal.kt");
doTest(fileName);
}
@TestMetadata("normal2.kt")
public void testNormal2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/normal2.kt");
doTest(fileName);
}
@TestMetadata("normal3.kt")
public void testNormal3() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/normal3.kt");
doTest(fileName);
}
@TestMetadata("thisParameter.kt")
public void testThisParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/thisParameter.kt");
doTest(fileName);
}
@TestMetadata("thisReceiver.kt")
public void testThisReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/thisReceiver.kt");
doTest(fileName);
}
@TestMetadata("untilThisParameter.kt")
public void testUntilThisParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/untilThisParameter.kt");
doTest(fileName);
}
@TestMetadata("withCommentAndSpaces.kt")
public void testWithCommentAndSpaces() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToWith/withCommentAndSpaces.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/convertTryFinallyToUseCall")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)