[Native] External tests: Preserve original line offsets

This commit is contained in:
Dmitriy Dolovov
2021-11-14 01:36:39 +03:00
parent cc667189ec
commit 503f159f4d
2 changed files with 21 additions and 15 deletions
@@ -228,7 +228,7 @@ private class ExtTestDataFile(
addAnnotationEntry(
objectDeclaration,
handler.psiFactory.createAnnotationEntry(THREAD_LOCAL_ANNOTATION)
).ensureSurroundedByWhiteSpace(" ")
).ensureSurroundedByWhiteSpace()
}
super.visitObjectDeclaration(objectDeclaration)
@@ -287,10 +287,10 @@ private class ExtTestDataFile(
if (oldPackageDirective != null) {
// Replace old package directive by the new one.
oldPackageDirective.replace(newPackageDirective).ensureSurroundedByWhiteSpace("\n\n")
oldPackageDirective.replace(newPackageDirective).ensureSurroundedByWhiteSpace()
} else {
// Insert the package directive immediately after file-level annotations.
file.addAfter(newPackageDirective, file.fileAnnotationList).ensureSurroundedByWhiteSpace("\n\n")
file.addAfter(newPackageDirective, file.fileAnnotationList).ensureSurroundedByWhiteSpace()
}
visitKtElement(file, file.collectAccessibleDeclarationNames())
@@ -482,16 +482,16 @@ private class ExtTestDataFile(
override fun visitKtFile(file: KtFile) {
val oldFileAnnotationList = file.fileAnnotationList
val newFileAnnotationsList = handler.psiFactory.createFile(buildString {
val newFileAnnotationList = handler.psiFactory.createFile(buildString {
allFileLevelAnnotationsSorted.forEach(::appendLine)
}).fileAnnotationList!!
if (oldFileAnnotationList != null) {
// Replace old annotations list by the new one.
oldFileAnnotationList.replace(newFileAnnotationsList).ensureSurroundedByWhiteSpace("\n\n")
oldFileAnnotationList.replace(newFileAnnotationList).ensureSurroundedByWhiteSpace()
} else {
// Insert the annotations list immediately before package directive.
file.addBefore(newFileAnnotationsList, file.packageDirective).ensureSurroundedByWhiteSpace("\n\n")
file.addBefore(newFileAnnotationList, file.packageDirective).ensureSurroundedByWhiteSpace()
}
}
})
@@ -805,7 +805,13 @@ private class ExtTestDataFileStructureFactory(parentDisposable: Disposable) : Te
private inner class FilesAndModules(originalTestDataFile: File, initialCleanUpTransformation: (String) -> String) {
private val testFileFactory = ExtTestFileFactory()
private val generatedFiles = TestFiles.createTestFiles(DEFAULT_FILE_NAME, originalTestDataFile.readText(), testFileFactory)
private val generatedFiles = TestFiles.createTestFiles(
/* testFileName = */ DEFAULT_FILE_NAME,
/* expectedText = */ originalTestDataFile.readText(),
/* factory = */ testFileFactory,
/* preserveLocations = */ true
)
private val lazyData: Triple<Map<String, ExtTestModule>, Map<ExtTestFile, KtFile>, MutableList<ExtTestFile>> by lazy {
// Clean up contents of every individual test file. Important: This should be done only after parsing testData file,
@@ -11,21 +11,21 @@ import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.nextLeaf
import org.jetbrains.kotlin.psi.psiUtil.prevLeaf
internal fun PsiElement.ensureSurroundedByWhiteSpace(whiteSpace: String): PsiElement =
ensureHasWhiteSpaceBefore(whiteSpace).ensureHasWhiteSpaceAfter(whiteSpace)
internal fun PsiElement.ensureSurroundedByWhiteSpace(): PsiElement =
ensureHasWhiteSpaceBefore().ensureHasWhiteSpaceAfter()
private fun PsiElement.ensureHasWhiteSpaceBefore(whiteSpace: String): PsiElement {
private fun PsiElement.ensureHasWhiteSpaceBefore(): PsiElement {
val (fileBoundaryReached, whiteSpaceBefore) = whiteSpaceBefore()
if (!fileBoundaryReached and !whiteSpaceBefore.endsWith(whiteSpace)) {
parent.addBefore(KtPsiFactory(project).createWhiteSpace(whiteSpace), this)
if (!fileBoundaryReached and !whiteSpaceBefore.endsWith(" ")) {
parent.addBefore(KtPsiFactory(project).createWhiteSpace(" "), this)
}
return this
}
private fun PsiElement.ensureHasWhiteSpaceAfter(whiteSpace: String): PsiElement {
private fun PsiElement.ensureHasWhiteSpaceAfter(): PsiElement {
val (fileBoundaryReached, whiteSpaceAfter) = whiteSpaceAfter()
if (!fileBoundaryReached and !whiteSpaceAfter.startsWith(whiteSpace)) {
parent.addAfter(KtPsiFactory(project).createWhiteSpace(whiteSpace), this)
if (!fileBoundaryReached and !whiteSpaceAfter.startsWith(" ")) {
parent.addAfter(KtPsiFactory(project).createWhiteSpace(" "), this)
}
return this
}