Skip to content

Commit 0a754f8

Browse files
Nivaldo Bondançafacebook-github-bot
authored andcommitted
Apply formatting to ktfmt code
Summary: This is here to showcase the changes from the previous diff in action Differential Revision: D79772884 fbshipit-source-id: c2200e1e198aa032bb76963dcc68dbfb349f4d7c
1 parent e14d99c commit 0a754f8

22 files changed

Lines changed: 9780 additions & 9391 deletions

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ private const val EXIT_CODE_SUCCESS = 0
3737

3838
private val USAGE =
3939
"""
40-
|Usage:
41-
| ktfmt [OPTIONS] File1.kt File2.kt ...
42-
| ktfmt @ARGFILE
43-
|
44-
|For more details see `ktfmt --help`
45-
|"""
40+
|Usage:
41+
| ktfmt [OPTIONS] File1.kt File2.kt ...
42+
| ktfmt @ARGFILE
43+
|
44+
|For more details see `ktfmt --help`
45+
|"""
4646
.trimMargin()
4747

4848
class Main(
4949
private val input: InputStream,
5050
private val out: PrintStream,
5151
private val err: PrintStream,
52-
private val inputArgs: Array<String>
52+
private val inputArgs: Array<String>,
5353
) {
5454
companion object {
5555
@JvmStatic

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ object Formatter {
150150
throw ParseError(
151151
"ktfmt does not support code which contains one of {\\u0003, \\u0004, \\u0005} character" +
152152
"; escape it",
153-
StringUtil.offsetToLineColumn(code, index))
153+
StringUtil.offsetToLineColumn(code, index),
154+
)
154155
}
155156
}
156157

@@ -172,7 +173,8 @@ object Formatter {
172173
} else if (element !is KtImportDirective && element !is PsiWhiteSpace) {
173174
throw ParseError(
174175
"Imports not contiguous: " + element.text,
175-
StringUtil.offsetToLineColumn(code, element.startOffset))
176+
StringUtil.offsetToLineColumn(code, element.startOffset),
177+
)
176178
}
177179
element = element.nextSibling
178180
}
@@ -189,6 +191,7 @@ object Formatter {
189191
return code.replaceRange(
190192
importList.startOffset,
191193
importList.endOffset,
192-
importsWithComments.joinToString(separator = "\n") { imprt -> imprt.text } + "\n")
194+
importsWithComments.joinToString(separator = "\n") { imprt -> imprt.text } + "\n",
195+
)
193196
}
194197
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class KotlinInput(private val text: String, file: KtFile) : Input() {
8383
tokenRangeSet.add(
8484
characterRangeToTokenRange(
8585
characterRange.lowerEndpoint(),
86-
characterRange.upperEndpoint() - characterRange.lowerEndpoint()))
86+
characterRange.upperEndpoint() - characterRange.lowerEndpoint(),
87+
))
8788
}
8889
return tokenRangeSet
8990
}
@@ -104,7 +105,8 @@ class KotlinInput(private val text: String, file: KtFile) : Input() {
104105
String.format(
105106
"error: invalid length %d, offset + length (%d) is outside the file",
106107
length,
107-
requiredLength))
108+
requiredLength,
109+
))
108110
}
109111
val expandedLength =
110112
when {
@@ -121,7 +123,9 @@ class KotlinInput(private val text: String, file: KtFile) : Input() {
121123
EMPTY_RANGE
122124
} else
123125
Range.closedOpen(
124-
enclosed.iterator().next().tok.index, getLast(enclosed).getTok().getIndex() + 1)
126+
enclosed.iterator().next().tok.index,
127+
getLast(enclosed).getTok().getIndex() + 1,
128+
)
125129
}
126130

127131
private fun makePositionToColumnMap(toks: List<KotlinTok>) =

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ import org.jetbrains.kotlin.psi.stubs.impl.KotlinPlaceHolderStubImpl
135135
/** An AST visitor that builds a stream of {@link Op}s to format. */
136136
class KotlinInputAstVisitor(
137137
private val options: FormattingOptions,
138-
private val builder: OpsBuilder
138+
private val builder: OpsBuilder,
139139
) : KtTreeVisitorVoid() {
140140

141141
/** Standard indentation for a block */
@@ -451,7 +451,8 @@ class KotlinInputAstVisitor(
451451
typeConstraintList = property.typeConstraintList,
452452
delegate = property.delegate,
453453
initializer = property.initializer,
454-
accessors = property.accessors)
454+
accessors = property.accessors,
455+
)
455456
}
456457
builder.guessToken(";")
457458
if (property.parent !is KtWhenExpression) {
@@ -644,7 +645,7 @@ class KotlinInputAstVisitor(
644645
*/
645646
private fun computeGroupingInfo(
646647
parts: List<KtExpression>,
647-
useBlockLikeLambdaStyle: Boolean
648+
useBlockLikeLambdaStyle: Boolean,
648649
): List<GroupingInfo> {
649650
val groupingInfos = List(parts.size) { GroupingInfo() }
650651
var lastIndexToOpen = 0
@@ -690,7 +691,7 @@ class KotlinInputAstVisitor(
690691
part: KtExpression,
691692
index: Int,
692693
previous: KtExpression,
693-
current: KtExpression
694+
current: KtExpression,
694695
): Boolean {
695696
// this is the second, and the first is short, avoid `.` "hanging in air"
696697
if (index == 1 && previous.text.length < options.continuationIndent) {
@@ -1280,7 +1281,7 @@ class KotlinInputAstVisitor(
12801281

12811282
internal enum class DeclarationKind {
12821283
FIELD,
1283-
PARAMETER
1284+
PARAMETER,
12841285
}
12851286

12861287
/**
@@ -1302,7 +1303,7 @@ class KotlinInputAstVisitor(
13021303
typeConstraintList: KtTypeConstraintList? = null,
13031304
initializer: KtExpression?,
13041305
delegate: KtPropertyDelegate? = null,
1305-
accessors: List<KtPropertyAccessor>? = null
1306+
accessors: List<KtPropertyAccessor>? = null,
13061307
): Int {
13071308
val verticalAnnotationBreak = genSym()
13081309

@@ -1821,7 +1822,7 @@ class KotlinInputAstVisitor(
18211822
/** For example, 'field' in @field:[Inject Named("WEB_VIEW")] */
18221823
override fun visitAnnotationUseSiteTarget(
18231824
annotationTarget: KtAnnotationUseSiteTarget,
1824-
data: Void?
1825+
data: Void?,
18251826
): Void? {
18261827
builder.token(annotationTarget.getAnnotationUseSiteTarget().renderName)
18271828
return null
@@ -1842,12 +1843,13 @@ class KotlinInputAstVisitor(
18421843
annotationEntry.calleeExpression,
18431844
null, // Type-arguments are included in the annotation's callee expression.
18441845
annotationEntry.valueArgumentList,
1845-
listOf())
1846+
listOf(),
1847+
)
18461848
}
18471849

18481850
override fun visitFileAnnotationList(
18491851
fileAnnotationList: KtFileAnnotationList,
1850-
data: Void?
1852+
data: Void?,
18511853
): Void? {
18521854
for (child in fileAnnotationList.node.children()) {
18531855
if (child is PsiElement) {
@@ -1912,7 +1914,10 @@ class KotlinInputAstVisitor(
19121914
whenEntry.guard?.let { guard ->
19131915
builder.space()
19141916
emitKeywordWithCondition(
1915-
"if", guard.getExpression(), surroundConditionWithParens = false)
1917+
"if",
1918+
guard.getExpression(),
1919+
surroundConditionWithParens = false,
1920+
)
19161921
}
19171922
}
19181923
}
@@ -2075,7 +2080,10 @@ class KotlinInputAstVisitor(
20752080
builder.breakOp(Doc.FillMode.UNIFIED, "", expressionBreakIndent)
20762081
builder.block(expressionBreakIndent) {
20772082
visitEachCommaSeparated(
2078-
expression.indexExpressions, expression.trailingComma != null, wrapInBlock = true)
2083+
expression.indexExpressions,
2084+
expression.trailingComma != null,
2085+
wrapInBlock = true,
2086+
)
20792087
}
20802088
}
20812089
builder.token("]")
@@ -2095,7 +2103,10 @@ class KotlinInputAstVisitor(
20952103
builder.breakOp(Doc.FillMode.UNIFIED, "", expressionBreakIndent)
20962104
builder.block(expressionBreakIndent) {
20972105
visitEachCommaSeparated(
2098-
destructuringDeclaration.entries, hasTrailingComma, wrapInBlock = true)
2106+
destructuringDeclaration.entries,
2107+
hasTrailingComma,
2108+
wrapInBlock = true,
2109+
)
20992110
}
21002111
}
21012112
builder.token(")")
@@ -2273,7 +2284,8 @@ class KotlinInputAstVisitor(
22732284
valOrVarKeyword = parameter.valOrVarKeyword?.text,
22742285
name = parameter.nameIdentifier?.text,
22752286
type = typeReference,
2276-
initializer = parameter.defaultValue)
2287+
initializer = parameter.defaultValue,
2288+
)
22772289
}
22782290
}
22792291
}
@@ -2306,7 +2318,8 @@ class KotlinInputAstVisitor(
23062318
receiverExpression.calleeExpression,
23072319
receiverExpression.typeArgumentList,
23082320
receiverExpression.valueArgumentList,
2309-
receiverExpression.lambdaArguments)
2321+
receiverExpression.lambdaArguments,
2322+
)
23102323
} else {
23112324
visit(receiverExpression)
23122325
}
@@ -2392,7 +2405,8 @@ class KotlinInputAstVisitor(
23922405
expression.trailingComma != null,
23932406
prefix = "[",
23942407
postfix = "]",
2395-
wrapInBlock = !options.manageTrailingCommas)
2408+
wrapInBlock = !options.manageTrailingCommas,
2409+
)
23962410
}
23972411
}
23982412

@@ -2575,7 +2589,8 @@ class KotlinInputAstVisitor(
25752589
token,
25762590
Doc.Token.RealOrImaginary.REAL,
25772591
plusIndentCommentsBefore,
2578-
/* breakAndIndentTrailingComment */ Optional.empty())
2592+
/* breakAndIndentTrailingComment */ Optional.empty(),
2593+
)
25792594
}
25802595

25812596
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class KotlinTok(
2828
private val position: Int,
2929
private val column: Int,
3030
val isToken: Boolean,
31-
private val kind: KtToken
31+
private val kind: KtToken,
3232
) : Input.Tok {
3333

3434
override fun getIndex(): Int = index

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import com.google.googlejavaformat.Input
2323
class KotlinToken(
2424
private val toksBefore: ImmutableList<KotlinTok>,
2525
private val kotlinTok: KotlinTok,
26-
private val toksAfter: ImmutableList<KotlinTok>
26+
private val toksAfter: ImmutableList<KotlinTok>,
2727
) : Input.Token {
2828

2929
override fun getTok(): KotlinTok = kotlinTok

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ object Parser {
5252
val configuration = CompilerConfiguration()
5353
configuration.put(
5454
CommonConfigurationKeys.MESSAGE_COLLECTOR_KEY,
55-
PrintingMessageCollector(System.err, PLAIN_RELATIVE_PATHS, false))
55+
PrintingMessageCollector(System.err, PLAIN_RELATIVE_PATHS, false),
56+
)
5657
env =
5758
KotlinCoreEnvironment.createForProduction(
58-
disposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
59+
disposable,
60+
configuration,
61+
EnvironmentConfigFiles.JVM_CONFIG_FILES,
62+
)
5963
}
6064

6165
fun parse(code: String): KtFile {
@@ -68,6 +72,8 @@ object Parser {
6872

6973
private fun throwParseError(fileContents: String, error: PsiErrorElement): Nothing {
7074
throw ParseError(
71-
error.errorDescription, StringUtil.offsetToLineColumn(fileContents, error.startOffset))
75+
error.errorDescription,
76+
StringUtil.offsetToLineColumn(fileContents, error.startOffset),
77+
)
7278
}
7379
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ class Tokenizer(private val fileText: String, val file: KtFile) : KtTreeVisitorV
6767
is PsiComment -> {
6868
if (element.text.startsWith("/*") && !element.text.endsWith("*/")) {
6969
throw ParseError(
70-
"Unclosed comment", StringUtil.offsetToLineColumn(fileText, element.startOffset))
70+
"Unclosed comment",
71+
StringUtil.offsetToLineColumn(fileText, element.startOffset),
72+
)
7173
}
7274
toks.add(
7375
KotlinTok(
@@ -111,7 +113,9 @@ class Tokenizer(private val fileText: String, val file: KtFile) : KtTreeVisitorV
111113
index = -1,
112114
originalText =
113115
fileText.substring(
114-
startIndex + matcher.start(), startIndex + matcher.end()),
116+
startIndex + matcher.start(),
117+
startIndex + matcher.end(),
118+
),
115119
text = text,
116120
position = startIndex + matcher.start(),
117121
column = 0,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ enum class CommentType(
3838
/** The closing string of the comment. */
3939
val suffix: String,
4040
/** For multi line comments, the prefix at each comment line after the first one. */
41-
val linePrefix: String
41+
val linePrefix: String,
4242
) {
4343
KDOC("/**", "*/", " * "),
4444
BLOCK("/*", "*/", ""),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ class FormattingTask(
7070
var orderedParameterNames: List<String> = emptyList(),
7171

7272
/** The type of comment being formatted. */
73-
val type: CommentType = comment.commentType()
73+
val type: CommentType = comment.commentType(),
7474
)

0 commit comments

Comments
 (0)