Skip to content

Commit

Permalink
Fixed ClassCastException (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed May 3, 2018
1 parent 000c0bd commit 326cdc5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.lang.FileASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement
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.util.PsiTreeUtil
import org.jetbrains.kotlin.lexer.KtTokens
Expand Down Expand Up @@ -51,8 +53,8 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {
nextChild@ for (child in node.children()) {
when (child.elementType) {
KtTokens.LPAR -> {
val prevLeaf = child.psi.prevLeaf()!!
if (prevLeaf.elementType == KtTokens.WHITE_SPACE && prevLeaf.textContains('\n')) {
val prevLeaf = child.psi.prevLeaf()
if (prevLeaf is PsiWhiteSpace && prevLeaf.textContains('\n')) {
emit(child.startOffset, errorMessage(child), true)
if (autoCorrect) {
prevLeaf.delete()
Expand All @@ -62,10 +64,10 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {
KtStubElementTypes.VALUE_PARAMETER,
KtTokens.RPAR -> {
var paramInnerIndentAdjustment = 0
val prevLeaf = child.psi.prevLeaf()!!
val prevLeaf = child.psi.prevLeaf()
val intendedIndent = if (child.elementType == KtStubElementTypes.VALUE_PARAMETER)
paramIndent else indent
if (prevLeaf.node.elementType == KtTokens.WHITE_SPACE) {
if (prevLeaf is PsiWhiteSpace) {
val spacing = prevLeaf.text
val cut = spacing.lastIndexOf("\n")
if (cut > -1) {
Expand All @@ -81,7 +83,7 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {
if (autoCorrect) {
val adjustedIndent = (if (cut > -1) spacing.substring(0, cut) else "") + intendedIndent
paramInnerIndentAdjustment = adjustedIndent.length - prevLeaf.textLength
prevLeaf.rawReplaceWithText(adjustedIndent)
(prevLeaf as LeafPsiElement).rawReplaceWithText(adjustedIndent)
}
} else {
emit(child.startOffset, errorMessage(child), true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package com.github.shyiko.ktlint.ruleset.standard

import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.KtStringTemplateEntry
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import kotlin.reflect.KClass

internal fun PsiElement.isPartOf(clazz: KClass<out PsiElement>) = getNonStrictParentOfType(clazz.java) != null
internal fun PsiElement.isPartOfString() = isPartOf(KtStringTemplateEntry::class)
internal fun PsiElement.prevLeaf(): LeafPsiElement? = PsiTreeUtil.prevLeaf(this) as LeafPsiElement?
internal fun PsiElement.nextLeaf(): LeafPsiElement? = PsiTreeUtil.nextLeaf(this) as LeafPsiElement?
internal fun PsiElement.prevLeaf(): PsiElement? = PsiTreeUtil.prevLeaf(this)
internal fun PsiElement.nextLeaf(): PsiElement? = PsiTreeUtil.nextLeaf(this)
internal fun ASTNode.visit(cb: (node: ASTNode) -> Unit) {
cb(this)
this.getChildren(null).forEach { it.visit(cb) }
Expand Down

0 comments on commit 326cdc5

Please # to comment.