Fixing merge of script fragments + UT

This commit is contained in:
Egor
2022-02-16 15:39:43 +02:00
committed by teamcity
parent f751f13c4d
commit 6d1fe3a962
2 changed files with 209 additions and 6 deletions
@@ -27,19 +27,32 @@ fun getMergedScriptText(script: SourceCode, configuration: ScriptCompilationConf
val curPos = if (prevFragment == null) 0 else prevFragment.range.end.absolutePos!!
if (prevFragment != null && prevFragment.range.end.absolutePos!! > fragmentStartPos) throw RuntimeException("Unsorted or overlapping fragments: previous: $prevFragment, current: $fragment")
if (curPos < fragmentStartPos) {
sb.append(
originalScriptText.subSequence(
curPos,
fragmentStartPos
).map { if (it == '\r' || it == '\n') it else ' ' }) // preserving lines layout
originalScriptText
.cleanContentPreservingLinesLayout(curPos, fragmentStartPos)
.forEach(sb::append)
}
sb.append(originalScriptText.subSequence(fragmentStartPos, fragmentEndPos))
prevFragment = fragment
}
val positionOfLastAppended = prevFragment?.range?.end?.absolutePos
if (positionOfLastAppended != null && positionOfLastAppended < originalScriptText.length) {
originalScriptText
.cleanContentPreservingLinesLayout(positionOfLastAppended)
.forEach(sb::append)
}
sb.toString()
}
}
/**
* Replaces every character with ' ' except end of line
*/
private fun String.cleanContentPreservingLinesLayout(
start: Int = 0,
end: Int = this.length
) = subSequence(start, end)
.map { if (it == '\r' || it == '\n') it else ' ' }
abstract class FileBasedScriptSource() : ExternalSourceCode {
abstract val file: File
}
@@ -119,4 +132,4 @@ private val ExternalSourceCode.textSafe: String?
text
} catch (e: Throwable) {
null
}
}