Allow empty single-line bodies in property accessors

This commit is contained in:
Nikolay Krasko
2016-12-12 20:24:15 +03:00
committed by Nikolay Krasko
parent 2841931ffa
commit 2bb48fc802
18 changed files with 64 additions and 40 deletions

View File

@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtPropertyAccessor
val MODIFIERS_LIST_ENTRIES = TokenSet.orSet(TokenSet.create(ANNOTATION_ENTRY, ANNOTATION), MODIFIER_KEYWORDS)
@@ -406,19 +407,33 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
inPosition(parent = CLASS_BODY, right = RBRACE).lineBreakIfLineBreakInParent(numSpacesOtherwise = 1)
inPosition(parent = BLOCK, right = RBRACE).customRule { block, left, right ->
val funNode = block.node.treeParent.psi as? KtFunction ?: return@customRule null
val psiElement = block.node.treeParent.psi
val empty = left.node.elementType == LBRACE
if (funNode.name != null && !empty) return@customRule null
when (psiElement) {
is KtFunction -> {
if (psiElement.name != null && !empty) return@customRule null
}
is KtPropertyAccessor ->
if (!empty) return@customRule null
else ->
return@customRule null
}
val spaces = if (empty) 0 else spacesInSimpleFunction
Spacing.createDependentLFSpacing(spaces, spaces, funNode.textRange,
Spacing.createDependentLFSpacing(spaces, spaces, psiElement.textRange,
codeStyleSettings.KEEP_LINE_BREAKS,
codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
}
inPosition(parent = BLOCK, left = LBRACE).customRule { parent, left, right ->
val funNode = parent.node.treeParent.psi as? KtFunction ?: return@customRule null
val psiElement = parent.node.treeParent.psi
val funNode = psiElement as? KtFunction ?: return@customRule null
if (funNode.name != null) return@customRule null
// Empty block is covered in above rule
Spacing.createDependentLFSpacing(spacesInSimpleFunction, spacesInSimpleFunction, funNode.textRange,
codeStyleSettings.KEEP_LINE_BREAKS,
codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)

View File

@@ -5,8 +5,7 @@ interface I {
class A : I {
override var someVar: String
get() = <caret><selection>TODO("not implemented")</selection> //To change initializer of created properties use File | Settings | File Templates.
set(value) {
}
set(value) {}
}
// ELEMENT_TEXT: "override var someVar: String"

View File

@@ -5,8 +5,7 @@ open class B {
class A : B {
override var someVar: String
get() = <caret><selection>super.someVar</selection>
set(value) {
}
set(value) {}
}
// ELEMENT_TEXT: "override var someVar: String"

View File

@@ -54,6 +54,6 @@ class KotlinPropertySetterBodyFixer : SmartEnterProcessorWithFixers.Fixer<Kotlin
endOffset--
}
doc.insertString(endOffset, "{}")
doc.insertString(endOffset, "{\n}")
}
}

View File

@@ -5,6 +5,5 @@ interface A {
class B : A {
override var Int.foo: Double
get() = <selection><caret>TODO("not implemented")</selection> //To change initializer of created properties use File | Settings | File Templates.
set(value) {
}
set(value) {}
}

View File

@@ -13,3 +13,11 @@ class Hi {
var badNoType get() = 1
}
class EmptyProperties {
var newline: String
get() {
return ""
}
set(value) {}
}

View File

@@ -19,3 +19,9 @@ get() = 1
var badNoType get() = 1
}
class EmptyProperties {
var newline: String
get() { return "" }
set(value) {}
}

View File

@@ -13,5 +13,4 @@ private var A.foo: Boolean
get() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
set() {
}
set() {}

View File

@@ -11,5 +11,4 @@ private var Int.foo: A<String>
get() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
set() {
}
set() {}

View File

@@ -11,8 +11,7 @@ private abstract class Base {
private class BaseImpl : Base() {
override var x: Int
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
set(value) {
}
set(value) {}
override fun toInt(arg: String): Int {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

View File

@@ -10,7 +10,6 @@ class Container {
class BaseImpl : Base {
override var z: Double
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
set(value) {
}
set(value) {}
}
}

View File

@@ -1111,6 +1111,19 @@ class SmartEnterTest : KotlinLightCodeInsightFixtureTestCase() {
"""
)
fun testSetter7() = doFileTest(
"""
var a : Int = 0
set(value){<caret>}
"""
,
"""
var a : Int = 0
set(value) {}
<caret>
"""
)
fun testSetterPrivate1() = doFileTest(
"""
var a : Int = 0

View File

@@ -18,7 +18,6 @@ internal class A(// comment for field2 getter
// comment for setProperty
var property: Int
get() = 1
set(value) {
} // end of getProperty
set(value) {} // end of getProperty
// end of setProperty
}

View File

@@ -18,8 +18,7 @@ internal interface I {
internal abstract class C : I {
override var isSomething1: Boolean
get() = true
set(b) {
}
set(b) {}
override var isSomething4: Boolean
get() = false
@@ -27,11 +26,9 @@ internal abstract class C : I {
override var isSomething5: Boolean
get
set(value) {
}
set(value) {}
override var something6: Boolean
get
set(value) {
}
set(value) {}
}

View File

@@ -26,18 +26,15 @@ internal open class B {
open var fromB2: String
get() = ""
set(value) {
}
set(value) {}
open var fromB3: String
get() = ""
set(value) {
}
set(value) {}
open var fromB4: String
get() = ""
set(value) {
}
set(value) {}
open fun setFromB5(value: String) {}
}
@@ -50,8 +47,7 @@ internal abstract class C(override val something1: Int) : B(), I {
override var something3: Int
get() = 0
set(value) {
}
set(value) {}
override var something4: Int
get() = 0

View File

@@ -90,7 +90,6 @@ internal class D : JavaClassDerivedFromKotlinClassWithProperties() {
override var someVar2: String
get() = super.someVar2
set(value) {
}
set(value) {}
}

View File

@@ -1,3 +1,2 @@
val t: T
get() {
}
get() {}

View File

@@ -5,7 +5,6 @@ package a
open class FooBean {
fun setBarBean(barBean: BarBean) {}
}
open class BarBean