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 78b76a4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -43,6 +44,7 @@ class DiktatRuleSetProviderTest {
.map { it.nameWithoutExtension }
.filterNot { it in ignoreFile }
val rulesName = DiktatRuleSetProvider().get()
.asSequence()
.onEachIndexed { index, rule ->
if (index != 0) {
Assertions.assertTrue(
Expand All @@ -53,8 +55,9 @@ class DiktatRuleSetProviderTest {
}
.map { (it as? DiktatRuleSetProvider.OrderedRule)?.rule ?: it }
.map { it::class.simpleName!! }
.filter { it != "DummyWarning" }
Assertions.assertEquals(filesName.sorted().toList(), rulesName.sorted())
.filterNot { it == "DummyWarning" }
.toList()
assertThat(rulesName.sorted()).containsExactlyElementsOf(filesName.sorted().toList())
}

@Test
Expand Down Expand Up @@ -122,6 +125,9 @@ class DiktatRuleSetProviderTest {
}

companion object {
private val ignoreFile = listOf("DiktatRuleSetProvider", "DiktatRule")
private val ignoreFile = listOf(
"DiktatRuleSetProvider",
"DiktatRule",
"IndentationError")
}
}

0 comments on commit 78b76a4

Please sign in to comment.