Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove line separator detection logic #7251

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,8 @@ open class KtCompiler(
}
}

fun createKtFile(@Language("kotlin") content: String, path: Path): KtFile {
val psiFile = psiFileFactory.createPhysicalFile(path.name, StringUtilRt.convertLineSeparators(content))

return psiFile.apply {
val normalizedAbsolutePath = path.absolute().normalize()
this.absolutePath = normalizedAbsolutePath
virtualFile.detectedLineSeparator = content.determineLineSeparator()
fun createKtFile(@Language("kotlin") content: String, path: Path): KtFile =
psiFileFactory.createPhysicalFile(path.name, StringUtilRt.convertLineSeparators(content)).apply {
absolutePath = path.absolute().normalize()
}
}
}

internal fun String.determineLineSeparator(): String {
val i = this.lastIndexOf('\n')
if (i == -1) {
return if (this.lastIndexOf('\r') == -1) System.lineSeparator() else "\r"
}
return if (i != 0 && this[i - 1] == '\r') "\r\n" else "\n"
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,4 @@ class KtCompilerSpec {
.withMessage("$cssPath is not a Kotlin file")
}
}

@Nested
inner class `line ending detection` {
@Test
fun `detects CRLF line endings`() {
assertThat("1\r\n2".determineLineSeparator()).isEqualTo("\r\n")
}

@Test
fun `detects LF line endings`() {
assertThat("1\n2".determineLineSeparator()).isEqualTo("\n")
}

@Test
fun `detects CR line endings`() {
assertThat("1\r2".determineLineSeparator()).isEqualTo("\r")
}
}
}