TrailingCommaInspection: should suggest fixes for call-site without warnings

#KT-39131 Fixed
This commit is contained in:
Dmitry Gridin
2020-05-22 19:39:55 +07:00
parent 10462c31d2
commit c78fd4aaca
28 changed files with 22396 additions and 6851 deletions
@@ -32,7 +32,7 @@ import kotlin.properties.Delegates
class TrailingCommaInspection(
@JvmField
var addCommaWarning: Boolean = false,
var addCommaWarning: Boolean = false
) : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = object : TrailingCommaVisitor() {
override val recursively: Boolean = false
@@ -40,9 +40,8 @@ class TrailingCommaInspection(
override fun process(trailingCommaContext: TrailingCommaContext) {
val element = trailingCommaContext.ktElement
if (!element.canAddTrailingCommaWithRegistryCheck()) return
useTrailingComma = CodeStyle.getSettings(element.project).kotlinCustomSettings.ALLOW_TRAILING_COMMA
val kotlinCustomSettings = CodeStyle.getSettings(element.project).kotlinCustomSettings
useTrailingComma = kotlinCustomSettings.addTrailingCommaIsAllowedFor(element)
when (trailingCommaContext.state) {
TrailingCommaState.MISSING, TrailingCommaState.EXISTS -> {
checkCommaPosition(element)
@@ -161,6 +160,7 @@ class TrailingCommaInspection(
val range = createFormatterTextRange(element)
val settings = CodeStyle.getSettings(project).clone()
settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA = true
settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA_ON_CALL_SITE = true
CodeStyle.doWithTemporarySettings(project, settings) {
CodeStyleManager.getInstance(project).reformatRange(element, range.startOffset, range.endOffset)
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
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
}
@@ -0,0 +1,10 @@
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
// FIX: Add line break
// DISABLE-ERRORS
fun a() {
val (
a,
b,
) = 1 to 2
}
@@ -0,0 +1,8 @@
// COMPILER_ARGUMENTS: -XXLanguage:+TrailingCommas
// FIX: Fix comma position
// DISABLE-ERRORS
fun a() {
val a = { (a, b // awd
,<caret>/**/), c, -> }
}
@@ -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,/**/
]
}
+7
View File
@@ -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,
->
}
}
@@ -13115,6 +13115,41 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
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")
public void testMissingLineBreak() throws Exception {
runTest("idea/testData/inspectionsLocal/trailingComma/missingLineBreak.kt");
@@ -13149,6 +13184,26 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
public void testRemoveComma4() throws Exception {
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")