"Add open to callable" intention introduced #KT-16786 Fixed

This commit is contained in:
shiraji
2017-04-10 01:01:04 +09:00
committed by Mikhail Glukhikh
parent cdb42c989e
commit 83169ad781
27 changed files with 263 additions and 0 deletions
@@ -0,0 +1,5 @@
open class Foo {
<spot>open</spot> fun foo() {
}
}
@@ -0,0 +1,5 @@
open class Foo {
fun foo() {
}
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention adds an open modifier.
</body>
</html>
+5
View File
@@ -1549,6 +1549,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddOpenModifierIntention</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,51 @@
/*
* 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.codeInsight.intention.LowPriorityAction
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.core.implicitModality
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
class AddOpenModifierIntention : SelfTargetingIntention<KtCallableDeclaration>(
KtCallableDeclaration::class.java, "Make open"
), LowPriorityAction {
override fun isApplicableTo(element: KtCallableDeclaration, caretOffset: Int): Boolean {
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
}
val implicitModality = element.implicitModality()
if (implicitModality == KtTokens.OPEN_KEYWORD || implicitModality == KtTokens.ABSTRACT_KEYWORD) return false
val ktClassOrObject = element.containingClassOrObject ?: return false
return ktClassOrObject.hasModifier(KtTokens.ENUM_KEYWORD)
|| ktClassOrObject.hasModifier(KtTokens.OPEN_KEYWORD)
|| ktClassOrObject.hasModifier(KtTokens.ABSTRACT_KEYWORD)
|| ktClassOrObject.hasModifier(KtTokens.SEALED_KEYWORD)
}
override fun applyTo(element: KtCallableDeclaration, editor: Editor?) {
element.addModifier(KtTokens.OPEN_KEYWORD)
}
}
+1
View File
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.AddOpenModifierIntention
@@ -0,0 +1,4 @@
abstract class Foo {
fun<caret> bar() {
}
}
@@ -0,0 +1,4 @@
abstract class Foo {
open fun bar() {
}
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
abstract class Foo {
abstract fun<caret> bar()
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
open class Foo {
open fun<caret> bar() {
}
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
data class Bar(val foo: Int, val bar: Int)
open class Foo {
<caret>var (foo, bar) = Bar(1, 1)
}
+5
View File
@@ -0,0 +1,5 @@
enum class E {
SINGLE;
<caret>fun bar() {}
}
@@ -0,0 +1,5 @@
enum class E {
SINGLE;
open fun bar() {}
}
+4
View File
@@ -0,0 +1,4 @@
open class Foo {
fun<caret> bar() {
}
}
@@ -0,0 +1,4 @@
open class Foo {
open fun bar() {
}
}
+5
View File
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
interface Foo {
fun<caret> bar()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
open class Foo {
fun bar() {
fun <caret> foo() {
}
}
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
open class Foo {
fun bar() {
var<caret> foo = 1
}
}
+6
View File
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
class Foo {
fun<caret> bar() {
}
}
+6
View File
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
open class Foo {
open fun bar(<caret>foo: Int) {
}
}
+6
View File
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
open class Foo {
private fun<caret> bar() {
}
}
+3
View File
@@ -0,0 +1,3 @@
open class Foo {
var<caret> bar = 0
}
@@ -0,0 +1,3 @@
open class Foo {
open var bar = 0
}
@@ -0,0 +1,4 @@
sealed class Foo {
fun<caret> bar() {
}
}
@@ -0,0 +1,4 @@
sealed class Foo {
open fun bar() {
}
}
+1
View File
@@ -4,6 +4,7 @@
// ACTION: Convert to block body
// ACTION: Move to companion object
// ACTION: Specify return type explicitly
// ACTION: Make open
interface Inter {
fun something(): String
@@ -531,6 +531,99 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/addOpenModifier")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddOpenModifier extends AbstractIntentionTest {
@TestMetadata("abstractClass.kt")
public void testAbstractClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/abstractClass.kt");
doTest(fileName);
}
@TestMetadata("abstractFunction.kt")
public void testAbstractFunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/abstractFunction.kt");
doTest(fileName);
}
public void testAllFilesPresentInAddOpenModifier() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addOpenModifier"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("alreadyOpen.kt")
public void testAlreadyOpen() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/alreadyOpen.kt");
doTest(fileName);
}
@TestMetadata("destructuringDeclaration.kt")
public void testDestructuringDeclaration() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/destructuringDeclaration.kt");
doTest(fileName);
}
@TestMetadata("enumClass.kt")
public void testEnumClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/enumClass.kt");
doTest(fileName);
}
@TestMetadata("function.kt")
public void testFunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/function.kt");
doTest(fileName);
}
@TestMetadata("interface.kt")
public void testInterface() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/interface.kt");
doTest(fileName);
}
@TestMetadata("localFunction.kt")
public void testLocalFunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/localFunction.kt");
doTest(fileName);
}
@TestMetadata("localProperty.kt")
public void testLocalProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/localProperty.kt");
doTest(fileName);
}
@TestMetadata("notOpen.kt")
public void testNotOpen() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/notOpen.kt");
doTest(fileName);
}
@TestMetadata("parameter.kt")
public void testParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/parameter.kt");
doTest(fileName);
}
@TestMetadata("private.kt")
public void testPrivate() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/private.kt");
doTest(fileName);
}
@TestMetadata("property.kt")
public void testProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/property.kt");
doTest(fileName);
}
@TestMetadata("sealedClass.kt")
public void testSealedClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addOpenModifier/sealedClass.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/addOperatorModifier")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)