Skip to content

Commit 109b90e

Browse files
committed
Wire up in Main
1 parent 3bedf86 commit 109b90e

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

  • core/src/main/java/com/facebook/ktfmt/cli

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ class Main(
116116
return EXIT_CODE_FAILURE
117117
}
118118

119+
if (parsedArgs.isPartialFormat() && files.size != 1) {
120+
err.println("partial formatting is only supported for a single file")
121+
return EXIT_CODE_FAILURE
122+
}
123+
119124
val returnCode = AtomicInteger(EXIT_CODE_SUCCESS)
120125
files.parallelStream().forEach {
121126
try {
@@ -146,7 +151,12 @@ class Main(
146151
else EditorConfigResolver.resolveFormattingOptions(file, args.formattingOptions)
147152
val bytes = if (file == null) input else FileInputStream(file)
148153
val code = BufferedReader(InputStreamReader(bytes, UTF_8)).readText().removePrefix(UTF8_BOM)
149-
val formattedCode = Formatter.format(formattingOptions, code)
154+
val formattedCode =
155+
if (!args.isPartialFormat()) {
156+
Formatter.format(formattingOptions, code)
157+
} else {
158+
Formatter.format(formattingOptions, code, args.lineRanges, args.characterRanges)
159+
}
150160
val alreadyFormatted = code == formattedCode
151161

152162
// stdin
@@ -190,4 +200,7 @@ class Main(
190200
throw e
191201
}
192202
}
203+
204+
private fun ParsedArgs.isPartialFormat(): Boolean =
205+
!lineRanges.isEmpty || !characterRanges.isEmpty
193206
}

0 commit comments

Comments
 (0)