TrailingCommaPostFormatProcessor: change recursive checker
#KT-34744
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
package org.jetbrains.kotlin.idea.formatter
|
package org.jetbrains.kotlin.idea.formatter
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger
|
import com.intellij.openapi.diagnostic.Logger
|
||||||
|
import com.intellij.openapi.util.Key
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiComment
|
import com.intellij.psi.PsiComment
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
@@ -13,7 +14,6 @@ import com.intellij.psi.PsiFile
|
|||||||
import com.intellij.psi.PsiWhiteSpace
|
import com.intellij.psi.PsiWhiteSpace
|
||||||
import com.intellij.psi.codeStyle.CodeStyleManager
|
import com.intellij.psi.codeStyle.CodeStyleManager
|
||||||
import com.intellij.psi.codeStyle.CodeStyleSettings
|
import com.intellij.psi.codeStyle.CodeStyleSettings
|
||||||
import com.intellij.psi.impl.source.PostprocessReformattingAspect
|
|
||||||
import com.intellij.psi.impl.source.codeStyle.PostFormatProcessor
|
import com.intellij.psi.impl.source.codeStyle.PostFormatProcessor
|
||||||
import com.intellij.psi.impl.source.codeStyle.PostFormatProcessorHelper
|
import com.intellij.psi.impl.source.codeStyle.PostFormatProcessorHelper
|
||||||
import com.intellij.psi.util.PsiUtil.getElementType
|
import com.intellij.psi.util.PsiUtil.getElementType
|
||||||
@@ -49,41 +49,41 @@ private class TrailingCommaPostFormatVisitor(val settings: CodeStyleSettings) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun processCommaOwner(parent: KtElement) {
|
private fun processCommaOwner(parent: KtElement) {
|
||||||
|
if (!postFormatIsEnable(parent)) return
|
||||||
|
parent.putUserData(IS_INSIDE, true)
|
||||||
|
|
||||||
val lastElement = trailingCommaOrLastElement(parent) ?: return
|
val lastElement = trailingCommaOrLastElement(parent) ?: return
|
||||||
val elementType = getElementType(lastElement)
|
val elementType = getElementType(lastElement)
|
||||||
when {
|
updatePsi(parent) {
|
||||||
needComma(parent, settings, false) -> {
|
when {
|
||||||
// add a missing comma
|
needComma(parent, settings, false) -> {
|
||||||
if (elementType != KtTokens.COMMA && trailingCommaAllowedInModule(parent)) {
|
// add a missing comma
|
||||||
lastElement.addCommaAfter(KtPsiFactory(parent))
|
if (elementType != KtTokens.COMMA && trailingCommaAllowedInModule(parent)) {
|
||||||
|
lastElement.addCommaAfter(KtPsiFactory(parent))
|
||||||
|
}
|
||||||
|
|
||||||
|
correctCommaPosition(parent)
|
||||||
|
}
|
||||||
|
needComma(parent, settings) -> {
|
||||||
|
correctCommaPosition(parent)
|
||||||
|
}
|
||||||
|
elementType == KtTokens.COMMA -> {
|
||||||
|
// remove redundant comma
|
||||||
|
lastElement.delete()
|
||||||
}
|
}
|
||||||
|
|
||||||
correctCommaPosition(parent)
|
|
||||||
}
|
}
|
||||||
needComma(parent, settings) -> {
|
|
||||||
correctCommaPosition(parent)
|
|
||||||
}
|
|
||||||
elementType == KtTokens.COMMA -> {
|
|
||||||
// remove redundant comma
|
|
||||||
lastElement.delete()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (postFormatIsEnable(parent)) {
|
|
||||||
updatePsi(parent)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updatePsi(element: KtElement) {
|
private fun updatePsi(element: KtElement, block: () -> Unit) {
|
||||||
val oldLength = element.textLength
|
val oldLength = element.parent.textLength
|
||||||
PostprocessReformattingAspect.getInstance(element.project).disablePostprocessFormattingInside {
|
block()
|
||||||
val result = CodeStyleManager.getInstance(element.project).reformat(element)
|
|
||||||
myPostProcessor.updateResultRange(oldLength, result.textLength)
|
val resultElement = CodeStyleManager.getInstance(element.project).reformat(element)
|
||||||
}
|
myPostProcessor.updateResultRange(oldLength, resultElement.parent.textLength)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun correctCommaPosition(parent: KtElement) {
|
private fun correctCommaPosition(parent: KtElement) {
|
||||||
if (!postFormatIsEnable(parent)) return
|
|
||||||
for (pointerToComma in findInvalidCommas(parent).map { it.createSmartPointer() }) {
|
for (pointerToComma in findInvalidCommas(parent).map { it.createSmartPointer() }) {
|
||||||
pointerToComma.element?.let {
|
pointerToComma.element?.let {
|
||||||
correctComma(it)
|
correctComma(it)
|
||||||
@@ -108,12 +108,11 @@ private class TrailingCommaPostFormatVisitor(val settings: CodeStyleSettings) :
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val LOG = Logger.getInstance(TrailingCommaVisitor::class.java)
|
private val LOG = Logger.getInstance(TrailingCommaVisitor::class.java)
|
||||||
|
private val IS_INSIDE = Key.create<Boolean>("TrailingCommaPostFormat")
|
||||||
|
private fun postFormatIsEnable(source: PsiElement): Boolean = source.getUserData(IS_INSIDE)?.not() ?: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun postFormatIsEnable(source: PsiElement): Boolean = !PostprocessReformattingAspect.getInstance(source.project).isDisabled
|
|
||||||
|
|
||||||
private fun PsiElement.addCommaAfter(factory: KtPsiFactory) {
|
private fun PsiElement.addCommaAfter(factory: KtPsiFactory) {
|
||||||
val comma = factory.createComma()
|
val comma = factory.createComma()
|
||||||
parent.addAfter(comma, this)
|
parent.addAfter(comma, this)
|
||||||
|
|||||||
Reference in New Issue
Block a user