Skip to content

Commit

Permalink
Added utf-8 BOM removal (#630)
Browse files Browse the repository at this point in the history
* Added utf-8 BOM removal

* Restore UTF8 BOM after formatting if it was present

* Fixed code style
  • Loading branch information
Raibaz authored and Tapchicoma committed Nov 12, 2019
1 parent baac14c commit 6864374
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ object KtLint {
val ANDROID_USER_DATA_KEY = Key<Boolean>("ANDROID")
val FILE_PATH_USER_DATA_KEY = Key<String>("FILE_PATH")
val DISABLED_RULES = Key<Set<String>>("DISABLED_RULES")
private const val UTF8_BOM = "\uFEFF"
const val STDIN_FILE = "<stdin>"

private val psiFileFactory: PsiFileFactory
Expand Down Expand Up @@ -130,7 +131,7 @@ object KtLint {
* @throws RuleExecutionException in case of internal failure caused by a bug in rule implementation
*/
fun lint(params: Params) {
val normalizedText = params.text.replace("\r\n", "\n").replace("\r", "\n")
val normalizedText = normalizeText(params.text)
val positionByOffset = calculateLineColByOffset(normalizedText)
val psiFileName = if (params.script) "file.kts" else "file.kt"
val psiFile = psiFileFactory.createFileFromText(psiFileName, KotlinLanguage.INSTANCE, normalizedText) as KtFile
Expand Down Expand Up @@ -169,6 +170,13 @@ object KtLint {
.forEach { e -> params.cb(e, false) }
}

private fun normalizeText(text: String): String {
return text
.replace("\r\n", "\n")
.replace("\r", "\n")
.replaceFirst(UTF8_BOM, "")
}

private fun userDataResolver(editorConfigPath: String?, debug: Boolean): (String?) -> Map<String, String> {
if (editorConfigPath != null) {
val userData = (
Expand Down Expand Up @@ -351,7 +359,8 @@ object KtLint {
* @throws RuleExecutionException in case of internal failure caused by a bug in rule implementation
*/
fun format(params: Params): String {
val normalizedText = params.text.replace("\r\n", "\n").replace("\r", "\n")
val hasUTF8BOM = params.text.startsWith(UTF8_BOM)
val normalizedText = normalizeText(params.text)
val positionByOffset = calculateLineColByOffset(normalizedText)
val psiFileName = if (params.script) "file.kts" else "file.kt"
val psiFile = psiFileFactory.createFileFromText(psiFileName, KotlinLanguage.INSTANCE, normalizedText) as KtFile
Expand Down Expand Up @@ -414,7 +423,13 @@ object KtLint {
.sortedWith(Comparator { (l), (r) -> if (l.line != r.line) l.line - r.line else l.col - r.col })
.forEach { (e, corrected) -> params.cb(e, corrected) }
}
return if (mutated) rootNode.text.replace("\n", determineLineSeparator(params.text, params.userData)) else params.text

if (!mutated) {
return params.text
}

return if (hasUTF8BOM) UTF8_BOM else "" + // Restore UTF8 BOM if it was present
rootNode.text.replace("\n", determineLineSeparator(params.text, params.userData))
}

private fun determineLineSeparator(fileContent: String, userData: Map<String, String>): String {
Expand Down

0 comments on commit 6864374

Please sign in to comment.