File tree Expand file tree Collapse file tree
core/src/main/java/com/facebook/ktfmt/cli Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments