Skip to content

Commit f8fcec6

Browse files
Nivaldo Bondançafacebook-github-bot
authored andcommitted
Codemod format for trailing commas change
Reviewed By: VladimirMakaev Differential Revision: D80576929 fbshipit-source-id: 1310f77f5d9d489b780b14875454ebda7f7adfc9
1 parent b7dfcf3 commit f8fcec6

23 files changed

Lines changed: 678 additions & 368 deletions

core/src/main/java/com/facebook/ktfmt/cli/Main.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class Main(
7272
result.addAll(
7373
File(arg).walkTopDown().filter {
7474
it.isFile && (it.extension == "kt" || it.extension == "kts")
75-
})
75+
}
76+
)
7677
}
7778
return result
7879
}

core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ data class ParsedArgs(
124124
stdinName =
125125
parseKeyValueArg("--stdin-name", arg)
126126
?: return ParseResult.Error(
127-
"Found option '${arg}', expected '${"--stdin-name"}=<value>'")
127+
"Found option '${arg}', expected '${"--stdin-name"}=<value>'"
128+
)
128129
arg.startsWith("--") -> return ParseResult.Error("Unexpected option: $arg")
129130
arg.startsWith("@") -> return ParseResult.Error("Unexpected option: $arg")
130131
else -> fileNames.add(arg)
@@ -137,7 +138,8 @@ data class ParsedArgs(
137138
val filesExceptStdin = fileNames - "-"
138139
return ParseResult.Error(
139140
"Cannot read from stdin and files in same run. Found stdin specifier '-'" +
140-
" and files ${filesExceptStdin.joinToString(", ")} ")
141+
" and files ${filesExceptStdin.joinToString(", ")} "
142+
)
141143
}
142144
} else if (stdinName != null) {
143145
return ParseResult.Error("--stdin-name can only be specified when reading from stdin")
@@ -150,7 +152,8 @@ data class ParsedArgs(
150152
dryRun,
151153
setExitIfChanged,
152154
stdinName,
153-
))
155+
)
156+
)
154157
}
155158

156159
private fun parseKeyValueArg(key: String, arg: String): String? {

core/src/main/java/com/facebook/ktfmt/format/Formatter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ object Formatter {
131131
val tokenRangeSet =
132132
kotlinInput.characterRangesToTokenRanges(ImmutableList.of(Range.closedOpen(0, code.length)))
133133
return WhitespaceTombstones.replaceTombstoneWithTrailingWhitespace(
134-
JavaOutput.applyReplacements(code, javaOutput.getFormatReplacements(tokenRangeSet)))
134+
JavaOutput.applyReplacements(code, javaOutput.getFormatReplacements(tokenRangeSet))
135+
)
135136
}
136137

137138
private fun createAstVisitor(options: FormattingOptions, builder: OpsBuilder): PsiElementVisitor {

core/src/main/java/com/facebook/ktfmt/format/KotlinInput.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class KotlinInput(private val text: String, file: KtFile) : Input() {
8484
characterRangeToTokenRange(
8585
characterRange.lowerEndpoint(),
8686
characterRange.upperEndpoint() - characterRange.lowerEndpoint(),
87-
))
87+
)
88+
)
8889
}
8990
return tokenRangeSet
9091
}
@@ -106,7 +107,8 @@ class KotlinInput(private val text: String, file: KtFile) : Input() {
106107
"error: invalid length %d, offset + length (%d) is outside the file",
107108
length,
108109
requiredLength,
109-
))
110+
)
111+
)
110112
}
111113
val expandedLength =
112114
when {

core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,10 @@ class KotlinInputAstVisitor(
657657
(receiverExpression as? KtQualifiedExpression)?.selectorExpression
658658
?: receiverExpression
659659
val current = checkNotNull(part.selectorExpression)
660-
if (lastIndexToOpen == 0 &&
661-
shouldGroupPartWithPrevious(parts, part, index, previous, current)) {
660+
if (
661+
lastIndexToOpen == 0 &&
662+
shouldGroupPartWithPrevious(parts, part, index, previous, current)
663+
) {
662664
// this and the previous items should be grouped for better style
663665
// we add another group to open in index 0
664666
groupingInfos[0].groupOpenCount++
@@ -702,21 +704,27 @@ class KotlinInputAstVisitor(
702704
return true
703705
}
704706
// this and the previous part are a package name, type name, or property
705-
if (previous is KtSimpleNameExpression &&
706-
current is KtSimpleNameExpression &&
707-
part is KtDotQualifiedExpression) {
707+
if (
708+
previous is KtSimpleNameExpression &&
709+
current is KtSimpleNameExpression &&
710+
part is KtDotQualifiedExpression
711+
) {
708712
return true
709713
}
710714
// this is `Foo` in `com.facebook.Foo`, so everything before it is a package name
711-
if (current.text.first().isUpperCase() &&
712-
current is KtSimpleNameExpression &&
713-
part is KtDotQualifiedExpression) {
715+
if (
716+
current.text.first().isUpperCase() &&
717+
current is KtSimpleNameExpression &&
718+
part is KtDotQualifiedExpression
719+
) {
714720
return true
715721
}
716722
// this is the `foo()` in `com.facebook.Foo.foo()` or in `Foo.foo()`
717-
if (current is KtCallExpression &&
718-
(previous !is KtCallExpression) &&
719-
previous.text?.firstOrNull()?.isUpperCase() == true) {
723+
if (
724+
current is KtCallExpression &&
725+
(previous !is KtCallExpression) &&
726+
previous.text?.firstOrNull()?.isUpperCase() == true
727+
) {
720728
return true
721729
}
722730
// this is an invocation and the last item, and the previous it not, i.e. `a.b.c()`
@@ -915,9 +923,11 @@ class KotlinInputAstVisitor(
915923
builder.breakOp(Doc.FillMode.UNIFIED, "", bracePlusBlockIndent)
916924
builder.block(bracePlusBlockIndent) {
917925
builder.blankLineWanted(OpsBuilder.BlankLineWanted.NO)
918-
if (expressionStatements.size == 1 &&
919-
expressionStatements.first() !is KtReturnExpression &&
920-
!bodyExpression.startsWithComment()) {
926+
if (
927+
expressionStatements.size == 1 &&
928+
expressionStatements.first() !is KtReturnExpression &&
929+
!bodyExpression.startsWithComment()
930+
) {
921931
visitStatement(expressionStatements[0])
922932
} else {
923933
visitStatements(expressionStatements)
@@ -1247,8 +1257,10 @@ class KotlinInputAstVisitor(
12471257
val operator = expression.operationReference.text
12481258

12491259
visit(baseExpression)
1250-
if (baseExpression is KtPostfixExpression &&
1251-
baseExpression.operationReference.text.last() == operator.first()) {
1260+
if (
1261+
baseExpression is KtPostfixExpression &&
1262+
baseExpression.operationReference.text.last() == operator.first()
1263+
) {
12521264
builder.space()
12531265
}
12541266
builder.token(operator)
@@ -1262,8 +1274,10 @@ class KotlinInputAstVisitor(
12621274
val operator = expression.operationReference.text
12631275

12641276
builder.token(operator)
1265-
if (baseExpression is KtPrefixExpression &&
1266-
operator.last() == baseExpression.operationReference.text.first()) {
1277+
if (
1278+
baseExpression is KtPrefixExpression &&
1279+
operator.last() == baseExpression.operationReference.text.first()
1280+
) {
12671281
builder.space()
12681282
}
12691283
visit(baseExpression)
@@ -1467,9 +1481,11 @@ class KotlinInputAstVisitor(
14671481

14681482
var carry = expression
14691483
if (carry is KtCallExpression) {
1470-
if (carry.valueArgumentList?.leftParenthesis == null &&
1471-
carry.lambdaArguments.isNotEmpty() &&
1472-
carry.typeArgumentList?.arguments.isNullOrEmpty()) {
1484+
if (
1485+
carry.valueArgumentList?.leftParenthesis == null &&
1486+
carry.lambdaArguments.isNotEmpty() &&
1487+
carry.typeArgumentList?.arguments.isNullOrEmpty()
1488+
) {
14731489
carry = carry.lambdaArguments[0].getArgumentExpression()
14741490
} else {
14751491
return false
@@ -2362,9 +2378,11 @@ class KotlinInputAstVisitor(
23622378
visit(expression.leftHandSide)
23632379
if (!openGroupBeforeLeft) builder.open(ZERO)
23642380
val parent = expression.parent
2365-
if (parent is KtValueArgument ||
2366-
parent is KtParenthesizedExpression ||
2367-
parent is KtContainerNode) {
2381+
if (
2382+
parent is KtValueArgument ||
2383+
parent is KtParenthesizedExpression ||
2384+
parent is KtContainerNode
2385+
) {
23682386
builder.breakOp(Doc.FillMode.UNIFIED, " ", expressionBreakIndent)
23692387
} else {
23702388
builder.space()
@@ -2527,7 +2545,8 @@ class KotlinInputAstVisitor(
25272545
child is PsiComment -> continue
25282546
child is KtScript && importListEmpty -> OpsBuilder.BlankLineWanted.PRESERVE
25292547
else -> OpsBuilder.BlankLineWanted.YES
2530-
})
2548+
}
2549+
)
25312550

25322551
visit(child)
25332552
isFirst = false
@@ -2550,8 +2569,9 @@ class KotlinInputAstVisitor(
25502569
builder.blankLineWanted(OpsBuilder.BlankLineWanted.PRESERVE)
25512570
} else if (lastChildIsContextReceiver) {
25522571
builder.blankLineWanted(OpsBuilder.BlankLineWanted.NO)
2553-
} else if (child !is PsiComment &&
2554-
(childGetsBlankLineBefore || lastChildHadBlankLineBefore)) {
2572+
} else if (
2573+
child !is PsiComment && (childGetsBlankLineBefore || lastChildHadBlankLineBefore)
2574+
) {
25552575
builder.blankLineWanted(OpsBuilder.BlankLineWanted.YES)
25562576
}
25572577
visit(child)

core/src/main/java/com/facebook/ktfmt/format/MultilineStringFormatter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ class MultilineStringFormatter(val continuationIndentSize: Int) {
146146
candidates.add(Candidate(isTrimMargin, trimOffset, stringOffset))
147147
}
148148
}
149-
})
149+
}
150+
)
150151
return candidates
151152
}
152153
}

core/src/main/java/com/facebook/ktfmt/format/ParseError.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import org.jetbrains.kotlin.com.intellij.psi.PsiElement
2121

2222
class ParseError(val errorDescription: String, val lineColumn: LineColumn) :
2323
IllegalArgumentException(
24-
"${lineColumn.line + 1}:${lineColumn.column + 1}: error: $errorDescription") {
24+
"${lineColumn.line + 1}:${lineColumn.column + 1}: error: $errorDescription"
25+
) {
2526

2627
constructor(
2728
errorDescription: String,

core/src/main/java/com/facebook/ktfmt/format/RedundantElementManager.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ object RedundantElementManager {
6868
redundantImportDetector.takeReferenceExpression(expression)
6969
super.visitReferenceExpression(expression)
7070
}
71-
})
71+
}
72+
)
7273

7374
val result = StringBuilder(code)
7475
val elementsToRemove =
@@ -104,7 +105,8 @@ object RedundantElementManager {
104105
trailingCommaSuggestor.takeElement(element)
105106
super.visitElement(element)
106107
}
107-
})
108+
}
109+
)
108110

109111
val result = StringBuilder(code)
110112
val suggestionElements = trailingCommaSuggestor.getTrailingCommaSuggestions()

core/src/main/java/com/facebook/ktfmt/format/RedundantSemicolonDetector.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ internal class RedundantSemicolonDetector {
6565

6666
val prevLeaf = element.prevLeaf(false)
6767
val prevConcreteSibling = element.getPrevSiblingIgnoringWhitespaceAndComments()
68-
if ((prevConcreteSibling is KtIfExpression || prevConcreteSibling is KtWhileExpression) &&
69-
prevLeaf is KtContainerNodeForControlStructureBody &&
70-
prevLeaf.text.isEmpty()) {
68+
if (
69+
(prevConcreteSibling is KtIfExpression || prevConcreteSibling is KtWhileExpression) &&
70+
prevLeaf is KtContainerNodeForControlStructureBody &&
71+
prevLeaf.text.isEmpty()
72+
) {
7173
return false
7274
}
7375

core/src/main/java/com/facebook/ktfmt/kdoc/KDocCommentsHelper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class KDocCommentsHelper(private val lineSeparator: String, private val maxLineL
5454
convertMarkup = false
5555
nestedListIndent = 4
5656
optimal = false // Use greedy line breaking for predictability.
57-
})
57+
}
58+
)
5859

5960
override fun rewrite(tok: Tok, maxWidth: Int, column0: Int): String {
6061
if (!tok.isComment) {

0 commit comments

Comments
 (0)