Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/Corrected_readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrgentum authored Jun 21, 2022
2 parents f7d0584 + 9e12cdf commit b0ba3c6
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package org.cqfn.diktat.ruleset.rules.chapter3.files

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.ruleset.constants.Warnings.LONG_LINE
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_WHITESPACE
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.rules.chapter3.LineLength
import org.cqfn.diktat.ruleset.rules.chapter6.classes.CompactInitialization
import org.cqfn.diktat.ruleset.utils.appendNewlineMergingWhiteSpace
import org.cqfn.diktat.ruleset.utils.calculateLineColByOffset
import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType
import org.cqfn.diktat.ruleset.utils.hasChildOfType

import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY
Expand All @@ -23,8 +30,10 @@ import com.pinterest.ktlint.core.ast.ElementType.CONSTRUCTOR_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.DOT
import com.pinterest.ktlint.core.ast.ElementType.DO_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.ELSE_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.ELVIS
import com.pinterest.ktlint.core.ast.ElementType.EQ
import com.pinterest.ktlint.core.ast.ElementType.EXCLEXCL
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.FINALLY_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.FOR_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.FUN
Expand Down Expand Up @@ -96,8 +105,14 @@ import org.slf4j.LoggerFactory
class WhiteSpaceRule(configRules: List<RulesConfig>) : DiktatRule(
NAME_ID,
configRules,
listOf(WRONG_WHITESPACE)
listOf(WRONG_WHITESPACE, LONG_LINE, WRONG_INDENTATION)
) {
private val configuration by lazy {
LineLength.LineLengthConfiguration(
configRules.getRuleConfig(LONG_LINE)?.configuration ?: emptyMap()
)
}
private lateinit var positionByOffset: (Int) -> Pair<Int, Int>
@Suppress("ComplexMethod")
override fun logic(node: ASTNode) {
when (node.elementType) {
Expand Down Expand Up @@ -274,7 +289,6 @@ class WhiteSpaceRule(configRules: List<RulesConfig>) : DiktatRule(
if (node.elementType == OPERATION_REFERENCE && node.treeParent.elementType.let { it == BINARY_EXPRESSION || it == POSTFIX_EXPRESSION || it == PROPERTY } ||
node.elementType != OPERATION_REFERENCE) {
val requiredNumSpaces = if (operatorNode.elementType in operatorsWithNoWhitespace) 0 else 1

handleToken(node, requiredNumSpaces, requiredNumSpaces)
}
}
Expand Down Expand Up @@ -366,15 +380,28 @@ class WhiteSpaceRule(configRules: List<RulesConfig>) : DiktatRule(
}
}

@Suppress("UnsafeCallOnNullableType")
private fun ASTNode.isNeedNewLineInOperatorReferences(): Boolean {
positionByOffset = this.findParentNodeWithSpecificType(FILE)!!.calculateLineColByOffset()
val offset = positionByOffset(this.startOffset).second
return offset + this.text.length >= configuration.lineLength
}

@Suppress("UnsafeCallOnNullableType")
private fun ASTNode.fixSpaceAround(requiredSpacesBefore: Int?, requiredSpacesAfter: Int?) {
if (requiredSpacesBefore == 1) {
selfOrParentsTreePrev()?.let { if (it.elementType == WHITE_SPACE) it.treePrev else it }?.leaveSingleWhiteSpace()
if (this.isNeedNewLineInOperatorReferences() && this.firstChildNode.elementType == ELVIS) {
this.treePrev.let { this.treeParent.appendNewlineMergingWhiteSpace(it, it) }
}
} else if (requiredSpacesBefore == 0) {
selfOrParentsTreePrev()?.removeIfWhiteSpace()
}

if (requiredSpacesAfter == 1) {
leaveSingleWhiteSpace()
if (this.isNeedNewLineInOperatorReferences() && this.firstChildNode.elementType != ELVIS) {
this.treeNext.let { this.treeParent.appendNewlineMergingWhiteSpace(it, it) }
}
} else if (requiredSpacesAfter == 0) {
// for `!!` and possibly other postfix expressions treeNext can be null
(treeNext ?: treeParent.treeNext).removeIfWhiteSpace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
}
}

@Test
@Tag("DiktatRuleSetProvider")
fun `fix can cause long line`() {
fixAndCompareSmokeTest("ManyLineTransformInLongLineExpected.kt", "ManyLineTransformInLongLineTest.kt")
}

companion object {
private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml"
private val unfixedLintErrors: MutableList<LintError> = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.cqfn.diktat

fun foo() {
(1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 or 17 or 18 or 19 or 20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 or 29 or 30 or 31
?: 32 or 33 or 34 or 35 or 36 or 37 or 38 or 39 ?: 40 or 41 or 42 or 43 or 44 or 45 or 46 or 47 or 48 or 49 or 50 or 51 or 52 or 53 + 54 or 55 or 56 or 57 or 58 or 59
?: 60 or 61 or 62 or 63 or 64 or 65 or 66 - 67 or 68 or 69 or 70 or 1 + 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 or 17 or 18 + 19 -
20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 or 29 or 30 or 31 or 32 or 33 or 34 or 35 or 36 or 37 or 38 or 39 or 40 or 41 || 42 or 43 or 44 or 45 or 46 or 47 && 48 or 49 ||
50 or 51 or 52 or 53 or 54 or 55 or 56 or 57 or 58 or 59 or 60 or 61 or 62 or 63 or 64 or 65 or 66 or 67 or 68 or 69 or 70 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or
12 or 13 or 14 or 15 or 16 or 17 or 18 or 19 or 20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 or 29 or 30 or 31 or 32 or 33 or 34 or 35 or 36 or 37 or 38 or 39 or 40 or 41 or
42 or 43 or 44 or 45 or 46 or 47 or 48 or 49 or 50 or 51 or 52 or 53 or 54 or 55 or 56 or 57 or 58 or 59 or 60 or 61 or 62 or 63 or 64 or 65 or 66 or 67 or 68 or 69 or 70)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package org.cqfn.diktat

fun foo(){
(1
or 2
or 3
or 4
or 5
or 6
or 7
or 8
or 9
or 10
or 11
or 12
or 13
or 14
or 15
or 16
or 17
or 18
or 19
or 20
or 21
or 22
or 23
or 24
or 25
or 26
or 27
or 28
or 29
or 30
or 31
?: 32
or 33
or 34
or 35
or 36
or 37
or 38
or 39 ?: 40
or 41
or 42
or 43
or 44
or 45
or 46
or 47
or 48
or 49
or 50
or 51
or 52
or 53
+ 54
or 55
or 56
or 57
or 58
or 59
?: 60
or 61
or 62
or 63
or 64
or 65
or 66
- 67
or 68
or 69
or 70
or 1
+ 2
or 3
or 4
or 5
or 6
or 7
or 8
or 9
or 10
or 11
or 12
or 13
or 14
or 15
or 16
or 17
or 18
+ 19
- 20
or 21
or 22
or 23
or 24
or 25
or 26
or 27
or 28
or 29
or 30
or 31
or 32
or 33
or 34
or 35
or 36
or 37
or 38
or 39
or 40
or 41
|| 42
or 43
or 44
or 45
or 46
or 47
&& 48
or 49
|| 50
or 51
or 52
or 53
or 54
or 55
or 56
or 57
or 58
or 59
or 60
or 61
or 62
or 63
or 64
or 65
or 66
or 67
or 68
or 69
or 70
or 1
or 2
or 3
or 4
or 5
or 6
or 7
or 8
or 9
or 10
or 11
or 12
or 13
or 14
or 15
or 16
or 17
or 18
or 19
or 20
or 21
or 22
or 23
or 24
or 25
or 26
or 27
or 28
or 29
or 30
or 31
or 32
or 33
or 34
or 35
or 36
or 37
or 38
or 39
or 40
or 41
or 42
or 43
or 44
or 45
or 46
or 47
or 48
or 49
or 50
or 51
or 52
or 53
or 54
or 55
or 56
or 57
or 58
or 59
or 60
or 61
or 62
or 63
or 64
or 65
or 66
or 67
or 68
or 69
or 70)
}

0 comments on commit b0ba3c6

Please sign in to comment.