From 7d2451c627d66e95ff2f59555dcf6945a787f411 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Fri, 6 Nov 2020 20:56:44 +0300 Subject: [PATCH 1/7] NPE is ASTNode.squeezeSpaces method ### What's done: Fixed bug --- .../org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt | 2 +- .../resources/test/smoke/src/main/kotlin/Example3Expected.kt | 4 ++++ .../test/resources/test/smoke/src/main/kotlin/Example3Test.kt | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt index f007a28682..f595bcdc20 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt @@ -204,7 +204,7 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo } private fun checkSpaceBetweenPropertyAndComment(node: ASTNode, configuration: CommentsFormattingConfiguration) { - if (node.treeParent.firstChildNode != node) { + if (node.treeParent.firstChildNode != node && node.treeParent.elementType != FILE) { if (!node.treePrev.isWhiteSpace()) { // if comment is like this: val a = 5// Comment COMMENT_WHITE_SPACE.warnAndFix(configRules, emitWarn, isFixMode, diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt index 715b3dfc0d..a6896a1efe 100644 --- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt @@ -1,5 +1,9 @@ package org.cqfn.diktat +/* + * Copyright (c) Your Company Name Here. 2010-2020 + */ + /** * @property name */ diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt index 3b78e1a839..4ef8f35081 100644 --- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt @@ -1,4 +1,6 @@ -package test.smoke.src.main.kotlin +/* + * Copyright (c) Your Company Name Here. 2010-2020 + */ class HttpClient { var name: String From bbd93c2ecb746709510c0851f8865204467cdd73 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Sat, 7 Nov 2020 18:55:20 +0300 Subject: [PATCH 2/7] NPE is ASTNode.squeezeSpaces method ### What's done: Made a little prettier --- .../cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt index f595bcdc20..a32b672bc2 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt @@ -110,12 +110,12 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo } private fun handleEolAndBlockComments(node: ASTNode, configuration: CommentsFormattingConfiguration) { - basicCommentsChecks(node, configuration) + if (node.treeParent.elementType != FILE) basicCommentsChecks(node, configuration) checkWhiteSpaceBeforeComment(node, configuration) } private fun basicCommentsChecks(node: ASTNode, configuration: CommentsFormattingConfiguration) { - checkSpaceBetweenPropertyAndComment(node, configuration) + checkSpaceBeforeComment(node, configuration) if (node.treeParent.elementType == BLOCK && node.treeNext != null) { // Checking if comment is inside a code block like fun{} @@ -203,8 +203,8 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo } } - private fun checkSpaceBetweenPropertyAndComment(node: ASTNode, configuration: CommentsFormattingConfiguration) { - if (node.treeParent.firstChildNode != node && node.treeParent.elementType != FILE) { + private fun checkSpaceBeforeComment(node: ASTNode, configuration: CommentsFormattingConfiguration) { + if (node.treeParent.firstChildNode != node) { if (!node.treePrev.isWhiteSpace()) { // if comment is like this: val a = 5// Comment COMMENT_WHITE_SPACE.warnAndFix(configRules, emitWarn, isFixMode, From bf5c999d852221f683e26f9a2f27ffeb77994f05 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Sat, 7 Nov 2020 18:59:28 +0300 Subject: [PATCH 3/7] NPE is ASTNode.squeezeSpaces method ### What's done: Made a little prettier --- .../ruleset/rules/kdoc/CommentsFormatting.kt | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt index a32b672bc2..0fd1ef9bf8 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt @@ -43,7 +43,6 @@ import com.pinterest.ktlint.core.ast.prevSibling import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl -import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType /** * This class handles rule 2.6 @@ -58,6 +57,12 @@ import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType * * Comments in if else should be inside code blocks. Exception: General if comment */ class CommentsFormatting(private val configRules: List) : Rule("kdoc-comments-codeblocks-formatting") { + companion object { + private const val APPROPRIATE_COMMENT_SPACES = 1 + private const val MAX_SPACES = 1 + private val COMMENT_TYPE = listOf(EOL_COMMENT, KDOC, BLOCK_COMMENT) + } + private var isFixMode: Boolean = false private lateinit var emitWarn: EmitType @@ -76,11 +81,7 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo configRules.getRuleConfig(COMMENT_WHITE_SPACE)?.configuration ?: mapOf()) when (node.elementType) { - CLASS, FUN, PROPERTY -> { - checkBlankLineAfterKdoc(node, EOL_COMMENT) - checkBlankLineAfterKdoc(node, KDOC) - checkBlankLineAfterKdoc(node, BLOCK_COMMENT) - } + CLASS, FUN, PROPERTY -> checkBlankLineAfterKdoc(node) IF -> handleIfElse(node) EOL_COMMENT, BLOCK_COMMENT -> handleEolAndBlockComments(node, configuration) KDOC -> handleKdocComments(node, configuration) @@ -90,12 +91,14 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo } } - private fun checkBlankLineAfterKdoc(node: ASTNode, type: IElementType) { - val kdoc = node.getFirstChildWithType(type) - val nodeAfterKdoc = kdoc?.treeNext - if (nodeAfterKdoc?.elementType == WHITE_SPACE && nodeAfterKdoc.numNewLines() > 1) { - WRONG_NEWLINES_AROUND_KDOC.warnAndFix(configRules, emitWarn, isFixMode, "redundant blank line after ${kdoc.text}", nodeAfterKdoc.startOffset, nodeAfterKdoc) { - nodeAfterKdoc.leaveOnlyOneNewLine() + private fun checkBlankLineAfterKdoc(node: ASTNode) { + COMMENT_TYPE.forEach { + val kdoc = node.getFirstChildWithType(it) + val nodeAfterKdoc = kdoc?.treeNext + if (nodeAfterKdoc?.elementType == WHITE_SPACE && nodeAfterKdoc.numNewLines() > 1) { + WRONG_NEWLINES_AROUND_KDOC.warnAndFix(configRules, emitWarn, isFixMode, "redundant blank line after ${kdoc.text}", nodeAfterKdoc.startOffset, nodeAfterKdoc) { + nodeAfterKdoc.leaveOnlyOneNewLine() + } } } } @@ -106,12 +109,12 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo } else if (node.treeParent.elementType != IF) { checkClassComment(node) } - checkWhiteSpaceBeforeComment(node, configuration) + checkWhiteSpaceBegginInComment(node, configuration) } private fun handleEolAndBlockComments(node: ASTNode, configuration: CommentsFormattingConfiguration) { if (node.treeParent.elementType != FILE) basicCommentsChecks(node, configuration) - checkWhiteSpaceBeforeComment(node, configuration) + checkWhiteSpaceBegginInComment(node, configuration) } private fun basicCommentsChecks(node: ASTNode, configuration: CommentsFormattingConfiguration) { @@ -222,7 +225,7 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo } @Suppress("ComplexMethod", "TOO_LONG_FUNCTION") - private fun checkWhiteSpaceBeforeComment(node: ASTNode, configuration: CommentsFormattingConfiguration) { + private fun checkWhiteSpaceBegginInComment(node: ASTNode, configuration: CommentsFormattingConfiguration) { if (node.elementType == EOL_COMMENT && node .text @@ -356,8 +359,4 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo */ val maxSpacesInComment = config["maxSpacesInComment"]?.toIntOrNull() ?: APPROPRIATE_COMMENT_SPACES } - companion object { - private const val APPROPRIATE_COMMENT_SPACES = 1 - private const val MAX_SPACES = 1 - } } From ae8baa1d6c8bab2e70569a85ab3e07238eedb0a9 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 9 Nov 2020 16:40:28 +0300 Subject: [PATCH 4/7] NPE is ASTNode.squeezeSpaces method ### What's done: Renamed --- .../cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt index 0fd1ef9bf8..5fafa11e25 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt @@ -109,12 +109,12 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo } else if (node.treeParent.elementType != IF) { checkClassComment(node) } - checkWhiteSpaceBegginInComment(node, configuration) + checkWhiteSpaceBeginInComment(node, configuration) } private fun handleEolAndBlockComments(node: ASTNode, configuration: CommentsFormattingConfiguration) { if (node.treeParent.elementType != FILE) basicCommentsChecks(node, configuration) - checkWhiteSpaceBegginInComment(node, configuration) + checkWhiteSpaceBeginInComment(node, configuration) } private fun basicCommentsChecks(node: ASTNode, configuration: CommentsFormattingConfiguration) { @@ -225,7 +225,7 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo } @Suppress("ComplexMethod", "TOO_LONG_FUNCTION") - private fun checkWhiteSpaceBegginInComment(node: ASTNode, configuration: CommentsFormattingConfiguration) { + private fun checkWhiteSpaceBeginInComment(node: ASTNode, configuration: CommentsFormattingConfiguration) { if (node.elementType == EOL_COMMENT && node .text From 604908ac9c451b9a09bc6110256a65ed0a4af3ef Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 10 Nov 2020 16:48:18 +0300 Subject: [PATCH 5/7] NPE is ASTNode.squeezeSpaces method ### What's done: Fixed bug --- .../ruleset/rules/kdoc/CommentsFormatting.kt | 40 +++++++++---------- .../chapter2/CommentsFormattingTest.kt | 18 +++++++++ 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt index 5fafa11e25..74b57ee8fe 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt @@ -8,14 +8,6 @@ import org.cqfn.diktat.ruleset.constants.Warnings.COMMENT_WHITE_SPACE import org.cqfn.diktat.ruleset.constants.Warnings.FIRST_COMMENT_NO_SPACES import org.cqfn.diktat.ruleset.constants.Warnings.IF_ELSE_COMMENTS import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_NEWLINES_AROUND_KDOC -import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType -import org.cqfn.diktat.ruleset.utils.findChildrenMatching -import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType -import org.cqfn.diktat.ruleset.utils.getFirstChildWithType -import org.cqfn.diktat.ruleset.utils.hasChildOfType -import org.cqfn.diktat.ruleset.utils.leaveOnlyOneNewLine -import org.cqfn.diktat.ruleset.utils.numNewLines -import org.cqfn.diktat.ruleset.utils.prevNodeUntilNode import com.pinterest.ktlint.core.Rule import com.pinterest.ktlint.core.ast.ElementType.BLOCK @@ -40,6 +32,7 @@ import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.isWhiteSpace import com.pinterest.ktlint.core.ast.prevSibling +import org.cqfn.diktat.ruleset.utils.* import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl @@ -113,7 +106,7 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo } private fun handleEolAndBlockComments(node: ASTNode, configuration: CommentsFormattingConfiguration) { - if (node.treeParent.elementType != FILE) basicCommentsChecks(node, configuration) + basicCommentsChecks(node, configuration) checkWhiteSpaceBeginInComment(node, configuration) } @@ -207,20 +200,27 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo } private fun checkSpaceBeforeComment(node: ASTNode, configuration: CommentsFormattingConfiguration) { - if (node.treeParent.firstChildNode != node) { - if (!node.treePrev.isWhiteSpace()) { - // if comment is like this: val a = 5// Comment - COMMENT_WHITE_SPACE.warnAndFix(configRules, emitWarn, isFixMode, - "There should be ${configuration.maxSpacesBeforeComment} space(s) before comment text, but are none in ${node.text}", node.startOffset, node) { - node.treeParent.addChild(PsiWhiteSpaceImpl(" ".repeat(configuration.maxSpacesBeforeComment)), node) - } - } else if (!node.treePrev.textContains('\n') && node.treePrev.text.length != configuration.maxSpacesBeforeComment) { - // if there are too many spaces before comment + if (node.treeParent.firstChildNode == node) + return + if (node.treeParent.elementType == FILE) { + if (node.treePrev.isWhiteSpace()) { COMMENT_WHITE_SPACE.warnAndFix(configRules, emitWarn, isFixMode, - "There should be ${configuration.maxSpacesBeforeComment} space(s) before comment text, but there are too many in ${node.text}", node.startOffset, node) { - (node.treePrev as LeafPsiElement).replaceWithText(" ".repeat(configuration.maxSpacesBeforeComment)) + "There should be 0 space(s) before comment text, but are none in ${node.text}", node.startOffset, node) { + node.treeParent.removeChild(node.treePrev) } } + } else if (!node.treePrev.isWhiteSpace()) { + // if comment is like this: val a = 5// Comment + COMMENT_WHITE_SPACE.warnAndFix(configRules, emitWarn, isFixMode, + "There should be ${configuration.maxSpacesBeforeComment} space(s) before comment text, but are none in ${node.text}", node.startOffset, node) { + node.treeParent.addChild(PsiWhiteSpaceImpl(" ".repeat(configuration.maxSpacesBeforeComment)), node) + } + } else if (!node.treePrev.textContains('\n') && node.treePrev.text.length != configuration.maxSpacesBeforeComment) { + // if there are too many spaces before comment + COMMENT_WHITE_SPACE.warnAndFix(configRules, emitWarn, isFixMode, + "There should be ${configuration.maxSpacesBeforeComment} space(s) before comment text, but there are too many in ${node.text}", node.startOffset, node) { + (node.treePrev as LeafPsiElement).replaceWithText(" ".repeat(configuration.maxSpacesBeforeComment)) + } } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt index cb1566ee06..6a63f5b706 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt @@ -458,4 +458,22 @@ class CommentsFormattingTest : LintTestBase(::CommentsFormatting){ lintMethod(code) } + + @Test + @Tag(WarningNames.COMMENT_WHITE_SPACE) + fun `check comment on file level` () { + val code = + """ + | /* + | * heh + | */ + |package org.cqfn.diktat.ruleset.chapter3 + | + |class Example { + |} + """.trimMargin() + + lintMethod(code, + LintError(1,2,ruleId, "${Warnings.COMMENT_WHITE_SPACE.warnText()} There should be 0 space(s) before comment text, but are none in /*...", true)) + } } From 1b13ee790d2f3c4243c05234af952c9f5536ebb3 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Fri, 13 Nov 2020 15:47:07 +0300 Subject: [PATCH 6/7] NPE is ASTNode.squeezeSpaces method ### What's done: Fixed after review --- .../org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt | 4 +++- .../cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt index 74b57ee8fe..566cf25a90 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt @@ -203,9 +203,11 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo if (node.treeParent.firstChildNode == node) return if (node.treeParent.elementType == FILE) { + //This case is for top-level comments that are located in the beginning of the line and therefore don't need any spaces before. if (node.treePrev.isWhiteSpace()) { COMMENT_WHITE_SPACE.warnAndFix(configRules, emitWarn, isFixMode, - "There should be 0 space(s) before comment text, but are none in ${node.text}", node.startOffset, node) { + "There should be 0 space(s) before comment text, but are ${node.treePrev.text.count { it == ' ' }} in ${node.text}", + node.startOffset, node) { node.treeParent.removeChild(node.treePrev) } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt index 6a63f5b706..3894340b90 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt @@ -474,6 +474,6 @@ class CommentsFormattingTest : LintTestBase(::CommentsFormatting){ """.trimMargin() lintMethod(code, - LintError(1,2,ruleId, "${Warnings.COMMENT_WHITE_SPACE.warnText()} There should be 0 space(s) before comment text, but are none in /*...", true)) + LintError(1,2,ruleId, "${Warnings.COMMENT_WHITE_SPACE.warnText()} There should be 0 space(s) before comment text, but are 1 in /*...", true)) } } From 6796788b54ae22f8e785d05defb55c32fbc4d25b Mon Sep 17 00:00:00 2001 From: kentr0w Date: Fri, 13 Nov 2020 16:29:56 +0300 Subject: [PATCH 7/7] NPE is ASTNode.squeezeSpaces method ### What's done: Fixed bugs --- .../ruleset/rules/kdoc/CommentsFormatting.kt | 9 +++++++-- .../ruleset/chapter2/CommentsFormattingTest.kt | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt index 566cf25a90..d504b340d4 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/CommentsFormatting.kt @@ -31,6 +31,7 @@ import com.pinterest.ktlint.core.ast.ElementType.THEN import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.isWhiteSpace +import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline import com.pinterest.ktlint.core.ast.prevSibling import org.cqfn.diktat.ruleset.utils.* import org.jetbrains.kotlin.com.intellij.lang.ASTNode @@ -204,11 +205,15 @@ class CommentsFormatting(private val configRules: List) : Rule("kdo return if (node.treeParent.elementType == FILE) { //This case is for top-level comments that are located in the beginning of the line and therefore don't need any spaces before. - if (node.treePrev.isWhiteSpace()) { + if (!node.treePrev.isWhiteSpaceWithNewline() && node.treePrev.text.count { it == ' '} > 0) { COMMENT_WHITE_SPACE.warnAndFix(configRules, emitWarn, isFixMode, "There should be 0 space(s) before comment text, but are ${node.treePrev.text.count { it == ' ' }} in ${node.text}", node.startOffset, node) { - node.treeParent.removeChild(node.treePrev) + if (node.treePrev.elementType == WHITE_SPACE) + (node.treePrev as LeafPsiElement).replaceWithText("\n") + + else + node.treeParent.addChild(PsiWhiteSpaceImpl("\n"), node) } } } else if (!node.treePrev.isWhiteSpace()) { diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt index 3894340b90..696be59985 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/CommentsFormattingTest.kt @@ -476,4 +476,21 @@ class CommentsFormattingTest : LintTestBase(::CommentsFormatting){ lintMethod(code, LintError(1,2,ruleId, "${Warnings.COMMENT_WHITE_SPACE.warnText()} There should be 0 space(s) before comment text, but are 1 in /*...", true)) } + + @Test + @Tag(WarningNames.COMMENT_WHITE_SPACE) + fun `check comment on file level without space` () { + val code = + """ + |/* + | * heh + | */ + |package org.cqfn.diktat.ruleset.chapter3 + | + |class Example { + |} + """.trimMargin() + + lintMethod(code) + } }