Single indent for declarations on adding new line (KT-26954)

#KT-26954 Fixed
This commit is contained in:
Nikolay Krasko
2019-04-03 16:30:20 +03:00
parent e4cc94e0a8
commit 814aca9c8b
2 changed files with 71 additions and 10 deletions
@@ -244,7 +244,7 @@ abstract class KotlinCommonBlock(
if (childParent != null) {
val parentType = childParent.elementType
if (parentType === KtNodeTypes.VALUE_PARAMETER_LIST || parentType === KtNodeTypes.VALUE_ARGUMENT_LIST) {
if (parentType === VALUE_PARAMETER_LIST || parentType === KtNodeTypes.VALUE_ARGUMENT_LIST) {
val prev = getPrevWithoutWhitespace(child)
if (childType === RPAR && (prev == null || prev.elementType !== TokenType.ERROR_ELEMENT)) {
return Indent.getNoneIndent()
@@ -305,18 +305,18 @@ abstract class KotlinCommonBlock(
null
)
KtNodeTypes.TRY -> ChildAttributes(Indent.getNoneIndent(), null)
TRY -> ChildAttributes(Indent.getNoneIndent(), null)
in QUALIFIED_EXPRESSIONS -> ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null)
KtNodeTypes.VALUE_PARAMETER_LIST, KtNodeTypes.VALUE_ARGUMENT_LIST -> {
VALUE_PARAMETER_LIST, KtNodeTypes.VALUE_ARGUMENT_LIST -> {
val subBlocks = getSubBlocks()
if (newChildIndex != 1 && newChildIndex != 0 && newChildIndex < subBlocks.size) {
val block = subBlocks[newChildIndex]
ChildAttributes(block.indent, block.alignment)
} else {
val indent =
if ((type == KtNodeTypes.VALUE_PARAMETER_LIST && !settings.kotlinCustomSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS) ||
if ((type == VALUE_PARAMETER_LIST && !settings.kotlinCustomSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS) ||
(type == KtNodeTypes.VALUE_ARGUMENT_LIST && !settings.kotlinCustomSettings.CONTINUATION_INDENT_IN_ARGUMENT_LISTS)
) {
Indent.getNormalIndent()
@@ -329,13 +329,20 @@ abstract class KotlinCommonBlock(
DOC_COMMENT -> ChildAttributes(Indent.getSpaceIndent(KDOC_COMMENT_INDENT), null)
KtNodeTypes.PARENTHESIZED -> getSuperChildAttributes(newChildIndex)
PARENTHESIZED -> getSuperChildAttributes(newChildIndex)
else -> {
val blocks = getSubBlocks()
if (newChildIndex != 0) {
val isIncomplete = if (newChildIndex < blocks.size) blocks[newChildIndex - 1].isIncomplete else isIncompleteInSuper()
if (isIncomplete) {
if (blocks.size == newChildIndex && !settings.kotlinCustomSettings.CONTINUATION_INDENT_FOR_EXPRESSION_BODIES) {
val lastInParent = blocks.last()
if (lastInParent is ASTBlock && lastInParent.node?.elementType in ALL_ASSIGNMENTS) {
return ChildAttributes(Indent.getNormalIndent(), null)
}
}
return getSuperChildAttributes(newChildIndex)
}
}
@@ -355,7 +362,7 @@ abstract class KotlinCommonBlock(
val kotlinCustomSettings = settings.kotlinCustomSettings
val parentType = node.elementType
return when {
parentType === KtNodeTypes.VALUE_PARAMETER_LIST ->
parentType === VALUE_PARAMETER_LIST ->
getAlignmentForChildInParenthesis(
kotlinCommonSettings.ALIGN_MULTILINE_PARAMETERS, KtNodeTypes.VALUE_PARAMETER, COMMA,
kotlinCommonSettings.ALIGN_MULTILINE_METHOD_BRACKETS, LPAR, RPAR
@@ -379,7 +386,7 @@ abstract class KotlinCommonBlock(
parentType === KtNodeTypes.SUPER_TYPE_LIST ->
createAlignmentStrategy(kotlinCommonSettings.ALIGN_MULTILINE_EXTENDS_LIST, getAlignment())
parentType === KtNodeTypes.PARENTHESIZED ->
parentType === PARENTHESIZED ->
object : CommonAlignmentStrategy() {
private var bracketsAlignment: Alignment? =
if (kotlinCommonSettings.ALIGN_MULTILINE_BINARY_OPERATION) Alignment.createAlignment() else null
@@ -513,7 +520,7 @@ abstract class KotlinCommonBlock(
return getWrappingStrategyForItemList(wrapSetting, KtNodeTypes.VALUE_ARGUMENT)
}
elementType === KtNodeTypes.VALUE_PARAMETER_LIST -> {
elementType === VALUE_PARAMETER_LIST -> {
if (parentElementType === KtNodeTypes.FUN ||
parentElementType === KtNodeTypes.PRIMARY_CONSTRUCTOR ||
parentElementType === KtNodeTypes.SECONDARY_CONSTRUCTOR) {
@@ -802,7 +809,7 @@ private val INDENT_RULES = arrayOf(
.set(Indent.getContinuationWithoutFirstIndent(false)),
strategy("Parenthesized expression")
.within(KtNodeTypes.PARENTHESIZED)
.within(PARENTHESIZED)
.set(Indent.getContinuationWithoutFirstIndent(false)),
strategy("Opening parenthesis for conditions")
@@ -840,7 +847,7 @@ private val INDENT_RULES = arrayOf(
.set(Indent.getNormalIndent()),
strategy("Parameter list")
.within(KtNodeTypes.VALUE_PARAMETER_LIST)
.within(VALUE_PARAMETER_LIST)
.forElement { it.elementType == KtNodeTypes.VALUE_PARAMETER && it.psi.prevSibling != null }
.continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_PARAMETER_LISTS, indentFirst = true),
@@ -11,6 +11,8 @@ import com.intellij.testFramework.LightCodeInsightTestCase
import com.intellij.testFramework.LightPlatformCodeInsightTestCase
import com.intellij.testFramework.LightPlatformTestCase
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.idea.formatter.KotlinStyleGuideCodeStyle
import org.jetbrains.kotlin.idea.formatter.ktCodeStyleSettings
class TypedHandlerTest : LightCodeInsightTestCase() {
private val dollar = '$'
@@ -810,6 +812,51 @@ class TypedHandlerTest : LightCodeInsightTestCase() {
)
}
fun testEnterInFunctionWithExpressionBody() {
doTypeTest(
'\n',
"""
|fun test() =<caret>
""",
"""
|fun test() =
| <caret>
""",
ENABLE_KOTLIN_OFFICIAL_CODE_STYLE
)
}
fun testEnterInMultiDeclaration() {
doTypeTest(
'\n',
"""
|fun test() {
| val (a, b) =<caret>
|}
""",
"""
|fun test() {
| val (a, b) =
| <caret>
|}
""",
ENABLE_KOTLIN_OFFICIAL_CODE_STYLE
)
}
fun testEnterInVariableDeclaration() {
doTypeTest(
'\n',
"""
|val test =<caret>
""",
"""
|val test =
| <caret>
""",
ENABLE_KOTLIN_OFFICIAL_CODE_STYLE
)
}
fun testMoveThroughGT() {
LightPlatformCodeInsightTestCase.configureFromFileText("a.kt", "val a: List<Set<Int<caret>>>")
@@ -870,4 +917,11 @@ class TypedHandlerTest : LightCodeInsightTestCase() {
private fun doLtGtTest(initText: String) {
doLtGtTest(initText, true)
}
companion object {
private val ENABLE_KOTLIN_OFFICIAL_CODE_STYLE: () -> Unit = {
val settings = ktCodeStyleSettings(LightPlatformTestCase.getProject())?.all ?: error("No Settings")
KotlinStyleGuideCodeStyle.apply(settings)
}
}
}