Add intention to convert top level val with object expression to object declaration (#974)
* Add intention to convert top level val with object expression to object Fixes #KT-14137 * fix intention description
This commit is contained in:
committed by
Dmitry Jemerov
parent
93b5eec71e
commit
8e00af5642
@@ -0,0 +1,9 @@
|
|||||||
|
interface B {
|
||||||
|
fun c()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
<spot>object a : B</spot> {
|
||||||
|
override fun c() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
interface B {
|
||||||
|
fun c()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
<spot>val a = object: B</spot> {
|
||||||
|
override fun c() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
Converts a top-level property that is initialized with an object expression to an object declaration.
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1574,6 +1574,11 @@
|
|||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
</intentionAction>
|
</intentionAction>
|
||||||
|
|
||||||
|
<intentionAction>
|
||||||
|
<className>org.jetbrains.kotlin.idea.intentions.ValToObjectIntention</className>
|
||||||
|
<category>Kotlin</category>
|
||||||
|
</intentionAction>
|
||||||
|
|
||||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
||||||
displayName="Object literal can be converted to lambda"
|
displayName="Object literal can be converted to lambda"
|
||||||
groupName="Kotlin"
|
groupName="Kotlin"
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* 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 com.intellij.psi.search.searches.ReferencesSearch
|
||||||
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
|
import org.jetbrains.kotlin.idea.references.KtReference
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.visibilityModifier
|
||||||
|
|
||||||
|
class ValToObjectIntention : SelfTargetingIntention<KtProperty>(KtProperty::class.java, "Convert to object declaration") {
|
||||||
|
|
||||||
|
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean {
|
||||||
|
if (element.isVar) return false
|
||||||
|
if (!element.isTopLevel) return false
|
||||||
|
|
||||||
|
val initializer = element.initializer as? KtObjectLiteralExpression ?: return false
|
||||||
|
if (initializer.objectDeclaration.getBody() == null) return false
|
||||||
|
|
||||||
|
if (element.getter != null) return false
|
||||||
|
if (element.annotationEntries.isNotEmpty()) return false
|
||||||
|
|
||||||
|
// disable if has non-Kotlin usages
|
||||||
|
return ReferencesSearch.search(element).all { it is KtReference && it.element.parent !is KtCallableReferenceExpression }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun applyTo(element: KtProperty, editor: Editor?) {
|
||||||
|
val modifier = element.visibilityModifier()
|
||||||
|
val name = element.name ?: return
|
||||||
|
val objectLiteral = element.initializer as? KtObjectLiteralExpression ?: return
|
||||||
|
val declaration = objectLiteral.objectDeclaration
|
||||||
|
val superTypeList = declaration.getSuperTypeList()
|
||||||
|
val body = declaration.getBody() ?: return
|
||||||
|
|
||||||
|
val prefix = modifier?.text?.plus(" ") ?: ""
|
||||||
|
val superTypesText = superTypeList?.text?.plus(" ") ?: ""
|
||||||
|
|
||||||
|
val replacementText = "${prefix}object $name: $superTypesText${body.text}"
|
||||||
|
val replaced = element.replaced(KtPsiFactory(element).createDeclarationByPattern<KtObjectDeclaration>(replacementText))
|
||||||
|
|
||||||
|
editor?.caretModel?.moveToOffset(replaced.nameIdentifier?.endOffset ?: return)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.idea.intentions.ValToObjectIntention
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
interface B {
|
||||||
|
}
|
||||||
|
|
||||||
|
annotation class Ann
|
||||||
|
|
||||||
|
@Ann
|
||||||
|
val <caret>a = object : B {
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
interface B {
|
||||||
|
}
|
||||||
|
|
||||||
|
val <caret>a = object : B {
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
val ref = ::a
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
interface B {
|
||||||
|
}
|
||||||
|
|
||||||
|
val <caret>a = object : B {
|
||||||
|
}
|
||||||
|
get() = field
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
interface B {
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
val <caret>a = object : B {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
interface B {
|
||||||
|
}
|
||||||
|
|
||||||
|
val <caret>a = object : B {
|
||||||
|
}
|
||||||
|
|
||||||
|
val c = a
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
interface B {
|
||||||
|
}
|
||||||
|
|
||||||
|
object a<caret> : B {
|
||||||
|
}
|
||||||
|
|
||||||
|
val c = a
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
interface B {
|
||||||
|
}
|
||||||
|
|
||||||
|
var <caret>a = object : B {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
import test.WithJavaUsageKt
|
||||||
|
|
||||||
|
class A {
|
||||||
|
void b() {
|
||||||
|
WithJavaUsageKt.getA();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
package test
|
||||||
|
|
||||||
|
interface B {
|
||||||
|
}
|
||||||
|
|
||||||
|
val <caret>a = object : B {
|
||||||
|
}
|
||||||
@@ -15616,4 +15616,55 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/intentions/valToObject")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class ValToObject extends AbstractIntentionTest {
|
||||||
|
public void testAllFilesPresentInValToObject() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/valToObject"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("annotations.kt")
|
||||||
|
public void testAnnotations() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/valToObject/annotations.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("callableReference.kt")
|
||||||
|
public void testCallableReference() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/valToObject/callableReference.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("getter.kt")
|
||||||
|
public void testGetter() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/valToObject/getter.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("nonTopLevel.kt")
|
||||||
|
public void testNonTopLevel() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/valToObject/nonTopLevel.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("simple.kt")
|
||||||
|
public void testSimple() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/valToObject/simple.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("var.kt")
|
||||||
|
public void testVar() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/valToObject/var.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("withJavaUsage.kt")
|
||||||
|
public void testWithJavaUsage() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/valToObject/withJavaUsage.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user