TrailingCommaInspection: should suggest fixes for call-site without warnings
#KT-39131 Fixed
This commit is contained in:
@@ -32,7 +32,7 @@ import kotlin.properties.Delegates
|
|||||||
|
|
||||||
class TrailingCommaInspection(
|
class TrailingCommaInspection(
|
||||||
@JvmField
|
@JvmField
|
||||||
var addCommaWarning: Boolean = false,
|
var addCommaWarning: Boolean = false
|
||||||
) : AbstractKotlinInspection() {
|
) : AbstractKotlinInspection() {
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = object : TrailingCommaVisitor() {
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = object : TrailingCommaVisitor() {
|
||||||
override val recursively: Boolean = false
|
override val recursively: Boolean = false
|
||||||
@@ -40,9 +40,8 @@ class TrailingCommaInspection(
|
|||||||
|
|
||||||
override fun process(trailingCommaContext: TrailingCommaContext) {
|
override fun process(trailingCommaContext: TrailingCommaContext) {
|
||||||
val element = trailingCommaContext.ktElement
|
val element = trailingCommaContext.ktElement
|
||||||
if (!element.canAddTrailingCommaWithRegistryCheck()) return
|
val kotlinCustomSettings = CodeStyle.getSettings(element.project).kotlinCustomSettings
|
||||||
|
useTrailingComma = kotlinCustomSettings.addTrailingCommaIsAllowedFor(element)
|
||||||
useTrailingComma = CodeStyle.getSettings(element.project).kotlinCustomSettings.ALLOW_TRAILING_COMMA
|
|
||||||
when (trailingCommaContext.state) {
|
when (trailingCommaContext.state) {
|
||||||
TrailingCommaState.MISSING, TrailingCommaState.EXISTS -> {
|
TrailingCommaState.MISSING, TrailingCommaState.EXISTS -> {
|
||||||
checkCommaPosition(element)
|
checkCommaPosition(element)
|
||||||
@@ -161,6 +160,7 @@ class TrailingCommaInspection(
|
|||||||
val range = createFormatterTextRange(element)
|
val range = createFormatterTextRange(element)
|
||||||
val settings = CodeStyle.getSettings(project).clone()
|
val settings = CodeStyle.getSettings(project).clone()
|
||||||
settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA = true
|
settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA = true
|
||||||
|
settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA_ON_CALL_SITE = true
|
||||||
CodeStyle.doWithTemporarySettings(project, settings) {
|
CodeStyle.doWithTemporarySettings(project, settings) {
|
||||||
CodeStyleManager.getInstance(project).reformatRange(element, range.startOffset, range.endOffset)
|
CodeStyleManager.getInstance(project).reformatRange(element, range.startOffset, range.endOffset)
|
||||||
}
|
}
|
||||||
|
|||||||
+3303
-207
File diff suppressed because it is too large
Load Diff
+3303
-207
File diff suppressed because it is too large
Load Diff
+7443
-2883
File diff suppressed because it is too large
Load Diff
Vendored
+667
-667
File diff suppressed because it is too large
Load Diff
+7443
-2883
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
val (a,
|
||||||
|
b,<caret>) = 1 to 2
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
val (
|
||||||
|
a,
|
||||||
|
b,
|
||||||
|
) = 1 to 2
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Fix comma position
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
val a = { (a, b // awd
|
||||||
|
,<caret>/**/), c, -> }
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Fix comma position
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
val a = {
|
||||||
|
(
|
||||||
|
a, b, // awd/**/
|
||||||
|
), c, -> }
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add trailing comma
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
b(1, 3, 2424,
|
||||||
|
awdawd<caret>)
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add trailing comma
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
b(
|
||||||
|
1, 3, 2424,
|
||||||
|
awdawd,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
g[<caret>1, 2, 3,/**/
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
g[
|
||||||
|
1, 2, 3,/**/
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
|
||||||
|
fun a(
|
||||||
|
a: Int, b: Any = fun(a: Int,) {},<caret>) {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
|
||||||
|
fun a(
|
||||||
|
a: Int, b: Any = fun(a: Int) {},
|
||||||
|
) {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
val a = { a: Int,
|
||||||
|
b: Int, <caret>->
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
val a = {
|
||||||
|
a: Int,
|
||||||
|
b: Int,
|
||||||
|
->
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
@Anno(
|
||||||
|
[<caret>1, 2, 3,
|
||||||
|
])
|
||||||
|
class A
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
@Anno(
|
||||||
|
[
|
||||||
|
1, 2, 3,
|
||||||
|
])
|
||||||
|
class A
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// PROBLEM: none
|
||||||
|
|
||||||
|
fun a(<caret>
|
||||||
|
a: Int,) {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
b<<caret>Int,
|
||||||
|
>()
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
b<
|
||||||
|
Int,
|
||||||
|
>()
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
|
||||||
|
fun <<caret>T,
|
||||||
|
B,> a() = Unit
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
|
||||||
|
fun <
|
||||||
|
T,
|
||||||
|
B,
|
||||||
|
> a() = Unit
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
when (val b = 5) {
|
||||||
|
1, 2,
|
||||||
|
3, <caret>->
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
|
||||||
|
// FIX: Add line break
|
||||||
|
|
||||||
|
fun a() {
|
||||||
|
when (val b = 5) {
|
||||||
|
1, 2,
|
||||||
|
3,
|
||||||
|
->
|
||||||
|
}
|
||||||
|
}
|
||||||
+55
@@ -13115,6 +13115,41 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/trailingComma/changeCommaPosition5.kt");
|
runTest("idea/testData/inspectionsLocal/trailingComma/changeCommaPosition5.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("destructuringDeclarations.kt")
|
||||||
|
public void testDestructuringDeclarations() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/destructuringDeclarations.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("destructuringDeclarationsInLambda.kt")
|
||||||
|
public void testDestructuringDeclarationsInLambda() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/destructuringDeclarationsInLambda.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("functionArguments.kt")
|
||||||
|
public void testFunctionArguments() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/functionArguments.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("indices.kt")
|
||||||
|
public void testIndices() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/indices.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("inner.kt")
|
||||||
|
public void testInner() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/inner.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambda.kt")
|
||||||
|
public void testLambda() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/lambda.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("literal.kt")
|
||||||
|
public void testLiteral() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/literal.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("missingLineBreak.kt")
|
@TestMetadata("missingLineBreak.kt")
|
||||||
public void testMissingLineBreak() throws Exception {
|
public void testMissingLineBreak() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/trailingComma/missingLineBreak.kt");
|
runTest("idea/testData/inspectionsLocal/trailingComma/missingLineBreak.kt");
|
||||||
@@ -13149,6 +13184,26 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
public void testRemoveComma4() throws Exception {
|
public void testRemoveComma4() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/trailingComma/removeComma4.kt");
|
runTest("idea/testData/inspectionsLocal/trailingComma/removeComma4.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("simple.kt")
|
||||||
|
public void testSimple() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/simple.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("typeArguments.kt")
|
||||||
|
public void testTypeArguments() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/typeArguments.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("typeParameters.kt")
|
||||||
|
public void testTypeParameters() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/typeParameters.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("whenEntry.kt")
|
||||||
|
public void testWhenEntry() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/trailingComma/whenEntry.kt");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/inspectionsLocal/unlabeledReturnInsideLambda")
|
@TestMetadata("idea/testData/inspectionsLocal/unlabeledReturnInsideLambda")
|
||||||
|
|||||||
Reference in New Issue
Block a user