Skip to content

Commit

Permalink
Always use UTF8 as the encoding for Kotlin code, regardless of the sy…
Browse files Browse the repository at this point in the history
…stem/default charset

Closes #373
  • Loading branch information
nreid260 committed Jan 19, 2024
1 parent 7d28ad1 commit 2c55d1c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 50 deletions.
9 changes: 9 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<!-- Tests that everything works when using an unusual default Charset. -->
<argLine>-Dfile.encoding=UTF-16</argLine>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
19 changes: 10 additions & 9 deletions core/src/main/java/com/facebook/ktfmt/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ import com.facebook.ktfmt.format.Formatter
import com.facebook.ktfmt.format.ParseError
import com.google.googlejavaformat.FormattingError
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.io.PrintStream
import java.nio.charset.StandardCharsets.UTF_8
import java.util.concurrent.atomic.AtomicInteger
import kotlin.system.exitProcess

Expand Down Expand Up @@ -125,7 +129,8 @@ class Main(
private fun format(file: File?): Boolean {
val fileName = file?.toString() ?: parsedArgs.stdinName ?: "<stdin>"
try {
val code = file?.readText() ?: BufferedReader(InputStreamReader(input)).readText()
val bytes = if (file == null) input else FileInputStream(file)
val code = BufferedReader(InputStreamReader(bytes, UTF_8)).readText()
val formattedCode = Formatter.format(parsedArgs.formattingOptions, code)
val alreadyFormatted = code == formattedCode

Expand All @@ -136,7 +141,7 @@ class Main(
out.println(fileName)
}
} else {
out.print(formattedCode)
BufferedWriter(OutputStreamWriter(out, UTF_8)).use { it.write(formattedCode) }
}
return alreadyFormatted
}
Expand All @@ -148,7 +153,7 @@ class Main(
} else {
// TODO(T111284144): Add tests
if (!alreadyFormatted) {
file.writeText(formattedCode)
file.writeText(formattedCode, UTF_8)
}
err.println("Done formatting $fileName")
}
Expand All @@ -158,18 +163,14 @@ class Main(
err.println("Error formatting $fileName: ${e.message}; skipping.")
throw e
} catch (e: ParseError) {
handleParseError(fileName, e)
err.println("$fileName:${e.message}")
throw e
} catch (e: FormattingError) {
for (diagnostic in e.diagnostics()) {
System.err.println("$fileName:$diagnostic")
err.println("$fileName:$diagnostic")
}
e.printStackTrace(err)
throw e
}
}

private fun handleParseError(fileName: String, e: ParseError) {
err.println("$fileName:${e.message}")
}
}
3 changes: 2 additions & 1 deletion core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.facebook.ktfmt.format.Formatter
import com.facebook.ktfmt.format.FormattingOptions
import java.io.File
import java.io.PrintStream
import java.nio.charset.StandardCharsets.UTF_8

/** ParsedArgs holds the arguments passed to ktfmt on the command-line, after parsing. */
data class ParsedArgs(
Expand All @@ -40,7 +41,7 @@ data class ParsedArgs(

fun processArgs(err: PrintStream, args: Array<String>): ParsedArgs {
if (args.size == 1 && args[0].startsWith("@")) {
return parseOptions(err, File(args[0].substring(1)).readLines().toTypedArray())
return parseOptions(err, File(args[0].substring(1)).readLines(UTF_8).toTypedArray())
} else {
return parseOptions(err, args)
}
Expand Down
Loading

0 comments on commit 2c55d1c

Please sign in to comment.