Join lines should remove useless trailing commas
#KT-39065 Fixed
This commit is contained in:
@@ -13,8 +13,7 @@ import com.intellij.psi.codeStyle.CodeStyleManager
|
|||||||
import org.jetbrains.kotlin.idea.core.util.containsLineBreakInRange
|
import org.jetbrains.kotlin.idea.core.util.containsLineBreakInRange
|
||||||
import org.jetbrains.kotlin.idea.formatter.trailingComma.TrailingCommaHelper
|
import org.jetbrains.kotlin.idea.formatter.trailingComma.TrailingCommaHelper
|
||||||
import org.jetbrains.kotlin.idea.formatter.trailingComma.TrailingCommaState
|
import org.jetbrains.kotlin.idea.formatter.trailingComma.TrailingCommaState
|
||||||
import org.jetbrains.kotlin.idea.formatter.trailingComma.canAddTrailingCommaWithRegistryCheck
|
import org.jetbrains.kotlin.idea.formatter.trailingComma.canAddTrailingComma
|
||||||
import org.jetbrains.kotlin.idea.formatter.trailingComma.existsOrMissing
|
|
||||||
import org.jetbrains.kotlin.psi.KtElement
|
import org.jetbrains.kotlin.psi.KtElement
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||||
@@ -26,12 +25,22 @@ class JoinWithTrailingCommaHandler : JoinLinesHandlerDelegate {
|
|||||||
val commaOwner = file.findElementAt(start)
|
val commaOwner = file.findElementAt(start)
|
||||||
?.parentsWithSelf
|
?.parentsWithSelf
|
||||||
?.filter { !document.containsLineBreakInRange(it.textRange) }
|
?.filter { !document.containsLineBreakInRange(it.textRange) }
|
||||||
?.findLast { it.canAddTrailingCommaWithRegistryCheck() } as? KtElement
|
?.findLast { it.canAddTrailingComma() } as? KtElement
|
||||||
?: return CANNOT_JOIN
|
?: return CANNOT_JOIN
|
||||||
|
|
||||||
if (TrailingCommaState.stateForElement(commaOwner).existsOrMissing) return CANNOT_JOIN
|
return when (TrailingCommaState.stateForElement(commaOwner)) {
|
||||||
val result = CodeStyleManager.getInstance(file.project).reformat(commaOwner) as KtElement
|
TrailingCommaState.REDUNDANT, TrailingCommaState.NOT_EXISTS -> {
|
||||||
return TrailingCommaHelper.elementAfterLastElement(result)?.startOffset ?: end - 1
|
val oldLen = commaOwner.textLength
|
||||||
|
val result = CodeStyleManager.getInstance(file.project).reformat(commaOwner) as KtElement
|
||||||
|
if (oldLen != result.textLength)
|
||||||
|
TrailingCommaHelper.elementAfterLastElement(result)?.startOffset ?: end - 1
|
||||||
|
else
|
||||||
|
CANNOT_JOIN
|
||||||
|
}
|
||||||
|
|
||||||
|
else ->
|
||||||
|
CANNOT_JOIN
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
fun a() {
|
fun a() {
|
||||||
b(1, 3, 2424,<caret> awdawd,)
|
b(1, 3, 2424, awdawd<caret>)
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
fun a() {
|
fun a() {
|
||||||
<caret>g[1, 2, 3,/**/
|
<caret> g[1, 2, 3,/**/
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
fun a() {
|
fun a() {
|
||||||
g[1, 2, 3,/**/<caret>]
|
g[1, 2, 3/**/<caret>]
|
||||||
}
|
}
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
@Anno([1, 2, 3,<caret> ])
|
@Anno([1, 2, 3]<caret>)
|
||||||
class A
|
class A
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
fun a() {
|
fun a() {
|
||||||
b<Int,<caret> >()
|
b<Int<caret>>()
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user