aboutsummaryrefslogtreecommitdiff
path: root/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoMultipleSpacesRule.kt
diff options
context:
space:
mode:
Diffstat (limited to 'ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoMultipleSpacesRule.kt')
-rw-r--r--ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoMultipleSpacesRule.kt20
1 files changed, 9 insertions, 11 deletions
diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoMultipleSpacesRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoMultipleSpacesRule.kt
index 36cf26fe..15aa1a2a 100644
--- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoMultipleSpacesRule.kt
+++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoMultipleSpacesRule.kt
@@ -33,7 +33,8 @@ class NoMultipleSpacesRule : Rule("no-multi-spaces") {
val psi = node.psi
if (psi is PsiComment) { comments.add(psi) }
}
- return comments.foldIndexed(mutableMapOf<Offset, CommentRelativeLocation>()) { i, acc, comment ->
+ return comments.foldIndexed(mutableMapOf()) { i, acc, comment ->
+ // todo: get rid of DiagnosticUtils (IndexOutOfBoundsException)
val pos = DiagnosticUtils.getLineAndColumnInPsiFile(fileNode.psi as PsiFile,
TextRange(comment.startOffset, comment.startOffset))
acc.put(comment.startOffset, CommentRelativeLocation(
@@ -46,12 +47,14 @@ class NoMultipleSpacesRule : Rule("no-multi-spaces") {
}
}
- override fun visit(node: ASTNode, autoCorrect: Boolean,
- emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) {
+ override fun visit(
+ node: ASTNode,
+ autoCorrect: Boolean,
+ emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
+ ) {
if (node.elementType == KtStubElementTypes.FILE) {
fileNode = node
- } else
- if (node is PsiWhiteSpace && !node.textContains('\n') && node.getTextLength() > 1) {
+ } else if (node is PsiWhiteSpace && !node.textContains('\n') && node.getTextLength() > 1) {
val nextLeaf = PsiTreeUtil.nextLeaf(node, true)
if (nextLeaf is PsiComment) {
val positionMap = commentMap
@@ -71,13 +74,8 @@ class NoMultipleSpacesRule : Rule("no-multi-spaces") {
}
emit(node.startOffset + 1, "Unnecessary space(s)", true)
if (autoCorrect) {
- (node as LeafPsiElement).replaceWithText(" ")
+ (node as LeafPsiElement).rawReplaceWithText(" ")
}
}
}
-
- private fun ASTNode.visit(cb: (node: ASTNode) -> Unit) {
- cb(this)
- this.getChildren(null).forEach { it.visit(cb) }
- }
}