mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-03-27 08:31:28 +00:00
Allow empty single-line bodies in property accessors
This commit is contained in:
committed by
Nikolay Krasko
parent
2841931ffa
commit
2bb48fc802
@@ -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)
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -54,6 +54,6 @@ class KotlinPropertySetterBodyFixer : SmartEnterProcessorWithFixers.Fixer<Kotlin
|
||||
endOffset--
|
||||
}
|
||||
|
||||
doc.insertString(endOffset, "{}")
|
||||
doc.insertString(endOffset, "{\n}")
|
||||
}
|
||||
}
|
||||
@@ -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) {}
|
||||
}
|
||||
|
||||
@@ -13,3 +13,11 @@ class Hi {
|
||||
|
||||
var badNoType get() = 1
|
||||
}
|
||||
|
||||
class EmptyProperties {
|
||||
var newline: String
|
||||
get() {
|
||||
return ""
|
||||
}
|
||||
set(value) {}
|
||||
}
|
||||
|
||||
6
idea/testData/formatter/PropertyAccessors.kt
vendored
6
idea/testData/formatter/PropertyAccessors.kt
vendored
@@ -19,3 +19,9 @@ get() = 1
|
||||
|
||||
var badNoType get() = 1
|
||||
}
|
||||
|
||||
class EmptyProperties {
|
||||
var newline: String
|
||||
get() { return "" }
|
||||
set(value) {}
|
||||
}
|
||||
|
||||
@@ -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() {}
|
||||
|
||||
@@ -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() {}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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) {}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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) {}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -90,7 +90,6 @@ internal class D : JavaClassDerivedFromKotlinClassWithProperties() {
|
||||
|
||||
override var someVar2: String
|
||||
get() = super.someVar2
|
||||
set(value) {
|
||||
}
|
||||
set(value) {}
|
||||
|
||||
}
|
||||
@@ -1,3 +1,2 @@
|
||||
val t: T
|
||||
get() {
|
||||
}
|
||||
get() {}
|
||||
@@ -5,7 +5,6 @@ package a
|
||||
|
||||
open class FooBean {
|
||||
fun setBarBean(barBean: BarBean) {}
|
||||
|
||||
}
|
||||
|
||||
open class BarBean
|
||||
Reference in New Issue
Block a user