Skip to content

Commit

Permalink
Add support for an extended indent after an opening ( in expressions
Browse files Browse the repository at this point in the history
### What's done:

 * The value of the indent is controlled with the `extendedIndentAfterOperators`
   flag.
 * Fixes #1448.
  • Loading branch information
0x6675636b796f75676974687562 committed Jul 25, 2022
1 parent 7f068a8 commit 0cd4aa1
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
* indentation if it's immediately followed by a newline.
*/
LPAR -> when {
treeNext.isWhiteSpaceWithNewline() -> SINGLE
treeNext.isWhiteSpaceWithNewline() -> IndentationAmount.valueOf(configuration.extendedIndentAfterOperators)
else -> NONE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ internal class IndentationConfig(config: Map<String, String>) : RuleConfiguratio
/**
* Is newline at the end of a file needed
*/
val newlineAtEnd = config["newlineAtEnd"]?.toBoolean() ?: true
val newlineAtEnd = config[NEWLINE_AT_END]?.toBoolean() ?: true

/**
* If true, in parameter list when parameters are split by newline they are indented with two indentations instead of one
*/
val extendedIndentOfParameters = config["extendedIndentOfParameters"]?.toBoolean() ?: false
val extendedIndentOfParameters = config[EXTENDED_INDENT_OF_PARAMETERS]?.toBoolean() ?: false

/**
* If true, if first parameter in parameter list is on the same line as opening parenthesis, then other parameters
* can be aligned with it
*/
val alignedParameters = config["alignedParameters"]?.toBoolean() ?: true
val alignedParameters = config[ALIGNED_PARAMETERS]?.toBoolean() ?: true

/**
* If `true`, expression bodies which begin on a separate line are indented
Expand Down Expand Up @@ -60,29 +60,63 @@ internal class IndentationConfig(config: Map<String, String>) : RuleConfiguratio
*
* @since 1.2.2
*/
val extendedIndentForExpressionBodies = config["extendedIndentForExpressionBodies"]?.toBoolean() ?: false
val extendedIndentForExpressionBodies = config[EXTENDED_INDENT_FOR_EXPRESSION_BODIES]?.toBoolean() ?: false

/**
* If true, if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one
*/
val extendedIndentAfterOperators = config["extendedIndentAfterOperators"]?.toBoolean() ?: true
val extendedIndentAfterOperators = config[EXTENDED_INDENT_AFTER_OPERATORS]?.toBoolean() ?: true

/**
* If true, when dot qualified expression starts on a new line, this line will be indented with
* two indentations instead of one
*/
val extendedIndentBeforeDot = config["extendedIndentBeforeDot"]?.toBoolean() ?: false
val extendedIndentBeforeDot = config[EXTENDED_INDENT_BEFORE_DOT]?.toBoolean() ?: false

/**
* The indentation size for each file
*/
val indentationSize = config["indentationSize"]?.toInt() ?: DEFAULT_INDENT_SIZE
val indentationSize = config[INDENTATION_SIZE]?.toInt() ?: DEFAULT_INDENTATION_SIZE

override fun equals(other: Any?): Boolean =
other is IndentationConfig && configWithExplicitDefaults == other.configWithExplicitDefaults

override fun hashCode(): Int =
configWithExplicitDefaults.hashCode()

override fun toString(): String =
"${javaClass.simpleName}$configWithExplicitDefaults"

internal companion object {
internal const val ALIGNED_PARAMETERS = "alignedParameters"

private companion object {
/**
* The default indent size (space characters), configurable via
* `indentationSize`.
*/
private const val DEFAULT_INDENT_SIZE = 4
private const val DEFAULT_INDENTATION_SIZE = 4
internal const val EXTENDED_INDENT_AFTER_OPERATORS = "extendedIndentAfterOperators"
internal const val EXTENDED_INDENT_BEFORE_DOT = "extendedIndentBeforeDot"
internal const val EXTENDED_INDENT_FOR_EXPRESSION_BODIES = "extendedIndentForExpressionBodies"
internal const val EXTENDED_INDENT_OF_PARAMETERS = "extendedIndentOfParameters"
internal const val INDENTATION_SIZE = "indentationSize"
internal const val NEWLINE_AT_END = "newlineAtEnd"

@Suppress(
"CUSTOM_GETTERS_SETTERS",
"STRING_TEMPLATE_QUOTES",
)
private val IndentationConfig.configWithExplicitDefaults: Map<String, String>
get() =
mutableMapOf<String, String>().apply {
putAll(config)
putIfAbsent(ALIGNED_PARAMETERS, "$alignedParameters")
putIfAbsent(EXTENDED_INDENT_AFTER_OPERATORS, "$extendedIndentAfterOperators")
putIfAbsent(EXTENDED_INDENT_BEFORE_DOT, "$extendedIndentBeforeDot")
putIfAbsent(EXTENDED_INDENT_FOR_EXPRESSION_BODIES, "$extendedIndentForExpressionBodies")
putIfAbsent(EXTENDED_INDENT_OF_PARAMETERS, "$extendedIndentOfParameters")
putIfAbsent(INDENTATION_SIZE, "$indentationSize")
putIfAbsent(NEWLINE_AT_END, "$newlineAtEnd")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationAmount.EXTENDED
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationAmount.NONE
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationAmount.SINGLE
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationConfigAware.Factory.withIndentationConfig
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.INDENTATION_SIZE

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.MethodOrderer.DisplayName
Expand All @@ -14,10 +15,10 @@ import org.junit.jupiter.params.provider.ValueSource

@TestMethodOrder(DisplayName::class)
class IndentationConfigAwareTest {
@ParameterizedTest(name = "indentationSize = {0}")
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
@ValueSource(ints = [2, 4, 8])
fun `Int + IndentationAmount`(indentationSize: Int) {
val config = IndentationConfig("indentationSize" to indentationSize)
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)

withIndentationConfig(config) {
assertThat(42 + NONE).isEqualTo(42)
Expand All @@ -26,10 +27,10 @@ class IndentationConfigAwareTest {
}
}

@ParameterizedTest(name = "indentationSize = {0}")
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
@ValueSource(ints = [2, 4, 8])
fun `Int - IndentationAmount`(indentationSize: Int) {
val config = IndentationConfig("indentationSize" to indentationSize)
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)

withIndentationConfig(config) {
assertThat(42 - NONE).isEqualTo(42)
Expand All @@ -38,10 +39,10 @@ class IndentationConfigAwareTest {
}
}

@ParameterizedTest(name = "indentationSize = {0}")
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
@ValueSource(ints = [2, 4, 8])
fun `IndentationAmount + Int`(indentationSize: Int) {
val config = IndentationConfig("indentationSize" to indentationSize)
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)

withIndentationConfig(config) {
assertThat(NONE + 42).isEqualTo(42 + NONE)
Expand All @@ -52,10 +53,10 @@ class IndentationConfigAwareTest {
}
}

@ParameterizedTest(name = "indentationSize = {0}")
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
@ValueSource(ints = [2, 4, 8])
fun `IndentationAmount - Int`(indentationSize: Int) {
val config = IndentationConfig("indentationSize" to indentationSize)
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)

withIndentationConfig(config) {
assertThat(NONE - 42).isEqualTo(-(42 - NONE))
Expand All @@ -66,10 +67,10 @@ class IndentationConfigAwareTest {
}
}

@ParameterizedTest(name = "indentationSize = {0}")
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
@ValueSource(ints = [2, 4, 8])
fun `IndentationAmount + IndentationAmount`(indentationSize: Int) {
val config = IndentationConfig("indentationSize" to indentationSize)
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)

withIndentationConfig(config) {
assertThat(NONE + SINGLE).isEqualTo(0 + SINGLE)
Expand All @@ -80,10 +81,10 @@ class IndentationConfigAwareTest {
}
}

@ParameterizedTest(name = "indentationSize = {0}")
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
@ValueSource(ints = [2, 4, 8])
fun `IndentationAmount - IndentationAmount`(indentationSize: Int) {
val config = IndentationConfig("indentationSize" to indentationSize)
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)

withIndentationConfig(config) {
assertThat(NONE - SINGLE).isEqualTo(0 - SINGLE)
Expand All @@ -95,10 +96,10 @@ class IndentationConfigAwareTest {
}
}

@ParameterizedTest(name = "indentationSize = {0}")
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
@ValueSource(ints = [2, 4, 8])
fun unaryPlus(indentationSize: Int) {
val config = IndentationConfig("indentationSize" to indentationSize)
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)

withIndentationConfig(config) {
assertThat(+NONE).isEqualTo(0)
Expand All @@ -109,10 +110,10 @@ class IndentationConfigAwareTest {
}
}

@ParameterizedTest(name = "indentationSize = {0}")
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
@ValueSource(ints = [2, 4, 8])
fun unaryMinus(indentationSize: Int) {
val config = IndentationConfig("indentationSize" to indentationSize)
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)

withIndentationConfig(config) {
assertThat(-NONE).isEqualTo(0)
Expand Down
Loading

0 comments on commit 0cd4aa1

Please sign in to comment.