Create from usage: make new member function/property private by default
#KT-6689 Fixed
This commit is contained in:
+8
-2
@@ -72,6 +72,7 @@ import com.intellij.openapi.editor.LogicalPosition
|
||||
import com.intellij.openapi.editor.actions.EditorActionUtil
|
||||
import com.intellij.openapi.editor.actions.LineEndAction
|
||||
import com.intellij.openapi.editor.actions.EnterAction
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
||||
|
||||
private val TYPE_PARAMETER_LIST_VARIABLE_NAME = "typeParameterList"
|
||||
private val TEMPLATE_FROM_USAGE_FUNCTION_BODY = "New Kotlin Function Body.kt"
|
||||
@@ -392,8 +393,13 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
||||
|
||||
val psiFactory = JetPsiFactory(currentFile)
|
||||
|
||||
val modifiers =
|
||||
if (containingElement is JetClassOrObject && containingElement.isAncestor(config.originalElement))
|
||||
"private "
|
||||
else ""
|
||||
|
||||
val declaration : JetNamedDeclaration = when (callableInfo.kind) {
|
||||
CallableKind.FUNCTION -> psiFactory.createFunction("fun<> $header {}")
|
||||
CallableKind.FUNCTION -> psiFactory.createFunction("${modifiers}fun<> $header {}")
|
||||
CallableKind.CONSTRUCTOR -> {
|
||||
with((callableInfo as ConstructorInfo).classInfo) {
|
||||
val classBody = when (kind) {
|
||||
@@ -422,7 +428,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
|
||||
}
|
||||
CallableKind.PROPERTY -> {
|
||||
val valVar = if ((callableInfo as PropertyInfo).writable) "var" else "val"
|
||||
psiFactory.createProperty("$valVar<> $header")
|
||||
psiFactory.createProperty("$modifiers$valVar<> $header")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ class A {
|
||||
return foo(2, "2")
|
||||
}
|
||||
|
||||
fun foo(i: Int, s: String): Int {
|
||||
private fun foo(i: Int, s: String): Int {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import kotlin.properties.ReadOnlyProperty
|
||||
class A<T>(val t: T) {
|
||||
val x: A<Int> by foo(t, "")
|
||||
|
||||
fun foo(t: T, s: String): ReadOnlyProperty<A<T>, A<Int>> {
|
||||
private fun foo(t: T, s: String): ReadOnlyProperty<A<T>, A<Int>> {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import kotlin.properties.ReadWriteProperty
|
||||
class A<T>(val t: T) {
|
||||
var x: A<Int> by foo(t, "")
|
||||
|
||||
fun foo(t: T, s: String): ReadWriteProperty<A<T>, A<Int>> {
|
||||
private fun foo(t: T, s: String): ReadWriteProperty<A<T>, A<Int>> {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ class A {
|
||||
return foo(2, "2")
|
||||
}
|
||||
|
||||
fun foo(i: Int, s: String): Int {
|
||||
private fun foo(i: Int, s: String): Int {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Create function 'foo'" "true"
|
||||
class A {
|
||||
fun test() {
|
||||
foo()
|
||||
}
|
||||
|
||||
private fun foo() {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ class A<T>(val n: T) {
|
||||
return this.foo(2, "2")
|
||||
}
|
||||
|
||||
fun foo(i: Int, s: String): A<Int> {
|
||||
private fun foo(i: Int, s: String): A<Int> {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -6,7 +6,7 @@ class A<T>(val n: T) {
|
||||
return this.foo(2, "2")
|
||||
}
|
||||
|
||||
fun foo(i: Int, s: String): A<Int> {
|
||||
private fun foo(i: Int, s: String): A<Int> {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ class A<T>(val n: T) {
|
||||
}
|
||||
}
|
||||
|
||||
fun foo(i: Int, s: String): A<Int> {
|
||||
private fun foo(i: Int, s: String): A<Int> {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Create function 'foo'" "true"
|
||||
class A {
|
||||
fun test() {
|
||||
<caret>foo()
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -4,7 +4,7 @@ class Foo<T> {
|
||||
val z: Iterable<T> = y[""]
|
||||
}
|
||||
|
||||
fun get(s: String): T {
|
||||
private fun get(s: String): T {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ class Foo<T> {
|
||||
val z: Iterable<T> = y["", w]
|
||||
}
|
||||
|
||||
fun get(s: String, w: T): T {
|
||||
private fun get(s: String, w: T): T {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ class Foo<T> {
|
||||
bar(z)
|
||||
}
|
||||
|
||||
fun <V> get(s: String, w: ArrayList<V>): String {
|
||||
private fun <V> get(s: String, w: ArrayList<V>): String {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ class Foo<T> {
|
||||
val z: Iterable<S> = y[""]
|
||||
}
|
||||
|
||||
fun get(s: String): T {
|
||||
private fun get(s: String): T {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ class Foo<T, S: Iterable<T>> {
|
||||
val z: U = y[""]
|
||||
}
|
||||
|
||||
fun get(s: String): T {
|
||||
private fun get(s: String): T {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ class Foo<T> {
|
||||
val z: Iterable<T> = y["", w]
|
||||
}
|
||||
|
||||
fun <V> get(s: String, w: Iterable<V>): T {
|
||||
private fun <V> get(s: String, w: Iterable<V>): T {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ class Foo<T> {
|
||||
val z: Iterable<T> = y["", w, v]
|
||||
}
|
||||
|
||||
fun <V, T1> get(s: String, w: ArrayList<V>, v: T1): T {
|
||||
private fun <V, T1> get(s: String, w: ArrayList<V>, v: T1): T {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ class Foo<T> {
|
||||
val z: Iterable<T> = y["", w]
|
||||
}
|
||||
|
||||
fun get(s: String, w: T): T {
|
||||
private fun get(s: String, w: T): T {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ class Foo<S> {
|
||||
val z: Iterable<T> = y["", w]
|
||||
}
|
||||
|
||||
fun get(s: String, w: S): S {
|
||||
private fun get(s: String, w: S): S {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ class Foo<T> {
|
||||
y["", w] = w
|
||||
}
|
||||
|
||||
fun set(s: String, w: T, value: T) {
|
||||
private fun set(s: String, w: T, value: T) {
|
||||
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,6 +5,6 @@
|
||||
import kotlin.properties.ReadOnlyProperty
|
||||
|
||||
class A<T> {
|
||||
val foo: ReadOnlyProperty<A<T>, A<Int>>
|
||||
private val foo: ReadOnlyProperty<A<T>, A<Int>>
|
||||
val x: A<Int> by foo
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
|
||||
class A {
|
||||
class B {
|
||||
val foo: Int
|
||||
private val foo: Int
|
||||
|
||||
fun test(): Int {
|
||||
return foo
|
||||
|
||||
+1
-1
@@ -5,6 +5,6 @@
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
|
||||
class A<T> {
|
||||
val foo: ReadWriteProperty<A<T>, A<Int>>
|
||||
private val foo: ReadWriteProperty<A<T>, A<Int>>
|
||||
var x: A<Int> by foo
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
|
||||
class A {
|
||||
object B {
|
||||
val foo: Int
|
||||
private val foo: Int
|
||||
|
||||
fun test(): Int {
|
||||
return foo
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Create property 'foo'" "true"
|
||||
// ERROR: Property must be initialized or be abstract
|
||||
class A {
|
||||
private var foo: Int
|
||||
|
||||
fun test() {
|
||||
foo = 1
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
// ERROR: Property must be initialized or be abstract
|
||||
|
||||
class A<T>(val n: T) {
|
||||
val foo: A<Int>
|
||||
private val foo: A<Int>
|
||||
|
||||
fun test(): A<Int> {
|
||||
return this.foo
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
|
||||
class A<T>(val n: T) {
|
||||
inner class B<U>(val m: U) {
|
||||
val foo: A<Int>
|
||||
private val foo: A<Int>
|
||||
|
||||
fun test(): A<Int> {
|
||||
return this.foo
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
// ERROR: Property must be initialized or be abstract
|
||||
|
||||
class A<T>(val n: T) {
|
||||
val foo: A<Int>
|
||||
private val foo: A<Int>
|
||||
|
||||
inner class B<U>(val m: U) {
|
||||
fun test(): A<Int> {
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Create property 'foo'" "true"
|
||||
// ERROR: Property must be initialized or be abstract
|
||||
class A {
|
||||
fun test() {
|
||||
<caret>foo = 1
|
||||
}
|
||||
}
|
||||
@@ -1660,6 +1660,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforePrivateForMembers.kt")
|
||||
public void testPrivateForMembers() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforePrivateForMembers.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforePropertyOnUserType.kt")
|
||||
public void testPropertyOnUserType() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforePropertyOnUserType.kt");
|
||||
@@ -2519,6 +2525,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforePrivateForMembers.kt")
|
||||
public void testPrivateForMembers() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/property/beforePrivateForMembers.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("beforeThisInClass.kt")
|
||||
public void testThisInClass() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createVariable/property/beforeThisInClass.kt");
|
||||
|
||||
Reference in New Issue
Block a user