Skip to content

Commit

Permalink
[#1347] Stop using DEFAULT_INDENT_SIZE directly (#1382)
Browse files Browse the repository at this point in the history
### What's done:

 * `DEFAULT_INDENT_SIZE` is no longer used directly, in favor of `IndentationConfig.indentationSize`.
 * `IndentationConfig.indentationSize` usages encapsulated where possible.
  • Loading branch information
0x6675636b796f75676974687562 authored Jun 21, 2022
1 parent fcbb82b commit 9288199
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
*/
val regularStringPart = templateEntry.firstChildNode as LeafPsiElement
val regularStringPartText = regularStringPart.checkRegularStringPart().text
val nodeStartIndentOrNegative = regularStringPartText.leadingSpaceCount() - actualIndent - DEFAULT_INDENT_SIZE
val nodeStartIndentOrNegative = (regularStringPartText.leadingSpaceCount() - actualIndent).unindent()
// shift of the node depending on its initial string template indent
val nodeStartIndent = nodeStartIndentOrNegative.zeroIfNegative()

Expand All @@ -385,7 +385,7 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
}

else -> {
val textIndent = (expectedIndent + DEFAULT_INDENT_SIZE).spaces
val textIndent = expectedIndent.indent().spaces

when {
// if string template is after literal_string
Expand All @@ -411,6 +411,30 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
}
}

/**
* Increases the indentation level by [level] * [IndentationConfig.indentationSize].
*
* @param level the indentation level, 1 by default.
* @see unindent
* @see IndentationConfig.indentationSize
* @see IndentContext.maybeIncrement
* @see IndentContext.dec
*/
private fun Int.indent(level: Int = 1): Int =
this + level * configuration.indentationSize

/**
* Decreases the indentation level by [level] * [IndentationConfig.indentationSize].
*
* @param level the indentation level, 1 by default.
* @see indent
* @see IndentationConfig.indentationSize
* @see IndentContext.maybeIncrement
* @see IndentContext.dec
*/
private fun Int.unindent(level: Int = 1): Int =
this - level * configuration.indentationSize

/**
* Class that contains state needed to calculate indent and keep track of exceptional indents.
* Tokens from [increasingTokens] are stored in stack [activeTokens]. When [WHITE_SPACE] with line break is encountered,
Expand All @@ -432,6 +456,10 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(

/**
* Checks whether indentation needs to be incremented and increments in this case.
*
* @see dec
* @see Int.indent
* @see Int.unindent
*/
fun maybeIncrement() {
if (activeTokens.isNotEmpty() && activeTokens.peek() != WHITE_SPACE) {
Expand All @@ -442,6 +470,10 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(

/**
* @param token a token that caused indentation decrement, for example a closing brace
*
* @see maybeIncrement
* @see Int.indent
* @see Int.unindent
*/
fun dec(token: IElementType) {
if (activeTokens.peek() == WHITE_SPACE) {
Expand Down Expand Up @@ -502,12 +534,6 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(

companion object {
private val log = LoggerFactory.getLogger(IndentationRule::class.java)

/**
* The default indent size (space characters), configurable via
* `indentationSize`.
*/
const val DEFAULT_INDENT_SIZE = 4
const val NAME_ID = "zct-indentation"
private val increasingTokens = listOf(LPAR, LBRACE, LBRACKET, LONG_TEMPLATE_ENTRY_START)
private val decreasingTokens = listOf(RPAR, RBRACE, RBRACKET, LONG_TEMPLATE_ENTRY_END)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.cqfn.diktat.ruleset.utils.indentation

import org.cqfn.diktat.common.config.rules.RuleConfiguration
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationRule

/**
* [RuleConfiguration] for indentation logic
Expand Down Expand Up @@ -37,5 +36,13 @@ internal class IndentationConfig(config: Map<String, String>) : RuleConfiguratio
/**
* The indentation size for each file
*/
val indentationSize = config["indentationSize"]?.toInt() ?: IndentationRule.DEFAULT_INDENT_SIZE
val indentationSize = config["indentationSize"]?.toInt() ?: DEFAULT_INDENT_SIZE

private companion object {
/**
* The default indent size (space characters), configurable via
* `indentationSize`.
*/
private const val DEFAULT_INDENT_SIZE = 4
}
}

0 comments on commit 9288199

Please sign in to comment.