diff --git a/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/FilteredSectionsVirtualFile.kt b/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/FilteredSectionsVirtualFile.kt index 4c6b02f089f..5cfc199ae65 100644 --- a/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/FilteredSectionsVirtualFile.kt +++ b/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/FilteredSectionsVirtualFile.kt @@ -41,7 +41,14 @@ class FilteredSectionsVirtualFile(val delegate: VirtualFile, val sectionIds: Col override fun getTimeStamp(): Long = delegate.timeStamp override fun getName(): String = delegate.name - override fun contentsToByteArray(): ByteArray = filterByteContents(sectionIds, delegate.contentsToByteArray(), delegate.charset) + override fun contentsToByteArray(): ByteArray { + val res = filterByteContents(sectionIds, delegate.contentsToByteArray(), delegate.charset) + val dbgDumpFile = delegate.canonicalPath?.let { java.io.File(it + ".${sectionIds.joinToString(".")}.dump") } + if (dbgDumpFile != null && !dbgDumpFile.exists()) { + dbgDumpFile.writeText(String(res)) + } + return res + } override fun getInputStream(): InputStream = ByteArrayInputStream(contentsToByteArray()) @@ -71,6 +78,11 @@ private fun filterByteContents(sectionIds: Collection, bytes: ByteArray, } private fun filterStringBuilderContents(content: StringBuilder, sectionIds: Collection): StringBuilder { + for (i in 0..content.length - 1) { + if (content[i] == '\r') { + content[i] = ' ' + } + } var curPos = 0 val sectionsIter = FilteredSectionsTokensRangeIterator(content, sectionIds) for (range in sectionsIter) { diff --git a/plugins/source-sections/source-sections-compiler/tests/org/jetbrains/kotlin/sourceSections/SourceSectionsTest.kt b/plugins/source-sections/source-sections-compiler/tests/org/jetbrains/kotlin/sourceSections/SourceSectionsTest.kt index a3785435f5e..99a43305175 100644 --- a/plugins/source-sections/source-sections-compiler/tests/org/jetbrains/kotlin/sourceSections/SourceSectionsTest.kt +++ b/plugins/source-sections/source-sections-compiler/tests/org/jetbrains/kotlin/sourceSections/SourceSectionsTest.kt @@ -115,6 +115,23 @@ class SourceSectionsTest : TestCaseWithTmpdir() { } } + fun testSourceSectionsFilterWithCRLF() { + val sourceToFiltered = getTestFiles(".filtered") + + createEnvironment() // creates VirtualFileManager + val fileCreator = FilteredSectionsVirtualFileExtension(TEST_ALLOWED_SECTIONS.toSet()) + + sourceToFiltered.forEach { (source, expectedResult) -> + val sourceWithCRLF = createTempFile(source.name) + sourceWithCRLF.writeText(source.readText().replace("\r\n", "\n").replace("\n", "\r\n")) + val filteredVF = fileCreator.createPreprocessedFile(StandardFileSystems.local().findFileByPath(sourceWithCRLF.canonicalPath)) + TestCase.assertNotNull("Cannot generate preprocessed file", filteredVF) + val expected = expectedResult.inputStream().trimmedLines(Charset.defaultCharset()) + val actual = ByteArrayInputStream(filteredVF!!.contentsToByteArray()).trimmedLines(filteredVF.charset) + TestCase.assertEquals("Unexpected result on preprocessing file '${source.name}'", expected, actual) + } + } + fun testSourceSectionsRun() { val sourceToOutput = getTestFiles(".out")