Skip to content

Commit

Permalink
Set extendedIndentAfterOperators to true by default (#1447)
Browse files Browse the repository at this point in the history
### What's done:

 * Once we now have a separate flag for assignment
   (`extendedIndentForExpressionBodies`, `false` by default, see #1443), let's set
   `extendedIndentAfterOperators` back to `true` to be consistent with how IDEA
   formats Kotlin source files.
  • Loading branch information
0x6675636b796f75676974687562 committed Jul 13, 2022
1 parent 0890bd6 commit a17825a
Show file tree
Hide file tree
Showing 73 changed files with 382 additions and 390 deletions.
2 changes: 1 addition & 1 deletion diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
# ij_kotlin_continuation_indent_for_expression_bodies in .editorconfig.
extendedIndentForExpressionBodies: false
# If true: if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one
extendedIndentAfterOperators: false
extendedIndentAfterOperators: true
# If true: when dot qualified expression starts on a new line, this line will be indented with two indentations instead of one
extendedIndentBeforeDot: false
# The indentation size for each file
Expand Down
2 changes: 1 addition & 1 deletion diktat-common/src/test/resources/test-rules-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
extendedIndentOfParameters: false
alignedParameters: true
extendedIndentForExpressionBodies: false
extendedIndentAfterOperators: false
extendedIndentAfterOperators: true
indentationSize: 4
- name: EMPTY_BLOCK_STRUCTURE_ERROR
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ open class DiktatJavaExecTaskBase @Inject constructor(
}

private fun getJavaExecJvmVersion(): JavaVersion = if (GradleVersion.version(gradleVersionString) >= GradleVersion.version("6.7") &&
javaLauncher.isPresent
javaLauncher.isPresent
) {
// Java Launchers are available since 6.7, but may not always be configured
javaLauncher.map { it.metadata.jvmVersion }.map(JavaVersion::toVersion).get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ abstract class DiktatBaseMojo : AbstractMojo() {
throw MojoExecutionException("Configuration file $diktatConfigFile doesn't exist")
}
log.info("Running diKTat plugin with configuration file $configFile and inputs $inputs" +
if (excludes.isNotEmpty()) " and excluding $excludes" else ""
if (excludes.isNotEmpty()) " and excluding $excludes" else ""
)

val ruleSets by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ enum class Warnings(
NO_BRACES_IN_CONDITIONALS_AND_LOOPS(true, "3.2.1", "in if, else, when, for, do, and while statements braces should be used. Exception: single line ternary operator statement"),
WRONG_ORDER_IN_CLASS_LIKE_STRUCTURES(true, "3.1.4", "the declaration part of a class-like code structures (class/interface/etc.) should be in the proper order"),
BLANK_LINE_BETWEEN_PROPERTIES(true, "3.1.4", "there should be no blank lines between properties without comments; comment, KDoc or annotation on property should have blank" +
" line before"),
" line before"),
TOP_LEVEL_ORDER(true, "3.1.5", "the declaration part of a top level elements should be in the proper order"),
BRACES_BLOCK_STRUCTURE_ERROR(true, "3.2.2", "braces should follow 1TBS style"),
WRONG_INDENTATION(true, "3.3.1", "only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
)
override fun get(): RuleSet {
log.debug("Will run $DIKTAT_RULE_SET_ID with $diktatConfigFile" +
" (it can be placed to the run directory or the default file from resources will be used)")
" (it can be placed to the run directory or the default file from resources will be used)")
val configPath = possibleConfigs
.firstOrNull { it != null && File(it).exists() }
diktatConfigFile = configPath
?: run {
val possibleConfigsList = possibleConfigs.toList()
log.warn(
"Configuration file not found in directory where diktat is run (${possibleConfigsList[0]}) " +
"or in the directory where diktat.jar is stored (${possibleConfigsList[1]}) " +
"or in system property <diktat.config.path> (${possibleConfigsList[2]}), " +
"the default file included in jar will be used. " +
"Some configuration options will be disabled or substituted with defaults. " +
"Custom configuration file should be placed in diktat working directory if run from CLI " +
"or provided as configuration options in plugins."
"or in the directory where diktat.jar is stored (${possibleConfigsList[1]}) " +
"or in system property <diktat.config.path> (${possibleConfigsList[2]}), " +
"the default file included in jar will be used. " +
"Some configuration options will be disabled or substituted with defaults. " +
"Custom configuration file should be placed in diktat working directory if run from CLI " +
"or provided as configuration options in plugins."
)
diktatConfigFile
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
}
// check if identifier of a property has a confusing name
if (confusingIdentifierNames.contains(variableName.text) && !isValidCatchIdentifier(variableName) &&
node.elementType == ElementType.PROPERTY
node.elementType == ElementType.PROPERTY
) {
warnConfusingName(variableName)
}
Expand All @@ -193,7 +193,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
?.forEach { (it.node.firstChildNode as LeafPsiElement).rawReplaceWithText(correctVariableName) }
if (variableName.treeParent.psi.run {
(this is KtProperty && isMember) ||
(this is KtParameter && getParentOfType<KtPrimaryConstructor>(true)?.valueParameters?.contains(this) == true)
(this is KtParameter && getParentOfType<KtPrimaryConstructor>(true)?.valueParameters?.contains(this) == true)
}) {
// For class members also check `@property` KDoc tag.
// If we are here, then `variableName` is definitely a node from a class or an object.
Expand Down Expand Up @@ -257,7 +257,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
destructingDeclaration.getAllChildrenWithType(DESTRUCTURING_DECLARATION_ENTRY)
.map { it.getIdentifierName()!! }
} else if (node.parents().count() > 1 && node.treeParent.elementType == VALUE_PARAMETER_LIST &&
node.treeParent.treeParent.elementType == FUNCTION_TYPE
node.treeParent.treeParent.elementType == FUNCTION_TYPE
) {
listOfNotNull(node.getIdentifierName())
} else {
Expand Down Expand Up @@ -440,7 +440,7 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
nodes.forEach {
val isValidOneCharVariable = oneCharIdentifiers.contains(it.text) && isVariable
if (it.text != "_" && !it.isTextLengthInRange(MIN_IDENTIFIER_LENGTH..MAX_IDENTIFIER_LENGTH) &&
!isValidOneCharVariable && !isValidCatchIdentifier(it)
!isValidOneCharVariable && !isValidCatchIdentifier(it)
) {
IDENTIFIER_LENGTH.warn(configRules, emitWarn, isFixMode, it.text, it.startOffset, it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
}
} ?: if (visitorCounter.incrementAndGet() == 1) {
log.error("Not able to find an external configuration for domain" +
" name in the common configuration (is it missing in yml config?)")
" name in the common configuration (is it missing in yml config?)")
} else {
@Suppress("RedundantUnitExpression")
Unit
Expand Down Expand Up @@ -127,7 +127,7 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(

return if (!filePathParts.contains(PACKAGE_PATH_ANCHOR)) {
log.error("Not able to determine a path to a scanned file or \"$PACKAGE_PATH_ANCHOR\" directory cannot be found in it's path." +
" Will not be able to determine correct package name. It can happen due to missing <$PACKAGE_PATH_ANCHOR> directory in the path")
" Will not be able to determine correct package name. It can happen due to missing <$PACKAGE_PATH_ANCHOR> directory in the path")
emptyList()
} else {
// creating a real package name:
Expand Down Expand Up @@ -202,8 +202,8 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
val wordFromPackage = word.replace("_", "")

return wordFromPackage[0].isDigit() ||
wordFromPackage.isKotlinKeyWord() ||
wordFromPackage.isJavaKeyWord()
wordFromPackage.isKotlinKeyWord() ||
wordFromPackage.isJavaKeyWord()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ class CommentsRule(configRules: List<RulesConfig>) : DiktatRule(
.forEach { (offset, parsedNode) ->
val invalidNode = errorNodesWithText.find {
it.second.trim().contains(parsedNode.text, false) ||
parsedNode.text.contains(it.second.trim(), false)
parsedNode.text.contains(it.second.trim(), false)
}?.first
if (invalidNode == null) {
logger.warn("Text [${parsedNode.text}] is a piece of code, created from comment; " +
"but no matching text in comments has been found in the file ${node.getFilePath()}")
"but no matching text in comments has been found in the file ${node.getFilePath()}")
} else {
COMMENTED_OUT_CODE.warn(
configRules,
Expand Down Expand Up @@ -168,9 +168,9 @@ class CommentsRule(configRules: List<RulesConfig>) : DiktatRule(
* are considered for this case.
*/
private fun String.isPossibleImport(): Boolean = trimStart().startsWith(importKeywordWithSpace) &&
substringAfter(importKeywordWithSpace, "").run {
startsWith('`') && endsWith('`') || !contains(' ')
}
substringAfter(importKeywordWithSpace, "").run {
startsWith('`') && endsWith('`') || !contains(' ')
}

@Suppress("MaxLineLength")
companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
private fun checkHeaderKdoc(node: ASTNode) {
node.findChildBefore(PACKAGE_DIRECTIVE, KDOC)?.let { headerKdoc ->
if (headerKdoc.treeNext != null && headerKdoc.treeNext.elementType == WHITE_SPACE &&
headerKdoc.treeNext.text.count { it == '\n' } != 2) {
headerKdoc.treeNext.text.count { it == '\n' } != 2) {
HEADER_WRONG_FORMAT.warnAndFix(configRules, emitWarn, isFixMode,
"header KDoc should have a new line after", headerKdoc.startOffset, headerKdoc) {
node.replaceChild(headerKdoc.treeNext, PsiWhiteSpaceImpl("\n\n"))
Expand All @@ -70,7 +70,7 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
}
?: run {
val numDeclaredClassesAndObjects = node.getAllChildrenWithType(ElementType.CLASS).size +
node.getAllChildrenWithType(ElementType.OBJECT_DECLARATION).size
node.getAllChildrenWithType(ElementType.OBJECT_DECLARATION).size
if (numDeclaredClassesAndObjects != 1) {
HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE.warn(configRules, emitWarn, isFixMode,
"there are $numDeclaredClassesAndObjects declared classes and/or objects", node.startOffset, node)
Expand Down Expand Up @@ -148,12 +148,12 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
val headerComment = node.findChildBefore(PACKAGE_DIRECTIVE, BLOCK_COMMENT)
// Depends only on content and doesn't consider years
val isCopyrightMatchesPatternExceptFirstYear = isCopyRightTextMatchesPattern(headerComment, copyrightText) ||
isCopyRightTextMatchesPattern(headerComment, copyrightWithCorrectYear)
isCopyRightTextMatchesPattern(headerComment, copyrightWithCorrectYear)

val isWrongCopyright = headerComment != null &&
!isCopyrightMatchesPatternExceptFirstYear &&
!isHeaderCommentContainText(headerComment, copyrightText) &&
!isHeaderCommentContainText(headerComment, copyrightWithCorrectYear)
!isCopyrightMatchesPatternExceptFirstYear &&
!isHeaderCommentContainText(headerComment, copyrightText) &&
!isHeaderCommentContainText(headerComment, copyrightWithCorrectYear)

val isMissingCopyright = headerComment == null && configuration.isCopyrightMandatory()
val isCopyrightInsideKdoc = (node.getAllChildrenWithType(KDOC) + node.getAllChildrenWithType(ElementType.EOL_COMMENT))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
return
}
} else if (node.treeParent.lastChildNode != node && node.treeParent.elementType != IF &&
node.treeParent.firstChildNode == node && node.treeParent.elementType != VALUE_ARGUMENT_LIST) {
node.treeParent.firstChildNode == node && node.treeParent.elementType != VALUE_ARGUMENT_LIST) {
// Else it's a class comment
checkClassComment(node)
}
Expand Down Expand Up @@ -224,41 +224,41 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
@Suppress("ComplexMethod", "TOO_LONG_FUNCTION")
private fun checkWhiteSpaceBeginInComment(node: ASTNode, configuration: CommentsFormattingConfiguration) {
if (node.elementType == EOL_COMMENT &&
node
.text
.trimStart('/')
.takeWhile { it == ' ' }
.length == configuration.maxSpacesInComment) {
node
.text
.trimStart('/')
.takeWhile { it == ' ' }
.length == configuration.maxSpacesInComment) {
return
}

if (node.elementType == BLOCK_COMMENT &&
(node.isIndentStyleComment() ||
node
.text
.trim('/', '*')
.takeWhile { it == ' ' }
.length == configuration.maxSpacesInComment ||
node
.text
.trim('/', '*')
.takeWhile { it == '\n' }
.isNotEmpty())) {
(node.isIndentStyleComment() ||
node
.text
.trim('/', '*')
.takeWhile { it == ' ' }
.length == configuration.maxSpacesInComment ||
node
.text
.trim('/', '*')
.takeWhile { it == '\n' }
.isNotEmpty())) {
return
}

if (node.elementType == KDOC) {
val section = node.getFirstChildWithType(KDOC_SECTION)
if (section != null &&
section.findChildrenMatching(KDOC_TEXT) { (it.treePrev != null && it.treePrev.elementType == KDOC_LEADING_ASTERISK) || it.treePrev == null }
.all { it.text.startsWith(" ".repeat(configuration.maxSpacesInComment)) }) {
section.findChildrenMatching(KDOC_TEXT) { (it.treePrev != null && it.treePrev.elementType == KDOC_LEADING_ASTERISK) || it.treePrev == null }
.all { it.text.startsWith(" ".repeat(configuration.maxSpacesInComment)) }) {
// it.treePrev == null if there is no \n at the beginning of KDoc
return
}

if (section != null &&
section.getAllChildrenWithType(KDOC_CODE_BLOCK_TEXT).isNotEmpty() &&
section.getAllChildrenWithType(KDOC_CODE_BLOCK_TEXT).all { it.text.startsWith(" ".repeat(configuration.maxSpacesInComment)) }) {
section.getAllChildrenWithType(KDOC_CODE_BLOCK_TEXT).isNotEmpty() &&
section.getAllChildrenWithType(KDOC_CODE_BLOCK_TEXT).all { it.text.startsWith(" ".repeat(configuration.maxSpacesInComment)) }) {
return
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
node.treeParent.treeParent.addChild(PsiWhiteSpaceImpl("\n"), node.treeParent)
}
} else if (node.treeParent.elementType != FILE &&
(node.treeParent.treePrev.numNewLines() == 1 || node.treeParent.treePrev.numNewLines() > 2)) {
(node.treeParent.treePrev.numNewLines() == 1 || node.treeParent.treePrev.numNewLines() > 2)) {
WRONG_NEWLINES_AROUND_KDOC.warnAndFix(configRules, emitWarn, isFixMode, node.text, node.startOffset, node) {
(node.treeParent.treePrev as LeafPsiElement).rawReplaceWithText("\n\n")
}
Expand All @@ -313,7 +313,7 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(

private fun checkFirstCommentSpaces(node: ASTNode) {
if (node.treePrev.isWhiteSpace() &&
(node.treePrev.numNewLines() > 1 || node.treePrev.numNewLines() == 0)) {
(node.treePrev.numNewLines() > 1 || node.treePrev.numNewLines() == 0)) {
FIRST_COMMENT_NO_BLANK_LINE.warnAndFix(configRules, emitWarn, isFixMode, node.text, node.startOffset, node) {
(node.treePrev as LeafPsiElement).rawReplaceWithText("\n")
}
Expand All @@ -332,7 +332,7 @@ class CommentsFormatting(configRules: List<RulesConfig>) : DiktatRule(
// `treeParent` is the first not-empty node in a file
true
} else if (node.treeParent.elementType != FILE && node.treeParent.treePrev != null &&
node.treeParent.treePrev.treePrev != null) {
node.treeParent.treePrev.treePrev != null) {
// When comment inside of a PROPERTY
node.treeParent
.treePrev
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class KdocComments(configRules: List<RulesConfig>) : DiktatRule(
@Suppress("UnsafeCallOnNullableType", "ComplexMethod")
private fun checkValueParameter(valueParameterNode: ASTNode) {
if (valueParameterNode.parents().none { it.elementType == PRIMARY_CONSTRUCTOR } ||
!valueParameterNode.hasChildOfType(VAL_KEYWORD) &&
!valueParameterNode.hasChildOfType(VAR_KEYWORD)
!valueParameterNode.hasChildOfType(VAL_KEYWORD) &&
!valueParameterNode.hasChildOfType(VAR_KEYWORD)
) {
return
}
Expand Down
Loading

0 comments on commit a17825a

Please sign in to comment.