Skip to content

Commit

Permalink
[#1347] Clean up the code
Browse files Browse the repository at this point in the history
### What's done:

 * `IndentationError` moved to a separate source file.
 * Top-level extensions moved from `IndentationRule.kt` to `StringUtils.kt`.
  • Loading branch information
0x6675636b796f75676974687562 committed Jun 20, 2022
1 parent e92bc9b commit d2e9455
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.cqfn.diktat.ruleset.rules.chapter3.files

/**
* @property expected expected indentation as a number of spaces
* @property actual actual indentation as a number of spaces
*/
internal data class IndentationError(val expected: Int, val actual: Int)
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationRule.Companion.NEWLINE
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationRule.Companion.SPACE
import org.cqfn.diktat.ruleset.utils.NEWLINE
import org.cqfn.diktat.ruleset.utils.SPACE
import org.cqfn.diktat.ruleset.utils.TAB
import org.cqfn.diktat.ruleset.utils.calculateLineColByOffset
import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType
import org.cqfn.diktat.ruleset.utils.getAllLeafsWithSpecificType
Expand All @@ -27,6 +28,8 @@ import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig
import org.cqfn.diktat.ruleset.utils.indentation.KdocIndentationChecker
import org.cqfn.diktat.ruleset.utils.indentation.SuperTypeListChecker
import org.cqfn.diktat.ruleset.utils.indentation.ValueParameterListChecker
import org.cqfn.diktat.ruleset.utils.isSpaceCharacter
import org.cqfn.diktat.ruleset.utils.lastIndent
import org.cqfn.diktat.ruleset.utils.leaveOnlyOneNewLine

import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
Expand Down Expand Up @@ -506,9 +509,6 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
*/
const val DEFAULT_INDENT_SIZE = 4
const val NAME_ID = "zct-indentation"
internal const val NEWLINE = '\n'
internal const val SPACE = ' '
internal const val TAB = '\t'
private val increasingTokens = listOf(LPAR, LBRACE, LBRACKET, LONG_TEMPLATE_ENTRY_START)
private val decreasingTokens = listOf(RPAR, RBRACE, RBRACKET, LONG_TEMPLATE_ENTRY_END)
private val matchingTokens = increasingTokens.zip(decreasingTokens)
Expand Down Expand Up @@ -558,17 +558,3 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
}
}
}

/**
* @property expected expected indentation as a number of spaces
* @property actual actual indentation as a number of spaces
*/
internal data class IndentationError(val expected: Int, val actual: Int)

/**
* @return indentation of the last line of this string
*/
internal fun String.lastIndent() = substringAfterLast(NEWLINE).count(::isSpaceCharacter)

private fun isSpaceCharacter(ch: Char): Boolean =
ch == SPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ package org.cqfn.diktat.ruleset.utils

import org.jetbrains.kotlin.lexer.KtTokens

internal const val NEWLINE = '\n'

internal const val SPACE = ' '

internal const val TAB = '\t'

@Suppress("VARIABLE_NAME_INCORRECT_FORMAT")
val JAVA = arrayOf("abstract", "assert", "boolean",
"break", "byte", "case", "catch", "char", "class", "const",
Expand Down Expand Up @@ -97,3 +103,15 @@ fun String.removePrefix(): String {
}
return this
}

/**
* @return the indentation of the last line of this string.
*/
internal fun String.lastIndent() = substringAfterLast(NEWLINE).count(::isSpaceCharacter)

/**
* @param ch the character to examine.
* @return `true` if [ch] is a [SPACE], `false` otherwise.
*/
internal fun isSpaceCharacter(ch: Char): Boolean =
ch == SPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package org.cqfn.diktat.ruleset.utils.indentation

import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationError
import org.cqfn.diktat.ruleset.rules.chapter3.files.lastIndent
import org.cqfn.diktat.ruleset.utils.hasParent
import org.cqfn.diktat.ruleset.utils.lastIndent

import com.pinterest.ktlint.core.ast.ElementType.ARROW
import com.pinterest.ktlint.core.ast.ElementType.AS_KEYWORD
Expand Down

0 comments on commit d2e9455

Please sign in to comment.